Forked from hicglobalsolutions/Amazon API authentication code
Created
January 21, 2020 18:40
-
-
Save Devarshi87/7c725b1ceeadd22e6599625154b69c44 to your computer and use it in GitHub Desktop.
Amazon API authentication code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*-************************************************************************************************************ | |
| * Name : AmazonAuthenticationUtility | |
| * @Author : Aditya Kumar | |
| * @Date : 02 Dec 2019 | |
| * @Description : The AmazonAuthenticationUtility controller is used for converting body data. | |
| * UPDATES | |
| * Version Developer Date Description | |
| *------------------------------------------------------------------------------------------- | |
| * 1.0 Aditya Kumar 02 Dec 2019 Initial Creation | |
| ****************************************************************************************************************/ | |
| public with sharing class AmazonAuthenticationUtility { | |
| public static String access_key; | |
| public static String secret_Key; | |
| public static String endpoint ; | |
| public static String host; | |
| public static String service; | |
| public static String method; | |
| public static String region; | |
| public static string amz_target; | |
| public AmazonAuthenticationUtility(){ | |
| } | |
| /* | |
| * Method Name : generateAuthorizationRequestForBody | |
| * @Description : This method is used detecting language. | |
| */ | |
| public static string generateAuthorizationRequestForBody( string requestBodyData, string sCode, string tCode){ | |
| access_key = 'xxxxxxxxxxxxxx’; | |
| secret_Key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | |
| method = 'POST'; | |
| region = 'us-east-1'; | |
| amz_target = 'AWSShineFrontendService_20170701.TranslateText'; | |
| service = 'translate'; | |
| host = service + '.' + region + '.amazonaws.com'; | |
| endpoint = 'https://' + host + '/'; | |
| system.debug('requestBodyData'+requestBodyData); | |
| string requestBody = '{'; | |
| requestBody += '"Text": "'+requestBodyData.replace('\r\n', ' ').replace('\n', ' ').replace('\r', ' ')+'",'; | |
| requestBody += '"SourceLanguageCode": "'+sCode+'",'; | |
| requestBody += '"TargetLanguageCode": "'+tCode+'"'; | |
| requestBody += '}'; | |
| String awsFormattedNow = awsFormattedDateTime(Datetime.now()); | |
| String date_stamp = awsFormattedDate(Datetime.now()); | |
| String canonical_headers = 'host:' + host + '\n' + 'x-amz-date:' + awsFormattedNow + '\n'+ 'x-amz-target:' + amz_target + '\n'; | |
| //# This lists the headers in the canonical_headers list, delimited with ";" and in alpha order. | |
| //# Note: The request can include any headers; canonical_headers and | |
| //# signed_headers include those that you want to be included in the | |
| //# hash of the request. "Host" and "x-amz-date" are always required. | |
| String signed_headers = 'host;x-amz-date;x-amz-target'; | |
| String canonical_request = generateCanonical_request( canonical_headers, signed_headers, requestBody); | |
| String credential_scope = date_stamp + '/' + region + '/' + service + '/' + 'aws4_request'; | |
| String string_to_sign = getStringToSign(awsFormattedNow, credential_scope, canonical_request ); | |
| String signatureKey = signature(date_stamp, secret_Key, string_to_sign ); | |
| HttpRequest httpReq = new HttpRequest(); | |
| httpReq.setMethod('POST'); | |
| httpReq.setEndpoint(endpoint); | |
| httpReq.setHeader('Host',host); | |
| httpReq.setHeader('Content-Type','application/x-amz-json-1.1'); | |
| httpReq.setHeader('X-Amz-Date',awsFormattedNow); | |
| httpReq.setHeader('Authorization', headerForAmazonAuthorization(access_key, signatureKey, credential_scope, signed_headers )); | |
| httpReq.setHeader('Content-Length', String.valueOf(requestBody.length())); | |
| httpReq.setHeader('X-Amz-Target', amz_target); | |
| httpReq.setBody(requestBody); | |
| Http http = new Http(); | |
| HttpResponse response = http.send(httpReq); | |
| System.debug('****response***'+response); | |
| System.debug('****body***'+response.getbody()); | |
| return response.getbody(); | |
| } | |
| /* | |
| * Method Name : awsFormattedDateTime | |
| * @Description : This method is used for Date Time formatting. | |
| */ | |
| private static string awsFormattedDateTime(Datetime now){ | |
| return now.formatGmt('YYYYMMdd\'T\'HHmmss\'Z\''); | |
| } | |
| /* | |
| * Method Name : awsFormattedDate | |
| * @Description : This method is used for Date formatting. | |
| */ | |
| private static string awsFormattedDate(Datetime now){ | |
| return now.formatGmt('YYYYMMdd'); | |
| } | |
| /* | |
| * Method Name : headerForAmazonAuthorization | |
| * @Description : This method is used as header for amazon authorization. | |
| */ | |
| private static string headerForAmazonAuthorization(String accessKey, String signature, String credential_scope, String signed_headers ){ | |
| return 'AWS4-HMAC-SHA256' + ' ' + 'Credential=' + accessKey + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature; | |
| } | |
| /* | |
| * Method Name : signature | |
| * @Description : This method is used to generate signature. | |
| */ | |
| private static string signature(String datestamp, String secret_key, String stringToSign ) { | |
| String hashAlgo = 'HmacSHA256'; | |
| String kSecret = 'AWS4' + secret_key; | |
| Blob kDate = Crypto.generateMac(hashAlgo, Blob.valueOf(datestamp), Blob.valueOf(kSecret)); | |
| Blob kRegion = Crypto.generateMac(hashAlgo, Blob.valueOf(region), kDate); | |
| Blob kService = Crypto.generateMac(hashAlgo, Blob.valueOf(service), kRegion); | |
| Blob kSigning = Crypto.generateMac(hashAlgo, Blob.valueOf('aws4_request'), kService); | |
| String strSignature = EncodingUtil.convertToHex(Crypto.generateMac(hashAlgo, Blob.valueOf(stringToSign), kSigning)); | |
| return strSignature.toLowerCase(); | |
| } | |
| /* | |
| * Method Name : generateCanonical_request | |
| * @Description : This method is used to generate canonical request. | |
| */ | |
| private static String generateCanonical_request(String canonical_headers, String signed_headers, String request_parameters ){ | |
| //Create payload hash. In this example, the payload (body of the request) contains the request parameters. | |
| String payload_hash = EncodingUtil.convertToHex(Crypto.generateDigest('SHA256', Blob.valueOf(request_parameters))).toLowerCase(); | |
| //Create canonical URI--the part of the URI from domain to query string (use '/' if no path) | |
| String canonical_uri = '/'; | |
| //# request parameters are passed in the body of the request and the query string is blank. | |
| String canonical_querystring = '' ; | |
| return method + '\n' + canonical_uri + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash; | |
| } | |
| /* | |
| * Method Name : getStringToSign | |
| * @Description : This method is used to get string to sign. | |
| */ | |
| private static String getStringToSign(String amz_dateTime, String credential_scope, String canonical_request ){ | |
| String signToString = 'AWS4-HMAC-SHA256' + '\n' ; | |
| signToString += amz_dateTime + '\n' ; | |
| signToString += credential_scope + '\n'; | |
| signToString += EncodingUtil.convertToHex(Crypto.generateDigest('SHA-256', Blob.valueOf(canonical_request))); | |
| return signToString; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment