byte[] getSignatureKey(String key, String dateStamp, String regionName, String serviceName) throws Exception {
byte[] kSecret = ("AWS4" + key).getBytes("UTF8");
byte[] kDate = HmacSHA256(dateStamp, kSecret);
byte[] kRegion = HmacSHA256(regionName, kDate);
byte[] kService = HmacSHA256(serviceName, kRegion);
byte[] kSigning = HmacSHA256("aws4_request", kService);
return kSigning;
}
String method = "PUT";
String host = "<yourbuket>.s3.us-east-2.amazonaws.com";
String region = "us-east-2";
String service = "s3";
String endpoint = "s3.us-east-2.amazonaws.com";
String canonical_uri = "/<yourFolder>/"+map.get('NomeArquivo');
String canonical_headers = "host:" + host + "\n"+ "x-amz-content-sha256:" + hashBody + "\n" + "x-amz-date:" + amzDate + "\n";
String method = "POST";
String region = "us-west-2";
String service = "execute-api";
String host = "us-east-2.amazonaws.com";
String canonical_uri = "https://sellingpartnerapi-fe.amazon.com/tokens/2021-03-01/restrictedDataToken";
String canonical_headers = "accept: application/json"+";\n"+"content-length:"+ body.length() + ";\n"+"content-type:application/json"+";\n"+"host:" + host + ";\n"+ "x-amz-content-sha256:" + hashBody + ";\n" + "x-amz-date:" + amzDate + ";\n";
/*****************************************************************
*
* Developer: Viana - SAP Senior Integration Consultant
*
******************************************************************/
/************
* *
* Libraries *
* *
*************/
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*;
import com.sap.gateway.ip.core.customdev.util.Message;
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat
import java.lang.Object
import java.util.List;
import java.util.TimeZone;
import org.apache.commons.codec.digest.DigestUtils;
/*******************************************************************************************************************************************************************************************
* *
* STEP - 1 : Description of function getTokens: Groovy to extract the values from the attributes "access_token" and "refresh_token" generate properties that will be use in the next calls *
* *
********************************************************************************************************************************************************************************************/
def Message getTokens(Message message) {
Reader reader = message.getBody(Reader)
def json = new JsonSlurper().parse(reader)
message.setProperty("TokenAWS", json.access_token)
message.setProperty("RefreshTokenAWS",json.refresh_token)
return message
}
/***************************************************************************************************************************************************************************************************************
* *
* STEP - 2 - Description of functiongetRestrictedDataToken: Groovy to extract the value from the attribute restrictedDataToken and create the property "restrictedDataToken" that will be use in the next call *
* *
****************************************************************************************************************************************************************************************************************/
def Message getRestrictedDataToken (Message message){
Reader reader = message.getBody(Reader)
def json = new JsonSlurper().parse(reader)
message.setHeader("x-amz-access-token", json.restrictedDataToken)
return message
}
/************************************************************************************************
* *
* STEP - 3 - Description of function signatureAWS : Groovy to generate the AWS signature header *
* *
*************************************************************************************************/
def Message signatureAWS(Message message) {
def body = message.getBody(java.lang.String) as String
//************* Hash do Body using apache commons DigestUtils sha256Hex *************
def hashBody = DigestUtils.sha256Hex(body)
//************* Mapping the properties - The filename was set in previous groovyScript *************
def map = message.getProperties()
//************* Iniciating variables *************
String method = "POST";
String region = "us-west-2";
String service = "execute-api";
String host = "us-east-2.amazonaws.com";
// Read AWS access key from security artifacts. Best practice is NOT to embed credentials in code.
def access_key = <your details as property from content modifier>
def secret_key = <your details as property from content modifier>
// Create a date for headers and the credential string
def now = new Date()
def amzFormat = new SimpleDateFormat( "yyyyMMdd'T'HHmmss'Z'" )
def formattedDate = new SimpleDateFormat("EEEE, MMMM dd, yyyy, hh:mm a '('zzz')'")
def stampFormat = new SimpleDateFormat( "yyyyMMdd" )
def amzDate = amzFormat.format(now)
def date_stamp = stampFormat.format(now)
//************* Canonical Request variables *************
String canonical_uri = "https://sellingpartnerapi-fe.amazon.com/tokens/2021-03-01/restrictedDataToken";
String canonical_querystring = "";
String canonical_headers = "accept: application/json"+";\n"+"content-length:"+ body.length() + ";\n"+"content-type:application/json"+";\n"+"host:" + host + ";\n"+ "x-amz-content-sha256:" + hashBody + ";\n" + "x-amz-date:" + amzDate + ";\n";
message.setProperty("Canonical_Headers",canonical_headers)
String signed_headers = "accept;content-length;content-type;host;x-amz-content-sha256;x-amz-date";
String canonical_request = method + "\n" + canonical_uri + "\n" + canonical_querystring + "\n" + canonical_headers + "\n" + signed_headers + "\n" + hashBody;
//************* Sing to Sing variables *************
String algorithm = "AWS4-HMAC-SHA256";
String credential_scope = date_stamp + "/" + region + "/" + service + "/" + "aws4_request";
String string_to_sign = algorithm + "\n" + amzDate + "\n" + credential_scope + "\n" + DigestUtils.sha256Hex(canonical_request);
//************* Generating the Singning Key *************
byte[] signing_key = getSignatureKey(secret_key, date_stamp, region, service);
//************* Generating the HmacSHA256 - Amazon *************
byte[] signature = HmacSHA256(string_to_sign,signing_key);
//************* Generating the Hex of the Signature *************
String strHexSignature = bytesToHex(signature);
//************* Generating the authorization header signed - Amazon V4 S3 Bucket *************
String authorization_header = algorithm + " " + "Credential=" + access_key + "/" + credential_scope + ", " + "SignedHeaders=" + signed_headers + ", " + "Signature=" + strHexSignature;
//************* Seting the headers of HTTP call *************
message.setHeader("x-amz-date",amzDate);
message.setHeader("x-amz-content-sha256", hashBody)
message.setHeader("Authorization", authorization_header);
message.setHeader("Host", "us-east-2.amazonaws.com");
message.setHeader("Content-type", "application/json");
message.setHeader("x-amz-access-token", message.getProperty("TokenAWS"))
message.setHeader("Accept","application/json")
//************* Setting the body to be store in Amazon *************
message.setBody(body)
return message
}
/**********************************************
* *
* Function to convert bytes to Hexadecimal *
* *
***********************************************/
String bytesToHex(byte[] bytes) {
char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars).toLowerCase();
}
/********************************************
* *
* Function to HmacSHA256 *
* *
*********************************************/
byte[] HmacSHA256(String data, byte[] key) throws Exception {
String algorithm="HmacSHA256";
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(key, algorithm));
return mac.doFinal(data.getBytes("UTF8"));
}
/**********************************************
* *
* Function to Get signature *
* *
***********************************************/
byte[] getSignatureKey(String key, String dateStamp, String regionName, String serviceName) throws Exception {
byte[] kSecret = ("AWS4" + key).getBytes("UTF8");
byte[] kDate = HmacSHA256(dateStamp, kSecret);
byte[] kRegion = HmacSHA256(regionName, kDate);
byte[] kService = HmacSHA256(serviceName, kRegion);
byte[] kSigning = HmacSHA256("aws4_request", kService);
return kSigning;
}
/*****************************************************************************************************************
* *
* STEP - 4 - Extract the value from the attribute "content" in base64 and return the string as a final response *
* *
******************************************************************************************************************/
def Message getContentB64 (Message message){
Reader reader = message.getBody(Reader)
def json = new JsonSlurper().parse(reader)
String base64AwsResponse = json.labelData.content
message.setBody(base64AwsResponse.substring( 1, base64AwsResponse.length() - 1 ) )
return message
}
def generateHex(String data) {
MessageDigest mac = MessageDigest.getInstance("SHA-256");
byte[] signatureBytes = mac.digest(data.getBytes(StandardCharsets.UTF_8));
StringBuffer hexString = new StringBuffer();
for (int j=0; j<signatureBytes.length; j++) {
String hex=Integer.toHexString(0xff & signatureBytes[j]);
if(hex.length()==1) hexString.append('0');
hexString.append(hex);
}
String encryptedSignature = hexString.toString();
String encryptHash = encryptedSignature.replace("-","");
encryptHash = encryptHash.toLowerCase();
return encryptHash;
}
{
"restrictedResources": [
{
"method": "POST",
"path": "/vendor/directFulfillment/shipping/2021-12-28/shippingLabels/{purchaseOrderNumber}"
}
]
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
3 | |
3 | |
3 | |
3 | |
3 | |
3 | |
3 | |
3 | |
3 |