Skip to content

Commit 69ee8d9

Browse files
committed
Polish OAuth 2.0 Authentication Builders
Issue gh-17861
1 parent c66a028 commit 69ee8d9

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-6
lines changed

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

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

103103
/**
104-
* A builder preserving the concrete {@link Authentication} type
104+
* A builder of {@link OAuth2AuthenticationToken} instances
105105
*
106106
* @since 7.0
107107
*/
@@ -124,6 +124,14 @@ public B principal(@Nullable Object principal) {
124124
return (B) this;
125125
}
126126

127+
/**
128+
* Use this
129+
* {@link org.springframework.security.oauth2.client.registration.ClientRegistration}
130+
* {@code registrationId}.
131+
* @param authorizedClientRegistrationId the registration id to use
132+
* @return the {@link Builder} for further configurations
133+
* @see OAuth2AuthenticationToken#getAuthorizedClientRegistrationId
134+
*/
127135
public B authorizedClientRegistrationId(String authorizedClientRegistrationId) {
128136
this.authorizedClientRegistrationId = authorizedClientRegistrationId;
129137
return (B) this;

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

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

120120
/**
121-
* A builder preserving the concrete {@link Authentication} type
121+
* A builder for {@link AbstractOAuth2TokenAuthenticationToken} implementations
122122
*
123+
* @param <B>
123124
* @since 7.0
124125
*/
125126
public abstract static class AbstractOAuth2TokenAuthenticationBuilder<T extends OAuth2Token, B extends AbstractOAuth2TokenAuthenticationBuilder<T, B>>
@@ -152,8 +153,13 @@ public B credentials(@Nullable Object credentials) {
152153
return (B) this;
153154
}
154155

156+
/**
157+
* The OAuth 2.0 Token to use
158+
* @param token the token to use
159+
* @return the {@link Builder} for further configurations
160+
*/
155161
public B token(T token) {
156-
Assert.notNull(token, "credentials cannot be null");
162+
Assert.notNull(token, "token cannot be null");
157163
this.token = token;
158164
return (B) this;
159165
}

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ protected Builder(BearerTokenAuthentication token) {
8989
this.attributes = token.getTokenAttributes();
9090
}
9191

92+
/**
93+
* Use this principal. 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,
@@ -97,13 +102,33 @@ public B principal(@Nullable Object principal) {
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) {
102-
Assert.isTrue(token.getTokenType() == OAuth2AccessToken.TokenType.BEARER,
103-
"credentials must be a bearer token");
124+
Assert.isTrue(token.getTokenType() == OAuth2AccessToken.TokenType.BEARER, "token must be a bearer token");
125+
super.credentials(token);
104126
return super.token(token);
105127
}
106128

129+
/**
130+
* {@inheritDoc}
131+
*/
107132
@Override
108133
public BearerTokenAuthentication build() {
109134
return new BearerTokenAuthentication(this);

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

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

22+
import org.jspecify.annotations.Nullable;
23+
2224
import org.springframework.security.core.Authentication;
2325
import org.springframework.security.core.GrantedAuthority;
2426
import org.springframework.security.core.Transient;
2527
import org.springframework.security.oauth2.jwt.Jwt;
28+
import org.springframework.util.Assert;
2629

2730
/**
2831
* An implementation of an {@link AbstractOAuth2TokenAuthenticationToken} representing a
@@ -96,9 +99,10 @@ public Builder<?> toBuilder() {
9699
}
97100

98101
/**
99-
* A builder preserving the concrete {@link Authentication} type
102+
* A builder for {@link JwtAuthenticationToken} instances
100103
*
101104
* @since 7.0
105+
* @see Authentication.Builder
102106
*/
103107
public static class Builder<B extends Builder<B>> extends AbstractOAuth2TokenAuthenticationBuilder<Jwt, B> {
104108

@@ -109,6 +113,44 @@ protected Builder(JwtAuthenticationToken token) {
109113
this.name = token.getName();
110114
}
111115

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

0 commit comments

Comments
 (0)