@whook/oauth2 27.0.2 → 27.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -25
- package/dist/index.d.ts +11 -7
- package/dist/index.js +9 -6
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +131 -15
- package/dist/index.test.js.map +1 -1
- package/dist/libs/clients.d.ts +17 -1
- package/dist/libs/clients.js +39 -6
- package/dist/libs/clients.js.map +1 -1
- package/dist/libs/clients.test.d.ts +1 -0
- package/dist/libs/clients.test.js +32 -0
- package/dist/libs/clients.test.js.map +1 -0
- package/dist/libs/errors.d.ts +24 -0
- package/dist/libs/errors.js +42 -0
- package/dist/libs/errors.js.map +1 -1
- package/dist/libs/verifier.d.ts +3 -3
- package/dist/libs/verifier.js +4 -3
- package/dist/libs/verifier.js.map +1 -1
- package/dist/routes/getOAuth2Authorize.d.ts +19 -6
- package/dist/routes/getOAuth2Authorize.js +29 -14
- package/dist/routes/getOAuth2Authorize.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.d.ts +3 -4
- package/dist/routes/getOAuth2WellKnownMetadata.js +19 -7
- package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.test.js +81 -0
- package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.d.ts +0 -2
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js +1 -2
- package/dist/routes/getOAuth2WellKnownProtectedResourceMetadata.js.map +1 -1
- package/dist/routes/postAuthLogin.d.ts +1 -1
- package/dist/routes/postAuthLogin.js +1 -1
- package/dist/routes/postAuthLogin.js.map +1 -1
- package/dist/routes/postAuthRefresh.d.ts +1 -1
- package/dist/routes/postAuthRefresh.js +1 -1
- package/dist/routes/postAuthRefresh.js.map +1 -1
- package/dist/routes/postOAuth2Acknowledge.d.ts +0 -3
- package/dist/routes/postOAuth2Acknowledge.js +1 -3
- package/dist/routes/postOAuth2Acknowledge.js.map +1 -1
- package/dist/routes/postOAuth2DeviceAcknowledge.d.ts +64 -0
- package/dist/routes/postOAuth2DeviceAcknowledge.js +84 -0
- package/dist/routes/postOAuth2DeviceAcknowledge.js.map +1 -0
- package/dist/routes/postOAuth2DeviceAcknowledge.test.d.ts +1 -0
- package/dist/routes/postOAuth2DeviceAcknowledge.test.js +369 -0
- package/dist/routes/postOAuth2DeviceAcknowledge.test.js.map +1 -0
- package/dist/routes/postOAuth2DeviceAuthorization.d.ts +133 -0
- package/dist/routes/postOAuth2DeviceAuthorization.js +177 -0
- package/dist/routes/postOAuth2DeviceAuthorization.js.map +1 -0
- package/dist/routes/postOAuth2DeviceAuthorization.test.d.ts +1 -0
- package/dist/routes/postOAuth2DeviceAuthorization.test.js +174 -0
- package/dist/routes/postOAuth2DeviceAuthorization.test.js.map +1 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.d.ts +1 -6
- package/dist/routes/postOAuth2PushedAuthorizationRequest.js +11 -14
- package/dist/routes/postOAuth2PushedAuthorizationRequest.js.map +1 -1
- package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js +47 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js.map +1 -1
- package/dist/routes/postOAuth2Token.d.ts +2 -7
- package/dist/routes/postOAuth2Token.js +2 -6
- package/dist/routes/postOAuth2Token.js.map +1 -1
- package/dist/routes/postOAuth2Token.test.js +107 -0
- package/dist/routes/postOAuth2Token.test.js.map +1 -1
- package/dist/routes/schemas.d.ts +178 -0
- package/dist/routes/schemas.js +188 -0
- package/dist/routes/schemas.js.map +1 -0
- package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +0 -70
- package/dist/services/oAuth2AuthorizationCodeGranter.js +7 -84
- package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -1
- package/dist/services/oAuth2AuthorizationRequests.js +4 -5
- package/dist/services/oAuth2AuthorizationRequests.js.map +1 -1
- package/dist/services/oAuth2ClientCredentialsGranter.d.ts +1 -16
- package/dist/services/oAuth2ClientCredentialsGranter.js +7 -25
- package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
- package/dist/services/oAuth2DeviceAuthorizationCodeGranter.test.d.ts +1 -0
- package/dist/services/oAuth2DeviceAuthorizationCodeGranter.test.js +332 -0
- package/dist/services/oAuth2DeviceAuthorizationCodeGranter.test.js.map +1 -0
- package/dist/services/oAuth2DeviceAuthorizationGranter.d.ts +54 -0
- package/dist/services/oAuth2DeviceAuthorizationGranter.js +67 -0
- package/dist/services/oAuth2DeviceAuthorizationGranter.js.map +1 -0
- package/dist/services/oAuth2Granters.js +0 -2
- package/dist/services/oAuth2Granters.js.map +1 -1
- package/dist/services/oAuth2PasswordGranter.d.ts +1 -22
- package/dist/services/oAuth2PasswordGranter.js +5 -30
- package/dist/services/oAuth2PasswordGranter.js.map +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.d.ts +1 -19
- package/dist/services/oAuth2RefreshTokenGranter.js +5 -28
- package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
- package/package.json +4 -4
- package/src/index.test.ts +169 -26
- package/src/index.ts +21 -26
- package/src/libs/clients.test.ts +47 -0
- package/src/libs/clients.ts +60 -10
- package/src/libs/errors.ts +72 -0
- package/src/libs/verifier.ts +11 -3
- package/src/routes/__snapshots__/postOAuth2DeviceAcknowledge.test.ts.snap +5 -0
- package/src/routes/getOAuth2Authorize.ts +41 -26
- package/src/routes/getOAuth2WellKnownMetadata.test.ts +96 -0
- package/src/routes/getOAuth2WellKnownMetadata.ts +24 -10
- package/src/routes/getOAuth2WellKnownProtectedResourceMetadata.ts +1 -3
- package/src/routes/postAuthLogin.ts +1 -1
- package/src/routes/postAuthRefresh.ts +1 -1
- package/src/routes/postOAuth2Acknowledge.ts +2 -4
- package/src/routes/postOAuth2DeviceAcknowledge.test.ts +408 -0
- package/src/routes/postOAuth2DeviceAcknowledge.ts +157 -0
- package/src/routes/postOAuth2DeviceAuthorization.test.ts +192 -0
- package/src/routes/postOAuth2DeviceAuthorization.ts +261 -0
- package/src/routes/postOAuth2PushedAuthorizationRequest.test.ts +59 -0
- package/src/routes/postOAuth2PushedAuthorizationRequest.ts +16 -22
- package/src/routes/postOAuth2Token.test.ts +119 -0
- package/src/routes/postOAuth2Token.ts +6 -16
- package/src/routes/schemas.ts +210 -0
- package/src/services/oAuth2AuthorizationCodeGranter.ts +11 -94
- package/src/services/oAuth2AuthorizationRequests.ts +5 -5
- package/src/services/oAuth2ClientCredentialsGranter.ts +8 -27
- package/src/services/oAuth2DeviceAuthorizationCodeGranter.test.ts +372 -0
- package/src/services/oAuth2DeviceAuthorizationGranter.ts +194 -0
- package/src/services/oAuth2Granters.ts +0 -2
- package/src/services/oAuth2PasswordGranter.ts +7 -33
- package/src/services/oAuth2RefreshTokenGranter.ts +6 -31
- package/dist/libs/schemas.d.ts +0 -31
- package/dist/libs/schemas.js +0 -33
- package/dist/libs/schemas.js.map +0 -1
- package/src/libs/schemas.ts +0 -37
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import { describe, test, beforeEach, jest, expect } from '@jest/globals';
|
|
2
|
+
import {
|
|
3
|
+
type WhookOAuth2Options,
|
|
4
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
5
|
+
} from './oAuth2Granters.js';
|
|
6
|
+
import initOAuth2DeviceCodeGranter, {
|
|
7
|
+
type WhookOAuth2DeviceAuthorizationCodeService,
|
|
8
|
+
} from './oAuth2DeviceAuthorizationGranter.js';
|
|
9
|
+
import { YError } from 'yerror';
|
|
10
|
+
|
|
11
|
+
describe('OAuth2DeviceCodeGranter', () => {
|
|
12
|
+
const OAUTH2: WhookOAuth2Options = {
|
|
13
|
+
authenticateURL: 'https://auth.example.com/sign_in',
|
|
14
|
+
allowedScopes: ['user', 'admin'],
|
|
15
|
+
rootClientId: 'root_app_id',
|
|
16
|
+
};
|
|
17
|
+
const oAuth2DeviceAuthorizationCode = {
|
|
18
|
+
check: jest.fn<WhookOAuth2DeviceAuthorizationCodeService['check']>(),
|
|
19
|
+
};
|
|
20
|
+
const readClientGrants = jest.fn<WhookOAuth2ReadClientGrantsService>();
|
|
21
|
+
const log = jest.fn();
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
oAuth2DeviceAuthorizationCode.check.mockReset();
|
|
25
|
+
readClientGrants.mockReset();
|
|
26
|
+
log.mockReset();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('should work with a valid request', async () => {
|
|
30
|
+
const oAuth2DeviceAuthorizationGranter = await initOAuth2DeviceCodeGranter({
|
|
31
|
+
OAUTH2,
|
|
32
|
+
readClientGrants,
|
|
33
|
+
oAuth2DeviceAuthorizationCode,
|
|
34
|
+
log,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
readClientGrants.mockResolvedValue({
|
|
38
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
39
|
+
allowedGrantTypes: [
|
|
40
|
+
'urn:ietf:params:oauth:grant-type:device_code',
|
|
41
|
+
'authorization_code',
|
|
42
|
+
],
|
|
43
|
+
allowedScopes: ['user', 'admin'],
|
|
44
|
+
authenticationData: {
|
|
45
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
46
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
47
|
+
scopes: ['user', 'admin'],
|
|
48
|
+
},
|
|
49
|
+
isPublicClient: false,
|
|
50
|
+
});
|
|
51
|
+
oAuth2DeviceAuthorizationCode.check.mockResolvedValueOnce({
|
|
52
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
53
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
54
|
+
scopes: ['user'],
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const authenticatorResult =
|
|
58
|
+
await oAuth2DeviceAuthorizationGranter.authenticate?.(
|
|
59
|
+
{
|
|
60
|
+
deviceCode: 'a_device_code',
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
64
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
65
|
+
scopes: ['user'],
|
|
66
|
+
},
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
expect({
|
|
70
|
+
authenticatorResult,
|
|
71
|
+
oAuth2DeviceAuthorizationCodeCheckCalls:
|
|
72
|
+
oAuth2DeviceAuthorizationCode.check.mock.calls,
|
|
73
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
74
|
+
logCalls: log.mock.calls,
|
|
75
|
+
}).toMatchInlineSnapshot(`
|
|
76
|
+
{
|
|
77
|
+
"authenticatorResult": {
|
|
78
|
+
"clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
|
|
79
|
+
"scopes": [
|
|
80
|
+
"user",
|
|
81
|
+
],
|
|
82
|
+
"userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
|
|
83
|
+
},
|
|
84
|
+
"logCalls": [
|
|
85
|
+
[
|
|
86
|
+
"debug",
|
|
87
|
+
"👫 - OAuth2DeviceAuthorizationGranter Service Initialized!",
|
|
88
|
+
],
|
|
89
|
+
],
|
|
90
|
+
"oAuth2DeviceAuthorizationCodeCheckCalls": [
|
|
91
|
+
[
|
|
92
|
+
{
|
|
93
|
+
"clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
|
|
94
|
+
"scopes": [
|
|
95
|
+
"user",
|
|
96
|
+
],
|
|
97
|
+
"userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
|
|
98
|
+
},
|
|
99
|
+
"a_device_code",
|
|
100
|
+
],
|
|
101
|
+
],
|
|
102
|
+
"readClientGrantsCalls": [
|
|
103
|
+
[
|
|
104
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
105
|
+
],
|
|
106
|
+
],
|
|
107
|
+
}
|
|
108
|
+
`);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test.each([undefined, false, true])(
|
|
112
|
+
'should enforce current client scopes with strictScopesChecks set to %p',
|
|
113
|
+
async (strictScopesChecks) => {
|
|
114
|
+
const oAuth2DeviceAuthorizationGranter = await initOAuth2DeviceCodeGranter({
|
|
115
|
+
OAUTH2: { ...OAUTH2, strictScopesChecks },
|
|
116
|
+
readClientGrants,
|
|
117
|
+
oAuth2DeviceAuthorizationCode,
|
|
118
|
+
log,
|
|
119
|
+
});
|
|
120
|
+
const storedAuthenticationData = {
|
|
121
|
+
clientId: 'client_app_id',
|
|
122
|
+
userId: 'user_id',
|
|
123
|
+
scopes: ['user', 'admin'],
|
|
124
|
+
};
|
|
125
|
+
readClientGrants.mockResolvedValue({
|
|
126
|
+
allowedRedirectURIS: [],
|
|
127
|
+
allowedGrantTypes: ['urn:ietf:params:oauth:grant-type:device_code'],
|
|
128
|
+
allowedScopes: ['user', 'new_scope'],
|
|
129
|
+
authenticationData: {
|
|
130
|
+
clientId: 'client_app_id',
|
|
131
|
+
userId: 'client_user_id',
|
|
132
|
+
scopes: ['user', 'new_scope'],
|
|
133
|
+
},
|
|
134
|
+
isPublicClient: true,
|
|
135
|
+
});
|
|
136
|
+
oAuth2DeviceAuthorizationCode.check.mockResolvedValueOnce(
|
|
137
|
+
storedAuthenticationData,
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
const result = oAuth2DeviceAuthorizationGranter.authenticate?.({
|
|
141
|
+
deviceCode: 'a_device_code',
|
|
142
|
+
clientId: 'client_app_id',
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
if (strictScopesChecks) {
|
|
146
|
+
await expect(result).rejects.toMatchObject({
|
|
147
|
+
code: 'E_OAUTH2_BAD_SCOPE',
|
|
148
|
+
debug: ['admin'],
|
|
149
|
+
});
|
|
150
|
+
} else {
|
|
151
|
+
await expect(result).resolves.toEqual({
|
|
152
|
+
...storedAuthenticationData,
|
|
153
|
+
scopes: ['user'],
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
expect(storedAuthenticationData.scopes).toEqual(['user', 'admin']);
|
|
157
|
+
},
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
test('should fail with a secret client and no authentication', async () => {
|
|
161
|
+
const oAuth2DeviceAuthorizationGranter = await initOAuth2DeviceCodeGranter({
|
|
162
|
+
OAUTH2,
|
|
163
|
+
readClientGrants,
|
|
164
|
+
oAuth2DeviceAuthorizationCode,
|
|
165
|
+
log,
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
readClientGrants.mockResolvedValue({
|
|
169
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
170
|
+
allowedGrantTypes: [
|
|
171
|
+
'urn:ietf:params:oauth:grant-type:device_code',
|
|
172
|
+
'authorization_code',
|
|
173
|
+
],
|
|
174
|
+
allowedScopes: ['user', 'admin'],
|
|
175
|
+
authenticationData: {
|
|
176
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
177
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
178
|
+
scopes: [],
|
|
179
|
+
},
|
|
180
|
+
isPublicClient: false,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
await oAuth2DeviceAuthorizationGranter.authenticate?.({
|
|
185
|
+
deviceCode: 'a_device_code',
|
|
186
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
187
|
+
});
|
|
188
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
189
|
+
} catch (err) {
|
|
190
|
+
expect({
|
|
191
|
+
err,
|
|
192
|
+
oAuth2DeviceAuthorizationCodeCheckCalls:
|
|
193
|
+
oAuth2DeviceAuthorizationCode.check.mock.calls,
|
|
194
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
195
|
+
logCalls: log.mock.calls,
|
|
196
|
+
}).toMatchInlineSnapshot(`
|
|
197
|
+
{
|
|
198
|
+
"err": [YError: E_OAUTH2_AUTHENTICATION_REQUIRED (["abbacaca-abba-caca-abba-cacaabbacaca"]): E_OAUTH2_AUTHENTICATION_REQUIRED],
|
|
199
|
+
"logCalls": [
|
|
200
|
+
[
|
|
201
|
+
"debug",
|
|
202
|
+
"👫 - OAuth2DeviceAuthorizationGranter Service Initialized!",
|
|
203
|
+
],
|
|
204
|
+
],
|
|
205
|
+
"oAuth2DeviceAuthorizationCodeCheckCalls": [],
|
|
206
|
+
"readClientGrantsCalls": [
|
|
207
|
+
[
|
|
208
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
209
|
+
],
|
|
210
|
+
],
|
|
211
|
+
}
|
|
212
|
+
`);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test('should fail with a client without device_code grant', async () => {
|
|
217
|
+
const oAuth2DeviceAuthorizationGranter = await initOAuth2DeviceCodeGranter({
|
|
218
|
+
OAUTH2,
|
|
219
|
+
readClientGrants,
|
|
220
|
+
oAuth2DeviceAuthorizationCode,
|
|
221
|
+
log,
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
readClientGrants.mockResolvedValue({
|
|
225
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
226
|
+
allowedGrantTypes: ['authorization_code'],
|
|
227
|
+
allowedScopes: ['user', 'admin'],
|
|
228
|
+
authenticationData: {
|
|
229
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
230
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
231
|
+
scopes: [],
|
|
232
|
+
},
|
|
233
|
+
isPublicClient: true,
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
try {
|
|
237
|
+
await oAuth2DeviceAuthorizationGranter.authenticate?.({
|
|
238
|
+
deviceCode: 'a_device_code',
|
|
239
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
240
|
+
});
|
|
241
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
242
|
+
} catch (err) {
|
|
243
|
+
expect({
|
|
244
|
+
err,
|
|
245
|
+
oAuth2DeviceAuthorizationCodeCheckCalls:
|
|
246
|
+
oAuth2DeviceAuthorizationCode.check.mock.calls,
|
|
247
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
248
|
+
logCalls: log.mock.calls,
|
|
249
|
+
}).toMatchInlineSnapshot(`
|
|
250
|
+
{
|
|
251
|
+
"err": [YError: E_OAUTH2_GRANT_TYPE_NOT_ALLOWED (["urn:ietf:params:oauth:grant-type:device_code",["authorization_code"]]): E_OAUTH2_GRANT_TYPE_NOT_ALLOWED],
|
|
252
|
+
"logCalls": [
|
|
253
|
+
[
|
|
254
|
+
"debug",
|
|
255
|
+
"👫 - OAuth2DeviceAuthorizationGranter Service Initialized!",
|
|
256
|
+
],
|
|
257
|
+
],
|
|
258
|
+
"oAuth2DeviceAuthorizationCodeCheckCalls": [],
|
|
259
|
+
"readClientGrantsCalls": [
|
|
260
|
+
[
|
|
261
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
262
|
+
],
|
|
263
|
+
],
|
|
264
|
+
}
|
|
265
|
+
`);
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
test('should translate denied device code errors', async () => {
|
|
270
|
+
const oAuth2DeviceAuthorizationGranter = await initOAuth2DeviceCodeGranter({
|
|
271
|
+
OAUTH2,
|
|
272
|
+
readClientGrants,
|
|
273
|
+
oAuth2DeviceAuthorizationCode,
|
|
274
|
+
log,
|
|
275
|
+
});
|
|
276
|
+
readClientGrants.mockResolvedValue({
|
|
277
|
+
allowedRedirectURIS: [],
|
|
278
|
+
allowedGrantTypes: ['urn:ietf:params:oauth:grant-type:device_code'],
|
|
279
|
+
allowedScopes: ['user'],
|
|
280
|
+
authenticationData: {
|
|
281
|
+
clientId: 'client_app_id',
|
|
282
|
+
userId: 'client_user_id',
|
|
283
|
+
scopes: ['user'],
|
|
284
|
+
},
|
|
285
|
+
isPublicClient: true,
|
|
286
|
+
});
|
|
287
|
+
oAuth2DeviceAuthorizationCode.check.mockRejectedValueOnce(
|
|
288
|
+
new YError('E_OAUTH2_ACCESS_DENIED', ['client_app_id']),
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
await expect(
|
|
292
|
+
oAuth2DeviceAuthorizationGranter.authenticate?.({
|
|
293
|
+
deviceCode: 'a_device_code',
|
|
294
|
+
clientId: 'client_app_id',
|
|
295
|
+
}),
|
|
296
|
+
).rejects.toMatchObject({
|
|
297
|
+
code: 'E_OAUTH2_DEVICE_ACCESS_DENIED',
|
|
298
|
+
debug: ['client_app_id'],
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test('should wrap bad device code errors', async () => {
|
|
303
|
+
const oAuth2DeviceAuthorizationGranter = await initOAuth2DeviceCodeGranter({
|
|
304
|
+
OAUTH2,
|
|
305
|
+
readClientGrants,
|
|
306
|
+
oAuth2DeviceAuthorizationCode,
|
|
307
|
+
log,
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
readClientGrants.mockResolvedValue({
|
|
311
|
+
allowedRedirectURIS: ['https://www.example.com'],
|
|
312
|
+
allowedGrantTypes: [
|
|
313
|
+
'urn:ietf:params:oauth:grant-type:device_code',
|
|
314
|
+
'authorization_code',
|
|
315
|
+
],
|
|
316
|
+
allowedScopes: ['user', 'admin'],
|
|
317
|
+
authenticationData: {
|
|
318
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
319
|
+
userId: 'abbacaca-abba-caca-abba-cacaabbab0b0',
|
|
320
|
+
scopes: ['user', 'admin'],
|
|
321
|
+
},
|
|
322
|
+
isPublicClient: true,
|
|
323
|
+
});
|
|
324
|
+
oAuth2DeviceAuthorizationCode.check.mockRejectedValueOnce(
|
|
325
|
+
new YError('E_BAD_TOKEN'),
|
|
326
|
+
);
|
|
327
|
+
|
|
328
|
+
try {
|
|
329
|
+
await oAuth2DeviceAuthorizationGranter.authenticate?.({
|
|
330
|
+
deviceCode: 'a_device_code',
|
|
331
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
332
|
+
});
|
|
333
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
334
|
+
} catch (err) {
|
|
335
|
+
expect({
|
|
336
|
+
err,
|
|
337
|
+
oAuth2DeviceAuthorizationCodeCheckCalls:
|
|
338
|
+
oAuth2DeviceAuthorizationCode.check.mock.calls,
|
|
339
|
+
readClientGrantsCalls: readClientGrants.mock.calls,
|
|
340
|
+
logCalls: log.mock.calls,
|
|
341
|
+
}).toMatchInlineSnapshot(`
|
|
342
|
+
{
|
|
343
|
+
"err": [YError: E_OAUTH2_BAD_DEVICE_CODE ([]): E_OAUTH2_BAD_DEVICE_CODE],
|
|
344
|
+
"logCalls": [
|
|
345
|
+
[
|
|
346
|
+
"debug",
|
|
347
|
+
"👫 - OAuth2DeviceAuthorizationGranter Service Initialized!",
|
|
348
|
+
],
|
|
349
|
+
],
|
|
350
|
+
"oAuth2DeviceAuthorizationCodeCheckCalls": [
|
|
351
|
+
[
|
|
352
|
+
{
|
|
353
|
+
"clientId": "abbacaca-abba-caca-abba-cacaabbacaca",
|
|
354
|
+
"scopes": [
|
|
355
|
+
"user",
|
|
356
|
+
"admin",
|
|
357
|
+
],
|
|
358
|
+
"userId": "abbacaca-abba-caca-abba-cacaabbab0b0",
|
|
359
|
+
},
|
|
360
|
+
"a_device_code",
|
|
361
|
+
],
|
|
362
|
+
],
|
|
363
|
+
"readClientGrantsCalls": [
|
|
364
|
+
[
|
|
365
|
+
"abbacaca-abba-caca-abba-cacaabbacaca",
|
|
366
|
+
],
|
|
367
|
+
],
|
|
368
|
+
}
|
|
369
|
+
`);
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
});
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { autoService, location } from 'knifecycle';
|
|
2
|
+
import { noop } from '@whook/whook';
|
|
3
|
+
import { pickYErrorWithCode, YError } from 'yerror';
|
|
4
|
+
import { type LogService } from 'common-services';
|
|
5
|
+
import {
|
|
6
|
+
type WhookOAuth2GranterService,
|
|
7
|
+
type WhookOAuth2ReadClientGrantsService,
|
|
8
|
+
type WhookOAuth2GranterDefinitions,
|
|
9
|
+
type WhookOAuth2ClientId,
|
|
10
|
+
type WhookOAuth2Options,
|
|
11
|
+
} from './oAuth2Granters.js';
|
|
12
|
+
import {
|
|
13
|
+
type WhookAuthenticationScope,
|
|
14
|
+
type WhookAuthenticationData,
|
|
15
|
+
} from '@whook/authorization';
|
|
16
|
+
import { checkGrantType } from '../libs/grants.js';
|
|
17
|
+
import { filterScopes } from '../libs/scopes.js';
|
|
18
|
+
import {
|
|
19
|
+
checkClientsIds,
|
|
20
|
+
checkGrants,
|
|
21
|
+
toUsableClientId,
|
|
22
|
+
} from '../libs/clients.js';
|
|
23
|
+
import { deviceAuthorizationTokenRequestBodySchema } from '../routes/schemas.js';
|
|
24
|
+
|
|
25
|
+
export const DEVICE_AUTHORIZATION_GRANT_TYPE =
|
|
26
|
+
deviceAuthorizationTokenRequestBodySchema.schema.properties.grant_type.const;
|
|
27
|
+
export const DEFAULT_DEVICE_AUTHORIZATION_INTERVAL = 5;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A type allowing to override the device code type
|
|
31
|
+
*/
|
|
32
|
+
export type WhookOAuth2DeviceCode = string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A type allowing to override the user code type
|
|
36
|
+
*/
|
|
37
|
+
export type WhookOAuth2UserCode = string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A service to create and check device codes
|
|
41
|
+
*/
|
|
42
|
+
export interface WhookOAuth2DeviceAuthorizationCodeService {
|
|
43
|
+
create: (
|
|
44
|
+
clientAuthenticationData: WhookAuthenticationData,
|
|
45
|
+
context: {
|
|
46
|
+
demandedScopes: WhookAuthenticationScope[];
|
|
47
|
+
filteredScopes: WhookAuthenticationScope[];
|
|
48
|
+
},
|
|
49
|
+
) => Promise<{
|
|
50
|
+
deviceCode: WhookOAuth2DeviceCode;
|
|
51
|
+
userCode: WhookOAuth2UserCode;
|
|
52
|
+
expiresAt: number;
|
|
53
|
+
}>;
|
|
54
|
+
/**
|
|
55
|
+
* Must reject unless expectedClientId exactly matches the stored challenge's
|
|
56
|
+
* client ID, before recording approval, denial, or acknowledged scopes.
|
|
57
|
+
*/
|
|
58
|
+
acknowledge: (
|
|
59
|
+
acknowledged: boolean,
|
|
60
|
+
userAuthenticationData: WhookAuthenticationData,
|
|
61
|
+
userCode: WhookOAuth2UserCode,
|
|
62
|
+
expectedClientId: WhookOAuth2ClientId,
|
|
63
|
+
acknowledgedScopes: WhookAuthenticationScope[],
|
|
64
|
+
) => Promise<void>;
|
|
65
|
+
check: (
|
|
66
|
+
clientAuthenticationData: WhookAuthenticationData,
|
|
67
|
+
deviceCode: WhookOAuth2DeviceCode,
|
|
68
|
+
) => Promise<WhookAuthenticationData>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface WhookOAuth2DeviceAuthorizationOptions {
|
|
72
|
+
/** The interval the device may poll for a token */
|
|
73
|
+
interval?: number;
|
|
74
|
+
/** The user code verification interface URI */
|
|
75
|
+
verificationURI: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface WhookOAuth2DeviceAuthorizationGranterDependencies {
|
|
79
|
+
OAUTH2: WhookOAuth2Options;
|
|
80
|
+
readClientGrants: WhookOAuth2ReadClientGrantsService;
|
|
81
|
+
oAuth2DeviceAuthorizationCode: Pick<
|
|
82
|
+
WhookOAuth2DeviceAuthorizationCodeService,
|
|
83
|
+
'check'
|
|
84
|
+
>;
|
|
85
|
+
log?: LogService;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface WhookOAuth2DeviceAuthorizationGranterDefinitions extends WhookOAuth2GranterDefinitions {
|
|
89
|
+
grantType: typeof DEVICE_AUTHORIZATION_GRANT_TYPE;
|
|
90
|
+
authenticateParameters: {
|
|
91
|
+
deviceCode: WhookOAuth2DeviceCode;
|
|
92
|
+
clientId?: WhookOAuth2ClientId;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export type WhookOAuth2DeviceAuthorizationGranterService =
|
|
97
|
+
WhookOAuth2GranterService<WhookOAuth2DeviceAuthorizationGranterDefinitions>;
|
|
98
|
+
|
|
99
|
+
// Device Code Grant
|
|
100
|
+
// https://datatracker.ietf.org/doc/html/rfc8628#section-3.4
|
|
101
|
+
async function initOAuth2DeviceAuthorizationGranter({
|
|
102
|
+
OAUTH2,
|
|
103
|
+
readClientGrants,
|
|
104
|
+
oAuth2DeviceAuthorizationCode,
|
|
105
|
+
log = noop,
|
|
106
|
+
}: WhookOAuth2DeviceAuthorizationGranterDependencies): Promise<WhookOAuth2DeviceAuthorizationGranterService> {
|
|
107
|
+
const authenticateWithDeviceCode: NonNullable<
|
|
108
|
+
WhookOAuth2DeviceAuthorizationGranterService['authenticate']
|
|
109
|
+
> = async ({ deviceCode, clientId }, optionalAuthenticationData) => {
|
|
110
|
+
const usableClientId = toUsableClientId([
|
|
111
|
+
optionalAuthenticationData?.clientId,
|
|
112
|
+
clientId,
|
|
113
|
+
]);
|
|
114
|
+
const grants = await readClientGrants(usableClientId);
|
|
115
|
+
|
|
116
|
+
checkGrants(usableClientId, grants);
|
|
117
|
+
|
|
118
|
+
if (!grants.isPublicClient) {
|
|
119
|
+
if (!optionalAuthenticationData) {
|
|
120
|
+
throw new YError('E_OAUTH2_AUTHENTICATION_REQUIRED', [
|
|
121
|
+
grants.authenticationData.clientId,
|
|
122
|
+
]);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
checkGrantType(grants.allowedGrantTypes, DEVICE_AUTHORIZATION_GRANT_TYPE);
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
const authorizationCodeAuthenticationData =
|
|
130
|
+
await oAuth2DeviceAuthorizationCode.check(
|
|
131
|
+
optionalAuthenticationData || grants.authenticationData,
|
|
132
|
+
deviceCode,
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
if (!authorizationCodeAuthenticationData.clientId) {
|
|
136
|
+
throw new YError('E_OAUTH2_UNEXPECTED_EMPTY_CLIENT_ID', []);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
checkClientsIds(usableClientId, [
|
|
140
|
+
authorizationCodeAuthenticationData.clientId,
|
|
141
|
+
]);
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
...authorizationCodeAuthenticationData,
|
|
145
|
+
scopes: filterScopes(
|
|
146
|
+
filterScopes(
|
|
147
|
+
authorizationCodeAuthenticationData.scopes,
|
|
148
|
+
OAUTH2.allowedScopes,
|
|
149
|
+
!!OAUTH2.strictScopesChecks,
|
|
150
|
+
),
|
|
151
|
+
grants.allowedScopes,
|
|
152
|
+
!!OAUTH2.strictScopesChecks,
|
|
153
|
+
),
|
|
154
|
+
};
|
|
155
|
+
} catch (err) {
|
|
156
|
+
const deniedError = pickYErrorWithCode(
|
|
157
|
+
err as Error,
|
|
158
|
+
'E_OAUTH2_ACCESS_DENIED',
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
if (deniedError) {
|
|
162
|
+
throw YError.wrap(deniedError, 'E_OAUTH2_DEVICE_ACCESS_DENIED', [
|
|
163
|
+
usableClientId,
|
|
164
|
+
]);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const castedErr = pickYErrorWithCode(err as Error, 'E_BAD_TOKEN');
|
|
168
|
+
|
|
169
|
+
if (castedErr) {
|
|
170
|
+
throw YError.wrap(castedErr, 'E_OAUTH2_BAD_DEVICE_CODE');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const castedErr2 = pickYErrorWithCode(err as Error, 'E_EXPIRED_TOKEN');
|
|
174
|
+
|
|
175
|
+
if (castedErr2) {
|
|
176
|
+
throw YError.wrap(castedErr2, 'E_OAUTH2_EXPIRED_DEVICE_CODE');
|
|
177
|
+
}
|
|
178
|
+
throw err;
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
log('debug', '👫 - OAuth2DeviceAuthorizationGranter Service Initialized!');
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
grantType: DEVICE_AUTHORIZATION_GRANT_TYPE,
|
|
186
|
+
issuesRefreshToken: true,
|
|
187
|
+
authenticate: authenticateWithDeviceCode,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export default location(
|
|
192
|
+
autoService(initOAuth2DeviceAuthorizationGranter),
|
|
193
|
+
import.meta.url,
|
|
194
|
+
);
|
|
@@ -133,9 +133,7 @@ export default location(
|
|
|
133
133
|
inject: [
|
|
134
134
|
'oAuth2ClientCredentialsGranter',
|
|
135
135
|
'oAuth2AuthorizationCodeGranter',
|
|
136
|
-
'oAuth2PasswordGranter',
|
|
137
136
|
'oAuth2RefreshTokenGranter',
|
|
138
|
-
'oAuth2ImplicitGranter',
|
|
139
137
|
],
|
|
140
138
|
},
|
|
141
139
|
async (services: Dependencies) =>
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { autoService, location } from 'knifecycle';
|
|
2
|
-
import { noop
|
|
2
|
+
import { noop } from '@whook/whook';
|
|
3
3
|
import { YError } from 'yerror';
|
|
4
4
|
import { type LogService } from 'common-services';
|
|
5
5
|
import {
|
|
@@ -14,32 +14,11 @@ import {
|
|
|
14
14
|
} from '@whook/authorization';
|
|
15
15
|
import { filterScopes } from '../libs/scopes.js';
|
|
16
16
|
import { checkGrantType } from '../libs/grants.js';
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
name: 'PasswordRequestBody',
|
|
23
|
-
schema: {
|
|
24
|
-
type: 'object',
|
|
25
|
-
description:
|
|
26
|
-
'Resource owner password credentials grant, see https://tools.ietf.org/html/rfc6749#section-4.3',
|
|
27
|
-
required: ['grant_type', 'username', 'password'],
|
|
28
|
-
properties: {
|
|
29
|
-
grant_type: {
|
|
30
|
-
type: 'string',
|
|
31
|
-
enum: [PASSWORD_GRANT_TYPE],
|
|
32
|
-
},
|
|
33
|
-
username: {
|
|
34
|
-
type: 'string',
|
|
35
|
-
},
|
|
36
|
-
password: {
|
|
37
|
-
type: 'string',
|
|
38
|
-
},
|
|
39
|
-
scope: refersTo(scopeSchema),
|
|
40
|
-
},
|
|
41
|
-
},
|
|
42
|
-
} as const satisfies WhookAPISchemaDefinition;
|
|
17
|
+
import { checkGrants } from '../libs/clients.js';
|
|
18
|
+
import { passwordTokenRequestBodySchema } from '../routes/schemas.js';
|
|
19
|
+
|
|
20
|
+
export const PASSWORD_GRANT_TYPE =
|
|
21
|
+
passwordTokenRequestBodySchema.schema.properties.grant_type.const;
|
|
43
22
|
|
|
44
23
|
/**
|
|
45
24
|
* A service to check a user password
|
|
@@ -103,12 +82,7 @@ async function initOAuth2PasswordGranter({
|
|
|
103
82
|
|
|
104
83
|
const grants = await readClientGrants(usableClientId);
|
|
105
84
|
|
|
106
|
-
|
|
107
|
-
throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
|
|
108
|
-
usableClientId,
|
|
109
|
-
grants.authenticationData.clientId,
|
|
110
|
-
]);
|
|
111
|
-
}
|
|
85
|
+
checkGrants(usableClientId, grants);
|
|
112
86
|
|
|
113
87
|
if (!grants.isPublicClient) {
|
|
114
88
|
if (!optionalAuthenticationData) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { autoService, location } from 'knifecycle';
|
|
2
|
-
import { noop
|
|
2
|
+
import { noop } from '@whook/whook';
|
|
3
3
|
import { pickYErrorWithCode, YError } from 'yerror';
|
|
4
4
|
import { type LogService } from 'common-services';
|
|
5
5
|
import {
|
|
@@ -15,30 +15,10 @@ import {
|
|
|
15
15
|
import { checkGrantType } from '../libs/grants.js';
|
|
16
16
|
import { filterScopes } from '../libs/scopes.js';
|
|
17
17
|
import { type WhookOAuth2AccessTokenService } from './oAuth2ImplicitGranter.js';
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
export const REFRESH_TOKEN_GRANT_TYPE =
|
|
22
|
-
|
|
23
|
-
export const refreshTokenRequestBodySchema = {
|
|
24
|
-
name: 'RefreshTokenRequestBody',
|
|
25
|
-
schema: {
|
|
26
|
-
type: 'object',
|
|
27
|
-
description:
|
|
28
|
-
'Token refresh grant type, see https://tools.ietf.org/html/rfc6749#section-6 .',
|
|
29
|
-
required: ['grant_type', 'refresh_token'],
|
|
30
|
-
properties: {
|
|
31
|
-
grant_type: {
|
|
32
|
-
type: 'string',
|
|
33
|
-
enum: [REFRESH_TOKEN_GRANT_TYPE],
|
|
34
|
-
},
|
|
35
|
-
refresh_token: {
|
|
36
|
-
type: 'string',
|
|
37
|
-
},
|
|
38
|
-
scope: refersTo(scopeSchema),
|
|
39
|
-
},
|
|
40
|
-
},
|
|
41
|
-
} as const satisfies WhookAPISchemaDefinition;
|
|
18
|
+
import { checkGrants, toUsableClientId } from '../libs/clients.js';
|
|
19
|
+
import { refreshTokenRequestBodySchema } from '../routes/schemas.js';
|
|
20
|
+
|
|
21
|
+
export const REFRESH_TOKEN_GRANT_TYPE = refreshTokenRequestBodySchema.schema.properties.grant_type.const;
|
|
42
22
|
|
|
43
23
|
/**
|
|
44
24
|
* A type allowing to override the refresh token type
|
|
@@ -108,12 +88,7 @@ async function initOAuth2RefreshTokenGranter({
|
|
|
108
88
|
}
|
|
109
89
|
}
|
|
110
90
|
|
|
111
|
-
|
|
112
|
-
throw new YError('E_OAUTH2_CLIENT_GRANTS_MISMATCH', [
|
|
113
|
-
usableClientId,
|
|
114
|
-
grants.authenticationData.clientId,
|
|
115
|
-
]);
|
|
116
|
-
}
|
|
91
|
+
checkGrants(usableClientId, grants);
|
|
117
92
|
|
|
118
93
|
checkGrantType(grants.allowedGrantTypes, REFRESH_TOKEN_GRANT_TYPE);
|
|
119
94
|
|