Skip to content

Commit 34c895d

Browse files
Add in-memory implementation for OAuth2AuthorizationService
1 parent 66e21b8 commit 34c895d

File tree

6 files changed

+401
-6
lines changed

6 files changed

+401
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.server.authorization;
17+
18+
import org.springframework.lang.Nullable;
19+
import org.springframework.security.oauth2.core.OAuth2AccessToken;
20+
import org.springframework.util.Assert;
21+
22+
import java.util.Collections;
23+
import java.util.Map;
24+
import java.util.Objects;
25+
26+
/**
27+
* An {@link OAuth2Authorization} implementation with {@link OAuth2AccessToken} access token.
28+
*
29+
* @author Krisztian Toth
30+
*/
31+
public class AccessTokenOAuth2Authorization extends OAuth2Authorization {
32+
private final OAuth2AccessToken accessToken;
33+
34+
/**
35+
* Creates an {@link AccessTokenOAuth2Authorization} object with the provided params and an empty, unmodifiable
36+
* {@code attributes} map.
37+
*
38+
* @see #AccessTokenOAuth2Authorization(String, String, Map, OAuth2AccessToken)
39+
*/
40+
public AccessTokenOAuth2Authorization(String registeredClientId, String principalName,
41+
OAuth2AccessToken accessToken) {
42+
this(registeredClientId, principalName, Collections.emptyMap(), accessToken);
43+
}
44+
45+
/**
46+
* Creates an {@link AccessTokenOAuth2Authorization} object with the provided parameters and an
47+
* {@link OAuth2AccessToken access token}.
48+
*
49+
* @see OAuth2Authorization#OAuth2Authorization(String, String, Map)
50+
*/
51+
public AccessTokenOAuth2Authorization(String registeredClientId, String principalName,
52+
@Nullable Map<String, Object> attributes, OAuth2AccessToken accessToken) {
53+
super(registeredClientId, principalName, attributes);
54+
Assert.notNull(accessToken, "accessToken cannot be null");
55+
this.accessToken = accessToken;
56+
}
57+
58+
@Override
59+
public String getTokenValue() {
60+
return accessToken.getTokenValue();
61+
}
62+
63+
@Override
64+
public TokenType getTokenType() {
65+
return TokenType.ACCESS_TOKEN;
66+
}
67+
68+
public OAuth2AccessToken getAccessToken() {
69+
return accessToken;
70+
}
71+
72+
@Override
73+
public boolean equals(Object o) {
74+
if (this == o) {
75+
return true;
76+
}
77+
if (o == null || getClass() != o.getClass()) {
78+
return false;
79+
}
80+
if (!super.equals(o)) {
81+
return false;
82+
}
83+
AccessTokenOAuth2Authorization that = (AccessTokenOAuth2Authorization) o;
84+
return Objects.equals(accessToken, that.accessToken);
85+
}
86+
87+
@Override
88+
public int hashCode() {
89+
return Objects.hash(super.hashCode(), accessToken);
90+
}
91+
92+
@Override
93+
public String toString() {
94+
return "AccessTokenOAuth2Authorization{" +
95+
"accessToken=" + accessToken +
96+
"} " + super.toString();
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.server.authorization;
17+
18+
import org.springframework.util.Assert;
19+
20+
import java.util.Map;
21+
22+
/**
23+
* An {@link OAuth2Authorization} implementation for authorization code grant. The authorization code must be provided
24+
* in the {@code attributes} map.
25+
*
26+
* @author Krisztian Toth
27+
*/
28+
public class AuthorizationCodeOAuth2Authorization extends OAuth2Authorization {
29+
30+
/**
31+
* Creates a {@link AuthorizationCodeOAuth2Authorization} object with the provided params. The authorization code
32+
* MUST be present in the attributes map.
33+
*
34+
* @see OAuth2Authorization#OAuth2Authorization(String, String, Map)
35+
*/
36+
public AuthorizationCodeOAuth2Authorization(String registeredClientId, String principalName,
37+
Map<String, Object> attributes) {
38+
super(registeredClientId, principalName, attributes);
39+
Assert.notNull(attributes.get(TokenType.AUTHORIZATION_CODE.getValue()), "attributes must contain '"
40+
+ TokenType.AUTHORIZATION_CODE.getValue() + "' entry with a non-null value");
41+
}
42+
43+
@Override
44+
public String getTokenValue() {
45+
return (String) getAttributes().get(TokenType.AUTHORIZATION_CODE.getValue());
46+
}
47+
48+
@Override
49+
public TokenType getTokenType() {
50+
return TokenType.AUTHORIZATION_CODE;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.server.authorization;
17+
18+
import org.springframework.util.Assert;
19+
20+
import java.util.Map;
21+
import java.util.Objects;
22+
import java.util.concurrent.ConcurrentHashMap;
23+
24+
/**
25+
* In-memory implementation of {@link OAuth2AuthorizationService}.
26+
*
27+
* @author Krisztian Toth
28+
*/
29+
public class InMemoryOAuth2AuthorizationService implements OAuth2AuthorizationService {
30+
private final Map<TokenWithType, OAuth2Authorization> tokenAuthorizationMap = new ConcurrentHashMap<>();
31+
32+
@Override
33+
public void save(OAuth2Authorization authorization) {
34+
Assert.notNull(authorization, "authorization cannot be null");
35+
TokenWithType tokenWithType = new TokenWithType(authorization.getTokenValue(), authorization.getTokenType());
36+
tokenAuthorizationMap.put(tokenWithType, authorization);
37+
}
38+
39+
@Override
40+
public OAuth2Authorization findByTokenAndTokenType(String token, TokenType tokenType) {
41+
Assert.hasText(token, "token cannot be empty");
42+
Assert.notNull(tokenType, "tokenType cannot be null");
43+
return tokenAuthorizationMap.get(new TokenWithType(token, tokenType));
44+
}
45+
46+
/**
47+
* Visible for testing.
48+
*/
49+
Map<TokenWithType, OAuth2Authorization> getTokenAuthorizationMap() {
50+
return tokenAuthorizationMap;
51+
}
52+
53+
static class TokenWithType {
54+
private final String token;
55+
private final TokenType tokenType;
56+
57+
public TokenWithType(String token, TokenType tokenType) {
58+
this.token = token;
59+
this.tokenType = tokenType;
60+
}
61+
62+
@Override
63+
public boolean equals(Object o) {
64+
if (this == o) {
65+
return true;
66+
}
67+
if (o == null || getClass() != o.getClass()) {
68+
return false;
69+
}
70+
TokenWithType that = (TokenWithType) o;
71+
return token.equals(that.token) &&
72+
tokenType.equals(that.tokenType);
73+
}
74+
75+
@Override
76+
public int hashCode() {
77+
return Objects.hash(token, tokenType);
78+
}
79+
}
80+
}

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

+68-6
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,79 @@
1515
*/
1616
package org.springframework.security.oauth2.server.authorization;
1717

18-
import org.springframework.security.oauth2.core.OAuth2AccessToken;
18+
import org.springframework.lang.Nullable;
19+
import org.springframework.util.Assert;
1920

21+
import java.util.Collections;
2022
import java.util.Map;
23+
import java.util.Objects;
2124

2225
/**
26+
* Represents a collection of attributes which describe an OAuth 2.0 authorization context.
27+
*
2328
* @author Joe Grandja
29+
* @author Krisztian Toth
2430
*/
25-
public class OAuth2Authorization {
26-
private String registeredClientId;
27-
private String principalName;
28-
private OAuth2AccessToken accessToken;
29-
private Map<String, Object> attributes;
31+
public abstract class OAuth2Authorization implements TokenContainer {
32+
private final String registeredClientId;
33+
private final String principalName;
34+
private final Map<String, Object> attributes;
35+
36+
/**
37+
* Creates an {@link OAuth2Authorization} object with the provided params.
38+
*
39+
* @param registeredClientId the client's identifier which issued the authorization
40+
* @param principalName the name of the principal the client wants to authorize
41+
* @param attributes additional attributes associated with the authorization
42+
*/
43+
public OAuth2Authorization(String registeredClientId, String principalName,
44+
@Nullable Map<String, Object> attributes) {
45+
Assert.hasText(registeredClientId, "registeredClientId cannot be empty");
46+
Assert.hasText(principalName, "principalName cannot be empty");
47+
this.registeredClientId = registeredClientId;
48+
this.principalName = principalName;
49+
this.attributes = attributes == null ?
50+
Collections.emptyMap() :
51+
Collections.unmodifiableMap(attributes);
52+
}
53+
54+
public String getRegisteredClientId() {
55+
return registeredClientId;
56+
}
57+
58+
public String getPrincipalName() {
59+
return principalName;
60+
}
61+
62+
public Map<String, Object> getAttributes() {
63+
return attributes;
64+
}
65+
66+
@Override
67+
public boolean equals(Object o) {
68+
if (this == o) {
69+
return true;
70+
}
71+
if (o == null || getClass() != o.getClass()) {
72+
return false;
73+
}
74+
OAuth2Authorization that = (OAuth2Authorization) o;
75+
return Objects.equals(registeredClientId, that.registeredClientId) &&
76+
Objects.equals(principalName, that.principalName) &&
77+
Objects.equals(attributes, that.attributes);
78+
}
79+
80+
@Override
81+
public int hashCode() {
82+
return Objects.hash(registeredClientId, principalName, attributes);
83+
}
3084

85+
@Override
86+
public String toString() {
87+
return "IOAuth2Authorization{" +
88+
"registeredClientId='" + registeredClientId + '\'' +
89+
", principalName='" + principalName + '\'' +
90+
", attributes=" + attributes +
91+
'}';
92+
}
3193
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.server.authorization;
17+
18+
/**
19+
* An interface for retrieving a token value and its type.
20+
*
21+
* @author Krisztian Toth
22+
*/
23+
public interface TokenContainer {
24+
25+
/**
26+
* Returns the token value.
27+
*
28+
* @return the token value
29+
*/
30+
String getTokenValue();
31+
32+
/**
33+
* Returns the token's type.
34+
*
35+
* @return the token's type
36+
*/
37+
TokenType getTokenType();
38+
}

0 commit comments

Comments
 (0)