Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implements AWS signature version 4 for signing requests #1047

Merged
merged 15 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions browser-test/test.crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ describe('Browser crypto tests', () => {
});

it('should expose a method to convert an ArrayBuffer to hex', () => {
const arrayBuffer = stringToArrayBuffer('Hello World!');
const expectedHexEncoding = '48656c6c6f20576f726c6421';
const arrayBuffer = new Uint8Array([4, 8, 0, 12, 16, 0])
.buffer as ArrayBuffer;
const expectedHexEncoding = '0408000c1000';

const calculatedHexEncoding = fromArrayBufferToHex(arrayBuffer);
assert.strictEqual(calculatedHexEncoding, expectedHexEncoding);
Expand Down
12 changes: 8 additions & 4 deletions src/auth/awsrequestsigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type HttpMethod =

/** Interface defining the AWS authorization header map for signed requests. */
interface AwsAuthHeaderMap {
amzDate: string;
amzDate?: string;
authorizationHeader: string;
canonicalQuerystring: string;
}
Expand Down Expand Up @@ -115,8 +115,9 @@ export class AwsRequestSigner {
);
// Append additional optional headers, eg. X-Amz-Target, Content-Type, etc.
const headers: {[key: string]: string} = Object.assign(
// Add x-amz-date if available.
headerMap.amzDate ? {'x-amz-date': headerMap.amzDate} : {},
{
'x-amz-date': headerMap.amzDate,
Authorization: headerMap.authorizationHeader,
host: uri.host,
},
Expand Down Expand Up @@ -230,8 +231,10 @@ async function generateAuthenticationHeaderMap(
const amzHeaders = Object.assign(
{
host,
'x-amz-date': amzDate,
},
// Previously the date was not fixed with x-amz- and could be provided manually.
// https://github.com/boto/botocore/blob/879f8440a4e9ace5d3cf145ce8b3d5e5ffb892ef/tests/unit/auth/aws4_testsuite/get-header-value-trim.req
reformattedAdditionalAmzHeaders.date ? {} : {'x-amz-date': amzDate},
reformattedAdditionalAmzHeaders
);
let canonicalHeaders = '';
Expand Down Expand Up @@ -273,7 +276,8 @@ async function generateAuthenticationHeaderMap(
`Signature=${fromArrayBufferToHex(signature)}`;

return {
amzDate,
// Do not return x-amz-date if date is available.
amzDate: reformattedAdditionalAmzHeaders.date ? undefined : amzDate,
authorizationHeader,
canonicalQuerystring,
};
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function fromArrayBufferToHex(arrayBuffer: ArrayBuffer): string {
// Convert bytes to hex string.
return byteArray
.map(byte => {
return byte === 0 ? '' : byte.toString(16).padStart(2, '0');
return byte.toString(16).padStart(2, '0');
})
.join('');
}