Search in sources :

Example 1 with AccessTokenRequestException

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());
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) AccessTokenRequestException(uk.gov.di.ipv.cri.address.library.exception.AccessTokenRequestException) ErrorObject(com.nimbusds.oauth2.sdk.ErrorObject) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Example 2 with AccessTokenRequestException

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());
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) AccessTokenRequestException(uk.gov.di.ipv.cri.address.library.exception.AccessTokenRequestException) ErrorObject(com.nimbusds.oauth2.sdk.ErrorObject) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Example 3 with AccessTokenRequestException

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());
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) AccessTokenRequestException(uk.gov.di.ipv.cri.address.library.exception.AccessTokenRequestException) ErrorObject(com.nimbusds.oauth2.sdk.ErrorObject) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Test(org.junit.jupiter.api.Test)

Example 4 with AccessTokenRequestException

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);
}
Also used : HTTPRequest(com.nimbusds.oauth2.sdk.http.HTTPRequest) AccessTokenRequestException(uk.gov.di.ipv.cri.address.library.exception.AccessTokenRequestException) URI(java.net.URI)

Aggregations

AccessTokenRequestException (uk.gov.di.ipv.cri.address.library.exception.AccessTokenRequestException)4 APIGatewayProxyRequestEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent)3 APIGatewayProxyResponseEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)3 ErrorObject (com.nimbusds.oauth2.sdk.ErrorObject)3 Test (org.junit.jupiter.api.Test)3 HTTPRequest (com.nimbusds.oauth2.sdk.http.HTTPRequest)1 URI (java.net.URI)1