use of org.apereo.cas.ticket.accesstoken.AccessTokenImpl in project cas by apereo.
the class OAuth20ProfileControllerTests method verifyOKWithAuthorizationHeader.
@Test
public void verifyOKWithAuthorizationHeader() throws Exception {
final Map<String, Object> map = new HashMap<>();
map.put(NAME, VALUE);
final List<String> list = Arrays.asList(VALUE, VALUE);
map.put(NAME2, list);
final Principal principal = CoreAuthenticationTestUtils.getPrincipal(ID, map);
final Authentication authentication = getAuthentication(principal);
final AccessTokenImpl accessToken = (AccessTokenImpl) accessTokenFactory.create(CoreAuthenticationTestUtils.getService(), authentication);
oAuth20ProfileController.getTicketRegistry().addTicket(accessToken);
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(GET, CONTEXT + OAuthConstants.PROFILE_URL);
mockRequest.addHeader("Authorization", OAuthConstants.BEARER_TOKEN + ' ' + accessToken.getId());
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final ResponseEntity<String> entity = oAuth20ProfileController.handleRequestInternal(mockRequest, mockResponse);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals(CONTENT_TYPE, mockResponse.getContentType());
final String expected = "{\"id\":\"" + ID + "\",\"attributes\":[{\"" + NAME + "\":\"" + VALUE + "\"},{\"" + NAME2 + "\":[\"" + VALUE + "\",\"" + VALUE + "\"]}]}";
final JsonNode expectedObj = MAPPER.readTree(expected);
final JsonNode receivedObj = MAPPER.readTree(entity.getBody());
assertEquals(expectedObj.get("id").asText(), receivedObj.get("id").asText());
final JsonNode expectedAttributes = expectedObj.get(ATTRIBUTES_PARAM);
final JsonNode receivedAttributes = receivedObj.get(ATTRIBUTES_PARAM);
assertEquals(expectedAttributes.findValue(NAME).asText(), receivedAttributes.findValue(NAME).asText());
assertEquals(expectedAttributes.findValues(NAME2), receivedAttributes.findValues(NAME2));
}
use of org.apereo.cas.ticket.accesstoken.AccessTokenImpl in project cas by apereo.
the class OAuth20ProfileControllerTests method verifyExpiredAccessToken.
@Test
public void verifyExpiredAccessToken() throws Exception {
final Principal principal = CoreAuthenticationTestUtils.getPrincipal(ID, new HashMap<>());
final Authentication authentication = getAuthentication(principal);
final DefaultAccessTokenFactory expiringAccessTokenFactory = new DefaultAccessTokenFactory(new AlwaysExpiresExpirationPolicy());
final AccessTokenImpl accessToken = (AccessTokenImpl) expiringAccessTokenFactory.create(CoreAuthenticationTestUtils.getService(), authentication);
oAuth20ProfileController.getTicketRegistry().addTicket(accessToken);
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(GET, CONTEXT + OAuthConstants.PROFILE_URL);
mockRequest.setParameter(OAuthConstants.ACCESS_TOKEN, accessToken.getId());
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final ResponseEntity<String> entity = oAuth20ProfileController.handleRequestInternal(mockRequest, mockResponse);
assertEquals(HttpStatus.UNAUTHORIZED, entity.getStatusCode());
assertEquals(CONTENT_TYPE, mockResponse.getContentType());
assertTrue(entity.getBody().contains(OAuthConstants.EXPIRED_ACCESS_TOKEN));
}
use of org.apereo.cas.ticket.accesstoken.AccessTokenImpl in project cas by apereo.
the class OAuth20ProfileControllerTests method verifyOK.
@Test
public void verifyOK() throws Exception {
final Map<String, Object> map = new HashMap<>();
map.put(NAME, VALUE);
final List<String> list = Arrays.asList(VALUE, VALUE);
map.put(NAME2, list);
final Principal principal = CoreAuthenticationTestUtils.getPrincipal(ID, map);
final Authentication authentication = getAuthentication(principal);
final AccessTokenImpl accessToken = (AccessTokenImpl) accessTokenFactory.create(CoreAuthenticationTestUtils.getService(), authentication);
oAuth20ProfileController.getTicketRegistry().addTicket(accessToken);
final MockHttpServletRequest mockRequest = new MockHttpServletRequest(GET, CONTEXT + OAuthConstants.PROFILE_URL);
mockRequest.setParameter(OAuthConstants.ACCESS_TOKEN, accessToken.getId());
final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
final ResponseEntity<String> entity = oAuth20ProfileController.handleRequestInternal(mockRequest, mockResponse);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals(CONTENT_TYPE, mockResponse.getContentType());
final String expected = "{\"id\":\"" + ID + "\",\"attributes\":[{\"" + NAME + "\":\"" + VALUE + "\"},{\"" + NAME2 + "\":[\"" + VALUE + "\",\"" + VALUE + "\"]}]}";
final JsonNode expectedObj = MAPPER.readTree(expected);
final JsonNode receivedObj = MAPPER.readTree(entity.getBody());
assertEquals(expectedObj.get("id").asText(), receivedObj.get("id").asText());
final JsonNode expectedAttributes = expectedObj.get(ATTRIBUTES_PARAM);
final JsonNode receivedAttributes = receivedObj.get(ATTRIBUTES_PARAM);
assertEquals(expectedAttributes.findValue(NAME).asText(), receivedAttributes.findValue(NAME).asText());
assertEquals(expectedAttributes.findValues(NAME2), receivedAttributes.findValues(NAME2));
}
Aggregations