Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added method to get the name of the identity property of the username… #951

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ public interface TokenProvider {
default void invalidate(ConnectionContext connectionContext) {
}

/**
* Provides the name of the property in jwt which is effective for user identity (i.e. client_id for ClientCredentialsTokenProvider and user_name for PasswordGrantTokenProvider)
*
* @return
*/
default String getUserIdentityProperty(){
return "user_name";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ Mono<Void> tokenRequestTransformer(Mono<HttpClientRequest> outbound) {
.then());
}

@Override
String getIdentityZoneSubdomain() {
return "client_id";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ Mono<Void> tokenRequestTransformer(Mono<HttpClientRequest> outbound) {
.then());
}

@Override
String getIdentityZoneSubdomain() {
return "client_id";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ Mono<Void> tokenRequestTransformer(Mono<HttpClientRequest> outbound) {
.then());
}

@Override
String getIdentityZoneSubdomain() {
return "user_name";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ Mono<Void> tokenRequestTransformer(Mono<HttpClientRequest> outbound) {
.then());
}

@Override
String getIdentityZoneSubdomain() {
return "client_id";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private String getUsername(String token) {
.parseClaimsJws(token);

return Optional
.ofNullable(jws.getBody().get("user_name", String.class))
.ofNullable(jws.getBody().get(this.tokenProvider.getUserIdentityProperty(), String.class))
.orElseThrow(() -> new IllegalStateException("Unable to retrieve username from token"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public void getInvalidToken() throws NoSuchAlgorithmException {
String invalidToken = String.format("bearer %s", getToken(keyPair.getPrivate(), Instant.now().minus(Duration.ofHours(1))));
String validToken = String.format("bearer %s", getToken(keyPair.getPrivate(), Instant.now().plus(Duration.ofHours(1))));
when(this.tokenProvider.getToken(this.connectionContext)).thenReturn(Mono.just(invalidToken), Mono.just(validToken));

when(this.tokenProvider.getUserIdentityProperty()).thenReturn("user_name");

this.usernameProvider
.get()
.as(StepVerifier::create)
Expand All @@ -79,21 +80,40 @@ public void getValidToken() throws NoSuchAlgorithmException {

String token = String.format("bearer %s", getToken(keyPair.getPrivate(), Instant.now().plus(Duration.ofHours(1))));
when(this.tokenProvider.getToken(this.connectionContext)).thenReturn(Mono.just(token));

when(this.tokenProvider.getUserIdentityProperty()).thenReturn("user_name");

this.usernameProvider
.get()
.as(StepVerifier::create)
.expectNext("test-username")
.expectComplete()
.verify(Duration.ofSeconds(1));
}

@Test
public void getValidTokenWithClient() throws NoSuchAlgorithmException {
KeyPair keyPair = getKeyPair();
when(this.signingKeyResolver.resolveSigningKey(any(JwsHeader.class), any(Claims.class))).thenReturn(keyPair.getPublic());

String token = String.format("bearer %s", getToken(keyPair.getPrivate(), Instant.now().plus(Duration.ofHours(1))));
when(this.tokenProvider.getToken(this.connectionContext)).thenReturn(Mono.just(token));
when(this.tokenProvider.getUserIdentityProperty()).thenReturn("client_id");

this.usernameProvider
.get()
.as(StepVerifier::create)
.expectNext("test-client")
.expectComplete()
.verify(Duration.ofSeconds(1));
}

@SuppressWarnings("unchecked")
private static String getToken(PrivateKey privateKey, Instant expiration) {
return Jwts.builder()
.setHeader((Map<String, Object>) new DefaultJwsHeader().setKeyId("test-key"))
.signWith(SignatureAlgorithm.RS256, privateKey)
.claim("user_name", "test-username")
.claim("client_id", "test-client")
.setExpiration(Date.from(expiration))
.compact();
}
Expand Down