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);
}
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")));
}
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();
}
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());
}
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());
}
}
Aggregations