Skip to content

Commit 88447bd

Browse files
committed
Merge branch '0.4.x' into 1.0.x
Closes gh-1382
2 parents 74040a6 + 6d21a65 commit 88447bd

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/oidc/authentication/OidcClientRegistrationAuthenticationProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ private OidcClientRegistrationAuthenticationToken registerClient(OidcClientRegis
203203
.clientSecret(this.passwordEncoder.encode(registeredClient.getClientSecret()))
204204
.build();
205205
this.registeredClientRepository.save(updatedRegisteredClient);
206+
if (ClientAuthenticationMethod.CLIENT_SECRET_JWT.getValue().equals(clientRegistrationAuthentication.getClientRegistration().getTokenEndpointAuthenticationMethod())) {
207+
// gh-1344 Return the hashed client_secret
208+
registeredClient = updatedRegisteredClient;
209+
}
206210
} else {
207211
this.registeredClientRepository.save(registeredClient);
208212
}

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/config/annotation/web/configurers/OidcClientRegistrationTests.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
6969
import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter;
7070
import org.springframework.security.oauth2.jose.TestJwks;
71+
import org.springframework.security.oauth2.jose.jws.MacAlgorithm;
7172
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
7273
import org.springframework.security.oauth2.jwt.JwsHeader;
7374
import org.springframework.security.oauth2.jwt.Jwt;
@@ -103,6 +104,8 @@
103104
import org.springframework.test.web.servlet.MvcResult;
104105
import org.springframework.web.util.UriComponentsBuilder;
105106

107+
import javax.crypto.spec.SecretKeySpec;
108+
106109
import static org.assertj.core.api.Assertions.assertThat;
107110
import static org.hamcrest.CoreMatchers.containsString;
108111
import static org.mockito.ArgumentMatchers.any;
@@ -400,6 +403,55 @@ public void requestWhenClientRegistersWithSecretThenClientAuthenticationSuccess(
400403
.andReturn();
401404
}
402405

406+
// gh-1344
407+
@Test
408+
public void requestWhenClientRegistersWithClientSecretJwtThenClientAuthenticationSuccess() throws Exception {
409+
this.spring.register(AuthorizationServerConfiguration.class).autowire();
410+
411+
// @formatter:off
412+
OidcClientRegistration clientRegistration = OidcClientRegistration.builder()
413+
.clientName("client-name")
414+
.redirectUri("https://client.example.com")
415+
.grantType(AuthorizationGrantType.AUTHORIZATION_CODE.getValue())
416+
.grantType(AuthorizationGrantType.CLIENT_CREDENTIALS.getValue())
417+
.tokenEndpointAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_JWT.getValue())
418+
.scope("scope1")
419+
.scope("scope2")
420+
.build();
421+
// @formatter:on
422+
423+
OidcClientRegistration clientRegistrationResponse = registerClient(clientRegistration);
424+
425+
JwsHeader jwsHeader = JwsHeader.with(MacAlgorithm.HS256)
426+
.build();
427+
428+
Instant issuedAt = Instant.now();
429+
Instant expiresAt = issuedAt.plus(1, ChronoUnit.HOURS);
430+
JwtClaimsSet jwtClaimsSet = JwtClaimsSet.builder()
431+
.issuer(clientRegistrationResponse.getClientId())
432+
.subject(clientRegistrationResponse.getClientId())
433+
.audience(Collections.singletonList(asUrl(this.authorizationServerSettings.getIssuer(), this.authorizationServerSettings.getTokenEndpoint())))
434+
.issuedAt(issuedAt)
435+
.expiresAt(expiresAt)
436+
.build();
437+
438+
JWKSet jwkSet = new JWKSet(TestJwks.jwk(
439+
new SecretKeySpec(clientRegistrationResponse.getClientSecret().getBytes(), "HS256")).build());
440+
JwtEncoder jwtClientAssertionEncoder = new NimbusJwtEncoder((jwkSelector, securityContext) -> jwkSelector.select(jwkSet));
441+
442+
Jwt jwtAssertion = jwtClientAssertionEncoder.encode(JwtEncoderParameters.from(jwsHeader, jwtClaimsSet));
443+
444+
this.mvc.perform(post(DEFAULT_TOKEN_ENDPOINT_URI)
445+
.param(OAuth2ParameterNames.GRANT_TYPE, AuthorizationGrantType.CLIENT_CREDENTIALS.getValue())
446+
.param(OAuth2ParameterNames.SCOPE, "scope1")
447+
.param(OAuth2ParameterNames.CLIENT_ASSERTION_TYPE, "urn:ietf:params:oauth:client-assertion-type:jwt-bearer")
448+
.param(OAuth2ParameterNames.CLIENT_ASSERTION, jwtAssertion.getTokenValue())
449+
.param(OAuth2ParameterNames.CLIENT_ID, clientRegistrationResponse.getClientId()))
450+
.andExpect(status().isOk())
451+
.andExpect(jsonPath("$.access_token").isNotEmpty())
452+
.andExpect(jsonPath("$.scope").value("scope1"));
453+
}
454+
403455
private OidcClientRegistration registerClient(OidcClientRegistration clientRegistration) throws Exception {
404456
// ***** (1) Obtain the "initial" access token used for registering the client
405457

0 commit comments

Comments
 (0)