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

Commit e80eefd

Browse files
fcrespeljgrandja
authored andcommitted
Allow missing "active" field in check_token/introspect response.
Also support both Boolean and String values for this field. Fixes gh-1533
1 parent 5d9ac62 commit e80eefd

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

spring-security-oauth2/src/main/java/org/springframework/security/oauth2/provider/token/RemoteTokenServices.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public OAuth2Authentication loadAuthentication(String accessToken) throws Authen
111111
}
112112

113113
// gh-838
114-
if (!Boolean.TRUE.equals(map.get("active"))) {
114+
if (map.containsKey("active") && !"true".equals(String.valueOf(map.get("active")))) {
115115
logger.debug("check_token returned active attribute: " + map.get("active"));
116116
throw new InvalidTokenException(accessToken);
117117
}

spring-security-oauth2/src/test/java/org/springframework/security/oauth2/provider/token/RemoteTokenServicesTest.java

+18-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void setUp() {
5353

5454
// gh-838
5555
@Test
56-
public void loadAuthenticationWhenIntrospectionResponseContainsActiveTrueThenReturnAuthentication() throws Exception {
56+
public void loadAuthenticationWhenIntrospectionResponseContainsActiveTrueBooleanThenReturnAuthentication() throws Exception {
5757
Map responseAttrs = new HashMap();
5858
responseAttrs.put("active", true); // "active" is the only required attribute as per RFC 7662 (https://tools.ietf.org/search/rfc7662#section-2.2)
5959
ResponseEntity<Map> response = new ResponseEntity<Map>(responseAttrs, HttpStatus.OK);
@@ -65,6 +65,19 @@ public void loadAuthenticationWhenIntrospectionResponseContainsActiveTrueThenRet
6565
assertNotNull(authentication);
6666
}
6767

68+
@Test
69+
public void loadAuthenticationWhenIntrospectionResponseContainsActiveTrueStringThenReturnAuthentication() throws Exception {
70+
Map responseAttrs = new HashMap();
71+
responseAttrs.put("active", "true"); // "active" is the only required attribute as per RFC 7662 (https://tools.ietf.org/search/rfc7662#section-2.2)
72+
ResponseEntity<Map> response = new ResponseEntity<Map>(responseAttrs, HttpStatus.OK);
73+
RestTemplate restTemplate = mock(RestTemplate.class);
74+
when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class), any(Class.class))).thenReturn(response);
75+
this.remoteTokenServices.setRestTemplate(restTemplate);
76+
77+
OAuth2Authentication authentication = this.remoteTokenServices.loadAuthentication("access-token-1234");
78+
assertNotNull(authentication);
79+
}
80+
6881
// gh-838
6982
@Test(expected = InvalidTokenException.class)
7083
public void loadAuthenticationWhenIntrospectionResponseContainsActiveFalseThenThrowInvalidTokenException() throws Exception {
@@ -79,14 +92,15 @@ public void loadAuthenticationWhenIntrospectionResponseContainsActiveFalseThenTh
7992
}
8093

8194
// gh-838
82-
@Test(expected = InvalidTokenException.class)
83-
public void loadAuthenticationWhenIntrospectionResponseMissingActiveAttributeThenThrowInvalidTokenException() throws Exception {
95+
@Test
96+
public void loadAuthenticationWhenIntrospectionResponseMissingActiveAttributeThenReturnAuthentication() throws Exception {
8497
Map responseAttrs = new HashMap();
8598
ResponseEntity<Map> response = new ResponseEntity<Map>(responseAttrs, HttpStatus.OK);
8699
RestTemplate restTemplate = mock(RestTemplate.class);
87100
when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class), any(Class.class))).thenReturn(response);
88101
this.remoteTokenServices.setRestTemplate(restTemplate);
89102

90-
this.remoteTokenServices.loadAuthentication("access-token-1234");
103+
OAuth2Authentication authentication = this.remoteTokenServices.loadAuthentication("access-token-1234");
104+
assertNotNull(authentication);
91105
}
92106
}

0 commit comments

Comments
 (0)