Skip to content

Commit 3cdf4b5

Browse files
committed
1 parent 000a1b4 commit 3cdf4b5

File tree

5 files changed

+197
-226
lines changed

5 files changed

+197
-226
lines changed

core/src/main/java/org/springframework/security/oauth2/server/authorization/InMemoryOAuth2AuthorizationService.java

+15-23
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,33 @@
1717

1818
import org.springframework.util.Assert;
1919

20-
import java.util.Collections;
2120
import java.util.List;
2221
import java.util.concurrent.CopyOnWriteArrayList;
2322

2423
/**
25-
* In-memory implementation of {@link OAuth2AuthorizationService}.
24+
* An {@link OAuth2AuthorizationService} that stores {@link OAuth2Authorization}'s in-memory.
2625
*
2726
* @author Krisztian Toth
27+
* @since 0.0.1
28+
* @see OAuth2AuthorizationService
2829
*/
2930
public final class InMemoryOAuth2AuthorizationService implements OAuth2AuthorizationService {
3031
private final List<OAuth2Authorization> authorizations;
3132

3233
/**
33-
* Creates an {@link InMemoryOAuth2AuthorizationService}.
34+
* Constructs an {@code InMemoryOAuth2AuthorizationService}.
3435
*/
3536
public InMemoryOAuth2AuthorizationService() {
36-
this(Collections.emptyList());
37+
this.authorizations = new CopyOnWriteArrayList<>();
3738
}
3839

3940
/**
40-
* Creates an {@link InMemoryOAuth2AuthorizationService} with the provided {@link List}<{@link OAuth2Authorization}>
41-
* as the in-memory store.
41+
* Constructs an {@code InMemoryOAuth2AuthorizationService} using the provided parameters.
4242
*
43-
* @param authorizations a {@link List}<{@link OAuth2Authorization}> object to use as the store
43+
* @param authorizations the initial {@code List} of {@link OAuth2Authorization}(s)
4444
*/
4545
public InMemoryOAuth2AuthorizationService(List<OAuth2Authorization> authorizations) {
46-
Assert.notNull(authorizations, "authorizations cannot be null");
46+
Assert.notEmpty(authorizations, "authorizations cannot be empty");
4747
this.authorizations = new CopyOnWriteArrayList<>(authorizations);
4848
}
4949

@@ -58,26 +58,18 @@ public OAuth2Authorization findByTokenAndTokenType(String token, TokenType token
5858
Assert.hasText(token, "token cannot be empty");
5959
Assert.notNull(tokenType, "tokenType cannot be null");
6060
return this.authorizations.stream()
61-
.filter(authorization -> doesMatch(authorization, token, tokenType))
61+
.filter(authorization -> hasToken(authorization, token, tokenType))
6262
.findFirst()
6363
.orElse(null);
64-
6564
}
6665

67-
private boolean doesMatch(OAuth2Authorization authorization, String token, TokenType tokenType) {
68-
if (tokenType.equals(TokenType.ACCESS_TOKEN)) {
69-
return isAccessTokenEqual(token, authorization);
70-
} else if (tokenType.equals(TokenType.AUTHORIZATION_CODE)) {
71-
return isAuthorizationCodeEqual(token, authorization);
66+
private boolean hasToken(OAuth2Authorization authorization, String token, TokenType tokenType) {
67+
if (TokenType.AUTHORIZATION_CODE.equals(tokenType)) {
68+
return token.equals(authorization.getAttributes().get(TokenType.AUTHORIZATION_CODE.getValue()));
69+
} else if (TokenType.ACCESS_TOKEN.equals(tokenType)) {
70+
return authorization.getAccessToken() != null &&
71+
authorization.getAccessToken().getTokenValue().equals(token);
7272
}
7373
return false;
7474
}
75-
76-
private boolean isAccessTokenEqual(String token, OAuth2Authorization authorization) {
77-
return authorization.getAccessToken() != null && token.equals(authorization.getAccessToken().getTokenValue());
78-
}
79-
80-
private boolean isAuthorizationCodeEqual(String token, OAuth2Authorization authorization) {
81-
return token.equals(authorization.getAttributes().get(TokenType.AUTHORIZATION_CODE.getValue()));
82-
}
8375
}

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

+65-67
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,29 @@
1616
package org.springframework.security.oauth2.server.authorization;
1717

1818
import org.springframework.security.oauth2.core.OAuth2AccessToken;
19+
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
1920
import org.springframework.util.Assert;
2021

22+
import java.io.Serializable;
2123
import java.util.Collections;
2224
import java.util.HashMap;
2325
import java.util.Map;
2426
import java.util.Objects;
2527
import java.util.function.Consumer;
2628

2729
/**
28-
* Represents a collection of attributes which describe an OAuth 2.0 authorization context.
30+
* A representation of an OAuth 2.0 Authorization,
31+
* which holds state related to the authorization granted to the {@link #getRegisteredClientId() client}
32+
* by the {@link #getPrincipalName() resource owner}.
2933
*
3034
* @author Joe Grandja
3135
* @author Krisztian Toth
36+
* @since 0.0.1
37+
* @see RegisteredClient
38+
* @see OAuth2AccessToken
3239
*/
33-
public class OAuth2Authorization {
40+
public class OAuth2Authorization implements Serializable {
41+
private static final long serialVersionUID = Version.SERIAL_VERSION_UID;
3442
private String registeredClientId;
3543
private String principalName;
3644
private OAuth2AccessToken accessToken;
@@ -39,43 +47,64 @@ public class OAuth2Authorization {
3947
protected OAuth2Authorization() {
4048
}
4149

50+
/**
51+
* Returns the identifier for the {@link RegisteredClient#getId() registered client}.
52+
*
53+
* @return the {@link RegisteredClient#getId()}
54+
*/
4255
public String getRegisteredClientId() {
4356
return this.registeredClientId;
4457
}
4558

59+
/**
60+
* Returns the resource owner's {@code Principal} name.
61+
*
62+
* @return the resource owner's {@code Principal} name
63+
*/
4664
public String getPrincipalName() {
4765
return this.principalName;
4866
}
4967

68+
/**
69+
* Returns the {@link OAuth2AccessToken access token} credential.
70+
*
71+
* @return the {@link OAuth2AccessToken}
72+
*/
5073
public OAuth2AccessToken getAccessToken() {
5174
return this.accessToken;
5275
}
5376

77+
/**
78+
* Returns the attribute(s) associated to the authorization.
79+
*
80+
* @return a {@code Map} of the attribute(s)
81+
*/
5482
public Map<String, Object> getAttributes() {
5583
return this.attributes;
5684
}
5785

5886
/**
59-
* Returns an attribute with the provided name or {@code null} if not found.
87+
* Returns the value of an attribute associated to the authorization.
6088
*
6189
* @param name the name of the attribute
62-
* @param <T> the type of the attribute
63-
* @return the found attribute or {@code null}
90+
* @param <T> the type of the attribute
91+
* @return the value of the attribute associated to the authorization, or {@code null} if not available
6492
*/
93+
@SuppressWarnings("unchecked")
6594
public <T> T getAttribute(String name) {
6695
Assert.hasText(name, "name cannot be empty");
6796
return (T) this.attributes.get(name);
6897
}
6998

7099
@Override
71-
public boolean equals(Object o) {
72-
if (this == o) {
100+
public boolean equals(Object obj) {
101+
if (this == obj) {
73102
return true;
74103
}
75-
if (o == null || getClass() != o.getClass()) {
104+
if (obj == null || getClass() != obj.getClass()) {
76105
return false;
77106
}
78-
OAuth2Authorization that = (OAuth2Authorization) o;
107+
OAuth2Authorization that = (OAuth2Authorization) obj;
79108
return Objects.equals(this.registeredClientId, that.registeredClientId) &&
80109
Objects.equals(this.principalName, that.principalName) &&
81110
Objects.equals(this.accessToken, that.accessToken) &&
@@ -88,59 +117,34 @@ public int hashCode() {
88117
}
89118

90119
/**
91-
* Returns an empty {@link Builder}.
120+
* Returns a new {@link Builder}, initialized with the provided {@link RegisteredClient#getId()}.
92121
*
122+
* @param registeredClient the {@link RegisteredClient}
93123
* @return the {@link Builder}
94124
*/
95-
public static Builder builder() {
96-
return new Builder();
125+
public static Builder withRegisteredClient(RegisteredClient registeredClient) {
126+
Assert.notNull(registeredClient, "registeredClient cannot be null");
127+
return new Builder(registeredClient.getId());
97128
}
98129

99130
/**
100-
* Returns a new {@link Builder}, initialized with the provided {@link OAuth2Authorization}.
101-
*
102-
* @param authorization the {@link OAuth2Authorization} to copy from
103-
* @return the {@link Builder}
131+
* A builder for {@link OAuth2Authorization}.
104132
*/
105-
public static Builder withAuthorization(OAuth2Authorization authorization) {
106-
Assert.notNull(authorization, "authorization cannot be null");
107-
return new Builder(authorization);
108-
}
109-
110-
/**
111-
* Builder class for {@link OAuth2Authorization}.
112-
*/
113-
public static class Builder {
133+
public static class Builder implements Serializable {
134+
private static final long serialVersionUID = Version.SERIAL_VERSION_UID;
114135
private String registeredClientId;
115136
private String principalName;
116137
private OAuth2AccessToken accessToken;
117138
private Map<String, Object> attributes = new HashMap<>();
118139

119-
protected Builder() {
120-
}
121-
122-
protected Builder(OAuth2Authorization authorization) {
123-
this.registeredClientId = authorization.registeredClientId;
124-
this.principalName = authorization.principalName;
125-
this.accessToken = authorization.accessToken;
126-
this.attributes = authorization.attributes;
127-
}
128-
129-
/**
130-
* Sets the registered client identifier.
131-
*
132-
* @param registeredClientId the client id
133-
* @return the {@link Builder}
134-
*/
135-
public Builder registeredClientId(String registeredClientId) {
140+
protected Builder(String registeredClientId) {
136141
this.registeredClientId = registeredClientId;
137-
return this;
138142
}
139143

140144
/**
141-
* Sets the principal name.
145+
* Sets the resource owner's {@code Principal} name.
142146
*
143-
* @param principalName the principal name
147+
* @param principalName the resource owner's {@code Principal} name
144148
* @return the {@link Builder}
145149
*/
146150
public Builder principalName(String principalName) {
@@ -149,7 +153,7 @@ public Builder principalName(String principalName) {
149153
}
150154

151155
/**
152-
* Sets the {@link OAuth2AccessToken}.
156+
* Sets the {@link OAuth2AccessToken access token} credential.
153157
*
154158
* @param accessToken the {@link OAuth2AccessToken}
155159
* @return the {@link Builder}
@@ -160,23 +164,24 @@ public Builder accessToken(OAuth2AccessToken accessToken) {
160164
}
161165

162166
/**
163-
* Adds the attribute with the specified name and {@link String} value to the attributes map.
167+
* Adds an attribute associated to the authorization.
164168
*
165-
* @param name the name of the attribute
169+
* @param name the name of the attribute
166170
* @param value the value of the attribute
167171
* @return the {@link Builder}
168172
*/
169-
public Builder attribute(String name, String value) {
173+
public Builder attribute(String name, Object value) {
170174
Assert.hasText(name, "name cannot be empty");
171-
Assert.hasText(value, "value cannot be empty");
175+
Assert.notNull(value, "value cannot be null");
172176
this.attributes.put(name, value);
173177
return this;
174178
}
175179

176180
/**
177-
* A {@code Consumer} of the attributes map allowing to access or modify its content.
181+
* A {@code Consumer} of the attributes {@code Map}
182+
* allowing the ability to add, replace, or remove.
178183
*
179-
* @param attributesConsumer a {@link Consumer} of the attributes map
184+
* @param attributesConsumer a {@link Consumer} of the attributes {@code Map}
180185
* @return the {@link Builder}
181186
*/
182187
public Builder attributes(Consumer<Map<String, Object>> attributesConsumer) {
@@ -190,22 +195,15 @@ public Builder attributes(Consumer<Map<String, Object>> attributesConsumer) {
190195
* @return the {@link OAuth2Authorization}
191196
*/
192197
public OAuth2Authorization build() {
193-
Assert.hasText(this.registeredClientId, "registeredClientId cannot be empty");
194198
Assert.hasText(this.principalName, "principalName cannot be empty");
195-
if (this.accessToken == null && this.attributes.get(TokenType.AUTHORIZATION_CODE.getValue()) == null) {
196-
throw new IllegalArgumentException("either accessToken has to be set or the authorization code with key '"
197-
+ TokenType.AUTHORIZATION_CODE.getValue() + "' must be provided in the attributes map");
198-
}
199-
return create();
200-
}
201-
202-
private OAuth2Authorization create() {
203-
OAuth2Authorization oAuth2Authorization = new OAuth2Authorization();
204-
oAuth2Authorization.registeredClientId = this.registeredClientId;
205-
oAuth2Authorization.principalName = this.principalName;
206-
oAuth2Authorization.accessToken = this.accessToken;
207-
oAuth2Authorization.attributes = Collections.unmodifiableMap(this.attributes);
208-
return oAuth2Authorization;
199+
Assert.notNull(this.attributes.get(TokenType.AUTHORIZATION_CODE.getValue()), "authorization code cannot be null");
200+
201+
OAuth2Authorization authorization = new OAuth2Authorization();
202+
authorization.registeredClientId = this.registeredClientId;
203+
authorization.principalName = this.principalName;
204+
authorization.accessToken = this.accessToken;
205+
authorization.attributes = Collections.unmodifiableMap(this.attributes);
206+
return authorization;
209207
}
210208
}
211209
}

core/src/main/java/org/springframework/security/oauth2/server/authorization/OAuth2AuthorizationService.java

+18
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,30 @@
1616
package org.springframework.security.oauth2.server.authorization;
1717

1818
/**
19+
* Implementations of this interface are responsible for the management
20+
* of {@link OAuth2Authorization OAuth 2.0 Authorization(s)}.
21+
*
1922
* @author Joe Grandja
23+
* @since 0.0.1
24+
* @see OAuth2Authorization
2025
*/
2126
public interface OAuth2AuthorizationService {
2227

28+
/**
29+
* Saves the {@link OAuth2Authorization}.
30+
*
31+
* @param authorization the {@link OAuth2Authorization}
32+
*/
2333
void save(OAuth2Authorization authorization);
2434

35+
/**
36+
* Returns the {@link OAuth2Authorization} containing the provided {@code token},
37+
* or {@code null} if not found.
38+
*
39+
* @param token the token credential
40+
* @param tokenType the {@link TokenType token type}
41+
* @return the {@link OAuth2Authorization} if found, otherwise {@code null}
42+
*/
2543
OAuth2Authorization findByTokenAndTokenType(String token, TokenType tokenType);
2644

2745
}

0 commit comments

Comments
 (0)