use of uk.gov.di.ipv.cri.address.library.exception.AccessTokenRequestException in project di-ipv-cri-address-api by alphagov.
the class AccessTokenHandlerTest method shouldReturn400WhenInvalidGrantTypeProvided.
@Test
void shouldReturn400WhenInvalidGrantTypeProvided() throws Exception {
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
String tokenRequestBody = "code=12345&redirect_uri=http://test.com&grant_type=" + GrantType.IMPLICIT.getValue() + "&client_id=test_client_id";
event.withBody(tokenRequestBody);
when(mockAddressSessionService.createTokenRequest(tokenRequestBody)).thenThrow(new AccessTokenRequestException(OAuth2Error.UNSUPPORTED_GRANT_TYPE));
APIGatewayProxyResponseEvent response = handler.handleRequest(event, context);
ErrorObject errorResponse = createErrorObjectFromResponse(response.getBody());
assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
assertEquals(OAuth2Error.UNSUPPORTED_GRANT_TYPE_CODE, errorResponse.getCode());
assertEquals(OAuth2Error.UNSUPPORTED_GRANT_TYPE.getDescription(), errorResponse.getDescription());
}
use of uk.gov.di.ipv.cri.address.library.exception.AccessTokenRequestException in project di-ipv-cri-address-api by alphagov.
the class AccessTokenHandlerTest method shouldReturn400WhenInvalidRedirectUriIsProvided.
@Test
void shouldReturn400WhenInvalidRedirectUriIsProvided() throws ParseException {
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);
when(mockAddressSessionService.createTokenRequest(tokenRequestBody)).thenThrow(new AccessTokenRequestException(OAuth2Error.INVALID_GRANT));
APIGatewayProxyResponseEvent response = handler.handleRequest(event, context);
ErrorObject errorResponse = createErrorObjectFromResponse(response.getBody());
assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
assertEquals(OAuth2Error.INVALID_GRANT.getCode(), errorResponse.getCode());
assertEquals(OAuth2Error.INVALID_GRANT.getDescription(), errorResponse.getDescription());
}
use of uk.gov.di.ipv.cri.address.library.exception.AccessTokenRequestException in project di-ipv-cri-address-api by alphagov.
the class AccessTokenHandlerTest method shouldReturn400WhenInvalidAuthorisationCodeProvided.
@Test
void shouldReturn400WhenInvalidAuthorisationCodeProvided() throws Exception {
String tokenRequestBody = "code=12345&redirect_uri=http://test.com&grant_type=authorization_code&client_id=test_client_id";
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
event.withBody(tokenRequestBody);
when(mockAddressSessionService.createTokenRequest(tokenRequestBody)).thenThrow(new AccessTokenRequestException(OAuth2Error.INVALID_GRANT));
APIGatewayProxyResponseEvent response = handler.handleRequest(event, context);
ErrorObject errorResponse = createErrorObjectFromResponse(response.getBody());
assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatusCode());
assertEquals(OAuth2Error.INVALID_GRANT.getCode(), errorResponse.getCode());
assertEquals(OAuth2Error.INVALID_GRANT.getDescription(), errorResponse.getDescription());
}
use of uk.gov.di.ipv.cri.address.library.exception.AccessTokenRequestException in project di-ipv-cri-address-api by alphagov.
the class AddressSessionService method createTokenRequest.
public TokenRequest createTokenRequest(String requestBody) throws com.nimbusds.oauth2.sdk.ParseException {
// The URI is not needed/consumed in the resultant TokenRequest
// therefore any value can be passed here to ensure the parse method
// successfully materialises a TokenRequest
URI arbitraryUri = URI.create("https://gds");
HTTPRequest request = new HTTPRequest(HTTPRequest.Method.POST, arbitraryUri);
request.setQuery(requestBody);
boolean invalidTokenRequest = request.getQueryParameters().keySet().containsAll(List.of(CODE, CLIENT_ID, REDIRECT_URI, GRANT_TYPE));
if (!invalidTokenRequest) {
throw new AccessTokenRequestException(OAuth2Error.INVALID_REQUEST);
}
validateTokenRequest(request.getQueryParameters());
request.setContentType(ContentType.APPLICATION_URLENCODED.getType());
return TokenRequest.parse(request);
}
Aggregations