use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project di-authentication-api by alphagov.
the class IdentityHandlerTest method shouldReturn401WhenBearerTokenIsNotParseable.
@Test
void shouldReturn401WhenBearerTokenIsNotParseable() throws AccessTokenException {
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
event.setHeaders(Map.of("Authorization", "this-is-not-a-valid-token"));
AccessTokenException accessTokenException = new AccessTokenException("Unable to parse AccessToken", INVALID_TOKEN);
when(accessTokenService.parse("this-is-not-a-valid-token", true)).thenThrow(accessTokenException);
APIGatewayProxyResponseEvent result = handler.handleRequest(event, context);
assertThat(result, hasStatus(401));
assertEquals(INVALID_TOKEN_RESPONSE, result.getMultiValueHeaders());
}
use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project di-authentication-api by alphagov.
the class IdentityHandlerTest method shouldReturnIdentityResponseForSuccessfulRequest.
@Test
void shouldReturnIdentityResponseForSuccessfulRequest() throws AccessTokenException, JsonProcessingException {
String serializedCredential = SignedCredentialHelper.generateCredential().serialize();
IdentityResponse identityResponse = new IdentityResponse(SUBJECT.getValue(), serializedCredential);
AccessToken accessToken = new BearerAccessToken();
when(accessTokenService.parse(accessToken.toAuthorizationHeader(), true)).thenReturn(accessTokenInfo);
when(identityService.populateIdentityResponse(accessTokenInfo)).thenReturn(identityResponse);
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
event.setHeaders(Map.of("Authorization", accessToken.toAuthorizationHeader()));
APIGatewayProxyResponseEvent result = handler.handleRequest(event, context);
assertThat(result, hasStatus(200));
IdentityResponse receivedIdentityResponse = new ObjectMapper().readValue(result.getBody(), IdentityResponse.class);
assertThat(receivedIdentityResponse.getIdentityCredential(), equalTo(serializedCredential));
assertThat(receivedIdentityResponse.getSub(), equalTo(SUBJECT.getValue()));
}
use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project di-authentication-api by alphagov.
the class IdentityHandlerTest method shouldReturn401WhenAccessTokenIsMissing.
@Test
void shouldReturn401WhenAccessTokenIsMissing() {
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
APIGatewayProxyResponseEvent result = handler.handleRequest(event, context);
assertThat(result, hasStatus(401));
}
use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project di-authentication-api by alphagov.
the class IPVCallbackHandlerTest method shouldRedirectToLoginUriForSuccessfulResponse.
@Test
void shouldRedirectToLoginUriForSuccessfulResponse() throws URISyntaxException {
usingValidSession();
usingValidClientSession();
TokenResponse successfulTokenResponse = new AccessTokenResponse(new Tokens(new BearerAccessToken(), null));
TokenRequest tokenRequest = mock(TokenRequest.class);
Map<String, String> responseHeaders = new HashMap<>();
responseHeaders.put("code", AUTH_CODE.getValue());
responseHeaders.put("state", STATE.getValue());
when(dynamoClientService.getClient(CLIENT_ID.getValue())).thenReturn(Optional.of(generateClientRegistry()));
when(responseService.validateResponse(responseHeaders, SESSION_ID)).thenReturn(Optional.empty());
when(dynamoService.getUserProfileFromEmail(TEST_EMAIL_ADDRESS)).thenReturn(Optional.of(generateUserProfile()));
when(ipvTokenService.constructTokenRequest(AUTH_CODE.getValue())).thenReturn(tokenRequest);
when(ipvTokenService.sendTokenRequest(tokenRequest)).thenReturn(successfulTokenResponse);
when(ipvTokenService.sendIpvInfoRequest(successfulTokenResponse.toSuccessResponse().getTokens().getBearerAccessToken())).thenReturn(SignedCredentialHelper.generateCredential().serialize());
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
event.setQueryStringParameters(responseHeaders);
event.setHeaders(Map.of(COOKIE, buildCookieString()));
APIGatewayProxyResponseEvent response = makeHandlerRequest(event);
assertThat(response, hasStatus(302));
URI redirectUri = new URIBuilder(LOGIN_URL).setPath("auth-code").build();
assertThat(response.getHeaders().get("Location"), equalTo(redirectUri.toString()));
}
use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent in project di-authentication-api by alphagov.
the class IPVCallbackHandlerTest method shouldThrowWhenAuthnResponseContainsError.
@Test
void shouldThrowWhenAuthnResponseContainsError() {
usingValidSession();
usingValidClientSession();
ErrorObject errorObject = new ErrorObject("invalid_request_redirect_uri", "redirect_uri param must be provided");
Map<String, String> responseHeaders = new HashMap<>();
responseHeaders.put("code", AUTH_CODE.getValue());
responseHeaders.put("state", STATE.getValue());
responseHeaders.put("error", errorObject.toString());
when(dynamoClientService.getClient(CLIENT_ID.getValue())).thenReturn(Optional.of(generateClientRegistry()));
when(responseService.validateResponse(responseHeaders, SESSION_ID)).thenReturn(Optional.of(new ErrorObject(errorObject.getCode())));
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
event.setHeaders(Map.of(COOKIE, buildCookieString()));
event.setQueryStringParameters(responseHeaders);
RuntimeException expectedException = assertThrows(RuntimeException.class, () -> handler.handleRequest(event, context), "Expected to throw exception");
assertThat(expectedException.getMessage(), equalTo("Error in IPV AuthorisationResponse"));
verifyNoInteractions(ipvTokenService);
}
Aggregations