|
| 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.core; |
| 22 | + |
| 23 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 24 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 25 | +import static org.awaitility.Awaitility.await; |
| 26 | +import static org.jose4j.jwx.HeaderParameterNames.TYPE; |
| 27 | +import static org.junit.jupiter.api.Assertions.*; |
| 28 | + |
| 29 | +import com.inrupt.client.Client; |
| 30 | +import com.inrupt.client.ClientProvider; |
| 31 | +import com.inrupt.client.Request; |
| 32 | +import com.inrupt.client.Response; |
| 33 | +import com.inrupt.client.auth.Session; |
| 34 | +import com.inrupt.client.openid.OpenIdConfig; |
| 35 | +import com.inrupt.client.openid.OpenIdSession; |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | +import java.io.InputStream; |
| 39 | +import java.io.UncheckedIOException; |
| 40 | +import java.net.URI; |
| 41 | +import java.security.KeyPair; |
| 42 | +import java.time.Instant; |
| 43 | +import java.util.*; |
| 44 | +import java.util.concurrent.atomic.AtomicReference; |
| 45 | + |
| 46 | +import org.apache.commons.io.IOUtils; |
| 47 | +import org.jose4j.jwk.PublicJsonWebKey; |
| 48 | +import org.jose4j.jws.AlgorithmIdentifiers; |
| 49 | +import org.jose4j.jws.JsonWebSignature; |
| 50 | +import org.jose4j.jwt.JwtClaims; |
| 51 | +import org.jose4j.lang.JoseException; |
| 52 | +import org.jose4j.lang.UncheckedJoseException; |
| 53 | +import org.junit.jupiter.api.AfterAll; |
| 54 | +import org.junit.jupiter.api.BeforeAll; |
| 55 | +import org.junit.jupiter.api.Test; |
| 56 | + |
| 57 | +class DefaultClientNonRdfTest { |
| 58 | + |
| 59 | + private static final String WEBID = "https://id.example/username"; |
| 60 | + private static final String SUB = "username"; |
| 61 | + private static final String ISS = "https://iss.example"; |
| 62 | + private static final String AZP = "https://app.example"; |
| 63 | + private static final MockHttpService mockHttpServer = new MockHttpService(); |
| 64 | + private static final Client client = ClientProvider.getClient(); |
| 65 | + private static final AtomicReference<String> baseUri = new AtomicReference<>(); |
| 66 | + |
| 67 | + @BeforeAll |
| 68 | + static void setup() { |
| 69 | + baseUri.set(mockHttpServer.start()); |
| 70 | + } |
| 71 | + |
| 72 | + @AfterAll |
| 73 | + static void teardown() { |
| 74 | + mockHttpServer.stop(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void clientLoader() { |
| 79 | + assertNotNull(client); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void testSendOfStringAsync() { |
| 84 | + final Request request = Request.newBuilder() |
| 85 | + .uri(URI.create(baseUri.get() + "/file")) |
| 86 | + .GET() |
| 87 | + .build(); |
| 88 | + |
| 89 | + final Response<String> response = client.send(request, Response.BodyHandlers.ofString()) |
| 90 | + .toCompletableFuture().join(); |
| 91 | + |
| 92 | + assertEquals(200, response.statusCode()); |
| 93 | + assertTrue(response.body().contains("Julie C. Sparks and David Widger")); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void testOfStringPublisherOpenidSession() { |
| 98 | + final Map<String, Object> claims = new HashMap<>(); |
| 99 | + claims.put("webid", WEBID); |
| 100 | + claims.put("sub", SUB); |
| 101 | + claims.put("iss", ISS); |
| 102 | + claims.put("azp", AZP); |
| 103 | + final String token = generateIdToken(claims); |
| 104 | + |
| 105 | + final Request request = Request.newBuilder() |
| 106 | + .uri(URI.create(baseUri.get() + "/postStringContainer/")) |
| 107 | + .header("Content-Type", "text/plain") |
| 108 | + .POST(Request.BodyPublishers.ofString("Test String 1")) |
| 109 | + .build(); |
| 110 | + |
| 111 | + final PublicJsonWebKey jwk = getDpopKey("/rsa-key.json"); |
| 112 | + final OpenIdConfig config = new OpenIdConfig(); |
| 113 | + config.setProofKeyPairs(Collections.singletonMap("RS256", |
| 114 | + new KeyPair(jwk.getPublicKey(), jwk.getPrivateKey()))); |
| 115 | + |
| 116 | + final Response<Void> response = client.session(OpenIdSession.ofIdToken(token, config)) |
| 117 | + .send(request, Response.BodyHandlers.discarding()) |
| 118 | + .toCompletableFuture().join(); |
| 119 | + |
| 120 | + assertEquals(201, response.statusCode()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void testOfStringPublisherUmaAnonSession() { |
| 125 | + final Request request = Request.newBuilder() |
| 126 | + .uri(URI.create(baseUri.get() + "/postStringContainer/")) |
| 127 | + .header("Content-Type", "text/plain") |
| 128 | + .POST(Request.BodyPublishers.ofString("Test String 1")) |
| 129 | + .build(); |
| 130 | + |
| 131 | + final Response<Void> response = client.session(Session.anonymous()) |
| 132 | + .send(request, Response.BodyHandlers.discarding()) |
| 133 | + .toCompletableFuture().join(); |
| 134 | + |
| 135 | + assertEquals(401, response.statusCode()); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void testNullSession() { |
| 140 | + assertThrows(NullPointerException.class, () -> |
| 141 | + client.session(null)); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void testSessionExpiredIdToken() { |
| 146 | + final Map<String, Object> claims = new HashMap<>(); |
| 147 | + claims.put("webid", WEBID); |
| 148 | + claims.put("sub", SUB); |
| 149 | + claims.put("iss", ISS); |
| 150 | + claims.put("azp", AZP); |
| 151 | + claims.put("exp", Instant.now().plusSeconds(2).getEpochSecond()); |
| 152 | + claims.put("iat", Instant.now().minusSeconds(61).getEpochSecond()); |
| 153 | + |
| 154 | + final String token = generateIdToken(claims); |
| 155 | + final OpenIdConfig config = new OpenIdConfig(); |
| 156 | + config.setExpGracePeriodSecs(0); |
| 157 | + final Session s = OpenIdSession.ofIdToken(token, config); |
| 158 | + |
| 159 | + // Wait for the token to expire |
| 160 | + await().atMost(5, SECONDS).until(() -> !s.getCredential(OpenIdSession.ID_TOKEN, null).isPresent()); |
| 161 | + |
| 162 | + final Request request = Request.newBuilder() |
| 163 | + .uri(URI.create(baseUri.get() + "/postStringContainer/")) |
| 164 | + .header("Content-Type", "text/plain") |
| 165 | + .POST(Request.BodyPublishers.ofString("Test String 1")) |
| 166 | + .build(); |
| 167 | + |
| 168 | + final Response<Void> response = client.session(s) |
| 169 | + .send(request, Response.BodyHandlers.discarding()) |
| 170 | + .toCompletableFuture().join(); |
| 171 | + |
| 172 | + assertEquals(401, response.statusCode()); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + void testSendRequestImage() { |
| 177 | + final Request request = Request.newBuilder() |
| 178 | + .uri(URI.create(baseUri.get() + "/solid.png")) |
| 179 | + .GET() |
| 180 | + .build(); |
| 181 | + |
| 182 | + final Response<byte[]> response = client.send(request, Response.BodyHandlers.ofByteArray()) |
| 183 | + .toCompletableFuture().join(); |
| 184 | + |
| 185 | + assertEquals(200, response.statusCode()); |
| 186 | + } |
| 187 | + |
| 188 | + static PublicJsonWebKey getDpopKey(final String resource) { |
| 189 | + try (final InputStream stream = DefaultClientNonRdfTest.class.getResourceAsStream(resource)) { |
| 190 | + final String jwks = IOUtils.toString(stream, UTF_8); |
| 191 | + return PublicJsonWebKey.Factory.newPublicJwk(jwks); |
| 192 | + } catch (final IOException ex) { |
| 193 | + throw new UncheckedIOException("Unable to read JWK", ex); |
| 194 | + } catch (final JoseException ex) { |
| 195 | + throw new UncheckedJoseException("Unable to generate DPoP token", ex); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + static String generateIdToken(final Map<String, Object> claims) { |
| 200 | + try (final InputStream resource = DefaultClientNonRdfTest.class.getResourceAsStream("/signing-key.json")) { |
| 201 | + final String jwks = IOUtils.toString(resource, UTF_8); |
| 202 | + final PublicJsonWebKey jwk = PublicJsonWebKey.Factory |
| 203 | + .newPublicJwk(jwks); |
| 204 | + |
| 205 | + final JsonWebSignature jws = new JsonWebSignature(); |
| 206 | + jws.setAlgorithmHeaderValue(AlgorithmIdentifiers.ECDSA_USING_P256_CURVE_AND_SHA256); |
| 207 | + jws.setHeader(TYPE, "JWT"); |
| 208 | + jws.setKey(jwk.getPrivateKey()); |
| 209 | + final JwtClaims jwtClaims = new JwtClaims(); |
| 210 | + jwtClaims.setJwtId(UUID.randomUUID().toString()); |
| 211 | + jwtClaims.setExpirationTimeMinutesInTheFuture(5); |
| 212 | + jwtClaims.setIssuedAtToNow(); |
| 213 | + // override/set claims |
| 214 | + claims.entrySet().forEach(entry -> jwtClaims.setClaim(entry.getKey(), entry.getValue())); |
| 215 | + jws.setPayload(jwtClaims.toJson()); |
| 216 | + |
| 217 | + return jws.getCompactSerialization(); |
| 218 | + } catch (final IOException ex) { |
| 219 | + throw new UncheckedIOException("Unable to read JWK", ex); |
| 220 | + } catch (final JoseException ex) { |
| 221 | + throw new UncheckedJoseException("Unable to generate DPoP token", ex); |
| 222 | + } |
| 223 | + } |
| 224 | +} |
0 commit comments