Skip to content

Commit 17cfb12

Browse files
authored
Merge pull request #44 from jeskew/fix/v4-signer-tweaks
Widen region option in SignatureV4 ctor to accept a region provider and move credentials to be a ctor argument
2 parents 5a51c52 + 66a74de commit 17cfb12

File tree

7 files changed

+160
-128
lines changed

7 files changed

+160
-128
lines changed

packages/signature-v4/__tests__/SignatureV4.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ const MockDate = () => new Date('2000-01-01T00:00:00.000Z');
2525
const signer = new SignatureV4({
2626
service: 'foo',
2727
region: 'us-bar-1',
28-
sha256: Sha256
28+
sha256: Sha256,
29+
credentials: {
30+
accessKeyId: 'foo',
31+
secretAccessKey: 'bar',
32+
}
2933
});
3034

3135
const minimalRequest: HttpRequest<any> = {
@@ -52,7 +56,6 @@ describe('SignatureV4', () => {
5256
it('should sign requests without bodies', async () => {
5357
const {query} = await signer.presignRequest({
5458
request: minimalRequest,
55-
credentials,
5659
expiration,
5760
currentDateConstructor: MockDate as any,
5861
});
@@ -72,7 +75,6 @@ describe('SignatureV4', () => {
7275
...minimalRequest,
7376
body: 'It was the best of times, it was the worst of times'
7477
},
75-
credentials,
7678
expiration,
7779
currentDateConstructor: MockDate as any,
7880
});
@@ -92,7 +94,6 @@ describe('SignatureV4', () => {
9294
...minimalRequest,
9395
body: new Uint8Array([0xde, 0xad, 0xbe, 0xef])
9496
},
95-
credentials,
9697
expiration,
9798
currentDateConstructor: MockDate as any,
9899
});
@@ -112,7 +113,6 @@ describe('SignatureV4', () => {
112113
...minimalRequest,
113114
body: new PassThrough()
114115
},
115-
credentials,
116116
expiration,
117117
currentDateConstructor: MockDate as any,
118118
});
@@ -129,12 +129,17 @@ describe('SignatureV4', () => {
129129
it(
130130
`should set and sign the ${TOKEN_QUERY_PARAM} query parameter if the credentials have a session token`,
131131
async () => {
132-
const {query} = await signer.presignRequest({
133-
request: minimalRequest,
132+
const signer = new SignatureV4({
133+
service: 'foo',
134+
region: 'us-bar-1',
135+
sha256: Sha256,
134136
credentials: {
135137
...credentials,
136138
sessionToken: 'baz',
137-
},
139+
}
140+
});
141+
const {query} = await signer.presignRequest({
142+
request: minimalRequest,
138143
expiration,
139144
currentDateConstructor: MockDate as any,
140145
});
@@ -158,15 +163,15 @@ describe('SignatureV4', () => {
158163
service: 'foo',
159164
region: 'us-bar-1',
160165
sha256: Sha256,
161-
unsignedPayload: true
166+
unsignedPayload: true,
167+
credentials,
162168
});
163169

164170
const {query} = await signer.presignRequest({
165171
request: {
166172
...minimalRequest,
167173
body: new Uint8Array([0xde, 0xad, 0xbe, 0xef]),
168174
},
169-
credentials,
170175
expiration,
171176
currentDateConstructor: MockDate as any,
172177
});
@@ -196,7 +201,6 @@ describe('SignatureV4', () => {
196201
...minimalRequest,
197202
headers,
198203
},
199-
credentials,
200204
expiration,
201205
hoistHeaders: false,
202206
currentDateConstructor: MockDate as any,
@@ -223,7 +227,6 @@ describe('SignatureV4', () => {
223227
...minimalRequest,
224228
headers,
225229
},
226-
credentials,
227230
expiration,
228231
hoistHeaders: false,
229232
currentDateConstructor: MockDate as any,
@@ -243,7 +246,6 @@ describe('SignatureV4', () => {
243246
[EXPIRES_QUERY_PARAM]: '1 week',
244247
}
245248
},
246-
credentials,
247249
expiration,
248250
hoistHeaders: false,
249251
currentDateConstructor: MockDate as any,
@@ -258,7 +260,6 @@ describe('SignatureV4', () => {
258260
return expect(
259261
signer.presignRequest({
260262
request: minimalRequest,
261-
credentials,
262263
expiration: new Date(),
263264
currentDateConstructor: MockDate as any,
264265
})
@@ -269,7 +270,6 @@ describe('SignatureV4', () => {
269270
it('should use the current date if no constructor supplied', async () => {
270271
const {query} = await signer.presignRequest({
271272
request: minimalRequest,
272-
credentials,
273273
expiration: Math.floor((Date.now() + 60 * 60 * 1000) / 1000),
274274
});
275275
expect((query as any)[AMZ_DATE_QUERY_PARAM]).toBe(
@@ -282,7 +282,6 @@ describe('SignatureV4', () => {
282282
it('should sign requests without bodies', async () => {
283283
const {headers} = await signer.signRequest({
284284
request: minimalRequest,
285-
credentials,
286285
currentDateConstructor: MockDate as any,
287286
});
288287
expect(headers[AUTH_HEADER]).toBe(
@@ -296,7 +295,6 @@ describe('SignatureV4', () => {
296295
...minimalRequest,
297296
body: 'It was the best of times, it was the worst of times'
298297
},
299-
credentials,
300298
currentDateConstructor: MockDate as any,
301299
});
302300
expect(headers[AUTH_HEADER]).toBe(
@@ -310,7 +308,6 @@ describe('SignatureV4', () => {
310308
...minimalRequest,
311309
body: new Uint8Array([0xde, 0xad, 0xbe, 0xef]),
312310
},
313-
credentials,
314311
currentDateConstructor: MockDate as any,
315312
});
316313
expect(headers[AUTH_HEADER]).toBe(
@@ -324,7 +321,6 @@ describe('SignatureV4', () => {
324321
...minimalRequest,
325322
body: new PassThrough(),
326323
},
327-
credentials,
328324
currentDateConstructor: MockDate as any,
329325
});
330326

@@ -337,7 +333,6 @@ describe('SignatureV4', () => {
337333
it(`should set the ${AMZ_DATE_HEADER}`, async () => {
338334
const {headers} = await signer.signRequest({
339335
request: minimalRequest,
340-
credentials,
341336
currentDateConstructor: MockDate as any,
342337
});
343338
expect(headers[AMZ_DATE_HEADER]).toBe('20000101T000000Z');
@@ -346,12 +341,17 @@ describe('SignatureV4', () => {
346341
it(
347342
`should set and sign the ${TOKEN_HEADER} header if the credentials have a session token`,
348343
async () => {
349-
const {headers} = await signer.signRequest({
350-
request: minimalRequest,
344+
const signer = new SignatureV4({
345+
service: 'foo',
346+
region: 'us-bar-1',
347+
sha256: Sha256,
351348
credentials: {
352349
...credentials,
353350
sessionToken: 'baz',
354351
},
352+
});
353+
const {headers} = await signer.signRequest({
354+
request: minimalRequest,
355355
currentDateConstructor: MockDate as any,
356356
});
357357
expect(headers[AUTH_HEADER]).toBe(
@@ -367,15 +367,15 @@ describe('SignatureV4', () => {
367367
service: 'foo',
368368
region: 'us-bar-1',
369369
sha256: Sha256,
370-
unsignedPayload: true
370+
unsignedPayload: true,
371+
credentials,
371372
});
372373

373374
const {headers} = await signer.signRequest({
374375
request: {
375376
...minimalRequest,
376377
body: new Uint8Array([0xde, 0xad, 0xbe, 0xef]),
377378
},
378-
credentials,
379379
currentDateConstructor: MockDate as any,
380380
});
381381
expect(headers[AUTH_HEADER]).toBe(
@@ -388,7 +388,6 @@ describe('SignatureV4', () => {
388388
it('should use the current date if no constructor supplied', async () => {
389389
const {headers} = await signer.signRequest({
390390
request: minimalRequest,
391-
credentials,
392391
});
393392
expect(headers[AMZ_DATE_HEADER]).toBe(
394393
iso8601(new Date()).replace(/[\-:]/g, '')
@@ -405,7 +404,6 @@ describe('SignatureV4', () => {
405404
'user-agent': 'baz',
406405
},
407406
},
408-
credentials,
409407
currentDateConstructor: MockDate as any,
410408
unsignableHeaders: {foo: true}
411409
});

0 commit comments

Comments
 (0)