-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverify.test.js
244 lines (237 loc) · 8.94 KB
/
verify.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"use strict";
const clone = require("clone");
const cap1HmacSha512 = require("./index.js");
const SECRETS =
{
"someId": "mySecret"
};
const VALID_PARAMS =
{
headers:
{
host: "localhost:8888",
connection: "close",
authorization: "CAP1-HMAC-SHA512 Credential=someId/20170702/localhost:8888/cap1_request,SignedHeaders=host;x-cap-date,Signature=zfJvsTapbKxdvWMCbRFbkqO99b6dtgS81ym4O7k9I3R_m31mmUy9qcv_0qnrVKVbGhVkADX5xTLEedi_pzfEZw",
"x-cap-date": "20170702T204657Z"
},
httpRequestMethod: "GET",
now: new Date("2017-07-02T20:46:57Z"),
path: "/somewhere",
queryString: "page=12",
secret: (keyId, callback) => callback(undefined, SECRETS[keyId])
};
it("does not mutate params", () =>
{
const params = clone(VALID_PARAMS);
Object.freeze(params);
Object.keys(params).map(key =>
{
Object.freeze(params[key]);
}
);
expect(() => cap1HmacSha512.verify(params, () => {})).not.toThrow();
}
);
it("throws if secret is not a function", () =>
{
const params = clone(VALID_PARAMS);
params.secret = { someId: "secret" };
expect(() => cap1HmacSha512.verify(params, () => {}))
.toThrow(new Error("secret is not a function"));
}
);
describe("result", () =>
{
describe("is false if", () =>
{
it("request is missing X-Cap-Date header", done =>
{
const params = clone(VALID_PARAMS);
delete params.headers["x-cap-date"];
cap1HmacSha512.verify(params, (error, authorized) =>
{
expect(error).toBe(undefined);
expect(authorized).toBe(false);
done();
}
);
}
);
it("request Authorization header does not have valid keyId", done =>
{
const params = clone(VALID_PARAMS);
params.headers.authorization = "CAP1-HMAC-SHA512 Credential=/20170702/localhost:8888/cap1_request,SignedHeaders=connection;host;x-cap-date,Signature=lEV0szOupNaXRoZPsGYziaF-liIIwA0lIyCHJCK9AxYD7fXaSAlEABgKhZOA4cw2polyV0HujGiZVU6TdQcGAQ";
cap1HmacSha512.verify(params, (error, authorized) =>
{
expect(error).toBe(undefined);
expect(authorized).toBe(false);
done();
}
);
}
);
it("request Authorization header does not match calculated Authorization header", done =>
{
const params = clone(VALID_PARAMS);
params.headers.authorization = "CAP1-HMAC-SHA512 Credential=someId/20170702/localhost:8888/cap1_request,SignedHeaders=connection;host;x-cap-date,Signature=lEV0szOupNaXRoZPsGYziaF-liIIwA0lIyCHJCK9AxYD7fXaSAlEABgKhZOA4cw2polyV0HujGiZVU6TdQcGAW";
cap1HmacSha512.verify(params, (error, authorized) =>
{
expect(error).toBe(undefined);
expect(authorized).toBe(false);
done();
}
);
}
);
it("request X-Cap-Date is more than 15 minutes earlier than now", done =>
{
const params = clone(VALID_PARAMS);
const capDate = new Date("2017-07-02T20:46:57Z");
params.now = new Date(capDate.getTime() + 1000*60*16);
cap1HmacSha512.verify(params, (error, authorized) =>
{
expect(error).toBe(undefined);
expect(authorized).toBe(false);
done()
}
);
}
);
it("request X-Cap-Date is more than 15 minutes later than now", done =>
{
const params = clone(VALID_PARAMS);
const capDate = new Date("2017-07-02T20:46:57Z");
params.now = new Date(capDate.getTime() - 1000*60*16);
cap1HmacSha512.verify(params, (error, authorized) =>
{
expect(error).toBe(undefined);
expect(authorized).toBe(false);
done()
}
);
}
);
it("secret function calls callback with an error", done =>
{
const params = clone(VALID_PARAMS);
params.secret = (_, callback) => callback(new Error("boom"));
cap1HmacSha512.verify(params, (error, authorized) =>
{
expect(error).toBe(undefined);
expect(authorized).toBe(false);
done()
}
);
}
);
it("secret function calls callback with no key material", done =>
{
const params = clone(VALID_PARAMS);
params.secret = (_, callback) => callback();
cap1HmacSha512.verify(params, (error, authorized) =>
{
expect(error).toBe(undefined);
expect(authorized).toBe(false);
done()
}
);
}
);
});
describe("is true if", () =>
{
it("request X-Cap-Date is within 15 minutes and Authorization header matches calculated Authorization header", done =>
{
const params = clone(VALID_PARAMS);
params.now = new Date("2017-07-02T20:46:57Z");
cap1HmacSha512.verify(params, (error, authorized) =>
{
expect(error).toBe(undefined);
expect(authorized).toBe(true);
done()
}
);
}
);
it("request matches signature", done =>
{
const signature = cap1HmacSha512.sign(
{
headers:
{
host: "localhost:8888",
connection: "close"
},
httpRequestMethod: "GET",
key: SECRETS["someId"],
keyId: "someId",
path: "/somewhere",
queryString: null
}
);
const params =
{
headers:
{
host: "localhost:8888",
connection: "close",
authorization: signature.authorization,
"x-cap-date": signature["x-cap-date"]
},
httpRequestMethod: "get",
path: "/somewhere",
queryString: null,
secret: (keyId, callback) => callback(undefined, SECRETS[keyId])
};
cap1HmacSha512.verify(params, (error, authorized) =>
{
expect(error).toBe(undefined);
expect(authorized).toBe(true);
done()
}
);
}
);
it("request has extra unsigned headers but signature matches signed headers", done =>
{
const signature = cap1HmacSha512.sign(
{
headers:
{
host: "localhost:8888",
connection: "close"
},
httpRequestMethod: "GET",
key: SECRETS["someId"],
keyId: "someId",
path: "/somewhere",
queryString: null
}
);
const params =
{
headers:
{
host: "localhost:8888",
connection: "close",
"content-length": 17,
"some-header": "that wasn't there before",
authorization: signature.authorization,
"x-cap-date": signature["x-cap-date"]
},
httpRequestMethod: "get",
path: "/somewhere",
queryString: null,
secret: (keyId, callback) => callback(undefined, SECRETS[keyId])
};
cap1HmacSha512.verify(params, (error, authorized) =>
{
expect(error).toBe(undefined);
expect(authorized).toBe(true);
done()
}
);
}
);
});
});