Created
March 19, 2019 21:51
-
-
Save kouks/4ba9d88282924a079cd127ddf98505d0 to your computer and use it in GitHub Desktop.
AWS Product API signature script
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
import { Request } from 'awi' | |
import { HmacSHA256, enc } from 'crypto-js' | |
export class SignatureFactory { | |
/** | |
* Compute the Amazon request signature. | |
* | |
* @param request The request sign | |
*/ | |
public static getSignature (request: Request) : string { | |
// Sort the query and join it to a string. | |
const query: string = Object.keys(request.query) | |
.sort() | |
.map(key => `${key}=${encodeURIComponent(request.query[key])}`) | |
.join('&') | |
// Create the string to sign. | |
const sts: string = `${request.method}\n${process.env.AMAZON_PRODUCTS_ENDPOINT}\n${request.path}\n${query}` | |
// Compute the Hmac hash with the secret key. | |
const signature: string = enc.Base64.stringify( | |
HmacSHA256(sts, process.env.AMAZON_PRODUCTS_SECRET_KEY) | |
) | |
return signature | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment