private JSONObject getAuthorizationDetailsForIAM()

in aws-android-sdk-appsync/src/main/java/com/amazonaws/mobileconnectors/appsync/SubscriptionAuthorizer.java [109:164]


    private JSONObject getAuthorizationDetailsForIAM(boolean connectionFlag,
                                                     Subscription subscription) throws JSONException {

        DefaultRequest canonicalRequest = new DefaultRequest("appsync");

        URI apiUrl;
        try {
            final String baseUrl = mServerUrl;
            final String connectionUrl = connectionFlag ? baseUrl + "/connect" : baseUrl;
            apiUrl = new URI(connectionUrl);
        } catch (URISyntaxException e) {
            throw new RuntimeException("Error constructing canonical URI for IAM request signature", e);
        }
        canonicalRequest.setEndpoint(apiUrl);

        canonicalRequest.addHeader("accept", "application/json, text/javascript");
        canonicalRequest.addHeader("content-encoding", "amz-1.0");
        canonicalRequest.addHeader("content-type", "application/json; charset=UTF-8");
        canonicalRequest.setHttpMethod(HttpMethodName.valueOf("POST"));

        if (connectionFlag) {
            canonicalRequest.setContent(new ByteArrayInputStream("{}".getBytes()));
        } else {
            canonicalRequest.setContent(new ByteArrayInputStream(getDataJson(subscription).getBytes()));
        }

        String apiRegion = apiUrl.getAuthority().split("\\.")[2];

        DomainType domainType = DomainType.from(mServerUrl);
        if (DomainType.CUSTOM == domainType) {
            apiRegion = getApiRegion();
        }

        if (connectionFlag){
            new AppSyncV4Signer(apiRegion, AppSyncV4Signer.ResourcePath.IAM_CONNECTION_RESOURCE_PATH)
                .sign(canonicalRequest, getCredentialsProvider().getCredentials());
        } else {
            new AppSyncV4Signer(apiRegion)
                .sign(canonicalRequest, getCredentialsProvider().getCredentials());
        }

        JSONObject authorizationMessage = new JSONObject();
        Map<String, String> signedHeaders = canonicalRequest.getHeaders();
        try {
            for(Map.Entry headerEntry : signedHeaders.entrySet()) {
                if (!headerEntry.getKey().equals("host")) {
                    authorizationMessage.put((String) headerEntry.getKey(), headerEntry.getValue());
                } else {
                    authorizationMessage.put("host", getHost(mServerUrl));
                }
            }
        } catch (JSONException | MalformedURLException e) {
            throw new RuntimeException("Error constructing authorization message json", e);
        }
        return authorizationMessage;
    }