Skip to content

Commit ccfe0f2

Browse files
Change Builder name
1 parent c1f2f7d commit ccfe0f2

File tree

4 files changed

+120
-101
lines changed

4 files changed

+120
-101
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/introspection/SpringOpaqueTokenIntrospector.java

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class SpringOpaqueTokenIntrospector implements OpaqueTokenIntrospector {
8282
* @param introspectionUri The introspection endpoint uri
8383
* @param clientId The client id authorized to introspect
8484
* @param clientSecret The client's secret
85-
* @deprecated
85+
* @deprecated Please use {@link SpringOpaqueTokenIntrospector.Builder}
8686
*/
8787
@Deprecated(since = "6.5", forRemoval = true)
8888
public SpringOpaqueTokenIntrospector(String introspectionUri, String clientId, String clientSecret) {
@@ -300,92 +300,101 @@ default List<String> getScopes() {
300300

301301
}
302302

303+
/**
304+
* Creates a {@code SpringOpaqueTokenIntrospector.Builder} with the provided
305+
* parameters
306+
* @param introspectionUri The introspection endpoint uri
307+
* @return the {@link SpringOpaqueTokenIntrospector.Builder}
308+
* @since 6.5
309+
*/
310+
public static Builder withIntrospectionUri(String introspectionUri) {
311+
Assert.notNull(introspectionUri, "introspectionUri cannot be null");
312+
return new Builder(introspectionUri);
313+
}
314+
303315
/**
304316
* Used to build {@link SpringOpaqueTokenIntrospector}.
305317
*
306318
* @author Ngoc Nhan
307319
* @since 6.5
308320
*/
309-
public static final class SpringOpaqueTokenIntrospectorBuilder {
321+
public static final class Builder {
310322

311323
private final String introspectionUri;
312324

313-
private SpringOpaqueTokenIntrospectorBuilder(String introspectionUri) {
325+
private String clientId;
326+
327+
private String clientSecret;
328+
329+
private Builder(String introspectionUri) {
314330
this.introspectionUri = introspectionUri;
315331
}
316332

317333
/**
318-
* Creates a {@code SpringOpaqueTokenIntrospectorBuilder} with the provided
334+
* Creates a {@code SpringOpaqueTokenIntrospector.Builder} with the provided
319335
* parameters
320-
* @param introspectionUri The introspection endpoint uri
321-
* @return the {@link SpringOpaqueTokenIntrospectorBuilder}
336+
* @param clientId The client id authorized that should be encoded
337+
* @param charset The charset to use
338+
* @return the {@link SpringOpaqueTokenIntrospector.Builder}
322339
* @since 6.5
323340
*/
324-
public static SpringOpaqueTokenIntrospectorBuilder withIntrospectionUri(String introspectionUri) {
325-
Assert.notNull(introspectionUri, "introspectionUri cannot be null");
326-
return new SpringOpaqueTokenIntrospectorBuilder(introspectionUri);
341+
public Builder clientId(String clientId, Charset charset) {
342+
Assert.notNull(clientId, "clientId cannot be null");
343+
Assert.notNull(charset, "charset cannot be null");
344+
this.clientId = URLEncoder.encode(clientId, charset);
345+
return this;
327346
}
328347

329348
/**
330-
* Creates a {@code SpringOpaqueTokenIntrospector} with the provided parameters
331-
* @param clientId The client id authorized that should be encode
332-
* @param clientSecret The client secret that should be encode for the authorized
333-
* client
334-
* @return the {@link SpringOpaqueTokenIntrospector}
349+
* Creates a {@code SpringOpaqueTokenIntrospector.Builder} with the provided
350+
* parameters
351+
* @param clientId The client id authorized
352+
* @return the {@link SpringOpaqueTokenIntrospector.Builder}
335353
* @since 6.5
336354
*/
337-
public SpringOpaqueTokenIntrospector introspectionEncodeClientCredentials(String clientId,
338-
String clientSecret) {
339-
return this.introspectionEncodeClientCredentials(clientId, clientSecret, StandardCharsets.UTF_8);
355+
public Builder clientId(String clientId) {
356+
Assert.notNull(clientId, "clientId cannot be null");
357+
this.clientId = clientId;
358+
return this;
340359
}
341360

342361
/**
343-
* Creates a {@code SpringOpaqueTokenIntrospector} with the provided parameters
344-
* @param clientId The client id authorized that should be encode
345-
* @param clientSecret The client secret that should be encode for the authorized
346-
* client
347-
* @param charset the charset to use
348-
* @return the {@link SpringOpaqueTokenIntrospector}
362+
* Creates a {@code SpringOpaqueTokenIntrospector.Builder} with the provided
363+
* parameters
364+
* @param clientSecret The client id authorized
365+
* @param charset The charset to use
366+
* @return the {@link SpringOpaqueTokenIntrospector.Builder}
349367
* @since 6.5
350368
*/
351-
public SpringOpaqueTokenIntrospector introspectionEncodeClientCredentials(String clientId, String clientSecret,
352-
Charset charset) {
353-
Assert.notNull(clientId, "clientId cannot be null");
369+
public Builder clientSecret(String clientSecret, Charset charset) {
354370
Assert.notNull(clientSecret, "clientSecret cannot be null");
355371
Assert.notNull(charset, "charset cannot be null");
356-
String encodeClientId = URLEncoder.encode(clientId, charset);
357-
String encodeClientSecret = URLEncoder.encode(clientSecret, charset);
358-
RestTemplate restTemplate = new RestTemplate();
359-
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(encodeClientId, encodeClientSecret));
360-
return new SpringOpaqueTokenIntrospector(this.introspectionUri, restTemplate);
372+
this.clientId = URLEncoder.encode(clientSecret, charset);
373+
return this;
361374
}
362375

363376
/**
364-
* Creates a {@code SpringOpaqueTokenIntrospector} with the provided parameters
365-
* @param clientId The client id authorized
366-
* @param clientSecret The client secret for the authorized client
367-
* @return the {@link SpringOpaqueTokenIntrospector}
377+
* Creates a {@code SpringOpaqueTokenIntrospector.Builder} with the provided
378+
* parameters
379+
* @param clientSecret The client id authorized
380+
* @return the {@link SpringOpaqueTokenIntrospector.Builder}
368381
* @since 6.5
369382
*/
370-
public SpringOpaqueTokenIntrospector introspectionClientCredentials(String clientId, String clientSecret) {
371-
Assert.notNull(clientId, "clientId cannot be null");
383+
public Builder clientSecret(String clientSecret) {
372384
Assert.notNull(clientSecret, "clientSecret cannot be null");
373-
RestTemplate restTemplate = new RestTemplate();
374-
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(clientId, clientSecret));
375-
return new SpringOpaqueTokenIntrospector(this.introspectionUri, restTemplate);
385+
this.clientSecret = clientSecret;
386+
return this;
376387
}
377388

378389
/**
379390
* Creates a {@code SpringOpaqueTokenIntrospector} with the provided parameters
380-
* The given {@link RestOperations} should perform its own client authentication
381-
* against the introspection endpoint.
382-
* @param restOperations The client for performing the introspection request
383391
* @return the {@link SpringOpaqueTokenIntrospector}
384392
* @since 6.5
385393
*/
386-
public SpringOpaqueTokenIntrospector withRestOperations(RestOperations restOperations) {
387-
Assert.notNull(restOperations, "restOperations cannot be null");
388-
return new SpringOpaqueTokenIntrospector(this.introspectionUri, restOperations);
394+
public SpringOpaqueTokenIntrospector build() {
395+
RestTemplate restTemplate = new RestTemplate();
396+
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(this.clientId, this.clientSecret));
397+
return new SpringOpaqueTokenIntrospector(this.introspectionUri, restTemplate);
389398
}
390399

391400
}

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/introspection/SpringReactiveOpaqueTokenIntrospector.java

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.net.URI;
2121
import java.net.URLEncoder;
2222
import java.nio.charset.Charset;
23-
import java.nio.charset.StandardCharsets;
2423
import java.time.Instant;
2524
import java.util.ArrayList;
2625
import java.util.Arrays;
@@ -77,7 +76,7 @@ public class SpringReactiveOpaqueTokenIntrospector implements ReactiveOpaqueToke
7776
* @param introspectionUri The introspection endpoint uri
7877
* @param clientId The client id authorized to introspect
7978
* @param clientSecret The client secret for the authorized client
80-
* @deprecated
79+
* @deprecated Please use {@link SpringReactiveOpaqueTokenIntrospector.Builder}
8180
*/
8281
@Deprecated(since = "6.5", forRemoval = true)
8382
public SpringReactiveOpaqueTokenIntrospector(String introspectionUri, String clientId, String clientSecret) {
@@ -254,96 +253,102 @@ default List<String> getScopes() {
254253

255254
}
256255

256+
/**
257+
* Creates a {@code SpringReactiveOpaqueTokenIntrospector.Builder} with the provided
258+
* parameters
259+
* @param introspectionUri The introspection endpoint uri
260+
* @return the {@link SpringReactiveOpaqueTokenIntrospector.Builder}
261+
* @since 6.5
262+
*/
263+
public static Builder withIntrospectionUri(String introspectionUri) {
264+
265+
return new Builder(introspectionUri);
266+
}
267+
257268
/**
258269
* Used to build {@link SpringReactiveOpaqueTokenIntrospector}.
259270
*
260271
* @author Ngoc Nhan
261272
* @since 6.5
262273
*/
263-
public static final class SpringReactiveOpaqueTokenIntrospectorBuilder {
274+
public static final class Builder {
264275

265276
private final String introspectionUri;
266277

267-
private SpringReactiveOpaqueTokenIntrospectorBuilder(String introspectionUri) {
278+
private String clientId;
279+
280+
private String clientSecret;
281+
282+
private Builder(String introspectionUri) {
268283
this.introspectionUri = introspectionUri;
269284
}
270285

271286
/**
272-
* Creates a {@code SpringReactiveOpaqueTokenIntrospectorBuilder} with the
287+
* Creates a {@code SpringReactiveOpaqueTokenIntrospector.Builder} with the
273288
* provided parameters
274-
* @param introspectionUri The introspection endpoint uri
275-
* @return the {@link SpringReactiveOpaqueTokenIntrospectorBuilder}
289+
* @param clientId The client id authorized that should be encoded
290+
* @param charset The charset to use
291+
* @return the {@link SpringReactiveOpaqueTokenIntrospector.Builder}
276292
* @since 6.5
277293
*/
278-
public static SpringReactiveOpaqueTokenIntrospectorBuilder withIntrospectionUri(String introspectionUri) {
279-
280-
return new SpringReactiveOpaqueTokenIntrospectorBuilder(introspectionUri);
294+
public SpringReactiveOpaqueTokenIntrospector.Builder clientId(String clientId, Charset charset) {
295+
Assert.notNull(clientId, "clientId cannot be null");
296+
Assert.notNull(charset, "charset cannot be null");
297+
this.clientId = URLEncoder.encode(clientId, charset);
298+
return this;
281299
}
282300

283301
/**
284-
* Creates a {@code SpringReactiveOpaqueTokenIntrospector} with the provided
285-
* parameters
286-
* @param clientId The client id authorized that should be encode
287-
* @param clientSecret The client secret that should be encode for the authorized
288-
* client
289-
* @return the {@link SpringReactiveOpaqueTokenIntrospector}
302+
* Creates a {@code SpringReactiveOpaqueTokenIntrospector.Builder} with the
303+
* provided parameters
304+
* @param clientId The client id authorized
305+
* @return the {@link SpringReactiveOpaqueTokenIntrospector.Builder}
290306
* @since 6.5
291307
*/
292-
public SpringReactiveOpaqueTokenIntrospector introspectionEncodeClientCredentials(String clientId,
293-
String clientSecret) {
294-
return this.introspectionEncodeClientCredentials(clientId, clientSecret, StandardCharsets.UTF_8);
308+
public SpringReactiveOpaqueTokenIntrospector.Builder clientId(String clientId) {
309+
Assert.notNull(clientId, "clientId cannot be null");
310+
this.clientId = clientId;
311+
return this;
295312
}
296313

297314
/**
298-
* Creates a {@code SpringReactiveOpaqueTokenIntrospector} with the provided
299-
* parameters
300-
* @param clientId The client id authorized that should be encode
301-
* @param clientSecret The client secret that should be encode for the authorized
302-
* client
303-
* @param charset the charset to use
304-
* @return the {@link SpringReactiveOpaqueTokenIntrospector}
315+
* Creates a {@code SpringReactiveOpaqueTokenIntrospector.Builder} with the
316+
* provided parameters
317+
* @param clientSecret The client id authorized that should be encoded
318+
* @param charset The charset to use
319+
* @return the {@link SpringReactiveOpaqueTokenIntrospector.Builder}
305320
* @since 6.5
306321
*/
307-
public SpringReactiveOpaqueTokenIntrospector introspectionEncodeClientCredentials(String clientId,
308-
String clientSecret, Charset charset) {
309-
Assert.notNull(clientId, "clientId cannot be null");
322+
public SpringReactiveOpaqueTokenIntrospector.Builder clientSecret(String clientSecret, Charset charset) {
310323
Assert.notNull(clientSecret, "clientSecret cannot be null");
311324
Assert.notNull(charset, "charset cannot be null");
312-
String encodeClientId = URLEncoder.encode(clientId, charset);
313-
String encodeClientSecret = URLEncoder.encode(clientSecret, charset);
314-
WebClient webClient = WebClient.builder()
315-
.defaultHeaders((h) -> h.setBasicAuth(encodeClientId, encodeClientSecret))
316-
.build();
317-
return new SpringReactiveOpaqueTokenIntrospector(this.introspectionUri, webClient);
325+
this.clientId = URLEncoder.encode(clientSecret, charset);
326+
return this;
318327
}
319328

320329
/**
321-
* Creates a {@code SpringReactiveOpaqueTokenIntrospector} with the provided
322-
* parameters
323-
* @param clientId The client id authorized
324-
* @param clientSecret The client secret for the authorized client
325-
* @return the {@link SpringReactiveOpaqueTokenIntrospector}
330+
* Creates a {@code SpringReactiveOpaqueTokenIntrospector.Builder} with the
331+
* provided parameters
332+
* @param clientSecret The client id authorized
333+
* @return the {@link SpringReactiveOpaqueTokenIntrospector.Builder}
326334
* @since 6.5
327335
*/
328-
public SpringReactiveOpaqueTokenIntrospector introspectionClientCredentials(String clientId,
329-
String clientSecret) {
330-
Assert.notNull(clientId, "clientId cannot be null");
336+
public SpringReactiveOpaqueTokenIntrospector.Builder clientSecret(String clientSecret) {
331337
Assert.notNull(clientSecret, "clientSecret cannot be null");
332-
WebClient webClient = WebClient.builder()
333-
.defaultHeaders((h) -> h.setBasicAuth(clientId, clientSecret))
334-
.build();
335-
return new SpringReactiveOpaqueTokenIntrospector(this.introspectionUri, webClient);
338+
this.clientSecret = clientSecret;
339+
return this;
336340
}
337341

338342
/**
339343
* Creates a {@code SpringReactiveOpaqueTokenIntrospector} with the provided
340344
* parameters
341-
* @param webClient The client for performing the introspection request
342345
* @return the {@link SpringReactiveOpaqueTokenIntrospector}
343346
* @since 6.5
344347
*/
345-
public SpringReactiveOpaqueTokenIntrospector withRestOperations(WebClient webClient) {
346-
Assert.notNull(webClient, "webClient cannot be null");
348+
public SpringReactiveOpaqueTokenIntrospector build() {
349+
WebClient webClient = WebClient.builder()
350+
.defaultHeaders((h) -> h.setBasicAuth(this.clientId, this.clientSecret))
351+
.build();
347352
return new SpringReactiveOpaqueTokenIntrospector(this.introspectionUri, webClient);
348353
}
349354

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/introspection/SpringOpaqueTokenIntrospectorTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.security.oauth2.server.resource.introspection;
1818

1919
import java.io.IOException;
20+
import java.nio.charset.StandardCharsets;
2021
import java.time.Instant;
2122
import java.util.Arrays;
2223
import java.util.Base64;
@@ -43,7 +44,6 @@
4344
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;
4445
import org.springframework.security.oauth2.core.OAuth2TokenIntrospectionClaimAccessor;
4546
import org.springframework.security.oauth2.core.OAuth2TokenIntrospectionClaimNames;
46-
import org.springframework.security.oauth2.server.resource.introspection.SpringOpaqueTokenIntrospector.SpringOpaqueTokenIntrospectorBuilder;
4747
import org.springframework.web.client.RestOperations;
4848

4949
import static org.assertj.core.api.Assertions.assertThat;
@@ -369,9 +369,11 @@ public void introspectWithEncodeClientCredentialsThenOk() throws Exception {
369369
""";
370370
server.setDispatcher(requiresAuth("client%25%261", "secret%40%242", response));
371371
String introspectUri = server.url("/introspect").toString();
372-
OpaqueTokenIntrospector introspectionClient = SpringOpaqueTokenIntrospectorBuilder
372+
OpaqueTokenIntrospector introspectionClient = SpringOpaqueTokenIntrospector
373373
.withIntrospectionUri(introspectUri)
374-
.introspectionEncodeClientCredentials("client%&1", "secret@$2");
374+
.clientId("client%&1", StandardCharsets.UTF_8)
375+
.clientSecret("secret@$2", StandardCharsets.UTF_8)
376+
.build();
375377
OAuth2AuthenticatedPrincipal authority = introspectionClient.introspect("token");
376378
// @formatter:off
377379
assertThat(authority.getAttributes())

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/introspection/SpringReactiveOpaqueTokenIntrospectorTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.security.oauth2.server.resource.introspection;
1818

1919
import java.io.IOException;
20+
import java.nio.charset.StandardCharsets;
2021
import java.time.Instant;
2122
import java.util.Arrays;
2223
import java.util.Base64;
@@ -293,9 +294,11 @@ public void introspectWithEncodeClientCredentialsThenOk() throws Exception {
293294
""";
294295
server.setDispatcher(requiresAuth("client%25%261", "secret%40%242", response));
295296
String introspectUri = server.url("/introspect").toString();
296-
ReactiveOpaqueTokenIntrospector introspectionClient = SpringReactiveOpaqueTokenIntrospectorBuilder
297+
ReactiveOpaqueTokenIntrospector introspectionClient = SpringReactiveOpaqueTokenIntrospector
297298
.withIntrospectionUri(introspectUri)
298-
.introspectionEncodeClientCredentials("client%&1", "secret@$2");
299+
.clientId("client%&1", StandardCharsets.UTF_8)
300+
.clientSecret("secret@$2", StandardCharsets.UTF_8)
301+
.build();
299302
OAuth2AuthenticatedPrincipal authority = introspectionClient.introspect("token").block();
300303
// @formatter:off
301304
assertThat(authority.getAttributes())

0 commit comments

Comments
 (0)