Skip to content

Commit eeea295

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 a871eac + a3511c4 commit eeea295

File tree

7 files changed

+173
-133
lines changed

7 files changed

+173
-133
lines changed

packages/signature-v4/__tests__/SignatureV4.ts

Lines changed: 26 additions & 28 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
});
@@ -73,7 +76,6 @@ describe("SignatureV4", () => {
7376
...minimalRequest,
7477
body: "It was the best of times, it was the worst of times"
7578
},
76-
credentials,
7779
expiration,
7880
currentDateConstructor: MockDate as any
7981
});
@@ -94,7 +96,6 @@ describe("SignatureV4", () => {
9496
...minimalRequest,
9597
body: new Uint8Array([0xde, 0xad, 0xbe, 0xef])
9698
},
97-
credentials,
9899
expiration,
99100
currentDateConstructor: MockDate as any
100101
});
@@ -115,7 +116,6 @@ describe("SignatureV4", () => {
115116
...minimalRequest,
116117
body: new PassThrough()
117118
},
118-
credentials,
119119
expiration,
120120
currentDateConstructor: MockDate as any
121121
});
@@ -131,12 +131,17 @@ describe("SignatureV4", () => {
131131
});
132132

133133
it(`should set and sign the ${TOKEN_QUERY_PARAM} query parameter if the credentials have a session token`, async () => {
134-
const { query } = await signer.presignRequest({
135-
request: minimalRequest,
134+
const signer = new SignatureV4({
135+
service: "foo",
136+
region: "us-bar-1",
137+
sha256: Sha256,
136138
credentials: {
137139
...credentials,
138140
sessionToken: "baz"
139-
},
141+
}
142+
});
143+
const { query } = await signer.presignRequest({
144+
request: minimalRequest,
140145
expiration,
141146
currentDateConstructor: MockDate as any
142147
});
@@ -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
@@ -225,7 +229,6 @@ describe("SignatureV4", () => {
225229
...minimalRequest,
226230
headers
227231
},
228-
credentials,
229232
expiration,
230233
hoistHeaders: false,
231234
currentDateConstructor: MockDate as any,
@@ -245,7 +248,6 @@ describe("SignatureV4", () => {
245248
[EXPIRES_QUERY_PARAM]: "1 week"
246249
}
247250
},
248-
credentials,
249251
expiration,
250252
hoistHeaders: false,
251253
currentDateConstructor: MockDate as any
@@ -257,7 +259,6 @@ describe("SignatureV4", () => {
257259
return expect(
258260
signer.presignRequest({
259261
request: minimalRequest,
260-
credentials,
261262
expiration: new Date(),
262263
currentDateConstructor: MockDate as any
263264
})
@@ -267,7 +268,6 @@ describe("SignatureV4", () => {
267268
it("should use the current date if no constructor supplied", async () => {
268269
const { query } = await signer.presignRequest({
269270
request: minimalRequest,
270-
credentials,
271271
expiration: Math.floor((Date.now() + 60 * 60 * 1000) / 1000)
272272
});
273273
expect((query as any)[AMZ_DATE_QUERY_PARAM]).toBe(
@@ -280,7 +280,6 @@ describe("SignatureV4", () => {
280280
it("should sign requests without bodies", async () => {
281281
const { headers } = await signer.signRequest({
282282
request: minimalRequest,
283-
credentials,
284283
currentDateConstructor: MockDate as any
285284
});
286285
expect(headers[AUTH_HEADER]).toBe(
@@ -294,7 +293,6 @@ describe("SignatureV4", () => {
294293
...minimalRequest,
295294
body: "It was the best of times, it was the worst of times"
296295
},
297-
credentials,
298296
currentDateConstructor: MockDate as any
299297
});
300298
expect(headers[AUTH_HEADER]).toBe(
@@ -308,7 +306,6 @@ describe("SignatureV4", () => {
308306
...minimalRequest,
309307
body: new Uint8Array([0xde, 0xad, 0xbe, 0xef])
310308
},
311-
credentials,
312309
currentDateConstructor: MockDate as any
313310
});
314311
expect(headers[AUTH_HEADER]).toBe(
@@ -322,7 +319,6 @@ describe("SignatureV4", () => {
322319
...minimalRequest,
323320
body: new PassThrough()
324321
},
325-
credentials,
326322
currentDateConstructor: MockDate as any
327323
});
328324

@@ -335,19 +331,23 @@ describe("SignatureV4", () => {
335331
it(`should set the ${AMZ_DATE_HEADER}`, async () => {
336332
const { headers } = await signer.signRequest({
337333
request: minimalRequest,
338-
credentials,
339334
currentDateConstructor: MockDate as any
340335
});
341336
expect(headers[AMZ_DATE_HEADER]).toBe("20000101T000000Z");
342337
});
343338

344339
it(`should set and sign the ${TOKEN_HEADER} header if the credentials have a session token`, async () => {
345-
const { headers } = await signer.signRequest({
346-
request: minimalRequest,
340+
const signer = new SignatureV4({
341+
service: "foo",
342+
region: "us-bar-1",
343+
sha256: Sha256,
347344
credentials: {
348345
...credentials,
349346
sessionToken: "baz"
350-
},
347+
}
348+
});
349+
const { headers } = await signer.signRequest({
350+
request: minimalRequest,
351351
currentDateConstructor: MockDate as any
352352
});
353353
expect(headers[AUTH_HEADER]).toBe(
@@ -360,15 +360,15 @@ describe("SignatureV4", () => {
360360
service: "foo",
361361
region: "us-bar-1",
362362
sha256: Sha256,
363-
unsignedPayload: true
363+
unsignedPayload: true,
364+
credentials
364365
});
365366

366367
const { headers } = await signer.signRequest({
367368
request: {
368369
...minimalRequest,
369370
body: new Uint8Array([0xde, 0xad, 0xbe, 0xef])
370371
},
371-
credentials,
372372
currentDateConstructor: MockDate as any
373373
});
374374
expect(headers[AUTH_HEADER]).toBe(
@@ -379,8 +379,7 @@ describe("SignatureV4", () => {
379379

380380
it("should use the current date if no constructor supplied", async () => {
381381
const { headers } = await signer.signRequest({
382-
request: minimalRequest,
383-
credentials
382+
request: minimalRequest
384383
});
385384
expect(headers[AMZ_DATE_HEADER]).toBe(
386385
iso8601(new Date()).replace(/[\-:]/g, "")
@@ -397,7 +396,6 @@ describe("SignatureV4", () => {
397396
"user-agent": "baz"
398397
}
399398
},
400-
credentials,
401399
currentDateConstructor: MockDate as any,
402400
unsignableHeaders: { foo: true }
403401
});

0 commit comments

Comments
 (0)