This repository was archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathoauthdb.js
More file actions
324 lines (292 loc) · 9.74 KB
/
oauthdb.js
File metadata and controls
324 lines (292 loc) · 9.74 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
const { assert } = require('chai');
const nock = require('nock');
const JWT = require('jsonwebtoken');
const P = require('../../lib/promise');
const oauthdbModule = require('../../lib/oauthdb');
const error = require('../../lib/error');
const { mockLog } = require('../mocks');
const mockConfig = {
publicUrl: 'https://accounts.example.com',
oauth: {
url: 'https://oauth.server.com',
secretKey: 'secret-key-oh-secret-key',
},
domain: 'accounts.example.com'
};
const MOCK_UID = '1a147912d8de4ab5842ecc9fb7186800';
const MOCK_CLIENT_ID = '0123456789ABCDEF';
const MOCK_SCOPES = 'mock-scope another-scope';
const MOCK_TOKEN = '8ddd955475561c723d38863defc558788aee362c4f28df76b997ae62646a7b43';
const MOCK_CLIENT_INFO = {
id: MOCK_CLIENT_ID,
name: 'mock client',
trusted: false,
redirect_uri: 'http://mock.client.com/redirect'
};
const LOCKBOX_CLIENT_INFO = {
id: 'e7ce535d93522896',
name: 'Firefox Lockbox for Android',
trusted: true,
image_uri: '',
redirect_uri: 'https://lockbox.firefox.com/fxa/android-redirect.html'
};
const mockOAuthServer = nock(mockConfig.oauth.url).defaultReplyHeaders({
'Content-Type': 'application/json'
});
function verifyJWT(token) {
return new P((resolve, reject) => {
JWT.verify(token, mockConfig.oauth.secretKey, { algorithm: 'HS256' }, (err, claims) => {
if (err) {
reject(err);
} else {
resolve(claims);
}
});
});
}
describe('oauthdb', () => {
let oauthdb;
afterEach(async () => {
assert.ok(nock.isDone(), 'there should be no pending request mocks at the end of a test');
if (oauthdb) {
await oauthdb.close();
}
});
describe('getClientInfo', () => {
it('gets client info', async () => {
mockOAuthServer.get(`/v1/client/${MOCK_CLIENT_ID}`)
.reply(200, MOCK_CLIENT_INFO);
oauthdb = oauthdbModule(mockLog(), mockConfig);
const info = await oauthdb.getClientInfo(MOCK_CLIENT_ID);
assert.deepEqual(info, MOCK_CLIENT_INFO);
});
it('gets client info for a real client that previous triggered validation errors', async () => {
mockOAuthServer.get(`/v1/client/${LOCKBOX_CLIENT_INFO.id}`)
.reply(200, LOCKBOX_CLIENT_INFO);
oauthdb = oauthdbModule(mockLog(), mockConfig);
const info = await oauthdb.getClientInfo(LOCKBOX_CLIENT_INFO.id);
assert.deepEqual(info, LOCKBOX_CLIENT_INFO);
});
it('returns correct error for unknown client_id', async () => {
mockOAuthServer.get(`/v1/client/${MOCK_CLIENT_ID}`)
.reply(400, {
code: 400,
errno: 101,
message: 'Unknown client',
clientId: MOCK_CLIENT_ID
});
oauthdb = oauthdbModule(mockLog(), mockConfig);
try {
await oauthdb.getClientInfo(MOCK_CLIENT_ID);
assert.fail('should have thrown');
} catch (err) {
assert.equal(err.errno, error.ERRNO.UNKNOWN_CLIENT_ID);
assert.equal(err.output.payload.clientId, MOCK_CLIENT_ID);
}
});
it('validates its input parameters', async () => {
oauthdb = oauthdbModule(mockLog(), mockConfig);
try {
await oauthdb.getClientInfo('invalid-client-id');
assert.fail('should have thrown');
} catch (err) {
assert.equal(err.errno, error.ERRNO.INTERNAL_VALIDATION_ERROR);
}
});
it('validates the response data', async () => {
mockOAuthServer.get(`/v1/client/${MOCK_CLIENT_ID}`)
.reply(200, {
id: MOCK_CLIENT_ID,
name: 'mock client',
trusted: false,
redirect_uri: 42 // invalid!
});
oauthdb = oauthdbModule(mockLog(), mockConfig);
try {
await oauthdb.getClientInfo(MOCK_CLIENT_ID);
assert.fail('should have thrown');
} catch (err) {
assert.equal(err.errno, error.ERRNO.INTERNAL_VALIDATION_ERROR);
}
});
});
describe('getScopedKeyData', () => {
const ZEROS = Buffer.alloc(32).toString('hex');
const MOCK_CREDENTIALS = {
uid: MOCK_UID,
verifierSetAt: 12345,
email: `${MOCK_UID }@example.com`,
lastAuthAt: () => 23456,
emailVerified: true,
tokenVerified: true,
authenticationMethods: ['pwd'],
authenticatorAssuranceLevel: 1
};
it('gets scoped key data, authenticating with a JWT', async () => {
let requestBody;
mockOAuthServer.post('/v1/key-data', body => {
requestBody = body;
return true;
}).reply(200, {
'mock-scope': {
identifier: 'mock-scope',
keyRotationSecret: ZEROS,
keyRotationTimestamp: 0,
}
});
oauthdb = oauthdbModule(mockLog(), mockConfig);
const keyData = await oauthdb.getScopedKeyData(MOCK_CREDENTIALS, {
client_id: MOCK_CLIENT_ID,
scope: MOCK_SCOPES
});
assert.equal(requestBody.client_id, MOCK_CLIENT_ID);
assert.equal(requestBody.scope, MOCK_SCOPES);
const claims = await verifyJWT(requestBody.assertion);
// We don't know the exact `iat` timestamp for the JWT.
assert.ok(claims.iat <= Date.now() / 1000);
assert.ok(claims.iat >= Date.now() / 1000 - 10);
assert.equal(claims.exp, claims.iat + 60);
delete claims.iat;
delete claims.exp;
assert.deepEqual(claims, {
aud: 'https://oauth.server.com',
'fxa-aal': 1,
'fxa-amr': ['pwd'],
'fxa-generation': 12345,
'fxa-lastAuthAt': 23456,
'fxa-tokenVerified': true,
'fxa-verifiedEmail': MOCK_CREDENTIALS.email,
iss: 'accounts.example.com',
sub: MOCK_UID
});
assert.deepEqual(keyData, {
'mock-scope': {
identifier: 'mock-scope',
keyRotationSecret: ZEROS,
keyRotationTimestamp: 0,
}
});
});
it('returns correct error for unknown client_id', async () => {
mockOAuthServer.post('/v1/key-data', body => true)
.reply(400, {
code: 400,
errno: 101,
message: 'Unknown client',
clientId: MOCK_CLIENT_ID
});
oauthdb = oauthdbModule(mockLog(), mockConfig);
try {
await oauthdb.getScopedKeyData(MOCK_CREDENTIALS, {
client_id: MOCK_CLIENT_ID,
scope: MOCK_SCOPES
});
assert.fail('should have thrown');
} catch (err) {
assert.equal(err.errno, error.ERRNO.UNKNOWN_CLIENT_ID);
assert.equal(err.output.payload.clientId, MOCK_CLIENT_ID);
}
});
it('returns correct error for stale auth-at', async () => {
mockOAuthServer.post('/v1/key-data', body => true)
.reply(400, {
code: 400,
errno: 119,
message: 'Stale auth timestamp',
authAt: 7
});
oauthdb = oauthdbModule(mockLog(), mockConfig);
try {
await oauthdb.getScopedKeyData(MOCK_CREDENTIALS, {
client_id: MOCK_CLIENT_ID,
scope: MOCK_SCOPES
});
assert.fail('should have thrown');
} catch (err) {
assert.equal(err.errno, error.ERRNO.STALE_AUTH_AT);
assert.equal(err.output.payload.authAt, 7);
}
});
it('validates its input parameters', async () => {
oauthdb = oauthdbModule(mockLog(), mockConfig);
try {
await oauthdb.getScopedKeyData(MOCK_CREDENTIALS, {
client_id: MOCK_CLIENT_ID,
scope: 'invalid!scope#'
});
assert.fail('should have thrown');
} catch (err) {
assert.equal(err.errno, error.ERRNO.INTERNAL_VALIDATION_ERROR);
}
});
it('validates the response data', async () => {
mockOAuthServer.post('/v1/key-data', body => true)
.reply(200, {
'mock-scope': {
identifier: 'mock-scope',
keyRotationSecret: 42, // invalid!
keyRotationTimestamp: 0,
}
});
oauthdb = oauthdbModule(mockLog(), mockConfig);
try {
await oauthdb.getScopedKeyData(MOCK_CREDENTIALS, {
client_id: MOCK_CLIENT_ID,
scope: MOCK_SCOPES
});
assert.fail('should have thrown');
} catch (err) {
assert.equal(err.errno, error.ERRNO.INTERNAL_VALIDATION_ERROR);
}
});
it('requires a verified account', async () => {
oauthdb = oauthdbModule(mockLog(), mockConfig);
try {
await oauthdb.getScopedKeyData(Object.assign({}, MOCK_CREDENTIALS, {
emailVerified: false
}), {
client_id: MOCK_CLIENT_ID,
scope: MOCK_SCOPES
});
assert.fail('should have thrown');
} catch (err) {
assert.equal(err.errno, error.ERRNO.ACCOUNT_UNVERIFIED);
}
});
it('enforces session verification', async () => {
oauthdb = oauthdbModule(mockLog(), mockConfig);
try {
await oauthdb.getScopedKeyData(Object.assign({}, MOCK_CREDENTIALS, {
mustVerify: true,
tokenVerified: false
}), {
client_id: MOCK_CLIENT_ID,
scope: MOCK_SCOPES
});
assert.fail('should have thrown');
} catch (err) {
assert.equal(err.errno, error.ERRNO.SESSION_UNVERIFIED);
}
});
});
describe('checkAccessToken', () => {
it('works', async () => {
const verifyResponse = {
user: MOCK_UID,
client_id: MOCK_CLIENT_ID,
scope: ['https://identity.mozilla.com/apps/oldsync', 'openid']
};
mockOAuthServer.post('/v1/verify', body => true)
.reply(200, verifyResponse);
oauthdb = oauthdbModule(mockLog(), mockConfig);
const response = await oauthdb.checkAccessToken({
token: MOCK_TOKEN
});
assert.deepEqual(verifyResponse, response);
});
});
});