Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.

Commit f38fff7

Browse files
committed
Allow overriding 'active' attribute in CheckTokenEndpoint
Fixes gh-1591
1 parent 16d39ad commit f38fff7

File tree

2 files changed

+66
-30
lines changed

2 files changed

+66
-30
lines changed

spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/endpoint/CheckTokenEndpoint.java

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
/*******************************************************************************
2-
* Cloud Foundry
3-
* Copyright (c) [2009-2014] Pivotal Software, Inc. All Rights Reserved.
1+
/*
2+
* Copyright 2009-2019 the original author or authors.
43
*
5-
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
6-
* You may not use this product except in compliance with the License.
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
77
*
8-
* This product includes a number of subcomponents with
9-
* separate copyright notices and license terms. Your use of these
10-
* subcomponents is subject to the terms and conditions of the
11-
* subcomponent's license, as noted in the LICENSE file.
12-
*******************************************************************************/
8+
* http://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+
*/
1316
package org.springframework.security.oauth2.provider.endpoint;
1417

1518
import org.apache.commons.logging.Log;
@@ -42,7 +45,7 @@ public class CheckTokenEndpoint {
4245

4346
private ResourceServerTokenServices resourceServerTokenServices;
4447

45-
private AccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
48+
private AccessTokenConverter accessTokenConverter = new CheckTokenAccessTokenConverter();
4649

4750
protected final Log logger = LogFactory.getLog(getClass());
4851

@@ -81,12 +84,7 @@ public void setAccessTokenConverter(AccessTokenConverter accessTokenConverter) {
8184

8285
OAuth2Authentication authentication = resourceServerTokenServices.loadAuthentication(token.getValue());
8386

84-
Map<String, Object> response = (Map<String, Object>)accessTokenConverter.convertAccessToken(token, authentication);
85-
86-
// gh-1070
87-
response.put("active", true); // Always true if token exists and not expired
88-
89-
return response;
87+
return accessTokenConverter.convertAccessToken(token, authentication);
9088
}
9189

9290
@ExceptionHandler(InvalidTokenException.class)
@@ -106,4 +104,35 @@ public int getHttpErrorCode() {
106104
return exceptionTranslator.translate(e400);
107105
}
108106

107+
static class CheckTokenAccessTokenConverter implements AccessTokenConverter {
108+
private final AccessTokenConverter accessTokenConverter;
109+
110+
CheckTokenAccessTokenConverter() {
111+
this(new DefaultAccessTokenConverter());
112+
}
113+
114+
CheckTokenAccessTokenConverter(AccessTokenConverter accessTokenConverter) {
115+
this.accessTokenConverter = accessTokenConverter;
116+
}
117+
118+
@Override
119+
public Map<String, ?> convertAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
120+
Map<String, Object> claims = (Map<String, Object>) this.accessTokenConverter.convertAccessToken(token, authentication);
121+
122+
// gh-1070
123+
claims.put("active", true); // Always true if token exists and not expired
124+
125+
return claims;
126+
}
127+
128+
@Override
129+
public OAuth2AccessToken extractAccessToken(String value, Map<String, ?> map) {
130+
return this.accessTokenConverter.extractAccessToken(value, map);
131+
}
132+
133+
@Override
134+
public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
135+
return this.accessTokenConverter.extractAuthentication(map);
136+
}
137+
}
109138
}

spring-security-oauth2/src/test/java/org/springframework/security/oauth2/provider/endpoint/CheckTokenEndpointTest.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 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,8 +25,8 @@
2525
import java.util.HashMap;
2626
import java.util.Map;
2727

28-
import static org.junit.Assert.assertEquals;
29-
import static org.junit.Assert.assertNotNull;
28+
import static org.junit.Assert.*;
29+
import static org.mockito.Matchers.any;
3030
import static org.mockito.Matchers.anyString;
3131
import static org.mockito.Mockito.mock;
3232
import static org.mockito.Mockito.when;
@@ -36,29 +36,36 @@
3636
*/
3737
public class CheckTokenEndpointTest {
3838
private CheckTokenEndpoint checkTokenEndpoint;
39+
private AccessTokenConverter accessTokenConverter;
3940

4041
@Before
4142
public void setUp() {
42-
ResourceServerTokenServices resourceServerTokenServices = mock(ResourceServerTokenServices.class);
4343
OAuth2AccessToken accessToken = mock(OAuth2AccessToken.class);
44-
OAuth2Authentication authentication = mock(OAuth2Authentication.class);
45-
when(resourceServerTokenServices.readAccessToken(anyString())).thenReturn(accessToken);
4644
when(accessToken.isExpired()).thenReturn(false);
47-
when(accessToken.getValue()).thenReturn("access-token-1234");
48-
when(resourceServerTokenServices.loadAuthentication(accessToken.getValue())).thenReturn(authentication);
45+
ResourceServerTokenServices resourceServerTokenServices = mock(ResourceServerTokenServices.class);
46+
when(resourceServerTokenServices.readAccessToken(anyString())).thenReturn(accessToken);
4947
this.checkTokenEndpoint = new CheckTokenEndpoint(resourceServerTokenServices);
50-
51-
AccessTokenConverter accessTokenConverter = mock(AccessTokenConverter.class);
52-
when(accessTokenConverter.convertAccessToken(accessToken, authentication)).thenReturn(new HashMap());
53-
this.checkTokenEndpoint.setAccessTokenConverter(accessTokenConverter);
48+
this.accessTokenConverter = mock(AccessTokenConverter.class);
49+
when(this.accessTokenConverter.convertAccessToken(any(OAuth2AccessToken.class), any(OAuth2Authentication.class))).thenReturn(new HashMap());
50+
this.checkTokenEndpoint.setAccessTokenConverter(new CheckTokenEndpoint.CheckTokenAccessTokenConverter(this.accessTokenConverter));
5451
}
5552

5653
// gh-1070
5754
@Test
58-
public void checkTokenWhenTokenValidThenReturnActiveAttribute() throws Exception {
55+
public void checkTokenWhenDefaultAccessTokenConverterThenActiveAttributeReturned() throws Exception {
5956
Map<String, ?> response = this.checkTokenEndpoint.checkToken("access-token-1234");
6057
Object active = response.get("active");
6158
assertNotNull("active is null", active);
6259
assertEquals("active not true", Boolean.TRUE, active);
6360
}
61+
62+
// gh-1591
63+
@Test
64+
public void checkTokenWhenCustomAccessTokenConverterThenActiveAttributeNotReturned() throws Exception {
65+
this.accessTokenConverter = mock(AccessTokenConverter.class);
66+
when(this.accessTokenConverter.convertAccessToken(any(OAuth2AccessToken.class), any(OAuth2Authentication.class))).thenReturn(new HashMap());
67+
this.checkTokenEndpoint.setAccessTokenConverter(this.accessTokenConverter);
68+
Map<String, ?> response = this.checkTokenEndpoint.checkToken("access-token-1234");
69+
assertNull("active is not null", response.get("active"));
70+
}
6471
}

0 commit comments

Comments
 (0)