Skip to content

Commit 043f6da

Browse files
committed
Polish OAuth 2.0 Authentication Builders
Issue gh-17861
1 parent 058e4b4 commit 043f6da

File tree

4 files changed

+89
-9
lines changed

4 files changed

+89
-9
lines changed

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/authentication/OAuth2AuthenticationToken.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ public Builder<?> toBuilder() {
101101
}
102102

103103
/**
104-
* A builder preserving the concrete {@link Authentication} type
105-
*
104+
* A builder of {@link OAuth2AuthenticationToken} instances
106105
* @since 7.0
107106
*/
108107
public static class Builder<B extends Builder<B>> extends AbstractAuthenticationBuilder<B> {
@@ -124,6 +123,13 @@ public B principal(@Nullable Object principal) {
124123
return (B) this;
125124
}
126125

126+
/**
127+
* Use this {@link org.springframework.security.oauth2.client.registration.ClientRegistration}
128+
* {@code registrationId}.
129+
* @param authorizedClientRegistrationId the registration id to use
130+
* @see OAuth2AuthenticationToken#getAuthorizedClientRegistrationId
131+
* @return
132+
*/
127133
public B authorizedClientRegistrationId(String authorizedClientRegistrationId) {
128134
this.authorizedClientRegistrationId = authorizedClientRegistrationId;
129135
return (B) this;

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/AbstractOAuth2TokenAuthenticationToken.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ public final T getToken() {
118118
public abstract Map<String, Object> getTokenAttributes();
119119

120120
/**
121-
* A builder preserving the concrete {@link Authentication} type
122-
*
121+
* A builder for {@link AbstractOAuth2TokenAuthenticationToken} implementations
122+
* @param <B>
123123
* @since 7.0
124124
*/
125125
public abstract static class AbstractOAuth2TokenAuthenticationBuilder<T extends OAuth2Token, B extends AbstractOAuth2TokenAuthenticationBuilder<T, B>>
@@ -152,8 +152,13 @@ public B credentials(@Nullable Object credentials) {
152152
return (B) this;
153153
}
154154

155+
/**
156+
* The OAuth 2.0 Token to use
157+
* @param token the token to use
158+
* @return the {@link Builder} for further configurations
159+
*/
155160
public B token(T token) {
156-
Assert.notNull(token, "credentials cannot be null");
161+
Assert.notNull(token, "token cannot be null");
157162
this.token = token;
158163
return (B) this;
159164
}

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/BearerTokenAuthentication.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,22 +88,48 @@ protected Builder(BearerTokenAuthentication token) {
8888
super(token);
8989
this.attributes = token.getTokenAttributes();
9090
}
91-
91+
/**
92+
* Use this principal.
93+
* Must be of type {@link OAuth2AuthenticatedPrincipal}
94+
* @param principal the principal to use
95+
* @return the {@link Builder} for further configurations
96+
*/
9297
@Override
9398
public B principal(@Nullable Object principal) {
9499
Assert.isInstanceOf(OAuth2AuthenticatedPrincipal.class, principal,
95-
"principal must be of type OAuth2AuthenticatedPrincipal");
100+
"principal must be of type OAuth2AuthenticatedPrincipal");
96101
this.attributes = ((OAuth2AuthenticatedPrincipal) principal).getAttributes();
97102
return super.principal(principal);
98103
}
99104

105+
/**
106+
* A synonym for {@link #token(OAuth2AccessToken)}
107+
* @param token the token to use
108+
* @return the {@link Builder} for further configurations
109+
*/
110+
@Override
111+
public B credentials(@Nullable Object token) {
112+
Assert.isInstanceOf(OAuth2AccessToken.class, token, "token must be of type OAuth2AccessToken");
113+
return token((OAuth2AccessToken) token);
114+
}
115+
116+
/**
117+
* Use this token. Must have a {@link OAuth2AccessToken#getTokenType()} as
118+
* {@link OAuth2AccessToken.TokenType#BEARER}.
119+
* @param token the token to use
120+
* @return the {@link Builder} for further configurations
121+
*/
100122
@Override
101123
public B token(OAuth2AccessToken token) {
102124
Assert.isTrue(token.getTokenType() == OAuth2AccessToken.TokenType.BEARER,
103-
"credentials must be a bearer token");
125+
"token must be a bearer token");
126+
super.credentials(token);
104127
return super.token(token);
105128
}
106129

130+
/**
131+
* {@inheritDoc}
132+
*/
107133
@Override
108134
public BearerTokenAuthentication build() {
109135
return new BearerTokenAuthentication(this);

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtAuthenticationToken.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@
1919
import java.util.Collection;
2020
import java.util.Map;
2121

22+
import org.jspecify.annotations.Nullable;
23+
24+
import org.springframework.security.authentication.AbstractAuthenticationToken;
2225
import org.springframework.security.core.Authentication;
2326
import org.springframework.security.core.GrantedAuthority;
2427
import org.springframework.security.core.Transient;
2528
import org.springframework.security.oauth2.jwt.Jwt;
29+
import org.springframework.util.Assert;
2630

2731
/**
2832
* An implementation of an {@link AbstractOAuth2TokenAuthenticationToken} representing a
@@ -96,9 +100,10 @@ public Builder<?> toBuilder() {
96100
}
97101

98102
/**
99-
* A builder preserving the concrete {@link Authentication} type
103+
* A builder for {@link JwtAuthenticationToken} instances
100104
*
101105
* @since 7.0
106+
* @see Authentication.Builder
102107
*/
103108
public static class Builder<B extends Builder<B>> extends AbstractOAuth2TokenAuthenticationBuilder<Jwt, B> {
104109

@@ -109,6 +114,44 @@ protected Builder(JwtAuthenticationToken token) {
109114
this.name = token.getName();
110115
}
111116

117+
/**
118+
* A synonym for {@link #token(Jwt)}
119+
* @return the {@link Builder} for further configurations
120+
*/
121+
@Override
122+
public B principal(@Nullable Object principal) {
123+
Assert.isInstanceOf(Jwt.class, principal, "principal must be of type Jwt");
124+
return token((Jwt) principal);
125+
}
126+
127+
/**
128+
* A synonym for {@link #token(Jwt)}
129+
* @return the {@link Builder} for further configurations
130+
*/
131+
@Override
132+
public B credentials(@Nullable Object credentials) {
133+
Assert.isInstanceOf(Jwt.class, credentials, "credentials must be of type Jwt");
134+
return token((Jwt) credentials);
135+
}
136+
137+
/**
138+
* Use this {@code token} as the token, principal, and credentials.
139+
* Also sets the {@code name} to {@link Jwt#getSubject}.
140+
* @param token the token to use
141+
* @return the {@link Builder} for further configurations
142+
*/
143+
@Override
144+
public B token(Jwt token) {
145+
super.principal(token);
146+
super.credentials(token);
147+
return super.token(token).name(token.getSubject());
148+
}
149+
150+
/**
151+
* The name to use.
152+
* @param name the name to use
153+
* @return the {@link Builder} for further configurations
154+
*/
112155
public B name(String name) {
113156
this.name = name;
114157
return (B) this;

0 commit comments

Comments
 (0)