4444import org .springframework .context .annotation .Bean ;
4545import org .springframework .context .annotation .Configuration ;
4646import org .springframework .context .annotation .Import ;
47+ import org .springframework .context .annotation .Primary ;
4748import org .springframework .http .HttpHeaders ;
4849import org .springframework .jdbc .core .JdbcOperations ;
4950import org .springframework .jdbc .core .JdbcTemplate ;
@@ -159,6 +160,8 @@ public class OAuth2ClientCredentialsGrantTests {
159160
160161 private static AuthenticationFailureHandler authenticationFailureHandler ;
161162
163+ private static PasswordEncoder passwordEncoder ;
164+
162165 public final SpringTestContext spring = new SpringTestContext ();
163166
164167 @ Autowired
@@ -184,6 +187,9 @@ public static void init() {
184187 authenticationProvidersConsumer = mock (Consumer .class );
185188 authenticationSuccessHandler = mock (AuthenticationSuccessHandler .class );
186189 authenticationFailureHandler = mock (AuthenticationFailureHandler .class );
190+ passwordEncoder = mock (PasswordEncoder .class );
191+ given (passwordEncoder .matches (any (), any ())).willReturn (true );
192+ given (passwordEncoder .upgradeEncoding (any ())).willReturn (false );
187193 db = new EmbeddedDatabaseBuilder ().generateUniqueName (true )
188194 .setType (EmbeddedDatabaseType .HSQL )
189195 .setScriptEncoding ("UTF-8" )
@@ -495,6 +501,26 @@ public void requestWhenTokenRequestWithDPoPProofThenReturnDPoPBoundAccessToken()
495501 .andExpect (jsonPath ("$.token_type" ).value (OAuth2AccessToken .TokenType .DPOP .getValue ()));
496502 }
497503
504+ @ Test
505+ public void requestWhenTokenRequestWithMultiplePasswordEncodersThenPrimaryPasswordEncoderUsed () throws Exception {
506+ this .spring .register (AuthorizationServerConfigurationWithMultiplePasswordEncoders .class ).autowire ();
507+
508+ RegisteredClient registeredClient = TestRegisteredClients .registeredClient2 ().build ();
509+ this .registeredClientRepository .save (registeredClient );
510+
511+ this .mvc
512+ .perform (post (DEFAULT_TOKEN_ENDPOINT_URI )
513+ .param (OAuth2ParameterNames .GRANT_TYPE , AuthorizationGrantType .CLIENT_CREDENTIALS .getValue ())
514+ .param (OAuth2ParameterNames .SCOPE , "scope1 scope2" )
515+ .header (HttpHeaders .AUTHORIZATION ,
516+ "Basic " + encodeBasicAuth (registeredClient .getClientId (), registeredClient .getClientSecret ())))
517+ .andExpect (status ().isOk ())
518+ .andExpect (jsonPath ("$.access_token" ).isNotEmpty ())
519+ .andExpect (jsonPath ("$.scope" ).value ("scope1 scope2" ));
520+
521+ verify (passwordEncoder ).matches (any (), any ());
522+ }
523+
498524 private static String generateDPoPProof (String tokenEndpointUri ) {
499525 // @formatter:off
500526 Map <String , Object > publicJwk = TestJwks .DEFAULT_EC_JWK
@@ -685,4 +711,16 @@ AuthorizationServerSettings authorizationServerSettings() {
685711
686712 }
687713
714+ @ EnableWebSecurity
715+ @ Configuration (proxyBeanMethods = false )
716+ static class AuthorizationServerConfigurationWithMultiplePasswordEncoders extends AuthorizationServerConfiguration {
717+
718+ @ Primary
719+ @ Bean
720+ PasswordEncoder primaryPasswordEncoder () {
721+ return passwordEncoder ;
722+ }
723+
724+ }
725+
688726}
0 commit comments