|
| 1 | +import { expect } from 'chai'; |
| 2 | + |
| 3 | +import { aws4Sign, type Options } from '../../src/aws4'; |
| 4 | + |
| 5 | +describe('Verify AWS4 signature generation', () => { |
| 6 | + const date = new Date('2025-12-15T12:34:56Z'); |
| 7 | + const awsCredentials = { |
| 8 | + accessKeyId: 'AKIDEXAMPLE', |
| 9 | + secretAccessKey: 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY' |
| 10 | + }; |
| 11 | + const awsSessionCredentials = { |
| 12 | + accessKeyId: 'AKIDEXAMPLE', |
| 13 | + secretAccessKey: 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYexamplekey', |
| 14 | + sessionToken: 'AQoDYXdzEJ' |
| 15 | + }; |
| 16 | + const host = 'sts.amazonaws.com'; |
| 17 | + const body = 'Action=GetCallerIdentity&Version=2011-06-15'; |
| 18 | + const request: Options = { |
| 19 | + method: 'POST', |
| 20 | + host, |
| 21 | + path: '/', |
| 22 | + region: 'us-east-1', |
| 23 | + service: 'sts', |
| 24 | + headers: { |
| 25 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 26 | + 'Content-Length': body.length, |
| 27 | + 'X-MongoDB-Server-Nonce': 'fakenonce', |
| 28 | + 'X-MongoDB-GS2-CB-Flag': 'n' |
| 29 | + }, |
| 30 | + body, |
| 31 | + date |
| 32 | + }; |
| 33 | + |
| 34 | + it('should generate correct credentials for missing credentials', () => { |
| 35 | + const signed = aws4Sign(request, undefined); |
| 36 | + |
| 37 | + expect(signed.headers['X-Amz-Date']).to.exist; |
| 38 | + expect(signed.headers['X-Amz-Date']).to.equal('20251215T123456Z'); |
| 39 | + expect(signed.headers['Authorization']).to.exist; |
| 40 | + expect(signed.headers['Authorization']).to.equal( |
| 41 | + 'AWS4-HMAC-SHA256 Credential=undefined/20251215/us-east-1/sts/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date;x-mongodb-gs2-cb-flag;x-mongodb-server-nonce, Signature=8854aaaeec4bf1f820435b60e216b610e92fa53cbfca71b269f2c334e02c1c45' |
| 42 | + ); |
| 43 | + |
| 44 | + // Uncomment the following lines if you want to compare with the old aws4 library. |
| 45 | + // Remember to import aws4 at the top of the file, like this: import * as aws4sign from 'aws4'; |
| 46 | + |
| 47 | + // const oldSigned = aws4sign.sign(request, undefined); |
| 48 | + // expect(oldSigned.headers['X-Amz-Date']).to.exist; |
| 49 | + // expect(oldSigned.headers['X-Amz-Date']).to.equal(signed.headers['X-Amz-Date']); |
| 50 | + // expect(oldSigned.headers['Authorization']).to.exist; |
| 51 | + // expect(oldSigned.headers['Authorization']).to.equal(signed.headers['Authorization']); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should generate correct credentials for permanent credentials', () => { |
| 55 | + const signed = aws4Sign(request, awsCredentials); |
| 56 | + |
| 57 | + expect(signed.headers['X-Amz-Date']).to.exist; |
| 58 | + expect(signed.headers['X-Amz-Date']).to.equal('20251215T123456Z'); |
| 59 | + expect(signed.headers['Authorization']).to.exist; |
| 60 | + expect(signed.headers['Authorization']).to.equal( |
| 61 | + 'AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20251215/us-east-1/sts/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date;x-mongodb-gs2-cb-flag;x-mongodb-server-nonce, Signature=48a66f9fc76829002a7a7ac5b92e4089395d9b88ea7d417ab146949b90eeab08' |
| 62 | + ); |
| 63 | + |
| 64 | + // Uncomment the following lines if you want to compare with the old aws4 library. |
| 65 | + // Remember to import aws4 at the top of the file, like this: import * as aws4sign from 'aws4'; |
| 66 | + |
| 67 | + // const oldSigned = aws4sign.sign(request, awsCredentials); |
| 68 | + // expect(oldSigned.headers['X-Amz-Date']).to.exist; |
| 69 | + // expect(oldSigned.headers['X-Amz-Date']).to.equal(signed.headers['X-Amz-Date']); |
| 70 | + // expect(oldSigned.headers['Authorization']).to.exist; |
| 71 | + // expect(oldSigned.headers['Authorization']).to.equal(signed.headers['Authorization']); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should generate correct credentials for session credentials', () => { |
| 75 | + const signed = aws4Sign(request, awsSessionCredentials); |
| 76 | + |
| 77 | + expect(signed.headers['X-Amz-Date']).to.exist; |
| 78 | + expect(signed.headers['X-Amz-Date']).to.equal('20251215T123456Z'); |
| 79 | + expect(signed.headers['Authorization']).to.exist; |
| 80 | + expect(signed.headers['Authorization']).to.equal( |
| 81 | + 'AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20251215/us-east-1/sts/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date;x-amz-security-token;x-mongodb-gs2-cb-flag;x-mongodb-server-nonce, Signature=bbcb06e2feb8651dced329789743ba283f92ef1302d34a7398cb1d35808a1a66' |
| 82 | + ); |
| 83 | + |
| 84 | + // Uncomment the following lines if you want to compare with the old aws4 library. |
| 85 | + // Remember to import aws4 at the top of the file, like this: import * as aws4sign from 'aws4'; |
| 86 | + |
| 87 | + // const oldSigned = aws4sign.sign(request, awsSessionCredentials); |
| 88 | + // expect(oldSigned.headers['X-Amz-Date']).to.exist; |
| 89 | + // expect(oldSigned.headers['X-Amz-Date']).to.equal(signed.headers['X-Amz-Date']); |
| 90 | + // expect(oldSigned.headers['Authorization']).to.exist; |
| 91 | + // expect(oldSigned.headers['Authorization']).to.equal(signed.headers['Authorization']); |
| 92 | + }); |
| 93 | +}); |
0 commit comments