Skip to content

Commit f44b8d2

Browse files
authored
Merge branch 'main' into JCL-359-deny-request-method
2 parents 0569bb0 + b188909 commit f44b8d2

File tree

11 files changed

+115
-13
lines changed

11 files changed

+115
-13
lines changed

access-grant/src/main/java/com/inrupt/client/accessgrant/AccessGrantSession.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ public String getId() {
104104
return id;
105105
}
106106

107+
@Override
108+
public void reset() {
109+
session.reset();
110+
tokenCache.invalidateAll();
111+
}
112+
107113
@Override
108114
public Optional<URI> getPrincipal() {
109115
return session.getPrincipal();

access-grant/src/test/java/com/inrupt/client/accessgrant/AccessGrantSessionTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
import static org.junit.jupiter.api.Assertions.*;
2525

2626
import com.inrupt.client.Request;
27+
import com.inrupt.client.auth.Authenticator;
28+
import com.inrupt.client.auth.Challenge;
2729
import com.inrupt.client.auth.Credential;
2830
import com.inrupt.client.auth.Session;
31+
import com.inrupt.client.openid.OpenIdAuthenticationProvider;
2932
import com.inrupt.client.openid.OpenIdSession;
3033
import com.inrupt.client.util.URIBuilder;
3134

@@ -106,6 +109,32 @@ void testAccessGrantSession() throws IOException {
106109
}
107110
}
108111

112+
@Test
113+
void testProtectedResource() throws IOException {
114+
final Map<String, Object> claims = new HashMap<>();
115+
claims.put("webid", WEBID);
116+
claims.put("sub", SUB);
117+
claims.put("iss", ISS);
118+
claims.put("azp", AZP);
119+
120+
final String token = AccessGrantTestUtils.generateIdToken(claims);
121+
122+
try (final InputStream resource = AccessGrantTest.class.getResourceAsStream("/access_grant3.json")) {
123+
final AccessGrant grant = AccessGrant.ofAccessGrant(resource);
124+
final Session session = AccessGrantSession.ofAccessGrant(OpenIdSession.ofIdToken(token), grant);
125+
final Request req = Request.newBuilder(URI.create("https://storage.example/protected-resource")).build();
126+
final Authenticator auth = new OpenIdAuthenticationProvider().getAuthenticator(Challenge.of("Bearer"));
127+
128+
final Optional<Credential> credential = session.authenticate(auth, req, Collections.emptySet())
129+
.toCompletableFuture().join();
130+
131+
assertTrue(credential.isPresent());
132+
assertTrue(session.fromCache(req).isPresent());
133+
session.reset();
134+
assertFalse(session.fromCache(req).isPresent());
135+
}
136+
}
137+
109138
@ParameterizedTest
110139
@MethodSource
111140
void ancestors(final URI parent, final URI resource, final boolean expected) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"@context": ["https://www.w3.org/2018/credentials/v1"],
3+
"type": ["VerifiablePresentation"],
4+
"verifiableCredential": [{
5+
"@context":[
6+
"https://www.w3.org/2018/credentials/v1",
7+
"https://w3id.org/security/suites/ed25519-2020/v1",
8+
"https://w3id.org/vc-revocation-list-2020/v1",
9+
"https://schema.inrupt.com/credentials/v1.jsonld"],
10+
"id":"https://accessgrant.example/credential/5c6060ad-2f16-4bc1-b022-dffb46bff626",
11+
"type":["VerifiableCredential","SolidAccessGrant"],
12+
"issuer":"https://accessgrant.example",
13+
"expirationDate":"2022-08-27T12:00:00Z",
14+
"issuanceDate":"2022-08-25T20:34:05.153Z",
15+
"credentialSubject":{
16+
"id":"https://id.example/grantor",
17+
"providedConsent":{
18+
"mode":["Read"],
19+
"hasStatus":"https://w3id.org/GConsent#ConsentStatusExplicitlyGiven",
20+
"isProvidedToPerson":"https://id.example/grantee",
21+
"forPurpose":["https://purpose.example/Purpose1"],
22+
"forPersonalData":["https://storage.example/protected-resource"]}},
23+
"proof":{
24+
"created":"2022-08-25T20:34:05.236Z",
25+
"proofPurpose":"assertionMethod",
26+
"proofValue":"nIeQF44XVik7onnAbdkbp8xxJ2C8JoTw6-VtCkAzxuWYRFsSfYpft5MuAJaivyeKDmaK82Lj_YsME2xgL2WIBQ",
27+
"type":"Ed25519Signature2020",
28+
"verificationMethod":"https://accessgrant.example/key/1e332728-4af5-46e4-a5db-4f7b89e3f378"}
29+
}]
30+
}
31+

api/src/main/java/com/inrupt/client/auth/Session.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public interface Session {
9393
*/
9494
Optional<String> selectThumbprint(Collection<String> algorithms);
9595

96+
/**
97+
* Reset the session state, clearing any internal caches.
98+
*/
99+
void reset();
100+
96101
/**
97102
* Fetch an authentication token from session values.
98103
*
@@ -159,6 +164,11 @@ public Optional<String> selectThumbprint(final Collection<String> algorithms) {
159164
return Optional.empty();
160165
}
161166

167+
@Override
168+
public void reset() {
169+
// no-op
170+
}
171+
162172
@Override
163173
public CompletionStage<Optional<Credential>> authenticate(final Authenticator authenticator,
164174
final Request request, final Set<String> algorithms) {

api/src/main/java/com/inrupt/client/spi/NoopCache.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,26 @@
3333
*/
3434
class NoopCache<T, U> implements ClientCache<T, U> {
3535

36+
private static final String KEY_NOT_NULL = "cache key may not be null!";
37+
private static final String VALUE_NOT_NULL = "cache value may not be null!";
38+
3639
@Override
3740
public U get(final T key) {
38-
Objects.requireNonNull(key, "cache key may not be null!");
41+
Objects.requireNonNull(key, KEY_NOT_NULL);
3942
return null;
4043
}
4144

4245
@Override
4346
public void put(final T key, final U value) {
4447
/* no-op */
45-
Objects.requireNonNull(key, "cache key may not be null!");
46-
Objects.requireNonNull(value, "cache value may not be null!");
48+
Objects.requireNonNull(key, KEY_NOT_NULL);
49+
Objects.requireNonNull(value, VALUE_NOT_NULL);
4750
}
4851

4952
@Override
5053
public void invalidate(final T key) {
5154
/* no-op */
52-
Objects.requireNonNull(key, "cache key may not be null!");
55+
Objects.requireNonNull(key, KEY_NOT_NULL);
5356
}
5457

5558
@Override
@@ -60,7 +63,7 @@ public void invalidateAll() {
6063
public static class NoopCacheBuilder implements CacheBuilderService {
6164
@Override
6265
public <T, U> ClientCache<T, U> build(final int size, final Duration duration) {
63-
return new NoopCache<T, U>();
66+
return new NoopCache<>();
6467
}
6568
}
6669
}

caffeine/src/main/java/com/inrupt/client/caffeine/CaffeineCacheBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public <T, U> ClientCache<T, U> build(final int maximumSize, final Duration dura
4949
* @return a cache suitable for use in the Inrupt Client libraries
5050
*/
5151
public static <T, U> ClientCache<T, U> ofCache(final Cache<T, U> cache) {
52-
return new CaffeineCache<T, U>(cache);
52+
return new CaffeineCache<>(cache);
5353
}
5454
}
5555

guava/src/main/java/com/inrupt/client/guava/GuavaCache.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
*/
3434
public class GuavaCache<T, U> implements ClientCache<T, U> {
3535

36+
private static final String KEY_NOT_NULL = "cache key may not be null!";
37+
private static final String VALUE_NOT_NULL = "cache value may not be null!";
38+
3639
private final Cache<T, U> cache;
3740

3841
/**
@@ -41,25 +44,25 @@ public class GuavaCache<T, U> implements ClientCache<T, U> {
4144
* @param cache the guava cache
4245
*/
4346
public GuavaCache(final Cache<T, U> cache) {
44-
this.cache = Objects.requireNonNull(cache, "cache may not be null!");
47+
this.cache = Objects.requireNonNull(cache, KEY_NOT_NULL);
4548
}
4649

4750
@Override
4851
public U get(final T key) {
49-
Objects.requireNonNull(key, "cache key may not be null!");
52+
Objects.requireNonNull(key, KEY_NOT_NULL);
5053
return cache.getIfPresent(key);
5154
}
5255

5356
@Override
5457
public void put(final T key, final U value) {
55-
Objects.requireNonNull(key, "cache key may not be null!");
56-
Objects.requireNonNull(value, "cache value may not be null!");
58+
Objects.requireNonNull(key, KEY_NOT_NULL);
59+
Objects.requireNonNull(value, VALUE_NOT_NULL);
5760
cache.put(key, value);
5861
}
5962

6063
@Override
6164
public void invalidate(final T key) {
62-
Objects.requireNonNull(key, "cache key may not be null!");
65+
Objects.requireNonNull(key, KEY_NOT_NULL);
6366
cache.invalidate(key);
6467
}
6568

guava/src/main/java/com/inrupt/client/guava/GuavaCacheBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public <T, U> ClientCache<T, U> build(final int maximumSize, final Duration dura
5050
* @return a cache suitable for use in the Inrupt Client libraries
5151
*/
5252
public static <T, U> ClientCache<T, U> ofCache(final Cache<T, U> cache) {
53-
return new GuavaCache<T, U>(cache);
53+
return new GuavaCache<>(cache);
5454
}
5555
}
5656

openid/src/main/java/com/inrupt/client/openid/OpenIdSession.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ public Optional<Credential> fromCache(final Request request) {
230230
return Optional.empty();
231231
}
232232

233+
@Override
234+
public void reset() {
235+
credential.set(null);
236+
requestCache.invalidateAll();
237+
}
238+
233239
@Override
234240
public CompletionStage<Optional<Credential>> authenticate(final Authenticator auth,
235241
final Request request, final Set<String> algorithms) {

openid/src/test/java/com/inrupt/client/openid/OpenIdSessionTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import com.inrupt.client.Request;
3030
import com.inrupt.client.auth.Authenticator;
31+
import com.inrupt.client.auth.Challenge;
3132
import com.inrupt.client.auth.Credential;
3233
import com.inrupt.client.auth.DPoP;
3334
import com.inrupt.client.auth.Session;
@@ -158,9 +159,14 @@ void testClientCredentials() {
158159
final Optional<URI> principal = session.getPrincipal();
159160
assertEquals(Optional.of(URI.create(WEBID)), principal);
160161
assertFalse(session.fromCache(null).isPresent());
161-
final Optional<Credential> credential = session.authenticate(null, Collections.emptySet())
162+
final Authenticator auth = new OpenIdAuthenticationProvider().getAuthenticator(Challenge.of("Bearer"));
163+
final Request req = Request.newBuilder(URI.create("https://storage.example")).build();
164+
final Optional<Credential> credential = session.authenticate(auth, req, Collections.emptySet())
162165
.toCompletableFuture().join();
163166
assertEquals(Optional.of(URI.create(WEBID)), credential.flatMap(Credential::getPrincipal));
167+
assertTrue(session.fromCache(req).isPresent());
168+
session.reset();
169+
assertFalse(session.fromCache(req).isPresent());
164170
}
165171

166172
@Test

0 commit comments

Comments
 (0)