use of uk.nhs.digital.intranet.model.AccessToken in project hippo by NHS-digital-website.
the class AccessTokenValveTest method setsRequestContextAttributeIfAccessTokenNotExpired.
@Test
public void setsRequestContextAttributeIfAccessTokenNotExpired() throws Exception {
final AccessToken accessToken = new AccessToken("token", "refresh", 3600);
when(servletRequest.getCookies()).thenReturn(new Cookie[] { ACCESS_TOKEN_COOKIE });
when(encoder.decode(ENCODED_COOKIE_VALUE)).thenReturn(accessToken);
valve.invoke(valveContext);
verify(requestContext).setAttribute(Constants.ACCESS_TOKEN_PROPERTY_NAME, "token");
verify(valveContext).invokeNext();
}
use of uk.nhs.digital.intranet.model.AccessToken in project hippo by NHS-digital-website.
the class AccessTokenValveTest method failsGracefullyIfCannotRequestNewAccessToken.
@Test
public void failsGracefullyIfCannotRequestNewAccessToken() throws Exception {
final AccessToken expiredAccessToken = new AccessToken("token", null, -3600);
final AccessToken completeAccessToken = new AccessToken("token", REFRESH_TOKEN, -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)).thenThrow(new AuthorizationException(HttpStatus.BAD_REQUEST, new RuntimeException()));
valve.invoke(valveContext);
verify(requestContext).removeAttribute(Constants.ACCESS_TOKEN_PROPERTY_NAME);
verify(servletResponse).addCookie(CookieProvider.EMPTY_ACCESS_TOKEN_COOKIE);
verify(servletResponse).addCookie(CookieProvider.EMPTY_REFRESH_TOKEN_COOKIE);
verify(valveContext).invokeNext();
}
use of uk.nhs.digital.intranet.model.AccessToken in project hippo by NHS-digital-website.
the class AccessTokenValveTest method usesNullRefreshTokenIfRefreshTokenCookieNotPresent.
@Test
public void usesNullRefreshTokenIfRefreshTokenCookieNotPresent() throws Exception {
final AccessToken expiredAccessToken = new AccessToken("token", null, -3600);
when(servletRequest.getCookies()).thenReturn(new Cookie[] { ACCESS_TOKEN_COOKIE });
when(encoder.decode(ENCODED_COOKIE_VALUE)).thenReturn(expiredAccessToken);
when(authorizationProvider.refreshAccessToken(expiredAccessToken)).thenThrow(new AuthorizationException(HttpStatus.BAD_REQUEST, new RuntimeException()));
valve.invoke(valveContext);
verify(requestContext).removeAttribute(Constants.ACCESS_TOKEN_PROPERTY_NAME);
verify(servletResponse).addCookie(CookieProvider.EMPTY_ACCESS_TOKEN_COOKIE);
verify(servletResponse).addCookie(CookieProvider.EMPTY_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 throwsExceptionIfAccessTokenResponseIsNullOnRefreshAccessToken.
@Test(expected = IllegalArgumentException.class)
public void throwsExceptionIfAccessTokenResponseIsNullOnRefreshAccessToken() throws Exception {
when(restTemplate.postForEntity(any(URI.class), any(HttpEntity.class), any())).thenReturn(ResponseEntity.ok().body(null));
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 callsGraphApiWithCorrectHeadersAndRefreshesAccessToken.
@Test
public void callsGraphApiWithCorrectHeadersAndRefreshesAccessToken() throws Exception {
when(restTemplate.postForEntity(any(URI.class), any(HttpEntity.class), any())).thenReturn(ResponseEntity.ok().body(TOKEN_RESPONSE));
final AccessToken oldAccessToken = new AccessToken(TOKEN, REFRESH_TOKEN, 1);
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", "refresh_token");
map.add("client_secret", CLIENT_SECRET);
map.add("refresh_token", REFRESH_TOKEN);
final HttpEntity<MultiValueMap<String, String>> httpRequest = getHttpRequest(map);
final AccessToken accessToken = authorizationProvider.refreshAccessToken(oldAccessToken);
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));
}
Aggregations