Last active
January 22, 2024 03:59
-
-
Save loginov-rocks/a572bf4013d77d96fa7dc885a8450734 to your computer and use it in GitHub Desktop.
WebSocket API Gateway IAM Signer
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 { defaultProvider } from '@aws-sdk/credential-provider-node'; | |
import { Hash } from '@aws-sdk/hash-node'; | |
import { HttpRequest } from '@aws-sdk/protocol-http'; | |
import { SignatureV4 } from '@aws-sdk/signature-v4'; | |
import { URL } from 'url'; | |
export const sign = async (region, url, query = {}, headers = {}) => { | |
const urlObject = new URL(url); | |
// Add query parameters passed as argument, if any. | |
if (Object.keys(query).length > 0) { | |
Object.keys(query).forEach((queryKey) => { | |
urlObject.searchParams.set(queryKey, query[queryKey]); | |
}); | |
} | |
const httpRequest = new HttpRequest({ | |
headers: { | |
...headers, | |
host: urlObject.hostname, | |
}, | |
hostname: urlObject.hostname, | |
path: urlObject.pathname, | |
protocol: urlObject.protocol, | |
query: Object.fromEntries(urlObject.searchParams), | |
}); | |
const signatureV4 = new SignatureV4({ | |
// Modify `credentials` to use a custom identity. | |
credentials: defaultProvider(), | |
region, | |
service: 'execute-api', | |
sha256: Hash.bind(null, 'sha256'), | |
}); | |
const signedHttpRequest = await signatureV4.sign(httpRequest); | |
return { | |
headers: signedHttpRequest.headers, | |
url: urlObject.toString(), | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment