Skip to content

Commit 0656fde

Browse files
committed
Remove OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME
Closes gh-829
1 parent 83be809 commit 0656fde

21 files changed

+115
-63
lines changed

docs/src/docs/asciidoc/examples/src/main/java/sample/jpa/entity/authorization/Authorization.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class Authorization {
3131
private String registeredClientId;
3232
private String principalName;
3333
private String authorizationGrantType;
34+
@Column(length = 1000)
35+
private String authorizedScopes;
3436
@Column(length = 4000)
3537
private String attributes;
3638
@Column(length = 500)
@@ -101,6 +103,14 @@ public void setAuthorizationGrantType(String authorizationGrantType) {
101103
this.authorizationGrantType = authorizationGrantType;
102104
}
103105

106+
public String getAuthorizedScopes() {
107+
return this.authorizedScopes;
108+
}
109+
110+
public void setAuthorizedScopes(String authorizedScopes) {
111+
this.authorizedScopes = authorizedScopes;
112+
}
113+
104114
public String getAttributes() {
105115
return attributes;
106116
}

docs/src/docs/asciidoc/examples/src/main/java/sample/jpa/service/authorization/JpaOAuth2AuthorizationService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ private OAuth2Authorization toObject(Authorization entity) {
115115
.id(entity.getId())
116116
.principalName(entity.getPrincipalName())
117117
.authorizationGrantType(resolveAuthorizationGrantType(entity.getAuthorizationGrantType()))
118+
.authorizedScopes(StringUtils.commaDelimitedListToSet(entity.getAuthorizedScopes()))
118119
.attributes(attributes -> attributes.putAll(parseMap(entity.getAttributes())));
119120
if (entity.getState() != null) {
120121
builder.attribute(OAuth2ParameterNames.STATE, entity.getState());
@@ -164,6 +165,7 @@ private Authorization toEntity(OAuth2Authorization authorization) {
164165
entity.setRegisteredClientId(authorization.getRegisteredClientId());
165166
entity.setPrincipalName(authorization.getPrincipalName());
166167
entity.setAuthorizationGrantType(authorization.getAuthorizationGrantType().getValue());
168+
entity.setAuthorizedScopes(StringUtils.collectionToDelimitedString(authorization.getAuthorizedScopes(), ","));
167169
entity.setAttributes(writeMap(authorization.getAttributes()));
168170
entity.setState(authorization.getAttribute(OAuth2ParameterNames.STATE));
169171

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/JdbcOAuth2AuthorizationService.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
8585
+ "registered_client_id, "
8686
+ "principal_name, "
8787
+ "authorization_grant_type, "
88+
+ "authorized_scopes, "
8889
+ "attributes, "
8990
+ "state, "
9091
+ "authorization_code_value, "
@@ -126,12 +127,12 @@ public class JdbcOAuth2AuthorizationService implements OAuth2AuthorizationServic
126127

127128
// @formatter:off
128129
private static final String SAVE_AUTHORIZATION_SQL = "INSERT INTO " + TABLE_NAME
129-
+ " (" + COLUMN_NAMES + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
130+
+ " (" + COLUMN_NAMES + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
130131
// @formatter:on
131132

132133
// @formatter:off
133134
private static final String UPDATE_AUTHORIZATION_SQL = "UPDATE " + TABLE_NAME
134-
+ " SET registered_client_id = ?, principal_name = ?, authorization_grant_type = ?, attributes = ?, state = ?,"
135+
+ " SET registered_client_id = ?, principal_name = ?, authorization_grant_type = ?, authorized_scopes = ?, attributes = ?, state = ?,"
135136
+ " authorization_code_value = ?, authorization_code_issued_at = ?, authorization_code_expires_at = ?, authorization_code_metadata = ?,"
136137
+ " access_token_value = ?, access_token_issued_at = ?, access_token_expires_at = ?, access_token_metadata = ?, access_token_type = ?, access_token_scopes = ?,"
137138
+ " oidc_id_token_value = ?, oidc_id_token_issued_at = ?, oidc_id_token_expires_at = ?, oidc_id_token_metadata = ?,"
@@ -342,11 +343,17 @@ public OAuth2Authorization mapRow(ResultSet rs, int rowNum) throws SQLException
342343
String id = rs.getString("id");
343344
String principalName = rs.getString("principal_name");
344345
String authorizationGrantType = rs.getString("authorization_grant_type");
346+
Set<String> authorizedScopes = Collections.emptySet();
347+
String authorizedScopesString = rs.getString("authorized_scopes");
348+
if (authorizedScopesString != null) {
349+
authorizedScopes = StringUtils.commaDelimitedListToSet(authorizedScopesString);
350+
}
345351
Map<String, Object> attributes = parseMap(getLobValue(rs, "attributes"));
346352

347353
builder.id(id)
348354
.principalName(principalName)
349355
.authorizationGrantType(new AuthorizationGrantType(authorizationGrantType))
356+
.authorizedScopes(authorizedScopes)
350357
.attributes((attrs) -> attrs.putAll(attributes));
351358

352359
String state = rs.getString("state");
@@ -485,6 +492,12 @@ public List<SqlParameterValue> apply(OAuth2Authorization authorization) {
485492
parameters.add(new SqlParameterValue(Types.VARCHAR, authorization.getPrincipalName()));
486493
parameters.add(new SqlParameterValue(Types.VARCHAR, authorization.getAuthorizationGrantType().getValue()));
487494

495+
String authorizedScopes = null;
496+
if (!CollectionUtils.isEmpty(authorization.getAuthorizedScopes())) {
497+
authorizedScopes = StringUtils.collectionToDelimitedString(authorization.getAuthorizedScopes(), ",");
498+
}
499+
parameters.add(new SqlParameterValue(Types.VARCHAR, authorizedScopes));
500+
488501
String attributes = writeMap(authorization.getAttributes());
489502
parameters.add(mapToSqlParameter("attributes", attributes));
490503

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/OAuth2Authorization.java

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import java.time.Instant;
2020
import java.util.Collections;
2121
import java.util.HashMap;
22+
import java.util.HashSet;
2223
import java.util.Map;
2324
import java.util.Objects;
25+
import java.util.Set;
2426
import java.util.UUID;
2527
import java.util.function.Consumer;
2628

@@ -51,18 +53,11 @@
5153
*/
5254
public class OAuth2Authorization implements Serializable {
5355
private static final long serialVersionUID = SpringAuthorizationServerVersion.SERIAL_VERSION_UID;
54-
55-
/**
56-
* The name of the {@link #getAttribute(String) attribute} used for the authorized scope(s).
57-
* The value of the attribute is of type {@code Set<String>}.
58-
*/
59-
public static final String AUTHORIZED_SCOPE_ATTRIBUTE_NAME =
60-
OAuth2Authorization.class.getName().concat(".AUTHORIZED_SCOPE");
61-
6256
private String id;
6357
private String registeredClientId;
6458
private String principalName;
6559
private AuthorizationGrantType authorizationGrantType;
60+
private Set<String> authorizedScopes;
6661
private Map<Class<? extends OAuth2Token>, Token<?>> tokens;
6762
private Map<String, Object> attributes;
6863

@@ -105,6 +100,16 @@ public AuthorizationGrantType getAuthorizationGrantType() {
105100
return this.authorizationGrantType;
106101
}
107102

103+
/**
104+
* Returns the authorized scope(s).
105+
*
106+
* @return the {@code Set} of authorized scope(s)
107+
* @since 0.4.0
108+
*/
109+
public Set<String> getAuthorizedScopes() {
110+
return this.authorizedScopes;
111+
}
112+
108113
/**
109114
* Returns the {@link Token} of type {@link OAuth2AccessToken}.
110115
*
@@ -194,14 +199,15 @@ public boolean equals(Object obj) {
194199
Objects.equals(this.registeredClientId, that.registeredClientId) &&
195200
Objects.equals(this.principalName, that.principalName) &&
196201
Objects.equals(this.authorizationGrantType, that.authorizationGrantType) &&
202+
Objects.equals(this.authorizedScopes, that.authorizedScopes) &&
197203
Objects.equals(this.tokens, that.tokens) &&
198204
Objects.equals(this.attributes, that.attributes);
199205
}
200206

201207
@Override
202208
public int hashCode() {
203209
return Objects.hash(this.id, this.registeredClientId, this.principalName,
204-
this.authorizationGrantType, this.tokens, this.attributes);
210+
this.authorizationGrantType, this.authorizedScopes, this.tokens, this.attributes);
205211
}
206212

207213
/**
@@ -227,6 +233,7 @@ public static Builder from(OAuth2Authorization authorization) {
227233
.id(authorization.getId())
228234
.principalName(authorization.getPrincipalName())
229235
.authorizationGrantType(authorization.getAuthorizationGrantType())
236+
.authorizedScopes(authorization.getAuthorizedScopes())
230237
.tokens(authorization.tokens)
231238
.attributes(attrs -> attrs.putAll(authorization.getAttributes()));
232239
}
@@ -380,6 +387,7 @@ public static class Builder implements Serializable {
380387
private final String registeredClientId;
381388
private String principalName;
382389
private AuthorizationGrantType authorizationGrantType;
390+
private Set<String> authorizedScopes;
383391
private Map<Class<? extends OAuth2Token>, Token<?>> tokens = new HashMap<>();
384392
private final Map<String, Object> attributes = new HashMap<>();
385393

@@ -420,6 +428,18 @@ public Builder authorizationGrantType(AuthorizationGrantType authorizationGrantT
420428
return this;
421429
}
422430

431+
/**
432+
* Sets the authorized scope(s).
433+
*
434+
* @param authorizedScopes the {@code Set} of authorized scope(s)
435+
* @return the {@link Builder}
436+
* @since 0.4.0
437+
*/
438+
public Builder authorizedScopes(Set<String> authorizedScopes) {
439+
this.authorizedScopes = authorizedScopes;
440+
return this;
441+
}
442+
423443
/**
424444
* Sets the {@link OAuth2AccessToken access token}.
425445
*
@@ -522,6 +542,12 @@ public OAuth2Authorization build() {
522542
authorization.registeredClientId = this.registeredClientId;
523543
authorization.principalName = this.principalName;
524544
authorization.authorizationGrantType = this.authorizationGrantType;
545+
authorization.authorizedScopes =
546+
Collections.unmodifiableSet(
547+
!CollectionUtils.isEmpty(this.authorizedScopes) ?
548+
new HashSet<>(this.authorizedScopes) :
549+
new HashSet<>()
550+
);
525551
authorization.tokens = Collections.unmodifiableMap(this.tokens);
526552
authorization.attributes = Collections.unmodifiableMap(this.attributes);
527553
return authorization;

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2AuthorizationCodeAuthenticationProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
134134
.principal(authorization.getAttribute(Principal.class.getName()))
135135
.providerContext(ProviderContextHolder.getProviderContext())
136136
.authorization(authorization)
137-
.authorizedScopes(authorization.getAttribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME))
137+
.authorizedScopes(authorization.getAuthorizedScopes())
138138
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
139139
.authorizationGrant(authorizationCodeAuthentication);
140140
// @formatter:on

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2AuthorizationCodeRequestAuthenticationProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ private Authentication authenticateAuthorizationRequest(Authentication authentic
265265
}
266266

267267
OAuth2Authorization authorization = authorizationBuilder(registeredClient, principal, authorizationRequest)
268+
.authorizedScopes(authorizationRequest.getScopes())
268269
.token(authorizationCode)
269-
.attribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME, authorizationRequest.getScopes())
270270
.build();
271271
this.authorizationService.save(authorization);
272272

@@ -392,10 +392,10 @@ private Authentication authenticateAuthorizationConsent(Authentication authentic
392392
}
393393

394394
OAuth2Authorization updatedAuthorization = OAuth2Authorization.from(authorization)
395+
.authorizedScopes(authorizedScopes)
395396
.token(authorizationCode)
396397
.attributes(attrs -> {
397398
attrs.remove(OAuth2ParameterNames.STATE);
398-
attrs.put(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME, authorizedScopes);
399399
})
400400
.build();
401401
this.authorizationService.save(updatedAuthorization);

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2ClientCredentialsAuthenticationProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
123123
OAuth2Authorization.Builder authorizationBuilder = OAuth2Authorization.withRegisteredClient(registeredClient)
124124
.principalName(clientPrincipal.getName())
125125
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
126-
.attribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME, authorizedScopes);
126+
.authorizedScopes(authorizedScopes);
127127
// @formatter:on
128128
if (generatedAccessToken instanceof ClaimAccessor) {
129129
authorizationBuilder.token(accessToken, (metadata) ->

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/OAuth2RefreshTokenAuthenticationProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
118118
// The requested scope MUST NOT include any scope not originally granted by the resource owner,
119119
// and if omitted is treated as equal to the scope originally granted by the resource owner.
120120
Set<String> scopes = refreshTokenAuthentication.getScopes();
121-
Set<String> authorizedScopes = authorization.getAttribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME);
121+
Set<String> authorizedScopes = authorization.getAuthorizedScopes();
122122
if (!authorizedScopes.containsAll(scopes)) {
123123
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_SCOPE);
124124
}

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/oidc/authentication/OidcClientRegistrationAuthenticationProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private OAuth2Authorization registerAccessToken(RegisteredClient registeredClien
233233
OAuth2Authorization.Builder authorizationBuilder = OAuth2Authorization.withRegisteredClient(registeredClient)
234234
.principalName(registeredClient.getClientId())
235235
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
236-
.attribute(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME, authorizedScopes);
236+
.authorizedScopes(authorizedScopes);
237237
// @formatter:on
238238
if (registrationAccessToken instanceof ClaimAccessor) {
239239
authorizationBuilder.token(accessToken, (metadata) ->

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/token/OAuth2TokenContext.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ default OAuth2Authorization getAuthorization() {
8888
* @return the authorized scope(s)
8989
*/
9090
default Set<String> getAuthorizedScopes() {
91-
return hasKey(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME) ?
92-
get(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME) :
91+
return hasKey(AbstractBuilder.AUTHORIZED_SCOPE_KEY) ?
92+
get(AbstractBuilder.AUTHORIZED_SCOPE_KEY) :
9393
Collections.emptySet();
9494
}
9595

@@ -130,6 +130,8 @@ default <T extends Authentication> T getAuthorizationGrant() {
130130
abstract class AbstractBuilder<T extends OAuth2TokenContext, B extends AbstractBuilder<T, B>> {
131131
private static final String PRINCIPAL_AUTHENTICATION_KEY =
132132
Authentication.class.getName().concat(".PRINCIPAL");
133+
private static final String AUTHORIZED_SCOPE_KEY =
134+
OAuth2Authorization.class.getName().concat(".AUTHORIZED_SCOPE");
133135
private static final String AUTHORIZATION_GRANT_AUTHENTICATION_KEY =
134136
Authentication.class.getName().concat(".AUTHORIZATION_GRANT");
135137
private final Map<Object, Object> context = new HashMap<>();
@@ -182,7 +184,7 @@ public B authorization(OAuth2Authorization authorization) {
182184
* @return the {@link AbstractBuilder} for further configuration
183185
*/
184186
public B authorizedScopes(Set<String> authorizedScopes) {
185-
return put(OAuth2Authorization.AUTHORIZED_SCOPE_ATTRIBUTE_NAME, authorizedScopes);
187+
return put(AUTHORIZED_SCOPE_KEY, authorizedScopes);
186188
}
187189

188190
/**

0 commit comments

Comments
 (0)