Search in sources :

Example 6 with AccessToken

use of uk.nhs.digital.intranet.model.AccessToken in project hippo by NHS-digital-website.

the class AccessTokenEncoderTest method encodesAndDecodesAccessToken.

@Test
public void encodesAndDecodesAccessToken() {
    final AccessToken initialAccessToken = new AccessToken("token", "refresh", 1000);
    final AccessTokenEncoder encoder = new AccessTokenEncoder();
    final String base64String = encoder.encode(initialAccessToken);
    final AccessToken resultAccessToken = encoder.decode(base64String);
    assertEquals(initialAccessToken.getToken(), resultAccessToken.getToken());
    assertEquals(initialAccessToken.getRefreshToken(), resultAccessToken.getRefreshToken());
    assertEquals(initialAccessToken.getExpirationDate(), resultAccessToken.getExpirationDate());
}
Also used : AccessToken(uk.nhs.digital.intranet.model.AccessToken) Test(org.junit.Test)

Example 7 with AccessToken

use of uk.nhs.digital.intranet.model.AccessToken in project hippo by NHS-digital-website.

the class AccessTokenValve method getNewAccessToken.

private AccessToken getNewAccessToken(final HstRequestContext requestContext, final AccessToken accessToken) throws AuthorizationException {
    try {
        final String refreshToken = getCookie(requestContext.getServletRequest(), Constants.REFRESH_TOKEN_COOKIE_NAME).map(Cookie::getValue).orElse(null);
        final AccessToken completeAccessToken = new AccessToken(accessToken.getToken(), refreshToken, accessToken.getExpirationDate());
        final AccessToken newAccessToken = authorizationProvider.refreshAccessToken(completeAccessToken);
        final Cookie accessTokenCookie = cookieProvider.getAccessTokenCookie(newAccessToken);
        final Cookie refreshTokenCookie = cookieProvider.getRefreshTokenCookie(newAccessToken);
        requestContext.getServletResponse().addCookie(accessTokenCookie);
        requestContext.getServletResponse().addCookie(refreshTokenCookie);
        return newAccessToken;
    } catch (final AuthorizationException e) {
        LOGGER.error("Received exception with status code {} from Microsoft Graph API when trying to refresh access token.", e.getStatusCode().value(), e.getCause());
        throw e;
    }
}
Also used : Cookie(javax.servlet.http.Cookie) AuthorizationException(uk.nhs.digital.intranet.model.exception.AuthorizationException) AccessToken(uk.nhs.digital.intranet.model.AccessToken)

Example 8 with AccessToken

use of uk.nhs.digital.intranet.model.AccessToken in project hippo by NHS-digital-website.

the class CookieProviderTest method returnsEncodedAccessTokenCookie.

@Test
public void returnsEncodedAccessTokenCookie() {
    final AccessToken accessToken = new AccessToken("token", "refresh-token", 10);
    final Cookie cookie = cookieProvider.getAccessTokenCookie(accessToken);
    assertEquals(Constants.ACCESS_TOKEN_COOKIE_NAME, cookie.getName());
    assertEquals(2592000, cookie.getMaxAge());
    assertEquals("/", cookie.getPath());
    assertEquals("expected_returned_value", cookie.getValue());
}
Also used : Cookie(javax.servlet.http.Cookie) AccessToken(uk.nhs.digital.intranet.model.AccessToken) Test(org.junit.Test)

Example 9 with AccessToken

use of uk.nhs.digital.intranet.model.AccessToken in project hippo by NHS-digital-website.

the class CookieProviderTest method returnsRefreshTokenCookie.

@Test
public void returnsRefreshTokenCookie() {
    final AccessToken accessToken = new AccessToken("token", "refresh-token", 10);
    final Cookie cookie = cookieProvider.getRefreshTokenCookie(accessToken);
    assertEquals(Constants.REFRESH_TOKEN_COOKIE_NAME, cookie.getName());
    assertEquals(2592000, cookie.getMaxAge());
    assertEquals("/", cookie.getPath());
    assertEquals("refresh-token", cookie.getValue());
}
Also used : Cookie(javax.servlet.http.Cookie) AccessToken(uk.nhs.digital.intranet.model.AccessToken) Test(org.junit.Test)

Example 10 with AccessToken

use of uk.nhs.digital.intranet.model.AccessToken in project hippo by NHS-digital-website.

the class AuthenticationResourceTest method requestsAccessTokenAndSavesCookieAndRedirectsToUri.

@Test
public void requestsAccessTokenAndSavesCookieAndRedirectsToUri() throws Exception {
    final AccessToken accessToken = new AccessToken("token", "refresh", 1);
    final Cookie accessTokenCookie = new Cookie("accessToken", "access_token_value");
    final Cookie refreshTokenCookie = new Cookie("refreshToken", "access_token_value");
    when(authorizationProvider.processAuthorizationResponse(AUTHORIZATION_CODE)).thenReturn(accessToken);
    when(cookieProvider.getAccessTokenCookie(accessToken)).thenReturn(accessTokenCookie);
    when(cookieProvider.getRefreshTokenCookie(accessToken)).thenReturn(refreshTokenCookie);
    authenticationResource.processResponse(response, AUTHORIZATION_CODE);
    verify(response).addCookie(accessTokenCookie);
    verify(response).addCookie(refreshTokenCookie);
    verify(response).sendRedirect(REDIRECT_URI);
}
Also used : Cookie(javax.servlet.http.Cookie) AccessToken(uk.nhs.digital.intranet.model.AccessToken) Test(org.junit.Test)

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