Skip to content
Advertisement

AWS Appsync Graphql Java Client – IAM authorisation

This is the schema for which I have implement business logic

type Query {
     getLicenseInformation(localmd5: String): License @aws_cognito_user_pools
   getUserInformation(username: String!): CognitoUser @aws_iam
    listUsers(searchString: String): [NamedResource] @aws_iam
}

I use RestTemplate as my Java client to consume graphql endpoint giving API key as authorization. I ad dthe api key in the header paart as x-api-key.

        RestTemplate restTemplate=new RestTemplate();
        HttpHeaders requestHeaders = new HttpHeaders();

        requestHeaders.set("x-api-key",api_key.getId());
        requestHeaders.set("Content-Type","application/graphql");

        HttpEntity entity = new HttpEntity(requestHeaders);
        ResponseEntity<String> exchange = restTemplate.exchange(URL, HttpMethod.POST, new HttpEntity(query,requestHeaders),String.class);

The above implementation retrieves the values from the backend. But according the schema which is defined by the other team, the authorization mode is not API key rather iam. So I have to configure the rest template accordingly.

Where in the Client side code in Java I can configure so that aws_iam is used as authorization method to retrieve the information from the endpoint. Dynamodb is the datasource

Advertisement

Answer

Building the request object like below helps:

private DefaultRequest prepareRequest(HttpMethodName method, InputStream content) {
        Map<String,String> headers = new HashMap<>();
        headers.put("Content-type", "application/json");
        headers.put("type", "AUTH_TYPE.AWS_IAM");
        headers.put("X-Amz-Security-Token",securityToken);
        DefaultRequest request = new DefaultRequest(API_GATEWAY_SERVICE_NAME);
        request.setHttpMethod(method);
        request.setContent(content);
        request.setEndpoint(this.endpoint);
        request.setHeaders(headers);

        return request;
    }
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement