Search in sources :

Example 1 with AddressSessionItem

use of uk.gov.di.ipv.cri.address.library.persistence.item.AddressSessionItem in project di-ipv-cri-address-api by alphagov.

the class DataStoreTest method setUp.

@BeforeEach
void setUp() {
    when(mockDynamoDbEnhancedClient.table(anyString(), ArgumentMatchers.<TableSchema<AddressSessionItem>>any())).thenReturn(mockDynamoDbTable);
    addressSessionItem = new AddressSessionItem();
    String accessToken = UUID.randomUUID().toString();
    dataStore = new DataStore<>(TEST_TABLE_NAME, AddressSessionItem.class, mockDynamoDbEnhancedClient);
}
Also used : AddressSessionItem(uk.gov.di.ipv.cri.address.library.persistence.item.AddressSessionItem) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with AddressSessionItem

use of uk.gov.di.ipv.cri.address.library.persistence.item.AddressSessionItem in project di-ipv-cri-address-api by alphagov.

the class AddressSessionServiceTest method shouldCallCreateOnAddressSessionDataStore.

@Test
void shouldCallCreateOnAddressSessionDataStore() {
    when(mockConfigurationService.getAddressSessionTtl()).thenReturn(1L);
    SessionRequest sessionRequest = mock(SessionRequest.class);
    when(sessionRequest.getClientId()).thenReturn("a client id");
    when(sessionRequest.getState()).thenReturn("state");
    when(sessionRequest.getRedirectUri()).thenReturn(URI.create("https://www.example.com/callback"));
    addressSessionService.createAndSaveAddressSession(sessionRequest);
    verify(mockDataStore).create(mockAddressSessionItem.capture());
    AddressSessionItem capturedValue = mockAddressSessionItem.getValue();
    assertNotNull(capturedValue.getSessionId());
    assertThat(capturedValue.getExpiryDate(), equalTo(fixedInstant.getEpochSecond() + 1));
    assertThat(capturedValue.getClientId(), equalTo("a client id"));
    assertThat(capturedValue.getState(), equalTo("state"));
    assertThat(capturedValue.getRedirectUri(), equalTo(URI.create("https://www.example.com/callback")));
}
Also used : AddressSessionItem(uk.gov.di.ipv.cri.address.library.persistence.item.AddressSessionItem) SessionRequest(uk.gov.di.ipv.cri.address.library.domain.SessionRequest) Test(org.junit.jupiter.api.Test)

Example 3 with AddressSessionItem

use of uk.gov.di.ipv.cri.address.library.persistence.item.AddressSessionItem in project di-ipv-cri-address-api by alphagov.

the class AddressSessionService method createAndSaveAddressSession.

public UUID createAndSaveAddressSession(SessionRequest sessionRequest) {
    AddressSessionItem addressSessionItem = new AddressSessionItem();
    addressSessionItem.setExpiryDate(clock.instant().plus(configurationService.getAddressSessionTtl(), ChronoUnit.SECONDS).getEpochSecond());
    addressSessionItem.setState(sessionRequest.getState());
    addressSessionItem.setClientId(sessionRequest.getClientId());
    addressSessionItem.setRedirectUri(sessionRequest.getRedirectUri());
    // TODO: create authorization_code, this is temporary see:
    // https://govukverify.atlassian.net/browse/KBV-237
    addressSessionItem.setAuthorizationCode(UUID.randomUUID().toString());
    dataStore.create(addressSessionItem);
    return addressSessionItem.getSessionId();
}
Also used : AddressSessionItem(uk.gov.di.ipv.cri.address.library.persistence.item.AddressSessionItem)

Example 4 with AddressSessionItem

use of uk.gov.di.ipv.cri.address.library.persistence.item.AddressSessionItem in project di-ipv-cri-address-api by alphagov.

the class AccessTokenHandlerTest method shouldReturnAccessTokenOnSuccessfulExchange.

@Test
void shouldReturnAccessTokenOnSuccessfulExchange() throws Exception {
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    String tokenRequestBody = "code=12345&redirect_uri=http://test.com&grant_type=authorization_code&client_id=test_client_id";
    event.withBody(tokenRequestBody);
    AddressSessionItem addressSessionItem = mock(AddressSessionItem.class);
    AccessToken accessToken = new BearerAccessToken();
    tokenResponse = new AccessTokenResponse(new Tokens(accessToken, null));
    // TODO: This here as a placeholder pending the story that generates the authorization code
    TokenRequest tokenRequest = mock(TokenRequest.class);
    when(tokenRequest.getAuthorizationGrant()).thenReturn(new AuthorizationCodeGrant(new AuthorizationCode("12345"), URI.create("http://test.com"), null));
    when(mockAddressSessionService.createTokenRequest(tokenRequestBody)).thenReturn(tokenRequest);
    when(mockAddressSessionService.createToken(any())).thenReturn(tokenResponse);
    when(mockAddressSessionService.getAddressSessionItemByValue(any())).thenReturn(addressSessionItem);
    APIGatewayProxyResponseEvent response = handler.handleRequest(event, context);
    Map<String, Object> responseBody = objectMapper.readValue(response.getBody(), new TypeReference<>() {
    });
    assertEquals(ContentType.APPLICATION_JSON.getType(), response.getHeaders().get("Content-Type"));
    assertEquals(HttpStatus.SC_OK, response.getStatusCode());
    assertEquals(tokenResponse.toSuccessResponse().getTokens().getAccessToken().getValue(), responseBody.get("access_token").toString());
}
Also used : AuthorizationCode(com.nimbusds.oauth2.sdk.AuthorizationCode) APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) AuthorizationCodeGrant(com.nimbusds.oauth2.sdk.AuthorizationCodeGrant) AccessToken(com.nimbusds.oauth2.sdk.token.AccessToken) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) TokenRequest(com.nimbusds.oauth2.sdk.TokenRequest) AddressSessionItem(uk.gov.di.ipv.cri.address.library.persistence.item.AddressSessionItem) ErrorObject(com.nimbusds.oauth2.sdk.ErrorObject) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) AccessTokenResponse(com.nimbusds.oauth2.sdk.AccessTokenResponse) Tokens(com.nimbusds.oauth2.sdk.token.Tokens) Test(org.junit.jupiter.api.Test)

Example 5 with AddressSessionItem

use of uk.gov.di.ipv.cri.address.library.persistence.item.AddressSessionItem in project di-ipv-cri-address-api by alphagov.

the class AccessTokenHandler method handleRequest.

public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
    try {
        TokenRequest tokenRequest = addressSessionService.createTokenRequest(input.getBody());
        String authorizationCodeFromRequest = ((AuthorizationCodeGrant) tokenRequest.getAuthorizationGrant()).getAuthorizationCode().getValue();
        AddressSessionItem addressSessionItem = addressSessionService.getAddressSessionItemByValue(authorizationCodeFromRequest);
        TokenResponse tokenResponse = addressSessionService.createToken(tokenRequest);
        AccessTokenResponse accessTokenResponse = tokenResponse.toSuccessResponse();
        addressSessionService.writeToken(accessTokenResponse, addressSessionItem);
        return ApiGatewayResponseGenerator.proxyJsonResponse(HttpStatus.SC_OK, accessTokenResponse.toJSONObject());
    } catch (ParseException e) {
        LOGGER.error("Token request could not be parsed: {} {}", e.getErrorObject().getDescription(), e);
        return ApiGatewayResponseGenerator.proxyJsonResponse(getHttpStatusCodeForErrorResponse(e.getErrorObject()), e.getErrorObject().toJSONObject());
    }
}
Also used : TokenResponse(com.nimbusds.oauth2.sdk.TokenResponse) AccessTokenResponse(com.nimbusds.oauth2.sdk.AccessTokenResponse) TokenRequest(com.nimbusds.oauth2.sdk.TokenRequest) AddressSessionItem(uk.gov.di.ipv.cri.address.library.persistence.item.AddressSessionItem) ParseException(com.nimbusds.oauth2.sdk.ParseException) AccessTokenResponse(com.nimbusds.oauth2.sdk.AccessTokenResponse)

Aggregations

AddressSessionItem (uk.gov.di.ipv.cri.address.library.persistence.item.AddressSessionItem)5 AccessTokenResponse (com.nimbusds.oauth2.sdk.AccessTokenResponse)2 TokenRequest (com.nimbusds.oauth2.sdk.TokenRequest)2 Test (org.junit.jupiter.api.Test)2 APIGatewayProxyRequestEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent)1 APIGatewayProxyResponseEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)1 AuthorizationCode (com.nimbusds.oauth2.sdk.AuthorizationCode)1 AuthorizationCodeGrant (com.nimbusds.oauth2.sdk.AuthorizationCodeGrant)1 ErrorObject (com.nimbusds.oauth2.sdk.ErrorObject)1 ParseException (com.nimbusds.oauth2.sdk.ParseException)1 TokenResponse (com.nimbusds.oauth2.sdk.TokenResponse)1 AccessToken (com.nimbusds.oauth2.sdk.token.AccessToken)1 BearerAccessToken (com.nimbusds.oauth2.sdk.token.BearerAccessToken)1 Tokens (com.nimbusds.oauth2.sdk.token.Tokens)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 SessionRequest (uk.gov.di.ipv.cri.address.library.domain.SessionRequest)1