|
| 1 | +/* |
| 2 | + * Copyright Inrupt Inc. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal in |
| 6 | + * the Software without restriction, including without limitation the rights to use, |
| 7 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 8 | + * Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | + * subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 15 | + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 16 | + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 17 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 18 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 19 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | +package com.inrupt.client.quarkus; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.*; |
| 24 | + |
| 25 | +import com.inrupt.client.auth.Session; |
| 26 | +import com.inrupt.client.openid.OpenIdSession; |
| 27 | + |
| 28 | +import io.smallrye.jwt.algorithm.SignatureAlgorithm; |
| 29 | +import io.smallrye.jwt.auth.principal.DefaultJWTParser; |
| 30 | +import io.smallrye.jwt.auth.principal.JWTAuthContextInfo; |
| 31 | +import io.smallrye.jwt.auth.principal.ParseException; |
| 32 | +import io.smallrye.jwt.build.Jwt; |
| 33 | +import io.smallrye.jwt.util.ResourceUtils; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.net.URI; |
| 37 | +import java.time.Instant; |
| 38 | +import java.util.Optional; |
| 39 | + |
| 40 | +import org.eclipse.microprofile.jwt.JsonWebToken; |
| 41 | +import org.jose4j.jwk.PublicJsonWebKey; |
| 42 | +import org.jose4j.lang.JoseException; |
| 43 | +import org.jose4j.lang.UncheckedJoseException; |
| 44 | +import org.junit.jupiter.api.Test; |
| 45 | + |
| 46 | +class SessionUtilsTest { |
| 47 | + |
| 48 | + static final String WEBID = "https://example.com/user"; |
| 49 | + static final String ISSUER = "https://issuer.example"; |
| 50 | + |
| 51 | + @Test |
| 52 | + void testSession() { |
| 53 | + final var token = generateIdToken("user", ISSUER, WEBID); |
| 54 | + final var session = SessionUtils.asSession(token, OpenIdSession::ofIdToken); |
| 55 | + assertTrue(session.isPresent()); |
| 56 | + session.ifPresent(s -> |
| 57 | + assertEquals(Optional.of(URI.create(WEBID)), s.getPrincipal())); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testAnonymousSession() { |
| 62 | + final var token = generateIdToken("user", ISSUER, WEBID); |
| 63 | + final var session = SessionUtils.asSession(token, t -> Session.anonymous()); |
| 64 | + assertTrue(session.isPresent()); |
| 65 | + session.ifPresent(s -> |
| 66 | + assertEquals(Optional.empty(), s.getPrincipal())); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testSessionNoMapper() { |
| 71 | + final var token = generateIdToken("user", ISSUER, WEBID); |
| 72 | + final var session = SessionUtils.asSession(token); |
| 73 | + assertTrue(session.isPresent()); |
| 74 | + session.ifPresent(s -> |
| 75 | + assertEquals(Optional.of(URI.create(WEBID)), s.getPrincipal())); |
| 76 | + } |
| 77 | + |
| 78 | + static JsonWebToken generateIdToken(final String sub, final String issuer, final String webid) { |
| 79 | + try { |
| 80 | + final var jwk = PublicJsonWebKey.Factory |
| 81 | + .newPublicJwk(ResourceUtils.readResource("testKey.json")); |
| 82 | + |
| 83 | + final var authContext = new JWTAuthContextInfo(jwk.getPublicKey(), issuer); |
| 84 | + authContext.setSignatureAlgorithm(SignatureAlgorithm.ES256); |
| 85 | + final var parser = new DefaultJWTParser(authContext); |
| 86 | + |
| 87 | + final var token = Jwt.claims() |
| 88 | + .subject(sub) |
| 89 | + .issuer(issuer) |
| 90 | + .claim("webid", webid) |
| 91 | + .expiresAt(Instant.now().plusSeconds(100)) |
| 92 | + .audience("solid").jws() |
| 93 | + .keyId("76GJ30ywXmKAxKdhJ1yPLA").sign(jwk.getPrivateKey()); |
| 94 | + return parser.parse(token); |
| 95 | + } catch (final IOException | JoseException ex) { |
| 96 | + throw new UncheckedJoseException("Could not build token", ex); |
| 97 | + } catch (final ParseException ex) { |
| 98 | + throw new IllegalArgumentException("Could not parse JWT", ex); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
0 commit comments