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 108
Expand file tree
/
Copy pathgrant-tokens-from-authorization-code.js
More file actions
168 lines (157 loc) · 5.39 KB
/
grant-tokens-from-authorization-code.js
File metadata and controls
168 lines (157 loc) · 5.39 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
/* 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 oauthdbModule = require('../../../lib/oauthdb');
const error = require('../../../lib/error');
const { mockLog } = require('../../mocks');
const MOCK_USER_ID = '5A6773A8D23E49FDAFCC976882E0B57E';
const MOCK_CLIENT_ID = '0123456789ABCDEF';
const MOCK_AUTHORIZATION_CODE = '1111112222223333334444445555556611111122222233333344444455555566';
const MOCK_ACCESS_TOKEN = 'aaaaaa2222223333334444445555556611111122222233333344444455555566';
const mockConfig = {
publicUrl: 'https://accounts.example.com',
oauth: {
url: 'https://oauth.server.com',
secretKey: 'secret-key-oh-secret-key',
},
domain: 'accounts.example.com'
};
const mockOAuthServer = nock(mockConfig.oauth.url).defaultReplyHeaders({
'Content-Type': 'application/json'
});
describe('oauthdb/grantTokensFromAuthorizationCode', () => {
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();
}
});
it('can exchange a code for some tokens', async () => {
mockOAuthServer.post('/v1/token', body => true)
.reply(200, {
access_token: MOCK_ACCESS_TOKEN,
user: MOCK_USER_ID,
scope: '',
token_type: 'bearer',
expires_in: 123,
auth_at: 123456,
});
oauthdb = oauthdbModule(mockLog(), mockConfig);
const res = await oauthdb.grantTokensFromAuthorizationCode({
client_id: MOCK_CLIENT_ID,
client_secret: MOCK_CLIENT_ID,
code: MOCK_AUTHORIZATION_CODE,
});
assert.deepEqual(res, {
access_token: MOCK_ACCESS_TOKEN,
user: MOCK_USER_ID,
scope: '',
token_type: 'bearer',
expires_in: 123,
auth_at: 123456,
});
});
it('correctly maps errno 102 to "incorrect client secret"', async () => {
mockOAuthServer.post('/v1/token', body => true)
.reply(400, {
errno: 102,
clientId: MOCK_CLIENT_ID,
});
oauthdb = oauthdbModule(mockLog(), mockConfig);
try {
await oauthdb.grantTokensFromAuthorizationCode({
client_id: MOCK_CLIENT_ID,
client_secret: 'abcdef',
code: MOCK_AUTHORIZATION_CODE,
});
assert.fail('should have thrown');
} catch (err) {
assert.equal(err.errno, error.ERRNO.INCORRECT_CLIENT_SECRET);
assert.equal(err.output.payload.clientId, MOCK_CLIENT_ID);
}
});
it('correctly maps errno 105 to "unknown authorization code"', async () => {
mockOAuthServer.post('/v1/token', body => true)
.reply(400, {
errno: 105,
code: 'mock-code',
});
oauthdb = oauthdbModule(mockLog(), mockConfig);
try {
await oauthdb.grantTokensFromAuthorizationCode({
client_id: MOCK_CLIENT_ID,
client_secret: 'abcdef',
code: MOCK_AUTHORIZATION_CODE,
});
assert.fail('should have thrown');
} catch (err) {
assert.equal(err.errno, error.ERRNO.UNKNOWN_AUTHORIZATION_CODE);
assert.equal(err.output.payload.code, 'mock-code');
}
});
it('correctly maps errno 106 to "mismatched authorization code"', async () => {
mockOAuthServer.post('/v1/token', body => true)
.reply(400, {
errno: 106,
code: 'mock-code',
clientId: MOCK_CLIENT_ID,
});
oauthdb = oauthdbModule(mockLog(), mockConfig);
try {
await oauthdb.grantTokensFromAuthorizationCode({
client_id: MOCK_CLIENT_ID,
client_secret: 'abcdef',
code: MOCK_AUTHORIZATION_CODE,
});
assert.fail('should have thrown');
} catch (err) {
assert.equal(err.errno, error.ERRNO.MISMATCH_AUTHORIZATION_CODE);
assert.equal(err.output.payload.clientId, MOCK_CLIENT_ID);
assert.equal(err.output.payload.code, 'mock-code');
}
});
it('correctly maps errno 107 to "expired authorization code"', async () => {
mockOAuthServer.post('/v1/token', body => true)
.reply(400, {
errno: 107,
code: 'mock-code',
expiredAt: 123456,
});
oauthdb = oauthdbModule(mockLog(), mockConfig);
try {
await oauthdb.grantTokensFromAuthorizationCode({
client_id: MOCK_CLIENT_ID,
client_secret: 'abcdef',
code: MOCK_AUTHORIZATION_CODE,
});
assert.fail('should have thrown');
} catch (err) {
assert.equal(err.errno, error.ERRNO.EXPIRED_AUTHORIZATION_CODE);
assert.equal(err.output.payload.code, 'mock-code');
assert.equal(err.output.payload.expiredAt, 123456);
}
});
it('correctly maps errno 117 to "incorrect pkce challenge"', async () => {
mockOAuthServer.post('/v1/token', body => true)
.reply(400, {
errno: 117,
pkceHashValue: 'mock-pkce-hash',
});
oauthdb = oauthdbModule(mockLog(), mockConfig);
try {
await oauthdb.grantTokensFromAuthorizationCode({
client_id: MOCK_CLIENT_ID,
code_verifier: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbb',
code: MOCK_AUTHORIZATION_CODE,
});
assert.fail('should have thrown');
} catch (err) {
assert.equal(err.errno, error.ERRNO.INVALID_PKCE_CHALLENGE);
assert.equal(err.output.payload.pkceHashValue, 'mock-pkce-hash');
}
});
});