Search in sources :

Example 11 with AccessToken

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();
}
Also used : AccessToken(uk.nhs.digital.intranet.model.AccessToken) Test(org.junit.Test)

Example 12 with AccessToken

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);
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) AccessToken(uk.nhs.digital.intranet.model.AccessToken) URI(java.net.URI) Test(org.junit.Test)

Example 13 with AccessToken

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));
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) AccessToken(uk.nhs.digital.intranet.model.AccessToken) URI(java.net.URI) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Example 14 with 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;
}
Also used : Cookie(javax.servlet.http.Cookie) AccessToken(uk.nhs.digital.intranet.model.AccessToken)

Example 15 with AccessToken

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;
}
Also used : Cookie(javax.servlet.http.Cookie) AuthorizationException(uk.nhs.digital.intranet.model.exception.AuthorizationException) AccessToken(uk.nhs.digital.intranet.model.AccessToken)

Aggregations

AccessToken (uk.nhs.digital.intranet.model.AccessToken)18 Test (org.junit.Test)12 Cookie (javax.servlet.http.Cookie)7 AuthorizationException (uk.nhs.digital.intranet.model.exception.AuthorizationException)6 URI (java.net.URI)4 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)3 MultiValueMap (org.springframework.util.MultiValueMap)3 HstRequestContext (org.hippoecm.hst.core.request.HstRequestContext)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1 HttpStatusCodeException (org.springframework.web.client.HttpStatusCodeException)1 TokenResponse (uk.nhs.digital.intranet.json.TokenResponse)1