|
| 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 org.jose4j.jwa.AlgorithmConstraints.ConstraintType.PERMIT; |
| 24 | +import static org.junit.jupiter.api.Assertions.*; |
| 25 | + |
| 26 | +import com.inrupt.client.Request; |
| 27 | +import com.inrupt.client.auth.Authenticator; |
| 28 | +import com.inrupt.client.auth.Credential; |
| 29 | +import com.inrupt.client.auth.DPoP; |
| 30 | +import com.inrupt.client.auth.Session; |
| 31 | +import com.inrupt.client.spi.ServiceProvider; |
| 32 | + |
| 33 | +import java.net.URI; |
| 34 | +import java.time.Instant; |
| 35 | +import java.util.Collection; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.Optional; |
| 38 | +import java.util.Set; |
| 39 | +import java.util.UUID; |
| 40 | +import java.util.concurrent.CompletableFuture; |
| 41 | +import java.util.concurrent.CompletionStage; |
| 42 | + |
| 43 | +import org.jose4j.jwt.JwtClaims; |
| 44 | +import org.jose4j.jwt.consumer.JwtConsumerBuilder; |
| 45 | +import org.jose4j.keys.resolvers.EmbeddedJwkVerificationKeyResolver; |
| 46 | +import org.junit.jupiter.api.Test; |
| 47 | + |
| 48 | +class DefaultClientDpopTest { |
| 49 | + |
| 50 | + // Example from RFC 9449, section 7.1 |
| 51 | + static final String ACCESS_TOKEN = "Kz~8mXK1EalYznwH-LC-1fBAo.4Ljp~zsPE_NeO.gxU"; |
| 52 | + static final String ATH = "fUHyO2r2Z3DZ53EsNrWBb0xWXoaNy59IiKCAqksmQEo"; |
| 53 | + |
| 54 | + @Test |
| 55 | + void testUpgradeRequestWithDpopBoundToken() throws Exception { |
| 56 | + final DPoP dpop = DPoP.of(); |
| 57 | + final String jkt = dpop.lookupThumbprint("ES256").orElseThrow(); |
| 58 | + final DefaultClient client = new DefaultClient(ServiceProvider.getHttpService(), new DpopSession(dpop)); |
| 59 | + final Credential credential = new Credential("DPoP", URI.create("https://issuer.example"), |
| 60 | + ACCESS_TOKEN, Instant.now().plusSeconds(300), null, jkt); |
| 61 | + final Request request = Request.newBuilder(URI.create("https://storage.example/resource?q=1#frag")) |
| 62 | + .header("DPoP", "stale-proof") |
| 63 | + .GET().build(); |
| 64 | + |
| 65 | + final Request upgraded = client.upgradeRequest(request, credential); |
| 66 | + |
| 67 | + assertEquals(Collections.singletonList("DPoP " + ACCESS_TOKEN), |
| 68 | + upgraded.headers().allValues("Authorization")); |
| 69 | + assertEquals(1, upgraded.headers().allValues("DPoP").size()); |
| 70 | + final String proof = upgraded.headers().firstValue("DPoP").orElseThrow(); |
| 71 | + final JwtClaims claims = new JwtConsumerBuilder() |
| 72 | + .setVerificationKeyResolver(new EmbeddedJwkVerificationKeyResolver()) |
| 73 | + .setJwsAlgorithmConstraints(PERMIT, "ES256") |
| 74 | + .setExpectedType(true, "dpop+jwt") |
| 75 | + .setRequireJwtId() |
| 76 | + .setRequireIssuedAt() |
| 77 | + .build().processToClaims(proof); |
| 78 | + assertEquals("GET", claims.getStringClaimValue("htm")); |
| 79 | + assertEquals("https://storage.example/resource", claims.getStringClaimValue("htu")); |
| 80 | + assertEquals(ATH, claims.getStringClaimValue("ath")); |
| 81 | + } |
| 82 | + |
| 83 | + static class DpopSession implements Session { |
| 84 | + private final DPoP dpop; |
| 85 | + private final String id = UUID.randomUUID().toString(); |
| 86 | + |
| 87 | + DpopSession(final DPoP dpop) { |
| 88 | + this.dpop = dpop; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public String getId() { |
| 93 | + return id; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public Optional<URI> getPrincipal() { |
| 98 | + return Optional.empty(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public Set<String> supportedSchemes() { |
| 103 | + return Collections.singleton("DPoP"); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Optional<Credential> getCredential(final URI name, final URI uri) { |
| 108 | + return Optional.empty(); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public Optional<Credential> fromCache(final Request request) { |
| 113 | + return Optional.empty(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public Optional<String> generateProof(final String jkt, final Request request) { |
| 118 | + return generateProof(jkt, request, null); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public Optional<String> generateProof(final String jkt, final Request request, final String accessToken) { |
| 123 | + return dpop.lookupAlgorithm(jkt) |
| 124 | + .map(alg -> dpop.generateProof(alg, request.uri(), request.method(), accessToken)); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public Optional<String> selectThumbprint(final Collection<String> algorithms) { |
| 129 | + return Optional.empty(); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void reset() { |
| 134 | + // no-op |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public CompletionStage<Optional<Credential>> authenticate(final Authenticator authenticator, |
| 139 | + final Request request, final Set<String> algorithms) { |
| 140 | + return CompletableFuture.completedFuture(Optional.empty()); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments