Search in sources :

Example 1 with TokenVendor

use of tenant.vendinglayer.TokenVendor in project saas-tenant-isolation-architecture by aws-samples.

the class ApiGatewayHandler method handlePostRequest.

public APIGatewayProxyResponseEvent handlePostRequest(final APIGatewayProxyRequestEvent input, final Context context) {
    // we vending the token by extracting the tenant ID from the JWT token contained in
    // the request headers
    TokenVendor tokenVendor = new TokenVendor();
    final AwsCredentialsProvider awsCredentialsProvider = tokenVendor.vendTokenJwt(input.getHeaders());
    // we parse the body of the POST request, currently we only accept a 'data' parameter to
    // be written to DynamoDB, anything else will be ignored
    Map<String, String> body;
    try {
        TypeReference<Map<String, String>> typeRef = new TypeReference<Map<String, String>>() {
        };
        body = mapper.readValue(input.getBody(), typeRef);
    } catch (JsonProcessingException e) {
        logger.error("Error parsing JSON body.", e);
        throw new RuntimeException(createBadRequestResponse(context.getAwsRequestId(), "Error parsing JSON body."));
    }
    String tenant = tokenVendor.getTenant();
    logger.info("TENANT ID: " + tenant);
    // TenantProduct class encapsulates writing to DynamoDB using the enhanced DynamoDB
    // client, which allows us to use POJOs
    TenantProduct tenantProduct = new TenantProduct(awsCredentialsProvider, tenant, body.get("data"));
    tenantProduct.save();
    Map<String, String> headers = new HashMap<>();
    headers.put("Content-Type", "application/json");
    return new APIGatewayProxyResponseEvent().withHeaders(headers).withStatusCode(201);
}
Also used : TokenVendor(tenant.vendinglayer.TokenVendor) TenantProduct(tenant.export.models.TenantProduct) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) TypeReference(com.fasterxml.jackson.core.type.TypeReference) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with TokenVendor

use of tenant.vendinglayer.TokenVendor in project saas-tenant-isolation-architecture by aws-samples.

the class ApiGatewayHandler method handleGetRequest.

public APIGatewayProxyResponseEvent handleGetRequest(final APIGatewayProxyRequestEvent input, final Context context) {
    // we vending the token by extracting the tenant ID from the JWT token contained in
    // the request headers
    TokenVendor tokenVendor = new TokenVendor();
    final AwsCredentialsProvider awsCredentialsProvider = tokenVendor.vendTokenJwt(input.getHeaders());
    String tenant = tokenVendor.getTenant();
    logger.info("TENANT ID: " + tenant);
    // TenantProduct class encapsulates writing to DynamoDB using the enhanced DynamoDB
    // client, which allows us to use POJOs
    TenantProduct tenantProduct = new TenantProduct(awsCredentialsProvider, tenant);
    tenantProduct = tenantProduct.load(tenantProduct);
    String body;
    try {
        body = mapper.writeValueAsString(tenantProduct);
    } catch (JsonProcessingException e) {
        logger.error("Error parsing JSON body.", e);
        throw new RuntimeException(createBadRequestResponse(context.getAwsRequestId(), "Error parsing JSON body."));
    }
    Map<String, String> headers = new HashMap<>();
    headers.put("Content-Type", "application/json");
    return new APIGatewayProxyResponseEvent().withHeaders(headers).withBody(body).withStatusCode(200);
}
Also used : TokenVendor(tenant.vendinglayer.TokenVendor) TenantProduct(tenant.export.models.TenantProduct) AwsCredentialsProvider(software.amazon.awssdk.auth.credentials.AwsCredentialsProvider) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

APIGatewayProxyResponseEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 AwsCredentialsProvider (software.amazon.awssdk.auth.credentials.AwsCredentialsProvider)2 TenantProduct (tenant.export.models.TenantProduct)2 TokenVendor (tenant.vendinglayer.TokenVendor)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1