Skip to content

Commit 88d604f

Browse files
committed
Merge branch '1.1.x'
2 parents 2dcbc58 + 0c4dcf5 commit 88d604f

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
@@ -217,6 +217,10 @@ private OidcClientRegistrationAuthenticationToken registerClient(OidcClientRegis
217217
.clientSecret(this.passwordEncoder.encode(registeredClient.getClientSecret()))
218218
.build();
219219
this.registeredClientRepository.save(updatedRegisteredClient);
220+
if (ClientAuthenticationMethod.CLIENT_SECRET_JWT.getValue().equals(clientRegistrationAuthentication.getClientRegistration().getTokenEndpointAuthenticationMethod())) {
221+
// gh-1344 Return the hashed client_secret
222+
registeredClient = updatedRegisteredClient;
223+
}
220224
} else {
221225
this.registeredClientRepository.save(registeredClient);
222226
}

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
@@ -23,6 +23,8 @@
2323
import java.util.Map;
2424
import java.util.function.Consumer;
2525

26+
import javax.crypto.spec.SecretKeySpec;
27+
2628
import jakarta.servlet.http.HttpServletResponse;
2729

2830
import com.nimbusds.jose.jwk.JWKSet;
@@ -70,6 +72,7 @@
7072
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
7173
import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter;
7274
import org.springframework.security.oauth2.jose.TestJwks;
75+
import org.springframework.security.oauth2.jose.jws.MacAlgorithm;
7376
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
7477
import org.springframework.security.oauth2.jwt.JwsHeader;
7578
import org.springframework.security.oauth2.jwt.Jwt;
@@ -406,6 +409,55 @@ public void requestWhenClientRegistersWithSecretThenClientAuthenticationSuccess(
406409
.andReturn();
407410
}
408411

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

0 commit comments

Comments
 (0)