Search in sources :

Example 6 with APIGatewayProxyResponseEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent in project di-ipv-cri-uk-passport-back by alphagov.

the class AccessTokenHandlerTest method shouldReturnAccessTokenOnSuccessfulExchange.

@Test
void shouldReturnAccessTokenOnSuccessfulExchange() throws Exception {
    APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
    String tokenRequestBody = "code=12345&redirect_uri=http://example.com&grant_type=authorization_code&client_id=test_client_id";
    event.setBody(tokenRequestBody);
    AccessToken accessToken = new BearerAccessToken();
    TokenResponse tokenResponse = new AccessTokenResponse(new Tokens(accessToken, null));
    when(mockAccessTokenService.generateAccessToken(any())).thenReturn(tokenResponse);
    when(mockAuthorizationCodeService.getAuthCodeItem("12345")).thenReturn(TEST_AUTH_CODE_ITEM);
    when(mockAccessTokenService.validateTokenRequest(any())).thenReturn(ValidationResult.createValidResult());
    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(200, response.getStatusCode());
    assertEquals(tokenResponse.toSuccessResponse().getTokens().getAccessToken().getValue(), responseBody.get("access_token").toString());
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) TokenResponse(com.nimbusds.oauth2.sdk.TokenResponse) AccessTokenResponse(com.nimbusds.oauth2.sdk.AccessTokenResponse) AccessToken(com.nimbusds.oauth2.sdk.token.AccessToken) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) ErrorObject(com.nimbusds.oauth2.sdk.ErrorObject) BearerAccessToken(com.nimbusds.oauth2.sdk.token.BearerAccessToken) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) AccessTokenResponse(com.nimbusds.oauth2.sdk.AccessTokenResponse) Tokens(com.nimbusds.oauth2.sdk.token.Tokens) Test(org.junit.jupiter.api.Test)

Example 7 with APIGatewayProxyResponseEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent in project serverless-java-frameworks-samples by aws-samples.

the class CreateProductFunction method apply.

@Override
public APIGatewayProxyResponseEvent apply(APIGatewayProxyRequestEvent requestEvent) {
    if (!requestEvent.getHttpMethod().equals(HttpMethod.PUT.name())) {
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.METHOD_NOT_ALLOWED).withBody("Only PUT method is supported");
    }
    try {
        String id = requestEvent.getPathParameters().get("id");
        String jsonPayload = requestEvent.getBody();
        Product product = objectMapper.readValue(jsonPayload, Product.class);
        if (!product.getId().equals(id)) {
            return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.BAD_REQUEST).withBody("Product ID in the body does not match path parameter");
        }
        productDao.putProduct(product);
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.CREATED).withBody("Product with id = " + id + " created");
    } catch (Exception e) {
        e.printStackTrace();
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR).withBody("Internal Server Error :: " + e.getMessage());
    }
}
Also used : Product(software.amazonaws.example.product.product.entity.Product) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)

Example 8 with APIGatewayProxyResponseEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent in project serverless-java-frameworks-samples by aws-samples.

the class CreateProductHandler method handleRequest.

public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent requestEvent, Context context) {
    if (!requestEvent.getHttpMethod().equals(SdkHttpMethod.PUT.name())) {
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.METHOD_NOT_ALLOWED).withBody("Only PUT method is supported");
    }
    try {
        String id = requestEvent.getPathParameters().get("id");
        String jsonPayload = requestEvent.getBody();
        Product product = objectMapper.readValue(jsonPayload, Product.class);
        if (!product.getId().equals(id)) {
            return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.BAD_REQUEST).withBody("Product ID in the body does not match path parameter");
        }
        productDao.putProduct(product);
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.CREATED).withBody("Product with id = " + id + " created");
    } catch (Exception e) {
        e.printStackTrace();
        return new APIGatewayProxyResponseEvent().withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR).withBody("Internal Server Error");
    }
}
Also used : Product(software.amazonaws.example.product.product.entity.Product) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)

Example 9 with APIGatewayProxyResponseEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent 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 10 with APIGatewayProxyResponseEvent

use of com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent 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)

Aggregations

APIGatewayProxyResponseEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)260 Test (org.junit.jupiter.api.Test)214 APIGatewayProxyRequestEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent)182 HashMap (java.util.HashMap)56 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)43 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)30 ErrorObject (com.nimbusds.oauth2.sdk.ErrorObject)22 URI (java.net.URI)21 NotifyRequest (uk.gov.di.authentication.shared.entity.NotifyRequest)17 UserProfile (uk.gov.di.authentication.shared.entity.UserProfile)17 Map (java.util.Map)16 ClientRegistry (uk.gov.di.authentication.shared.entity.ClientRegistry)14 ClientSession (uk.gov.di.authentication.shared.entity.ClientSession)14 Context (com.amazonaws.services.lambda.runtime.Context)13 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)13 AuthenticationRequest (com.nimbusds.openid.connect.sdk.AuthenticationRequest)13 NotifyRequest (uk.gov.di.accountmanagement.entity.NotifyRequest)13 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 Subject (com.nimbusds.oauth2.sdk.id.Subject)12 URIBuilder (org.apache.http.client.utils.URIBuilder)11