Skip to content

Commit 704f986

Browse files
nkonevjzheaux
authored andcommitted
Make OAuth2AccessTokenResponse converters public
1 parent d22b476 commit 704f986

File tree

5 files changed

+366
-102
lines changed

5 files changed

+366
-102
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2002-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.core.http.converter;
17+
18+
import org.springframework.core.convert.converter.Converter;
19+
import org.springframework.security.oauth2.core.OAuth2AccessToken;
20+
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
21+
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
22+
import org.springframework.util.StringUtils;
23+
24+
import java.util.*;
25+
26+
/**
27+
* A {@link Converter} that converts the provided
28+
* OAuth 2.0 Access Token Response parameters to an {@link OAuth2AccessTokenResponse}.
29+
*
30+
* @author Joe Grandja
31+
* @author Nikita Konev
32+
* @since 5.3
33+
*/
34+
public final class OAuth2AccessTokenResponseConverter implements Converter<Map<String, String>, OAuth2AccessTokenResponse> {
35+
private static final Set<String> TOKEN_RESPONSE_PARAMETER_NAMES = new HashSet<>(Arrays.asList(
36+
OAuth2ParameterNames.ACCESS_TOKEN,
37+
OAuth2ParameterNames.EXPIRES_IN,
38+
OAuth2ParameterNames.REFRESH_TOKEN,
39+
OAuth2ParameterNames.SCOPE,
40+
OAuth2ParameterNames.TOKEN_TYPE
41+
));
42+
43+
@Override
44+
public OAuth2AccessTokenResponse convert(Map<String, String> tokenResponseParameters) {
45+
String accessToken = tokenResponseParameters.get(OAuth2ParameterNames.ACCESS_TOKEN);
46+
47+
OAuth2AccessToken.TokenType accessTokenType = null;
48+
if (OAuth2AccessToken.TokenType.BEARER.getValue().equalsIgnoreCase(
49+
tokenResponseParameters.get(OAuth2ParameterNames.TOKEN_TYPE))) {
50+
accessTokenType = OAuth2AccessToken.TokenType.BEARER;
51+
}
52+
53+
long expiresIn = 0;
54+
if (tokenResponseParameters.containsKey(OAuth2ParameterNames.EXPIRES_IN)) {
55+
try {
56+
expiresIn = Long.parseLong(tokenResponseParameters.get(OAuth2ParameterNames.EXPIRES_IN));
57+
} catch (NumberFormatException ex) {
58+
}
59+
}
60+
61+
Set<String> scopes = Collections.emptySet();
62+
if (tokenResponseParameters.containsKey(OAuth2ParameterNames.SCOPE)) {
63+
String scope = tokenResponseParameters.get(OAuth2ParameterNames.SCOPE);
64+
scopes = new HashSet<>(Arrays.asList(StringUtils.delimitedListToStringArray(scope, " ")));
65+
}
66+
67+
String refreshToken = tokenResponseParameters.get(OAuth2ParameterNames.REFRESH_TOKEN);
68+
69+
Map<String, Object> additionalParameters = new LinkedHashMap<>();
70+
for (Map.Entry<String, String> entry : tokenResponseParameters.entrySet()) {
71+
if (!TOKEN_RESPONSE_PARAMETER_NAMES.contains(entry.getKey())) {
72+
additionalParameters.put(entry.getKey(), entry.getValue());
73+
}
74+
}
75+
76+
return OAuth2AccessTokenResponse.withToken(accessToken)
77+
.tokenType(accessTokenType)
78+
.expiresIn(expiresIn)
79+
.scopes(scopes)
80+
.refreshToken(refreshToken)
81+
.additionalParameters(additionalParameters)
82+
.build();
83+
}
84+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,24 +25,12 @@
2525
import org.springframework.http.converter.HttpMessageConverter;
2626
import org.springframework.http.converter.HttpMessageNotReadableException;
2727
import org.springframework.http.converter.HttpMessageNotWritableException;
28-
import org.springframework.security.oauth2.core.OAuth2AccessToken;
2928
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
30-
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
3129
import org.springframework.util.Assert;
32-
import org.springframework.util.CollectionUtils;
33-
import org.springframework.util.StringUtils;
3430

3531
import java.nio.charset.Charset;
3632
import java.nio.charset.StandardCharsets;
37-
import java.time.Instant;
38-
import java.time.temporal.ChronoUnit;
39-
import java.util.HashSet;
4033
import java.util.Map;
41-
import java.util.Set;
42-
import java.util.Arrays;
43-
import java.util.Collections;
44-
import java.util.LinkedHashMap;
45-
import java.util.HashMap;
4634

4735
/**
4836
* A {@link HttpMessageConverter} for an {@link OAuth2AccessTokenResponse OAuth 2.0 Access Token Response}.
@@ -125,93 +113,4 @@ public final void setTokenResponseParametersConverter(Converter<OAuth2AccessToke
125113
this.tokenResponseParametersConverter = tokenResponseParametersConverter;
126114
}
127115

128-
/**
129-
* A {@link Converter} that converts the provided
130-
* OAuth 2.0 Access Token Response parameters to an {@link OAuth2AccessTokenResponse}.
131-
*/
132-
private static class OAuth2AccessTokenResponseConverter implements Converter<Map<String, String>, OAuth2AccessTokenResponse> {
133-
private static final Set<String> TOKEN_RESPONSE_PARAMETER_NAMES = new HashSet<>(Arrays.asList(
134-
OAuth2ParameterNames.ACCESS_TOKEN,
135-
OAuth2ParameterNames.TOKEN_TYPE,
136-
OAuth2ParameterNames.EXPIRES_IN,
137-
OAuth2ParameterNames.REFRESH_TOKEN,
138-
OAuth2ParameterNames.SCOPE
139-
));
140-
141-
@Override
142-
public OAuth2AccessTokenResponse convert(Map<String, String> tokenResponseParameters) {
143-
String accessToken = tokenResponseParameters.get(OAuth2ParameterNames.ACCESS_TOKEN);
144-
145-
OAuth2AccessToken.TokenType accessTokenType = null;
146-
if (OAuth2AccessToken.TokenType.BEARER.getValue().equalsIgnoreCase(
147-
tokenResponseParameters.get(OAuth2ParameterNames.TOKEN_TYPE))) {
148-
accessTokenType = OAuth2AccessToken.TokenType.BEARER;
149-
}
150-
151-
long expiresIn = 0;
152-
if (tokenResponseParameters.containsKey(OAuth2ParameterNames.EXPIRES_IN)) {
153-
try {
154-
expiresIn = Long.parseLong(tokenResponseParameters.get(OAuth2ParameterNames.EXPIRES_IN));
155-
} catch (NumberFormatException ex) { }
156-
}
157-
158-
Set<String> scopes = Collections.emptySet();
159-
if (tokenResponseParameters.containsKey(OAuth2ParameterNames.SCOPE)) {
160-
String scope = tokenResponseParameters.get(OAuth2ParameterNames.SCOPE);
161-
scopes = new HashSet<>(Arrays.asList(StringUtils.delimitedListToStringArray(scope, " ")));
162-
}
163-
164-
String refreshToken = tokenResponseParameters.get(OAuth2ParameterNames.REFRESH_TOKEN);
165-
166-
Map<String, Object> additionalParameters = new LinkedHashMap<>();
167-
for (Map.Entry<String, String> entry : tokenResponseParameters.entrySet()) {
168-
if (!TOKEN_RESPONSE_PARAMETER_NAMES.contains(entry.getKey())) {
169-
additionalParameters.put(entry.getKey(), entry.getValue());
170-
}
171-
}
172-
173-
return OAuth2AccessTokenResponse.withToken(accessToken)
174-
.tokenType(accessTokenType)
175-
.expiresIn(expiresIn)
176-
.scopes(scopes)
177-
.refreshToken(refreshToken)
178-
.additionalParameters(additionalParameters)
179-
.build();
180-
}
181-
}
182-
183-
/**
184-
* A {@link Converter} that converts the provided {@link OAuth2AccessTokenResponse}
185-
* to a {@code Map} representation of the OAuth 2.0 Access Token Response parameters.
186-
*/
187-
private static class OAuth2AccessTokenResponseParametersConverter implements Converter<OAuth2AccessTokenResponse, Map<String, String>> {
188-
189-
@Override
190-
public Map<String, String> convert(OAuth2AccessTokenResponse tokenResponse) {
191-
Map<String, String> parameters = new HashMap<>();
192-
193-
long expiresIn = -1;
194-
if (tokenResponse.getAccessToken().getExpiresAt() != null) {
195-
expiresIn = ChronoUnit.SECONDS.between(Instant.now(), tokenResponse.getAccessToken().getExpiresAt());
196-
}
197-
198-
parameters.put(OAuth2ParameterNames.ACCESS_TOKEN, tokenResponse.getAccessToken().getTokenValue());
199-
parameters.put(OAuth2ParameterNames.TOKEN_TYPE, tokenResponse.getAccessToken().getTokenType().getValue());
200-
parameters.put(OAuth2ParameterNames.EXPIRES_IN, String.valueOf(expiresIn));
201-
if (!CollectionUtils.isEmpty(tokenResponse.getAccessToken().getScopes())) {
202-
parameters.put(OAuth2ParameterNames.SCOPE,
203-
StringUtils.collectionToDelimitedString(tokenResponse.getAccessToken().getScopes(), " "));
204-
}
205-
if (tokenResponse.getRefreshToken() != null) {
206-
parameters.put(OAuth2ParameterNames.REFRESH_TOKEN, tokenResponse.getRefreshToken().getTokenValue());
207-
}
208-
if (!CollectionUtils.isEmpty(tokenResponse.getAdditionalParameters())) {
209-
for (Map.Entry<String, Object> entry : tokenResponse.getAdditionalParameters().entrySet()) {
210-
parameters.put(entry.getKey(), entry.getValue().toString());
211-
}
212-
}
213-
214-
return parameters;
215-
}
216-
}
217116
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2002-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.core.http.converter;
17+
18+
import org.springframework.core.convert.converter.Converter;
19+
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
20+
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
21+
import org.springframework.util.CollectionUtils;
22+
import org.springframework.util.StringUtils;
23+
24+
import java.time.Instant;
25+
import java.time.temporal.ChronoUnit;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
/**
30+
* A {@link Converter} that converts the provided {@link OAuth2AccessTokenResponse}
31+
* to a {@code Map} representation of the OAuth 2.0 Access Token Response parameters.
32+
*
33+
* @author Joe Grandja
34+
* @author Nikita Konev
35+
* @since 5.3
36+
*/
37+
public final class OAuth2AccessTokenResponseParametersConverter implements Converter<OAuth2AccessTokenResponse, Map<String, String>> {
38+
39+
@Override
40+
public Map<String, String> convert(OAuth2AccessTokenResponse tokenResponse) {
41+
Map<String, String> parameters = new HashMap<>();
42+
43+
long expiresIn = -1;
44+
if (tokenResponse.getAccessToken().getExpiresAt() != null) {
45+
expiresIn = ChronoUnit.SECONDS.between(Instant.now(), tokenResponse.getAccessToken().getExpiresAt());
46+
}
47+
48+
parameters.put(OAuth2ParameterNames.ACCESS_TOKEN, tokenResponse.getAccessToken().getTokenValue());
49+
parameters.put(OAuth2ParameterNames.TOKEN_TYPE, tokenResponse.getAccessToken().getTokenType().getValue());
50+
parameters.put(OAuth2ParameterNames.EXPIRES_IN, String.valueOf(expiresIn));
51+
if (!CollectionUtils.isEmpty(tokenResponse.getAccessToken().getScopes())) {
52+
parameters.put(OAuth2ParameterNames.SCOPE,
53+
StringUtils.collectionToDelimitedString(tokenResponse.getAccessToken().getScopes(), " "));
54+
}
55+
if (tokenResponse.getRefreshToken() != null) {
56+
parameters.put(OAuth2ParameterNames.REFRESH_TOKEN, tokenResponse.getRefreshToken().getTokenValue());
57+
}
58+
if (!CollectionUtils.isEmpty(tokenResponse.getAdditionalParameters())) {
59+
for (Map.Entry<String, Object> entry : tokenResponse.getAdditionalParameters().entrySet()) {
60+
parameters.put(entry.getKey(), entry.getValue().toString());
61+
}
62+
}
63+
64+
return parameters;
65+
}
66+
}

0 commit comments

Comments
 (0)