Skip to content

Commit e7588fb

Browse files
edouardhuerwinch
authored andcommitted
Fixed validation in ClientRegistration.Builder
ClientRegistration.Builder defaulted to validating as an authorization_code registration, though a custom grant type could be in use. The actual grant_type is now verified for every case. - Fixed validation in ClientRegistration.Builder - New test that fails unless the issue is fixed. Also made OAuth2AuthorizationGrantRequestEntityUtils public to help implementing custom token response clients. Fixes gh-7040
1 parent 57bc456 commit e7588fb

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/ClientRegistration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ public ClientRegistration build() {
486486
this.validateClientCredentialsGrantType();
487487
} else if (AuthorizationGrantType.IMPLICIT.equals(this.authorizationGrantType)) {
488488
this.validateImplicitGrantType();
489-
} else {
489+
} else if (AuthorizationGrantType.AUTHORIZATION_CODE.equals(this.authorizationGrantType)) {
490490
this.validateAuthorizationCodeGrantType();
491491
}
492492
return this.create();

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/registration/ClientRegistrationTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,4 +535,27 @@ public void buildWhenClientCredentialsGrantTokenUriIsNullThenThrowIllegalArgumen
535535
.build()
536536
).isInstanceOf(IllegalArgumentException.class);
537537
}
538+
539+
@Test
540+
public void buildWhenCustomGrantAllAttributesProvidedThenAllAttributesAreSet() {
541+
AuthorizationGrantType customGrantType = new AuthorizationGrantType("CUSTOM");
542+
ClientRegistration registration = ClientRegistration.withRegistrationId(REGISTRATION_ID)
543+
.clientId(CLIENT_ID)
544+
.clientSecret(CLIENT_SECRET)
545+
.clientAuthenticationMethod(ClientAuthenticationMethod.BASIC)
546+
.authorizationGrantType(customGrantType)
547+
.scope(SCOPES.toArray(new String[0]))
548+
.tokenUri(TOKEN_URI)
549+
.clientName(CLIENT_NAME)
550+
.build();
551+
552+
assertThat(registration.getRegistrationId()).isEqualTo(REGISTRATION_ID);
553+
assertThat(registration.getClientId()).isEqualTo(CLIENT_ID);
554+
assertThat(registration.getClientSecret()).isEqualTo(CLIENT_SECRET);
555+
assertThat(registration.getClientAuthenticationMethod()).isEqualTo(ClientAuthenticationMethod.BASIC);
556+
assertThat(registration.getAuthorizationGrantType()).isEqualTo(customGrantType);
557+
assertThat(registration.getScopes()).isEqualTo(SCOPES);
558+
assertThat(registration.getProviderDetails().getTokenUri()).isEqualTo(TOKEN_URI);
559+
assertThat(registration.getClientName()).isEqualTo(CLIENT_NAME);
560+
}
538561
}

0 commit comments

Comments
 (0)