|
| 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.spring; |
| 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.build.Jwt; |
| 29 | +import io.smallrye.jwt.util.ResourceUtils; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.net.URI; |
| 33 | +import java.time.Instant; |
| 34 | +import java.util.Optional; |
| 35 | + |
| 36 | +import org.jose4j.jwk.PublicJsonWebKey; |
| 37 | +import org.jose4j.lang.JoseException; |
| 38 | +import org.jose4j.lang.UncheckedJoseException; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.springframework.security.core.authority.AuthorityUtils; |
| 41 | +import org.springframework.security.oauth2.core.oidc.OidcIdToken; |
| 42 | +import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser; |
| 43 | + |
| 44 | +class SessionUtilsTest { |
| 45 | + |
| 46 | + static final String WEBID = "https://example.com/user"; |
| 47 | + static final String ISSUER = "https://issuer.example"; |
| 48 | + |
| 49 | + @Test |
| 50 | + void testSession() { |
| 51 | + final var token = generateIdToken("user", ISSUER, WEBID, Instant.now().plusSeconds(30)); |
| 52 | + final var oauthUser = new DefaultOidcUser(AuthorityUtils.NO_AUTHORITIES, token); |
| 53 | + final var session = SessionUtils.asSession(oauthUser, OpenIdSession::ofIdToken); |
| 54 | + assertTrue(session.isPresent()); |
| 55 | + session.ifPresent(s -> |
| 56 | + assertEquals(Optional.of(URI.create(WEBID)), s.getPrincipal())); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void testExpiredSession() { |
| 61 | + final var token = generateIdToken("user", ISSUER, WEBID, Instant.now().minusSeconds(30)); |
| 62 | + final var oauthUser = new DefaultOidcUser(AuthorityUtils.NO_AUTHORITIES, token); |
| 63 | + final var session = SessionUtils.asSession(oauthUser, OpenIdSession::ofIdToken); |
| 64 | + assertFalse(session.isPresent()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testAnonymousSession() { |
| 69 | + final var token = generateIdToken("user", ISSUER, WEBID, Instant.now().plusSeconds(30)); |
| 70 | + final var oauthUser = new DefaultOidcUser(AuthorityUtils.NO_AUTHORITIES, token); |
| 71 | + final var session = SessionUtils.asSession(oauthUser, t -> Session.anonymous()); |
| 72 | + assertTrue(session.isPresent()); |
| 73 | + session.ifPresent(s -> |
| 74 | + assertEquals(Optional.empty(), s.getPrincipal())); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testSessionNoMapper() { |
| 79 | + final var token = generateIdToken("user", ISSUER, WEBID, Instant.now().plusSeconds(30)); |
| 80 | + final var oauthUser = new DefaultOidcUser(AuthorityUtils.NO_AUTHORITIES, token); |
| 81 | + final var session = SessionUtils.asSession(oauthUser); |
| 82 | + assertTrue(session.isPresent()); |
| 83 | + session.ifPresent(s -> |
| 84 | + assertEquals(Optional.of(URI.create(WEBID)), s.getPrincipal())); |
| 85 | + } |
| 86 | + |
| 87 | + static OidcIdToken generateIdToken(final String sub, final String issuer, final String webid, final Instant exp) { |
| 88 | + try { |
| 89 | + final var jwk = PublicJsonWebKey.Factory |
| 90 | + .newPublicJwk(ResourceUtils.readResource("testKey.json")); |
| 91 | + final var issued = Instant.now().isBefore(exp) ? Instant.now() : exp.minusSeconds(30); |
| 92 | + final var token = Jwt.claims() |
| 93 | + .subject(sub) |
| 94 | + .issuer(issuer) |
| 95 | + .issuedAt(issued) |
| 96 | + .expiresAt(exp) |
| 97 | + .claim("webid", webid) |
| 98 | + .audience("solid").jws() |
| 99 | + .keyId("E1amq-dXv6METCuD2iRwAQ").sign(jwk.getPrivateKey()); |
| 100 | + return OidcIdToken.withTokenValue(token).expiresAt(Instant.now().plusSeconds(100)).subject(sub) |
| 101 | + .issuer(issuer).issuedAt(issued).expiresAt(exp).claim("webid", webid).build(); |
| 102 | + } catch (final IOException | JoseException ex) { |
| 103 | + throw new UncheckedJoseException("Could not build token", ex); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
0 commit comments