25
25
import java .net .URI ;
26
26
import java .net .URISyntaxException ;
27
27
import java .util .Collections ;
28
- import java .util .HashSet ;
29
28
import java .util .LinkedHashSet ;
30
29
import java .util .Set ;
31
30
import java .util .function .Consumer ;
36
35
* @author Joe Grandja
37
36
* @author Anoop Garlapati
38
37
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-2">Section 2 Client Registration</a>
38
+ * @since 0.0.1
39
39
*/
40
40
public class RegisteredClient implements Serializable {
41
41
private static final long serialVersionUID = Version .SERIAL_VERSION_UID ;
42
42
private String id ;
43
43
private String clientId ;
44
44
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 ;
50
49
51
50
protected RegisteredClient () {
52
51
}
@@ -157,8 +156,7 @@ public static class Builder implements Serializable {
157
156
private String id ;
158
157
private String clientId ;
159
158
private String clientSecret ;
160
- private Set <ClientAuthenticationMethod > clientAuthenticationMethods =
161
- new LinkedHashSet <>(Collections .singletonList (ClientAuthenticationMethod .BASIC ));
159
+ private Set <ClientAuthenticationMethod > clientAuthenticationMethods = new LinkedHashSet <>();
162
160
private Set <AuthorizationGrantType > authorizationGrantTypes = new LinkedHashSet <>();
163
161
private Set <String > redirectUris = new LinkedHashSet <>();
164
162
private Set <String > scopes = new LinkedHashSet <>();
@@ -171,13 +169,18 @@ protected Builder(RegisteredClient registeredClient) {
171
169
this .id = registeredClient .id ;
172
170
this .clientId = registeredClient .clientId ;
173
171
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
+ }
181
184
}
182
185
183
186
/**
@@ -214,8 +217,8 @@ public Builder clientSecret(String clientSecret) {
214
217
}
215
218
216
219
/**
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.
219
222
*
220
223
* @param clientAuthenticationMethod the authentication method
221
224
* @return the {@link Builder}
@@ -226,10 +229,10 @@ public Builder clientAuthenticationMethod(ClientAuthenticationMethod clientAuthe
226
229
}
227
230
228
231
/**
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 .
231
234
*
232
- * @param clientAuthenticationMethodsConsumer the authentication method(s) {@link Consumer}
235
+ * @param clientAuthenticationMethodsConsumer a {@code Consumer} of the authentication method(s)
233
236
* @return the {@link Builder}
234
237
*/
235
238
public Builder clientAuthenticationMethods (
@@ -239,8 +242,7 @@ public Builder clientAuthenticationMethods(
239
242
}
240
243
241
244
/**
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.
244
246
*
245
247
* @param authorizationGrantType the authorization grant type
246
248
* @return the {@link Builder}
@@ -251,9 +253,10 @@ public Builder authorizationGrantType(AuthorizationGrantType authorizationGrantT
251
253
}
252
254
253
255
/**
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.
255
258
*
256
- * @param authorizationGrantTypesConsumer the authorization grant type(s) {@link Consumer}
259
+ * @param authorizationGrantTypesConsumer a {@code Consumer} of the authorization grant type(s)
257
260
* @return the {@link Builder}
258
261
*/
259
262
public Builder authorizationGrantTypes (Consumer <Set <AuthorizationGrantType >> authorizationGrantTypesConsumer ) {
@@ -262,9 +265,9 @@ public Builder authorizationGrantTypes(Consumer<Set<AuthorizationGrantType>> aut
262
265
}
263
266
264
267
/**
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 .
266
269
*
267
- * @param redirectUri the redirect URI to add
270
+ * @param redirectUri the redirect URI
268
271
* @return the {@link Builder}
269
272
*/
270
273
public Builder redirectUri (String redirectUri ) {
@@ -273,9 +276,10 @@ public Builder redirectUri(String redirectUri) {
273
276
}
274
277
275
278
/**
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.
277
281
*
278
- * @param redirectUrisConsumer the redirect URI(s) {@link Consumer}
282
+ * @param redirectUrisConsumer a {@link Consumer} of the redirect URI(s)
279
283
* @return the {@link Builder}
280
284
*/
281
285
public Builder redirectUris (Consumer <Set <String >> redirectUrisConsumer ) {
@@ -284,9 +288,9 @@ public Builder redirectUris(Consumer<Set<String>> redirectUrisConsumer) {
284
288
}
285
289
286
290
/**
287
- * Adds the scope to the set of scopes used by the client .
291
+ * Adds a scope the client may use .
288
292
*
289
- * @param scope the scope to add
293
+ * @param scope the scope
290
294
* @return the {@link Builder}
291
295
*/
292
296
public Builder scope (String scope ) {
@@ -295,9 +299,10 @@ public Builder scope(String scope) {
295
299
}
296
300
297
301
/**
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.
299
304
*
300
- * @param scopesConsumer the scope(s) {@link Consumer}
305
+ * @param scopesConsumer a {@link Consumer} of the scope(s)
301
306
* @return the {@link Builder}
302
307
*/
303
308
public Builder scopes (Consumer <Set <String >> scopesConsumer ) {
@@ -311,17 +316,18 @@ public Builder scopes(Consumer<Set<String>> scopesConsumer) {
311
316
* @return a {@link RegisteredClient}
312
317
*/
313
318
public RegisteredClient build () {
314
- Assert .notEmpty (this .clientAuthenticationMethods , "clientAuthenticationMethods cannot be empty" );
319
+ Assert .hasText (this .clientId , "clientId cannot be empty" );
315
320
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 )) {
319
322
Assert .hasText (this .clientSecret , "clientSecret cannot be empty" );
320
323
Assert .notEmpty (this .redirectUris , "redirectUris cannot be empty" );
321
324
}
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 ();
325
331
}
326
332
327
333
private RegisteredClient create () {
@@ -380,5 +386,4 @@ private static boolean validateRedirectUri(String redirectUri) {
380
386
}
381
387
}
382
388
}
383
-
384
389
}
0 commit comments