use of uk.nhs.digital.intranet.model.AccessToken in project hippo by NHS-digital-website.
the class AccessTokenValveTest method requestsNewAccessTokenIfAccessTokenIsExpired.
@Test
public void requestsNewAccessTokenIfAccessTokenIsExpired() throws Exception {
final AccessToken expiredAccessToken = new AccessToken("token", null, -3600);
final AccessToken completeAccessToken = new AccessToken("token", REFRESH_TOKEN, -3600);
final AccessToken newAccessToken = new AccessToken("new-token", "refresh", 3600);
when(servletRequest.getCookies()).thenReturn(new Cookie[] { ACCESS_TOKEN_COOKIE, REFRESH_TOKEN_COOKIE });
when(encoder.decode(ENCODED_COOKIE_VALUE)).thenReturn(expiredAccessToken);
when(authorizationProvider.refreshAccessToken(completeAccessToken)).thenReturn(newAccessToken);
when(cookieProvider.getAccessTokenCookie(newAccessToken)).thenReturn(ACCESS_TOKEN_COOKIE);
when(cookieProvider.getRefreshTokenCookie(newAccessToken)).thenReturn(REFRESH_TOKEN_COOKIE);
valve.invoke(valveContext);
verify(requestContext).setAttribute(Constants.ACCESS_TOKEN_PROPERTY_NAME, "new-token");
verify(servletResponse).addCookie(ACCESS_TOKEN_COOKIE);
verify(servletResponse).addCookie(REFRESH_TOKEN_COOKIE);
verify(valveContext).invokeNext();
}
use of uk.nhs.digital.intranet.model.AccessToken in project hippo by NHS-digital-website.
the class MicrosoftGraphAuthorizationProviderTest method throwsAuthorizationExceptionIfGraphApiThrowsHttpExceptionOnRefreshAccessToken.
@Test(expected = AuthorizationException.class)
public void throwsAuthorizationExceptionIfGraphApiThrowsHttpExceptionOnRefreshAccessToken() throws Exception {
when(restTemplate.postForEntity(any(URI.class), any(HttpEntity.class), any())).thenThrow(new HttpClientErrorException(HttpStatus.BAD_REQUEST));
final AccessToken oldAccessToken = new AccessToken(TOKEN, REFRESH_TOKEN, 1);
authorizationProvider.refreshAccessToken(oldAccessToken);
}
use of uk.nhs.digital.intranet.model.AccessToken in project hippo by NHS-digital-website.
the class MicrosoftGraphAuthorizationProviderTest method callsGraphApiWithCorrectHeadersAndReturnsAccessToken.
@Test
public void callsGraphApiWithCorrectHeadersAndReturnsAccessToken() throws Exception {
when(restTemplate.postForEntity(any(URI.class), any(HttpEntity.class), any())).thenReturn(ResponseEntity.ok().body(TOKEN_RESPONSE));
final MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("client_id", APP_ID);
map.add("scope", SCOPE);
map.add("redirect_uri", REDIRECT_URI);
map.add("grant_type", "authorization_code");
map.add("client_secret", CLIENT_SECRET);
map.add("code", AUTHORIZATION_CODE);
final HttpEntity<MultiValueMap<String, String>> httpRequest = getHttpRequest(map);
final AccessToken accessToken = authorizationProvider.processAuthorizationResponse(AUTHORIZATION_CODE);
verify(restTemplate).postForEntity(URI.create(TOKEN_URL), httpRequest, TokenResponse.class);
assertNotNull(accessToken);
assertEquals(TOKEN, accessToken.getToken());
assertEquals(REFRESH_TOKEN, accessToken.getRefreshToken());
assertTrue("Token expiration should be within 5 seconds of calculated time", timeWithin5Seconds(accessToken));
}
use of uk.nhs.digital.intranet.model.AccessToken in project hippo by NHS-digital-website.
the class CookieProvider method getAccessTokenCookie.
public Cookie getAccessTokenCookie(final AccessToken accessToken) {
final AccessToken lightAccessToken = new AccessToken(accessToken.getToken(), null, accessToken.getExpirationDate());
final String encodedAccessToken = encoder.encode(lightAccessToken);
final Cookie accessTokenCookie = new Cookie(Constants.ACCESS_TOKEN_COOKIE_NAME, encodedAccessToken);
accessTokenCookie.setPath(COOKIE_PATH);
accessTokenCookie.setMaxAge(TTL);
return accessTokenCookie;
}
use of uk.nhs.digital.intranet.model.AccessToken in project hippo by NHS-digital-website.
the class AuthenticationResource method processResponse.
@GET
@Path("/response")
public boolean processResponse(@Context HttpServletResponse response, @QueryParam("code") final String authorizationCode) throws IOException {
try {
final AccessToken accessToken = authorizationProvider.processAuthorizationResponse(authorizationCode);
final Cookie accessTokenCookie = cookieProvider.getAccessTokenCookie(accessToken);
final Cookie refreshTokenCookie = cookieProvider.getRefreshTokenCookie(accessToken);
response.addCookie(accessTokenCookie);
response.addCookie(refreshTokenCookie);
} catch (final AuthorizationException e) {
LOGGER.error("Received exception with status code {} from Microsoft Graph API when trying to acquire access token.", e.getStatusCode().value(), e.getCause());
} finally {
response.sendRedirect(redirectUri);
}
return true;
}
Aggregations