Skip to content

Commit 78f44c7

Browse files
committed
Polish gh-70
1 parent 8700ff1 commit 78f44c7

File tree

6 files changed

+84
-104
lines changed

6 files changed

+84
-104
lines changed

core/src/main/java/org/springframework/security/oauth2/server/authorization/Version.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@
1919
* Internal class used for serialization across Spring Security Authorization Server classes.
2020
*
2121
* @author Anoop Garlapati
22+
* @since 0.0.1
2223
*/
23-
public class Version {
24+
public final class Version {
25+
private static final int MAJOR = 0;
26+
private static final int MINOR = 0;
27+
private static final int PATCH = 1;
28+
2429
/**
2530
* Global Serialization value for Spring Security Authorization Server classes.
2631
*/
27-
public static final long SERIAL_VERSION_UID = "0.0.1".hashCode();
32+
public static final long SERIAL_VERSION_UID = getVersion().hashCode();
33+
34+
public static String getVersion() {
35+
return MAJOR + "." + MINOR + "." + PATCH;
36+
}
2837
}

core/src/main/java/org/springframework/security/oauth2/server/authorization/client/InMemoryRegisteredClientRepository.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* @author Anoop Garlapati
2929
* @see RegisteredClientRepository
3030
* @see RegisteredClient
31+
* @since 0.0.1
3132
*/
3233
public final class InMemoryRegisteredClientRepository implements RegisteredClientRepository {
3334
private final Map<String, RegisteredClient> idRegistrationMap;
@@ -66,8 +67,8 @@ public InMemoryRegisteredClientRepository(List<RegisteredClient> registrations)
6667
idRegistrationMapResult.put(id, registration);
6768
clientIdRegistrationMapResult.put(clientId, registration);
6869
}
69-
idRegistrationMap = idRegistrationMapResult;
70-
clientIdRegistrationMap = clientIdRegistrationMapResult;
70+
this.idRegistrationMap = idRegistrationMapResult;
71+
this.clientIdRegistrationMap = clientIdRegistrationMapResult;
7172
}
7273

7374
@Override

core/src/main/java/org/springframework/security/oauth2/server/authorization/client/RegisteredClient.java

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.net.URI;
2626
import java.net.URISyntaxException;
2727
import java.util.Collections;
28-
import java.util.HashSet;
2928
import java.util.LinkedHashSet;
3029
import java.util.Set;
3130
import java.util.function.Consumer;
@@ -36,17 +35,17 @@
3635
* @author Joe Grandja
3736
* @author Anoop Garlapati
3837
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-2">Section 2 Client Registration</a>
38+
* @since 0.0.1
3939
*/
4040
public class RegisteredClient implements Serializable {
4141
private static final long serialVersionUID = Version.SERIAL_VERSION_UID;
4242
private String id;
4343
private String clientId;
4444
private String clientSecret;
45-
private Set<ClientAuthenticationMethod> clientAuthenticationMethods =
46-
Collections.singleton(ClientAuthenticationMethod.BASIC);
47-
private Set<AuthorizationGrantType> authorizationGrantTypes = Collections.emptySet();
48-
private Set<String> redirectUris = Collections.emptySet();
49-
private Set<String> scopes = Collections.emptySet();
45+
private Set<ClientAuthenticationMethod> clientAuthenticationMethods;
46+
private Set<AuthorizationGrantType> authorizationGrantTypes;
47+
private Set<String> redirectUris;
48+
private Set<String> scopes;
5049

5150
protected RegisteredClient() {
5251
}
@@ -157,8 +156,7 @@ public static class Builder implements Serializable {
157156
private String id;
158157
private String clientId;
159158
private String clientSecret;
160-
private Set<ClientAuthenticationMethod> clientAuthenticationMethods =
161-
new LinkedHashSet<>(Collections.singletonList(ClientAuthenticationMethod.BASIC));
159+
private Set<ClientAuthenticationMethod> clientAuthenticationMethods = new LinkedHashSet<>();
162160
private Set<AuthorizationGrantType> authorizationGrantTypes = new LinkedHashSet<>();
163161
private Set<String> redirectUris = new LinkedHashSet<>();
164162
private Set<String> scopes = new LinkedHashSet<>();
@@ -171,13 +169,18 @@ protected Builder(RegisteredClient registeredClient) {
171169
this.id = registeredClient.id;
172170
this.clientId = registeredClient.clientId;
173171
this.clientSecret = registeredClient.clientSecret;
174-
this.clientAuthenticationMethods = registeredClient.clientAuthenticationMethods == null ? null :
175-
new HashSet<>(registeredClient.clientAuthenticationMethods);
176-
this.authorizationGrantTypes = registeredClient.authorizationGrantTypes == null ? null :
177-
new HashSet<>(registeredClient.authorizationGrantTypes);
178-
this.redirectUris = registeredClient.redirectUris == null ? null :
179-
new HashSet<>(registeredClient.redirectUris);
180-
this.scopes = registeredClient.scopes == null ? null : new HashSet<>(registeredClient.scopes);
172+
if (!CollectionUtils.isEmpty(registeredClient.clientAuthenticationMethods)) {
173+
this.clientAuthenticationMethods.addAll(registeredClient.clientAuthenticationMethods);
174+
}
175+
if (!CollectionUtils.isEmpty(registeredClient.authorizationGrantTypes)) {
176+
this.authorizationGrantTypes.addAll(registeredClient.authorizationGrantTypes);
177+
}
178+
if (!CollectionUtils.isEmpty(registeredClient.redirectUris)) {
179+
this.redirectUris.addAll(registeredClient.redirectUris);
180+
}
181+
if (!CollectionUtils.isEmpty(registeredClient.scopes)) {
182+
this.scopes.addAll(registeredClient.scopes);
183+
}
181184
}
182185

183186
/**
@@ -214,8 +217,8 @@ public Builder clientSecret(String clientSecret) {
214217
}
215218

216219
/**
217-
* Adds the {@link ClientAuthenticationMethod authentication method} to the set of
218-
* client authentication methods used when authenticating the client with the authorization server.
220+
* Adds an {@link ClientAuthenticationMethod authentication method}
221+
* the client may use when authenticating with the authorization server.
219222
*
220223
* @param clientAuthenticationMethod the authentication method
221224
* @return the {@link Builder}
@@ -226,10 +229,10 @@ public Builder clientAuthenticationMethod(ClientAuthenticationMethod clientAuthe
226229
}
227230

228231
/**
229-
* Sets the {@link ClientAuthenticationMethod authentication method(s)} used
230-
* when authenticating the client with the authorization server.
232+
* A {@code Consumer} of the {@link ClientAuthenticationMethod authentication method(s)}
233+
* allowing the ability to add, replace, or remove.
231234
*
232-
* @param clientAuthenticationMethodsConsumer the authentication method(s) {@link Consumer}
235+
* @param clientAuthenticationMethodsConsumer a {@code Consumer} of the authentication method(s)
233236
* @return the {@link Builder}
234237
*/
235238
public Builder clientAuthenticationMethods(
@@ -239,8 +242,7 @@ public Builder clientAuthenticationMethods(
239242
}
240243

241244
/**
242-
* Adds the {@link AuthorizationGrantType authorization grant type} to
243-
* the set of authorization grant types that the client may use.
245+
* Adds an {@link AuthorizationGrantType authorization grant type} the client may use.
244246
*
245247
* @param authorizationGrantType the authorization grant type
246248
* @return the {@link Builder}
@@ -251,9 +253,10 @@ public Builder authorizationGrantType(AuthorizationGrantType authorizationGrantT
251253
}
252254

253255
/**
254-
* Sets the {@link AuthorizationGrantType authorization grant type(s)} that the client may use.
256+
* A {@code Consumer} of the {@link AuthorizationGrantType authorization grant type(s)}
257+
* allowing the ability to add, replace, or remove.
255258
*
256-
* @param authorizationGrantTypesConsumer the authorization grant type(s) {@link Consumer}
259+
* @param authorizationGrantTypesConsumer a {@code Consumer} of the authorization grant type(s)
257260
* @return the {@link Builder}
258261
*/
259262
public Builder authorizationGrantTypes(Consumer<Set<AuthorizationGrantType>> authorizationGrantTypesConsumer) {
@@ -262,9 +265,9 @@ public Builder authorizationGrantTypes(Consumer<Set<AuthorizationGrantType>> aut
262265
}
263266

264267
/**
265-
* Adds the redirect URI to the set of redirect URIs that the client may use in redirect-based flows.
268+
* Adds a redirect URI the client may use in a redirect-based flow.
266269
*
267-
* @param redirectUri the redirect URI to add
270+
* @param redirectUri the redirect URI
268271
* @return the {@link Builder}
269272
*/
270273
public Builder redirectUri(String redirectUri) {
@@ -273,9 +276,10 @@ public Builder redirectUri(String redirectUri) {
273276
}
274277

275278
/**
276-
* Sets the redirect URI(s) that the client may use in redirect-based flows.
279+
* A {@code Consumer} of the redirect URI(s)
280+
* allowing the ability to add, replace, or remove.
277281
*
278-
* @param redirectUrisConsumer the redirect URI(s) {@link Consumer}
282+
* @param redirectUrisConsumer a {@link Consumer} of the redirect URI(s)
279283
* @return the {@link Builder}
280284
*/
281285
public Builder redirectUris(Consumer<Set<String>> redirectUrisConsumer) {
@@ -284,9 +288,9 @@ public Builder redirectUris(Consumer<Set<String>> redirectUrisConsumer) {
284288
}
285289

286290
/**
287-
* Adds the scope to the set of scopes used by the client.
291+
* Adds a scope the client may use.
288292
*
289-
* @param scope the scope to add
293+
* @param scope the scope
290294
* @return the {@link Builder}
291295
*/
292296
public Builder scope(String scope) {
@@ -295,9 +299,10 @@ public Builder scope(String scope) {
295299
}
296300

297301
/**
298-
* Sets the scope(s) used by the client.
302+
* A {@code Consumer} of the scope(s)
303+
* allowing the ability to add, replace, or remove.
299304
*
300-
* @param scopesConsumer the scope(s) {@link Consumer}
305+
* @param scopesConsumer a {@link Consumer} of the scope(s)
301306
* @return the {@link Builder}
302307
*/
303308
public Builder scopes(Consumer<Set<String>> scopesConsumer) {
@@ -311,17 +316,18 @@ public Builder scopes(Consumer<Set<String>> scopesConsumer) {
311316
* @return a {@link RegisteredClient}
312317
*/
313318
public RegisteredClient build() {
314-
Assert.notEmpty(this.clientAuthenticationMethods, "clientAuthenticationMethods cannot be empty");
319+
Assert.hasText(this.clientId, "clientId cannot be empty");
315320
Assert.notEmpty(this.authorizationGrantTypes, "authorizationGrantTypes cannot be empty");
316-
if (authorizationGrantTypes.contains(AuthorizationGrantType.AUTHORIZATION_CODE)) {
317-
Assert.hasText(this.id, "id cannot be empty");
318-
Assert.hasText(this.clientId, "clientId cannot be empty");
321+
if (this.authorizationGrantTypes.contains(AuthorizationGrantType.AUTHORIZATION_CODE)) {
319322
Assert.hasText(this.clientSecret, "clientSecret cannot be empty");
320323
Assert.notEmpty(this.redirectUris, "redirectUris cannot be empty");
321324
}
322-
this.validateScopes();
323-
this.validateRedirectUris();
324-
return this.create();
325+
if (CollectionUtils.isEmpty(this.clientAuthenticationMethods)) {
326+
this.clientAuthenticationMethods.add(ClientAuthenticationMethod.BASIC);
327+
}
328+
validateScopes();
329+
validateRedirectUris();
330+
return create();
325331
}
326332

327333
private RegisteredClient create() {
@@ -380,5 +386,4 @@ private static boolean validateRedirectUri(String redirectUri) {
380386
}
381387
}
382388
}
383-
384389
}

core/src/main/java/org/springframework/security/oauth2/server/authorization/client/RegisteredClientRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* @author Joe Grandja
2222
* @author Anoop Garlapati
2323
* @see RegisteredClient
24+
* @since 0.0.1
2425
*/
2526
public interface RegisteredClientRepository {
2627

core/src/test/java/org/springframework/security/oauth2/server/authorization/client/InMemoryRegisteredClientRepositoryTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void constructorListRegisteredClientWhenNullThenThrowIllegalArgumentExcep
5151
}
5252

5353
@Test
54-
public void constructorListClientRegistrationWhenEmptyThenThrowIllegalArgumentException() {
54+
public void constructorListRegisteredClientWhenEmptyThenThrowIllegalArgumentException() {
5555
assertThatThrownBy(() -> {
5656
List<RegisteredClient> registrations = Collections.emptyList();
5757
new InMemoryRegisteredClientRepository(registrations);
@@ -82,34 +82,34 @@ public void constructorListRegisteredClientWhenDuplicateClientIdThenThrowIllegal
8282
@Test
8383
public void findByIdWhenFoundThenFound() {
8484
String id = this.registration.getId();
85-
assertThat(clients.findById(id)).isEqualTo(this.registration);
85+
assertThat(this.clients.findById(id)).isEqualTo(this.registration);
8686
}
8787

8888
@Test
8989
public void findByIdWhenNotFoundThenNull() {
9090
String missingId = this.registration.getId() + "MISSING";
91-
assertThat(clients.findById(missingId)).isNull();
91+
assertThat(this.clients.findById(missingId)).isNull();
9292
}
9393

9494
@Test
9595
public void findByIdWhenNullThenThrowIllegalArgumentException() {
96-
assertThatThrownBy(() -> clients.findById(null)).isInstanceOf(IllegalArgumentException.class);
96+
assertThatThrownBy(() -> this.clients.findById(null)).isInstanceOf(IllegalArgumentException.class);
9797
}
9898

9999
@Test
100100
public void findByClientIdWhenFoundThenFound() {
101101
String clientId = this.registration.getClientId();
102-
assertThat(clients.findByClientId(clientId)).isEqualTo(this.registration);
102+
assertThat(this.clients.findByClientId(clientId)).isEqualTo(this.registration);
103103
}
104104

105105
@Test
106106
public void findByClientIdWhenNotFoundThenNull() {
107107
String missingClientId = this.registration.getClientId() + "MISSING";
108-
assertThat(clients.findByClientId(missingClientId)).isNull();
108+
assertThat(this.clients.findByClientId(missingClientId)).isNull();
109109
}
110110

111111
@Test
112112
public void findByClientIdWhenNullThenThrowIllegalArgumentException() {
113-
assertThatThrownBy(() -> clients.findByClientId(null)).isInstanceOf(IllegalArgumentException.class);
113+
assertThatThrownBy(() -> this.clients.findByClientId(null)).isInstanceOf(IllegalArgumentException.class);
114114
}
115115
}

0 commit comments

Comments
 (0)