@whook/oauth2 26.0.0 → 26.0.3
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/dist/index.d.ts +6 -2
- package/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +421 -2
- package/dist/index.test.js.map +1 -1
- package/dist/libs/authorizationRequests.d.ts +6 -0
- package/dist/libs/authorizationRequests.js +22 -0
- package/dist/libs/authorizationRequests.js.map +1 -0
- package/dist/libs/authorizationRequests.test.d.ts +1 -0
- package/dist/libs/authorizationRequests.test.js +24 -0
- package/dist/libs/authorizationRequests.test.js.map +1 -0
- package/dist/libs/errors.d.ts +31 -1
- package/dist/libs/errors.js +49 -0
- package/dist/libs/errors.js.map +1 -1
- package/dist/libs/redirectURI.d.ts +1 -0
- package/dist/libs/redirectURI.js +16 -0
- package/dist/libs/redirectURI.js.map +1 -1
- package/dist/libs/schemas.d.ts +8 -0
- package/dist/libs/schemas.js +8 -0
- package/dist/libs/schemas.js.map +1 -1
- package/dist/libs/verifier.d.ts +3 -0
- package/dist/libs/verifier.js +14 -0
- package/dist/libs/verifier.js.map +1 -1
- package/dist/routes/getOAuth2Authorize.d.ts +30 -14
- package/dist/routes/getOAuth2Authorize.js +92 -30
- package/dist/routes/getOAuth2Authorize.js.map +1 -1
- package/dist/routes/getOAuth2Authorize.test.js +826 -19
- package/dist/routes/getOAuth2Authorize.test.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.d.ts +10 -3
- package/dist/routes/getOAuth2WellKnownMetadata.js +30 -5
- package/dist/routes/getOAuth2WellKnownMetadata.js.map +1 -1
- package/dist/routes/getOAuth2WellKnownMetadata.test.js +20 -18
- package/dist/routes/getOAuth2WellKnownMetadata.test.js.map +1 -1
- package/dist/routes/postOAuth2PushedAuthorizationRequest.d.ts +137 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.js +187 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.js.map +1 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.test.d.ts +1 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js +554 -0
- package/dist/routes/postOAuth2PushedAuthorizationRequest.test.js.map +1 -0
- package/dist/routes/postOAuth2Token.d.ts +12 -0
- package/dist/routes/postOAuth2Token.js +12 -0
- package/dist/routes/postOAuth2Token.js.map +1 -1
- package/dist/services/authCookies.js +8 -4
- package/dist/services/authCookies.js.map +1 -1
- package/dist/services/oAuth2AuthorizationCodeGranter.d.ts +2 -7
- package/dist/services/oAuth2AuthorizationCodeGranter.js +5 -22
- package/dist/services/oAuth2AuthorizationCodeGranter.js.map +1 -1
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js +12 -141
- package/dist/services/oAuth2AuthorizationCodeGranter.test.js.map +1 -1
- package/dist/services/oAuth2AuthorizationRequests.d.ts +41 -0
- package/dist/services/oAuth2AuthorizationRequests.js +61 -0
- package/dist/services/oAuth2AuthorizationRequests.js.map +1 -0
- package/dist/services/oAuth2AuthorizationRequests.test.d.ts +1 -0
- package/dist/services/oAuth2AuthorizationRequests.test.js +249 -0
- package/dist/services/oAuth2AuthorizationRequests.test.js.map +1 -0
- package/dist/services/oAuth2Granters.d.ts +10 -12
- package/dist/services/oAuth2Granters.js.map +1 -1
- package/dist/services/oAuth2ImplicitGranter.js +3 -14
- package/dist/services/oAuth2ImplicitGranter.js.map +1 -1
- package/dist/services/oAuth2ImplicitGranter.test.js +25 -163
- package/dist/services/oAuth2ImplicitGranter.test.js.map +1 -1
- package/package.json +7 -6
- package/src/index.test.ts +459 -3
- package/src/index.ts +17 -0
- package/src/libs/authorizationRequests.test.ts +40 -0
- package/src/libs/authorizationRequests.ts +42 -0
- package/src/libs/errors.ts +87 -1
- package/src/libs/redirectURI.ts +22 -0
- package/src/libs/schemas.ts +10 -0
- package/src/libs/verifier.ts +18 -0
- package/src/routes/getOAuth2Authorize.test.ts +940 -23
- package/src/routes/getOAuth2Authorize.ts +168 -49
- package/src/routes/getOAuth2WellKnownMetadata.test.ts +22 -20
- package/src/routes/getOAuth2WellKnownMetadata.ts +44 -8
- package/src/routes/postOAuth2PushedAuthorizationRequest.test.ts +681 -0
- package/src/routes/postOAuth2PushedAuthorizationRequest.ts +292 -0
- package/src/routes/postOAuth2Token.ts +12 -0
- package/src/services/authCookies.ts +8 -4
- package/src/services/oAuth2AuthorizationCodeGranter.test.ts +12 -169
- package/src/services/oAuth2AuthorizationCodeGranter.ts +8 -31
- package/src/services/oAuth2AuthorizationRequests.test.ts +282 -0
- package/src/services/oAuth2AuthorizationRequests.ts +141 -0
- package/src/services/oAuth2Granters.ts +12 -14
- package/src/services/oAuth2ImplicitGranter.test.ts +29 -190
- package/src/services/oAuth2ImplicitGranter.ts +3 -19
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { describe, test, beforeEach, jest, expect } from '@jest/globals';
|
|
2
|
+
import { type KVStoreService } from 'memory-kv-store';
|
|
3
|
+
import { YError } from 'yerror';
|
|
4
|
+
import initOAuth2AuthorizationRequests, {
|
|
5
|
+
type WhookOAuth2AuthorizationRequestData,
|
|
6
|
+
} from './oAuth2AuthorizationRequests.js';
|
|
7
|
+
import { RandomBytesService } from 'common-services';
|
|
8
|
+
|
|
9
|
+
describe('OAuth2AuthorizationRequests', () => {
|
|
10
|
+
const oAuth2AuthorizationRequestsStore = {
|
|
11
|
+
getDelete:
|
|
12
|
+
jest.fn<
|
|
13
|
+
KVStoreService<WhookOAuth2AuthorizationRequestData>['getDelete']
|
|
14
|
+
>(),
|
|
15
|
+
set: jest.fn<KVStoreService<WhookOAuth2AuthorizationRequestData>['set']>(),
|
|
16
|
+
};
|
|
17
|
+
const randomBytes = jest.fn<RandomBytesService>();
|
|
18
|
+
const time = jest.fn<() => number>();
|
|
19
|
+
const log = jest.fn();
|
|
20
|
+
|
|
21
|
+
const CLIENT_ID = 'a_client_id';
|
|
22
|
+
const REQUEST_ID = 'a-request-id-1-2-3';
|
|
23
|
+
const REQUEST_URI =
|
|
24
|
+
'urn:ietf:params:oauth:request_uri:arequestid123' as const;
|
|
25
|
+
const PARAMETERS = {
|
|
26
|
+
response_type: 'code',
|
|
27
|
+
client_id: CLIENT_ID,
|
|
28
|
+
redirect_uri: 'https://www.example.com/callback',
|
|
29
|
+
scope: 'user admin',
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
oAuth2AuthorizationRequestsStore.getDelete.mockReset();
|
|
34
|
+
oAuth2AuthorizationRequestsStore.set.mockReset();
|
|
35
|
+
randomBytes.mockReset();
|
|
36
|
+
time.mockReset();
|
|
37
|
+
log.mockReset();
|
|
38
|
+
randomBytes.mockResolvedValue(Buffer.from(REQUEST_ID));
|
|
39
|
+
time.mockReturnValue(1000);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('should create an authorization request with default TTL', async () => {
|
|
43
|
+
const oAuth2AuthorizationRequests = await initOAuth2AuthorizationRequests({
|
|
44
|
+
oAuth2AuthorizationRequestsStore,
|
|
45
|
+
randomBytes,
|
|
46
|
+
time,
|
|
47
|
+
log,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const result = await oAuth2AuthorizationRequests.create(
|
|
51
|
+
CLIENT_ID,
|
|
52
|
+
PARAMETERS,
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
expect({
|
|
56
|
+
result,
|
|
57
|
+
storeSetCalls: oAuth2AuthorizationRequestsStore.set.mock.calls,
|
|
58
|
+
randomBytesCalls: randomBytes.mock.calls,
|
|
59
|
+
logCalls: log.mock.calls,
|
|
60
|
+
}).toMatchInlineSnapshot(`
|
|
61
|
+
{
|
|
62
|
+
"logCalls": [
|
|
63
|
+
[
|
|
64
|
+
"debug",
|
|
65
|
+
"👫 - OAuth2AuthorizationRequests Service Initialized!",
|
|
66
|
+
],
|
|
67
|
+
],
|
|
68
|
+
"randomBytesCalls": [
|
|
69
|
+
[
|
|
70
|
+
16,
|
|
71
|
+
],
|
|
72
|
+
],
|
|
73
|
+
"result": {
|
|
74
|
+
"expiresIn": 90000,
|
|
75
|
+
"requestURI": "urn:ietf:params:oauth:request_uri:YS1yZXF1ZXN0LWlkLTEtMi0z",
|
|
76
|
+
},
|
|
77
|
+
"storeSetCalls": [
|
|
78
|
+
[
|
|
79
|
+
"YS1yZXF1ZXN0LWlkLTEtMi0z",
|
|
80
|
+
{
|
|
81
|
+
"clientId": "a_client_id",
|
|
82
|
+
"expiresAt": 91000,
|
|
83
|
+
"parameters": {
|
|
84
|
+
"client_id": "a_client_id",
|
|
85
|
+
"redirect_uri": "https://www.example.com/callback",
|
|
86
|
+
"response_type": "code",
|
|
87
|
+
"scope": "user admin",
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
90000,
|
|
91
|
+
],
|
|
92
|
+
],
|
|
93
|
+
}
|
|
94
|
+
`);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('should check and consume a valid authorization request', async () => {
|
|
98
|
+
const oAuth2AuthorizationRequests = await initOAuth2AuthorizationRequests({
|
|
99
|
+
OAUTH2_PAR: {
|
|
100
|
+
mode: 'required',
|
|
101
|
+
ttl: 60000,
|
|
102
|
+
},
|
|
103
|
+
oAuth2AuthorizationRequestsStore,
|
|
104
|
+
randomBytes,
|
|
105
|
+
time,
|
|
106
|
+
log,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
oAuth2AuthorizationRequestsStore.getDelete.mockResolvedValue({
|
|
110
|
+
clientId: CLIENT_ID,
|
|
111
|
+
parameters: PARAMETERS,
|
|
112
|
+
expiresAt: 61000,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const requestData = await oAuth2AuthorizationRequests.check(
|
|
116
|
+
CLIENT_ID,
|
|
117
|
+
REQUEST_URI,
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
expect({
|
|
121
|
+
requestData,
|
|
122
|
+
storeGetDeleteCalls:
|
|
123
|
+
oAuth2AuthorizationRequestsStore.getDelete.mock.calls,
|
|
124
|
+
randomBytesCalls: randomBytes.mock.calls,
|
|
125
|
+
logCalls: log.mock.calls,
|
|
126
|
+
}).toMatchInlineSnapshot(`
|
|
127
|
+
{
|
|
128
|
+
"logCalls": [
|
|
129
|
+
[
|
|
130
|
+
"debug",
|
|
131
|
+
"👫 - OAuth2AuthorizationRequests Service Initialized!",
|
|
132
|
+
],
|
|
133
|
+
],
|
|
134
|
+
"randomBytesCalls": [],
|
|
135
|
+
"requestData": {
|
|
136
|
+
"clientId": "a_client_id",
|
|
137
|
+
"expiresAt": 61000,
|
|
138
|
+
"parameters": {
|
|
139
|
+
"client_id": "a_client_id",
|
|
140
|
+
"redirect_uri": "https://www.example.com/callback",
|
|
141
|
+
"response_type": "code",
|
|
142
|
+
"scope": "user admin",
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
"storeGetDeleteCalls": [
|
|
146
|
+
[
|
|
147
|
+
"arequestid123",
|
|
148
|
+
],
|
|
149
|
+
],
|
|
150
|
+
}
|
|
151
|
+
`);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test('should fail when checking a non-existing request URI', async () => {
|
|
155
|
+
const oAuth2AuthorizationRequests = await initOAuth2AuthorizationRequests({
|
|
156
|
+
oAuth2AuthorizationRequestsStore,
|
|
157
|
+
randomBytes,
|
|
158
|
+
time,
|
|
159
|
+
log,
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
oAuth2AuthorizationRequestsStore.getDelete.mockResolvedValue(undefined);
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
await oAuth2AuthorizationRequests.check(CLIENT_ID, REQUEST_URI);
|
|
166
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
167
|
+
} catch (err) {
|
|
168
|
+
expect({
|
|
169
|
+
err,
|
|
170
|
+
storeGetDeleteCalls:
|
|
171
|
+
oAuth2AuthorizationRequestsStore.getDelete.mock.calls,
|
|
172
|
+
randomBytesCalls: randomBytes.mock.calls,
|
|
173
|
+
logCalls: log.mock.calls,
|
|
174
|
+
}).toMatchInlineSnapshot(`
|
|
175
|
+
{
|
|
176
|
+
"err": [YError: E_OAUTH2_BAD_REQUEST_URI (["urn:ietf:params:oauth:request_uri:arequestid123",null,null]): E_OAUTH2_BAD_REQUEST_URI],
|
|
177
|
+
"logCalls": [
|
|
178
|
+
[
|
|
179
|
+
"debug",
|
|
180
|
+
"👫 - OAuth2AuthorizationRequests Service Initialized!",
|
|
181
|
+
],
|
|
182
|
+
],
|
|
183
|
+
"randomBytesCalls": [],
|
|
184
|
+
"storeGetDeleteCalls": [
|
|
185
|
+
[
|
|
186
|
+
"arequestid123",
|
|
187
|
+
],
|
|
188
|
+
],
|
|
189
|
+
}
|
|
190
|
+
`);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test('should fail and delete request when expired', async () => {
|
|
195
|
+
const oAuth2AuthorizationRequests = await initOAuth2AuthorizationRequests({
|
|
196
|
+
oAuth2AuthorizationRequestsStore,
|
|
197
|
+
randomBytes,
|
|
198
|
+
time,
|
|
199
|
+
log,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// time() vaut 1000, expiresAt est à 500 (déjà expiré)
|
|
203
|
+
oAuth2AuthorizationRequestsStore.getDelete.mockResolvedValue({
|
|
204
|
+
clientId: CLIENT_ID,
|
|
205
|
+
parameters: PARAMETERS,
|
|
206
|
+
expiresAt: 500,
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
try {
|
|
210
|
+
await oAuth2AuthorizationRequests.check(CLIENT_ID, REQUEST_URI);
|
|
211
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
212
|
+
} catch (err) {
|
|
213
|
+
expect({
|
|
214
|
+
err,
|
|
215
|
+
storeGetDeleteCalls:
|
|
216
|
+
oAuth2AuthorizationRequestsStore.getDelete.mock.calls,
|
|
217
|
+
randomBytesCalls: randomBytes.mock.calls,
|
|
218
|
+
logCalls: log.mock.calls,
|
|
219
|
+
}).toMatchInlineSnapshot(`
|
|
220
|
+
{
|
|
221
|
+
"err": [YError: E_OAUTH2_BAD_REQUEST_URI (["urn:ietf:params:oauth:request_uri:arequestid123",500,1000]): E_OAUTH2_BAD_REQUEST_URI],
|
|
222
|
+
"logCalls": [
|
|
223
|
+
[
|
|
224
|
+
"debug",
|
|
225
|
+
"👫 - OAuth2AuthorizationRequests Service Initialized!",
|
|
226
|
+
],
|
|
227
|
+
],
|
|
228
|
+
"randomBytesCalls": [],
|
|
229
|
+
"storeGetDeleteCalls": [
|
|
230
|
+
[
|
|
231
|
+
"arequestid123",
|
|
232
|
+
],
|
|
233
|
+
],
|
|
234
|
+
}
|
|
235
|
+
`);
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test('should fail when client ID does not match request client ID', async () => {
|
|
240
|
+
const oAuth2AuthorizationRequests = await initOAuth2AuthorizationRequests({
|
|
241
|
+
oAuth2AuthorizationRequestsStore,
|
|
242
|
+
randomBytes,
|
|
243
|
+
time,
|
|
244
|
+
log,
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
oAuth2AuthorizationRequestsStore.getDelete.mockResolvedValue({
|
|
248
|
+
clientId: 'other_client_id',
|
|
249
|
+
parameters: PARAMETERS,
|
|
250
|
+
expiresAt: 2000,
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
try {
|
|
254
|
+
await oAuth2AuthorizationRequests.check(CLIENT_ID, REQUEST_URI);
|
|
255
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
256
|
+
} catch (err) {
|
|
257
|
+
expect({
|
|
258
|
+
err,
|
|
259
|
+
storeGetDeleteCalls:
|
|
260
|
+
oAuth2AuthorizationRequestsStore.getDelete.mock.calls,
|
|
261
|
+
randomBytesCalls: randomBytes.mock.calls,
|
|
262
|
+
logCalls: log.mock.calls,
|
|
263
|
+
}).toMatchInlineSnapshot(`
|
|
264
|
+
{
|
|
265
|
+
"err": [YError: E_OAUTH2_CLIENT_MISMATCH (["a_client_id","other_client_id"]): E_OAUTH2_CLIENT_MISMATCH],
|
|
266
|
+
"logCalls": [
|
|
267
|
+
[
|
|
268
|
+
"debug",
|
|
269
|
+
"👫 - OAuth2AuthorizationRequests Service Initialized!",
|
|
270
|
+
],
|
|
271
|
+
],
|
|
272
|
+
"randomBytesCalls": [],
|
|
273
|
+
"storeGetDeleteCalls": [
|
|
274
|
+
[
|
|
275
|
+
"arequestid123",
|
|
276
|
+
],
|
|
277
|
+
],
|
|
278
|
+
}
|
|
279
|
+
`);
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { autoService, location } from 'knifecycle';
|
|
2
|
+
import { noop } from '@whook/whook';
|
|
3
|
+
import {
|
|
4
|
+
type RandomBytesService,
|
|
5
|
+
type LogService,
|
|
6
|
+
type TimeService,
|
|
7
|
+
} from 'common-services';
|
|
8
|
+
import { type KVStoreService } from 'memory-kv-store';
|
|
9
|
+
import { type WhookOAuth2ClientId } from './oAuth2Granters.js';
|
|
10
|
+
import { YError } from 'yerror';
|
|
11
|
+
import {
|
|
12
|
+
buildRequestURI,
|
|
13
|
+
readRequestURI,
|
|
14
|
+
type WhookOAuth2AuthorizationRequestURI,
|
|
15
|
+
} from '../libs/authorizationRequests.js';
|
|
16
|
+
import { type WhookOAuth2AuthorizeRequestParameters } from '../routes/getOAuth2Authorize.js';
|
|
17
|
+
|
|
18
|
+
export interface WhookOAuth2AuthorizationRequestData {
|
|
19
|
+
clientId: WhookOAuth2ClientId;
|
|
20
|
+
parameters: WhookOAuth2AuthorizeRequestParameters;
|
|
21
|
+
expiresAt: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface WhookOAuth2AuthorizationRequestsOptions {
|
|
25
|
+
/** Whether PAR is activated and required */
|
|
26
|
+
mode: 'enabled' | 'required' | 'disabled';
|
|
27
|
+
/** Time to live in milliseconds */
|
|
28
|
+
ttl?: number;
|
|
29
|
+
}
|
|
30
|
+
export const DEFAULT_OAUTH2_PAR = {
|
|
31
|
+
mode: 'disabled',
|
|
32
|
+
ttl: 90 * 1000,
|
|
33
|
+
} as const satisfies WhookOAuth2AuthorizationRequestsOptions;
|
|
34
|
+
|
|
35
|
+
export interface WhookOAuth2AuthorizationRequestsConfig {
|
|
36
|
+
OAUTH2_PAR?: WhookOAuth2AuthorizationRequestsOptions;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface WhookOAuth2AuthorizationRequestsDependencies extends WhookOAuth2AuthorizationRequestsConfig {
|
|
40
|
+
oAuth2AuthorizationRequestsStore: Pick<
|
|
41
|
+
KVStoreService<WhookOAuth2AuthorizationRequestData>,
|
|
42
|
+
'set' | 'getDelete'
|
|
43
|
+
>;
|
|
44
|
+
randomBytes: RandomBytesService;
|
|
45
|
+
time?: TimeService;
|
|
46
|
+
log?: LogService;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A service to create and check authorization requests
|
|
51
|
+
*/
|
|
52
|
+
export interface WhookOAuth2AuthorizationRequestsService {
|
|
53
|
+
create: (
|
|
54
|
+
clientId: WhookOAuth2ClientId,
|
|
55
|
+
parameters: WhookOAuth2AuthorizeRequestParameters,
|
|
56
|
+
) => Promise<{
|
|
57
|
+
requestURI: WhookOAuth2AuthorizationRequestURI;
|
|
58
|
+
expiresIn: number;
|
|
59
|
+
}>;
|
|
60
|
+
check: (
|
|
61
|
+
clientId: WhookOAuth2ClientId,
|
|
62
|
+
requestURI: WhookOAuth2AuthorizationRequestURI,
|
|
63
|
+
) => Promise<WhookOAuth2AuthorizationRequestData | undefined>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async function initOAuth2AuthorizationRequests({
|
|
67
|
+
OAUTH2_PAR,
|
|
68
|
+
oAuth2AuthorizationRequestsStore,
|
|
69
|
+
randomBytes,
|
|
70
|
+
time = Date.now.bind(Date),
|
|
71
|
+
log = noop,
|
|
72
|
+
}: WhookOAuth2AuthorizationRequestsDependencies): Promise<WhookOAuth2AuthorizationRequestsService> {
|
|
73
|
+
const oAuth2PAROptions: Required<WhookOAuth2AuthorizationRequestsOptions> = {
|
|
74
|
+
...DEFAULT_OAUTH2_PAR,
|
|
75
|
+
...OAUTH2_PAR,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
log('debug', '👫 - OAuth2AuthorizationRequests Service Initialized!');
|
|
79
|
+
|
|
80
|
+
const oAuth2AuthorizationRequests: WhookOAuth2AuthorizationRequestsService = {
|
|
81
|
+
create: async (clientId, parameters) => {
|
|
82
|
+
const currentTime = time();
|
|
83
|
+
const requestId = (await randomBytes(16)).toString('base64url');
|
|
84
|
+
const expiresAt = currentTime + oAuth2PAROptions.ttl;
|
|
85
|
+
const request: WhookOAuth2AuthorizationRequestData = {
|
|
86
|
+
parameters,
|
|
87
|
+
expiresAt,
|
|
88
|
+
clientId,
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
await oAuth2AuthorizationRequestsStore.set(
|
|
92
|
+
requestId,
|
|
93
|
+
request,
|
|
94
|
+
oAuth2PAROptions.ttl,
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
requestURI: buildRequestURI(requestId),
|
|
99
|
+
expiresIn: oAuth2PAROptions.ttl,
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
check: async (clientId, requestURI) => {
|
|
103
|
+
const requestId = readRequestURI(requestURI);
|
|
104
|
+
const currentTime = time();
|
|
105
|
+
const request =
|
|
106
|
+
await oAuth2AuthorizationRequestsStore.getDelete(requestId);
|
|
107
|
+
|
|
108
|
+
if (!request) {
|
|
109
|
+
throw new YError('E_OAUTH2_BAD_REQUEST_URI', [
|
|
110
|
+
requestURI,
|
|
111
|
+
undefined,
|
|
112
|
+
undefined,
|
|
113
|
+
]);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (request.expiresAt <= currentTime) {
|
|
117
|
+
throw new YError('E_OAUTH2_BAD_REQUEST_URI', [
|
|
118
|
+
requestURI,
|
|
119
|
+
request.expiresAt,
|
|
120
|
+
currentTime,
|
|
121
|
+
]);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (clientId !== request.clientId) {
|
|
125
|
+
throw new YError('E_OAUTH2_CLIENT_MISMATCH', [
|
|
126
|
+
clientId,
|
|
127
|
+
request.clientId,
|
|
128
|
+
]);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return request;
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
return oAuth2AuthorizationRequests;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export default location(
|
|
139
|
+
autoService(initOAuth2AuthorizationRequests),
|
|
140
|
+
import.meta.url,
|
|
141
|
+
);
|
|
@@ -18,12 +18,7 @@ declare module '@whook/authorization' {
|
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
* A service that returns acknowledgments for a given client
|
|
23
|
-
*/
|
|
24
|
-
export type WhookOAuth2ReadClientGrantsService = (
|
|
25
|
-
clientId: WhookOAuth2ClientId,
|
|
26
|
-
) => Promise<{
|
|
21
|
+
export interface WhookOAuth2ClientGrants {
|
|
27
22
|
/** Grant types the client can be used with */
|
|
28
23
|
allowedGrantTypes: string[];
|
|
29
24
|
/** Scopes the client can delegate */
|
|
@@ -36,21 +31,25 @@ export type WhookOAuth2ReadClientGrantsService = (
|
|
|
36
31
|
isPublicClient: boolean;
|
|
37
32
|
/** Whether the client can acknowledge for a user */
|
|
38
33
|
canAcknowledge?: boolean;
|
|
39
|
-
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* A service that returns acknowledgments for a given client
|
|
38
|
+
*/
|
|
39
|
+
export type WhookOAuth2ReadClientGrantsService = (
|
|
40
|
+
clientId: WhookOAuth2ClientId,
|
|
41
|
+
) => Promise<WhookOAuth2ClientGrants>;
|
|
40
42
|
|
|
41
43
|
/**
|
|
42
44
|
* A function handling the OAuth2 authorize step
|
|
43
45
|
*/
|
|
44
|
-
export type WhookOAuth2GranterAuthorize
|
|
46
|
+
export type WhookOAuth2GranterAuthorize = (
|
|
45
47
|
authorizeParameters: {
|
|
46
48
|
clientId: WhookOAuth2ClientId;
|
|
47
|
-
|
|
49
|
+
clientGrants: WhookOAuth2ClientGrants;
|
|
48
50
|
demandedScopes: WhookAuthenticationScope[];
|
|
49
51
|
},
|
|
50
|
-
additionalParameters: T,
|
|
51
52
|
) => Promise<{
|
|
52
|
-
clientId: WhookOAuth2ClientId;
|
|
53
|
-
redirectURI: string;
|
|
54
53
|
scopes: WhookAuthenticationScope[];
|
|
55
54
|
}>;
|
|
56
55
|
|
|
@@ -89,7 +88,6 @@ export type WhookOAuth2GranterAuthenticate<T extends object> = (
|
|
|
89
88
|
export interface WhookOAuth2GranterDefinitions {
|
|
90
89
|
grantType?: string;
|
|
91
90
|
responseType?: string;
|
|
92
|
-
authorizeParameters: Record<string, string>;
|
|
93
91
|
acknowledgeParameters: Record<string, unknown>;
|
|
94
92
|
acknowledgedData: Record<string, unknown>;
|
|
95
93
|
authenticateParameters: Record<string, unknown>;
|
|
@@ -101,7 +99,7 @@ export interface WhookOAuth2GranterService<
|
|
|
101
99
|
grantType: T['grantType'];
|
|
102
100
|
responseType?: T['responseType'];
|
|
103
101
|
issuesRefreshToken: boolean;
|
|
104
|
-
authorize?: WhookOAuth2GranterAuthorize
|
|
102
|
+
authorize?: WhookOAuth2GranterAuthorize;
|
|
105
103
|
acknowledge?: WhookOAuth2GranterAcknowledge<
|
|
106
104
|
T['acknowledgeParameters'],
|
|
107
105
|
T['acknowledgedData']
|