@whook/oauth2 8.3.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.
Files changed (146) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +384 -0
  3. package/dist/handlers/getOAuth2Authorize.d.ts +26 -0
  4. package/dist/handlers/getOAuth2Authorize.js +192 -0
  5. package/dist/handlers/getOAuth2Authorize.js.map +1 -0
  6. package/dist/handlers/getOAuth2Authorize.mjs +169 -0
  7. package/dist/handlers/getOAuth2Authorize.mjs.map +1 -0
  8. package/dist/handlers/getOAuth2Authorize.test.d.ts +1 -0
  9. package/dist/handlers/getOAuth2Authorize.test.js +122 -0
  10. package/dist/handlers/getOAuth2Authorize.test.js.map +1 -0
  11. package/dist/handlers/getOAuth2Authorize.test.mjs +114 -0
  12. package/dist/handlers/getOAuth2Authorize.test.mjs.map +1 -0
  13. package/dist/handlers/postAuthLogin.d.ts +45 -0
  14. package/dist/handlers/postAuthLogin.js +125 -0
  15. package/dist/handlers/postAuthLogin.js.map +1 -0
  16. package/dist/handlers/postAuthLogin.mjs +108 -0
  17. package/dist/handlers/postAuthLogin.mjs.map +1 -0
  18. package/dist/handlers/postAuthLogin.test.d.ts +1 -0
  19. package/dist/handlers/postAuthLogin.test.js +90 -0
  20. package/dist/handlers/postAuthLogin.test.js.map +1 -0
  21. package/dist/handlers/postAuthLogin.test.mjs +85 -0
  22. package/dist/handlers/postAuthLogin.test.mjs.map +1 -0
  23. package/dist/handlers/postAuthLogout.d.ts +12 -0
  24. package/dist/handlers/postAuthLogout.js +46 -0
  25. package/dist/handlers/postAuthLogout.js.map +1 -0
  26. package/dist/handlers/postAuthLogout.mjs +33 -0
  27. package/dist/handlers/postAuthLogout.mjs.map +1 -0
  28. package/dist/handlers/postAuthLogout.test.d.ts +1 -0
  29. package/dist/handlers/postAuthLogout.test.js +35 -0
  30. package/dist/handlers/postAuthLogout.test.js.map +1 -0
  31. package/dist/handlers/postAuthLogout.test.mjs +30 -0
  32. package/dist/handlers/postAuthLogout.test.mjs.map +1 -0
  33. package/dist/handlers/postAuthRefresh.d.ts +45 -0
  34. package/dist/handlers/postAuthRefresh.js +140 -0
  35. package/dist/handlers/postAuthRefresh.js.map +1 -0
  36. package/dist/handlers/postAuthRefresh.mjs +122 -0
  37. package/dist/handlers/postAuthRefresh.mjs.map +1 -0
  38. package/dist/handlers/postAuthRefresh.test.d.ts +1 -0
  39. package/dist/handlers/postAuthRefresh.test.js +99 -0
  40. package/dist/handlers/postAuthRefresh.test.js.map +1 -0
  41. package/dist/handlers/postAuthRefresh.test.mjs +94 -0
  42. package/dist/handlers/postAuthRefresh.test.mjs.map +1 -0
  43. package/dist/handlers/postOAuth2Acknowledge.d.ts +28 -0
  44. package/dist/handlers/postOAuth2Acknowledge.js +165 -0
  45. package/dist/handlers/postOAuth2Acknowledge.js.map +1 -0
  46. package/dist/handlers/postOAuth2Acknowledge.mjs +150 -0
  47. package/dist/handlers/postOAuth2Acknowledge.mjs.map +1 -0
  48. package/dist/handlers/postOAuth2Acknowledge.test.d.ts +1 -0
  49. package/dist/handlers/postOAuth2Acknowledge.test.js +148 -0
  50. package/dist/handlers/postOAuth2Acknowledge.test.js.map +1 -0
  51. package/dist/handlers/postOAuth2Acknowledge.test.mjs +140 -0
  52. package/dist/handlers/postOAuth2Acknowledge.test.mjs.map +1 -0
  53. package/dist/handlers/postOAuth2Token.d.ts +36 -0
  54. package/dist/handlers/postOAuth2Token.js +263 -0
  55. package/dist/handlers/postOAuth2Token.js.map +1 -0
  56. package/dist/handlers/postOAuth2Token.mjs +242 -0
  57. package/dist/handlers/postOAuth2Token.mjs.map +1 -0
  58. package/dist/handlers/postOAuth2Token.test.d.ts +1 -0
  59. package/dist/handlers/postOAuth2Token.test.js +166 -0
  60. package/dist/handlers/postOAuth2Token.test.js.map +1 -0
  61. package/dist/handlers/postOAuth2Token.test.mjs +160 -0
  62. package/dist/handlers/postOAuth2Token.test.mjs.map +1 -0
  63. package/dist/index.d.ts +17 -0
  64. package/dist/index.js +230 -0
  65. package/dist/index.js.map +1 -0
  66. package/dist/index.mjs +15 -0
  67. package/dist/index.mjs.map +1 -0
  68. package/dist/index.test.d.ts +1 -0
  69. package/dist/index.test.js +759 -0
  70. package/dist/index.test.js.map +1 -0
  71. package/dist/index.test.mjs +748 -0
  72. package/dist/index.test.mjs.map +1 -0
  73. package/dist/services/authCookies.d.ts +26 -0
  74. package/dist/services/authCookies.js +74 -0
  75. package/dist/services/authCookies.js.map +1 -0
  76. package/dist/services/authCookies.mjs +58 -0
  77. package/dist/services/authCookies.mjs.map +1 -0
  78. package/dist/services/authCookies.test.d.ts +1 -0
  79. package/dist/services/authCookies.test.js +82 -0
  80. package/dist/services/authCookies.test.js.map +1 -0
  81. package/dist/services/authCookies.test.mjs +77 -0
  82. package/dist/services/authCookies.test.mjs.map +1 -0
  83. package/dist/services/oAuth2ClientCredentialsGranter.d.ts +17 -0
  84. package/dist/services/oAuth2ClientCredentialsGranter.js +56 -0
  85. package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -0
  86. package/dist/services/oAuth2ClientCredentialsGranter.mjs +41 -0
  87. package/dist/services/oAuth2ClientCredentialsGranter.mjs.map +1 -0
  88. package/dist/services/oAuth2CodeGranter.d.ts +17 -0
  89. package/dist/services/oAuth2CodeGranter.js +115 -0
  90. package/dist/services/oAuth2CodeGranter.js.map +1 -0
  91. package/dist/services/oAuth2CodeGranter.mjs +98 -0
  92. package/dist/services/oAuth2CodeGranter.mjs.map +1 -0
  93. package/dist/services/oAuth2CodeGranter.test.d.ts +1 -0
  94. package/dist/services/oAuth2CodeGranter.test.js +86 -0
  95. package/dist/services/oAuth2CodeGranter.test.js.map +1 -0
  96. package/dist/services/oAuth2CodeGranter.test.mjs +81 -0
  97. package/dist/services/oAuth2CodeGranter.test.mjs.map +1 -0
  98. package/dist/services/oAuth2Granters.d.ts +79 -0
  99. package/dist/services/oAuth2Granters.js +140 -0
  100. package/dist/services/oAuth2Granters.js.map +1 -0
  101. package/dist/services/oAuth2Granters.mjs +127 -0
  102. package/dist/services/oAuth2Granters.mjs.map +1 -0
  103. package/dist/services/oAuth2PasswordGranter.d.ts +16 -0
  104. package/dist/services/oAuth2PasswordGranter.js +55 -0
  105. package/dist/services/oAuth2PasswordGranter.js.map +1 -0
  106. package/dist/services/oAuth2PasswordGranter.mjs +40 -0
  107. package/dist/services/oAuth2PasswordGranter.mjs.map +1 -0
  108. package/dist/services/oAuth2RefreshTokenGranter.d.ts +15 -0
  109. package/dist/services/oAuth2RefreshTokenGranter.js +62 -0
  110. package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -0
  111. package/dist/services/oAuth2RefreshTokenGranter.mjs +47 -0
  112. package/dist/services/oAuth2RefreshTokenGranter.mjs.map +1 -0
  113. package/dist/services/oAuth2TokenGranter.d.ts +15 -0
  114. package/dist/services/oAuth2TokenGranter.js +92 -0
  115. package/dist/services/oAuth2TokenGranter.js.map +1 -0
  116. package/dist/services/oAuth2TokenGranter.mjs +78 -0
  117. package/dist/services/oAuth2TokenGranter.mjs.map +1 -0
  118. package/package.json +205 -0
  119. package/src/__snapshots__/index.test.ts.snap +396 -0
  120. package/src/handlers/__snapshots__/getOAuth2Authorize.test.ts.snap +38 -0
  121. package/src/handlers/__snapshots__/postOAuth2Acknowledge.test.ts.snap +61 -0
  122. package/src/handlers/__snapshots__/postOAuth2Token.test.ts.snap +52 -0
  123. package/src/handlers/getOAuth2Authorize.test.ts +142 -0
  124. package/src/handlers/getOAuth2Authorize.ts +222 -0
  125. package/src/handlers/postAuthLogin.test.ts +88 -0
  126. package/src/handlers/postAuthLogin.ts +123 -0
  127. package/src/handlers/postAuthLogout.test.ts +33 -0
  128. package/src/handlers/postAuthLogout.ts +38 -0
  129. package/src/handlers/postAuthRefresh.test.ts +97 -0
  130. package/src/handlers/postAuthRefresh.ts +146 -0
  131. package/src/handlers/postOAuth2Acknowledge.test.ts +175 -0
  132. package/src/handlers/postOAuth2Acknowledge.ts +209 -0
  133. package/src/handlers/postOAuth2Token.test.ts +153 -0
  134. package/src/handlers/postOAuth2Token.ts +302 -0
  135. package/src/index.test.ts +936 -0
  136. package/src/index.ts +105 -0
  137. package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +49 -0
  138. package/src/services/authCookies.test.ts +93 -0
  139. package/src/services/authCookies.ts +82 -0
  140. package/src/services/oAuth2ClientCredentialsGranter.ts +76 -0
  141. package/src/services/oAuth2CodeGranter.test.ts +94 -0
  142. package/src/services/oAuth2CodeGranter.ts +133 -0
  143. package/src/services/oAuth2Granters.ts +274 -0
  144. package/src/services/oAuth2PasswordGranter.ts +75 -0
  145. package/src/services/oAuth2RefreshTokenGranter.ts +77 -0
  146. package/src/services/oAuth2TokenGranter.ts +105 -0
@@ -0,0 +1,936 @@
1
+ import {
2
+ runServer,
3
+ prepareServer,
4
+ prepareEnvironment as basePrepareEnvironment,
5
+ DEFAULT_ERRORS_DESCRIPTORS,
6
+ } from '@whook/whook';
7
+ import {
8
+ AUTHORIZATION_ERRORS_DESCRIPTORS,
9
+ wrapHandlerWithAuthorization,
10
+ } from '@whook/authorization';
11
+ import { constant, initializer } from 'knifecycle';
12
+ import axios from 'axios';
13
+ import YError from 'yerror';
14
+ import {
15
+ BEARER as BEARER_MECHANISM,
16
+ BASIC as BASIC_MECHANISM,
17
+ } from 'http-auth-utils';
18
+ import {
19
+ initGetOAuth2Authorize,
20
+ getOAuth2AuthorizeDefinition,
21
+ getOAuth2AuthorizeResponseTypeParameter,
22
+ getOAuth2AuthorizeClientIdParameter,
23
+ getOAuth2AuthorizeRedirectURIParameter,
24
+ getOAuth2AuthorizeScopeParameter,
25
+ getOAuth2AuthorizeStateParameter,
26
+ initPostOAuth2Acknowledge,
27
+ postOAuth2AcknowledgeDefinition,
28
+ initPostOAuth2Token,
29
+ postOAuth2TokenDefinition,
30
+ OAUTH2_ERRORS_DESCRIPTORS,
31
+ initOAuth2CodeGranter,
32
+ initOAuth2PasswordGranter,
33
+ initOAuth2RefreshTokenGranter,
34
+ initOAuth2Granters,
35
+ initOAuth2ClientCredentialsGranter,
36
+ initOAuth2TokenGranter,
37
+ postOAuth2TokenAuthorizationCodeTokenRequestBodySchema,
38
+ postOAuth2TokenPasswordTokenRequestBodySchema,
39
+ postOAuth2TokenClientCredentialsTokenRequestBodySchema,
40
+ postOAuth2TokenTokenBodySchema,
41
+ postOAuth2TokenRefreshTokenRequestBodySchema,
42
+ } from '.';
43
+ import type { OAuth2Options } from '.';
44
+ import type { Knifecycle, Dependencies } from 'knifecycle';
45
+ import type { OpenAPIV3 } from 'openapi-types';
46
+
47
+ describe('OAuth2 server', () => {
48
+ const BASE_PATH = '/v1';
49
+ const PORT = 4444;
50
+ const HOST = 'localhost';
51
+ const logger = {
52
+ info: jest.fn(),
53
+ error: jest.fn(),
54
+ };
55
+ const debug = jest.fn();
56
+ const time = jest.fn();
57
+ const $autoload = jest.fn();
58
+
59
+ const API: OpenAPIV3.Document = {
60
+ openapi: '3.0.2',
61
+ info: {
62
+ version: '1.0.0',
63
+ title: 'Sample OpenAPI',
64
+ description: 'A sample OpenAPI file for testing purpose.',
65
+ },
66
+ paths: {
67
+ [getOAuth2AuthorizeDefinition.path]: {
68
+ [getOAuth2AuthorizeDefinition.method]:
69
+ getOAuth2AuthorizeDefinition.operation,
70
+ },
71
+ [postOAuth2AcknowledgeDefinition.path]: {
72
+ [postOAuth2AcknowledgeDefinition.method]: {
73
+ ...postOAuth2AcknowledgeDefinition.operation,
74
+ security: [
75
+ {
76
+ bearerAuth: ['user'],
77
+ },
78
+ ],
79
+ },
80
+ },
81
+ [postOAuth2TokenDefinition.path]: {
82
+ [postOAuth2TokenDefinition.method]: {
83
+ ...postOAuth2TokenDefinition.operation,
84
+ security: [
85
+ {
86
+ basicAuth: ['oauth'],
87
+ },
88
+ ],
89
+ },
90
+ },
91
+ },
92
+ components: {
93
+ securitySchemes: {
94
+ bearerAuth: {
95
+ type: 'http',
96
+ description: 'Bearer authentication with a user API token',
97
+ scheme: 'bearer',
98
+ },
99
+ basicAuth: {
100
+ type: 'http',
101
+ description: 'Basic authentication of an API client',
102
+ scheme: 'basic',
103
+ },
104
+ },
105
+ parameters: [
106
+ getOAuth2AuthorizeResponseTypeParameter,
107
+ getOAuth2AuthorizeClientIdParameter,
108
+ getOAuth2AuthorizeRedirectURIParameter,
109
+ getOAuth2AuthorizeScopeParameter,
110
+ getOAuth2AuthorizeStateParameter,
111
+ ].reduce(
112
+ (parametersHash, { name, parameter }) => ({
113
+ ...parametersHash,
114
+ [name]: parameter,
115
+ }),
116
+ {},
117
+ ),
118
+ schemas: [
119
+ postOAuth2TokenAuthorizationCodeTokenRequestBodySchema,
120
+ postOAuth2TokenPasswordTokenRequestBodySchema,
121
+ postOAuth2TokenClientCredentialsTokenRequestBodySchema,
122
+ postOAuth2TokenRefreshTokenRequestBodySchema,
123
+ postOAuth2TokenTokenBodySchema,
124
+ ].reduce(
125
+ (schemasHash, { name, schema }) => ({
126
+ ...schemasHash,
127
+ [name]: schema,
128
+ }),
129
+ {},
130
+ ),
131
+ },
132
+ };
133
+ const OAUTH2: OAuth2Options = {
134
+ authenticateURL: 'https://auth.example.com/sign_in',
135
+ };
136
+ const authentication = { check: jest.fn() };
137
+ const checkApplication = jest.fn();
138
+ const oAuth2AccessToken = { create: jest.fn(), check: jest.fn() };
139
+ const oAuth2RefreshToken = { create: jest.fn(), check: jest.fn() };
140
+ const oAuth2ClientCredentials = { check: jest.fn() };
141
+ const oAuth2Code = { create: jest.fn(), check: jest.fn() };
142
+ const oAuth2Password = { check: jest.fn() };
143
+
144
+ let $instance;
145
+
146
+ async function prepareEnvironment() {
147
+ const $ = await basePrepareEnvironment();
148
+
149
+ $.register(
150
+ initializer(
151
+ {
152
+ name: '$autoload',
153
+ type: 'service',
154
+ singleton: true,
155
+ },
156
+ async () => $autoload,
157
+ ),
158
+ );
159
+ $.register(constant('BASE_PATH', BASE_PATH));
160
+ $.register(constant('API', API));
161
+ $.register(constant('ENV', {}));
162
+ $.register(constant('NODE_ENV', 'test'));
163
+ $.register(constant('PORT', PORT));
164
+ $.register(constant('HOST', HOST));
165
+ $.register(constant('DEBUG_NODE_ENVS', []));
166
+ $.register(constant('NODE_ENVS', ['test']));
167
+ $.register(constant('MECHANISMS', [BEARER_MECHANISM, BASIC_MECHANISM]));
168
+ $.register(constant('logger', logger));
169
+ $.register(constant('time', time));
170
+ $.register(constant('debug', debug));
171
+
172
+ // OAuth2 Specifics
173
+ $.register(constant('OAUTH2', OAUTH2));
174
+ $.register(
175
+ constant('ERRORS_DESCRIPTORS', {
176
+ ...DEFAULT_ERRORS_DESCRIPTORS,
177
+ ...AUTHORIZATION_ERRORS_DESCRIPTORS,
178
+ ...OAUTH2_ERRORS_DESCRIPTORS,
179
+ }),
180
+ );
181
+ $.register(
182
+ initializer(
183
+ {
184
+ name: 'HANDLERS',
185
+ type: 'service',
186
+ inject: [
187
+ 'getOAuth2Authorize',
188
+ 'postOAuth2Acknowledge',
189
+ 'postOAuth2Token',
190
+ ],
191
+ singleton: true,
192
+ },
193
+ async (services) => services,
194
+ ),
195
+ );
196
+ $.register(constant('authentication', authentication));
197
+ $.register(constant('checkApplication', checkApplication));
198
+ $.register(constant('oAuth2AccessToken', oAuth2AccessToken));
199
+ $.register(constant('oAuth2RefreshToken', oAuth2RefreshToken));
200
+ $.register(constant('oAuth2ClientCredentials', oAuth2ClientCredentials));
201
+ $.register(constant('oAuth2Code', oAuth2Code));
202
+ $.register(constant('oAuth2Password', oAuth2Password));
203
+ [
204
+ initGetOAuth2Authorize,
205
+ wrapHandlerWithAuthorization(initPostOAuth2Acknowledge),
206
+ wrapHandlerWithAuthorization(initPostOAuth2Token),
207
+ initOAuth2Granters,
208
+ initOAuth2ClientCredentialsGranter,
209
+ initOAuth2CodeGranter,
210
+ initOAuth2PasswordGranter,
211
+ initOAuth2RefreshTokenGranter,
212
+ initOAuth2TokenGranter,
213
+ ].forEach((handlerInitializer) => $.register(handlerInitializer));
214
+
215
+ return $;
216
+ }
217
+
218
+ $autoload.mockImplementation(async (serviceName) => {
219
+ throw new YError('E_UNMATCHED_DEPENDENCY', serviceName);
220
+ });
221
+ process.env.ISOLATED_ENV = '1';
222
+
223
+ beforeAll(async () => {
224
+ const { $instance: _instance } = await runServer<{
225
+ $instance: Knifecycle<Dependencies>;
226
+ }>(prepareEnvironment, prepareServer, [
227
+ '$instance',
228
+ 'httpServer',
229
+ 'process',
230
+ ]);
231
+ $instance = _instance;
232
+ });
233
+
234
+ afterAll(async () => {
235
+ await $instance.destroy();
236
+ });
237
+
238
+ beforeEach(() => {
239
+ logger.info.mockReset();
240
+ logger.error.mockReset();
241
+ debug.mockReset();
242
+ time.mockReset();
243
+ $autoload.mockClear();
244
+ [
245
+ oAuth2AccessToken.create,
246
+ oAuth2AccessToken.check,
247
+ oAuth2RefreshToken.create,
248
+ oAuth2RefreshToken.check,
249
+ oAuth2ClientCredentials.check,
250
+ oAuth2Code.check,
251
+ oAuth2Code.create,
252
+ oAuth2Password.check,
253
+ checkApplication,
254
+ authentication.check,
255
+ ].forEach((mock) => mock.mockReset());
256
+ });
257
+
258
+ describe('with the password flow', () => {
259
+ it('should produce new tokens', async () => {
260
+ time.mockReturnValue(Date.parse('2010-03-06T00:00:00Z'));
261
+ [
262
+ oAuth2AccessToken.check,
263
+ oAuth2RefreshToken.check,
264
+ oAuth2ClientCredentials.check,
265
+ oAuth2Code.check,
266
+ oAuth2Code.create,
267
+ oAuth2RefreshToken.check,
268
+ ].forEach((mock) =>
269
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
270
+ );
271
+ authentication.check.mockResolvedValueOnce({
272
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
273
+ scope: 'user,oauth',
274
+ });
275
+ checkApplication.mockResolvedValueOnce(undefined);
276
+ oAuth2Password.check.mockResolvedValueOnce({
277
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
278
+ scope: 'user,auth',
279
+ authentication: {
280
+ userId: 1,
281
+ },
282
+ });
283
+ oAuth2AccessToken.create.mockResolvedValueOnce({
284
+ token: 'an_access_token',
285
+ expiresAt: Date.parse('2010-03-07T00:00:00Z'),
286
+ });
287
+ oAuth2RefreshToken.create.mockResolvedValueOnce({
288
+ token: 'a_refresh_token',
289
+ expiresAt: Date.parse('2180-03-06T00:00:00Z'),
290
+ });
291
+
292
+ const { status, headers, data } = await axios({
293
+ method: 'post',
294
+ url: `http://${HOST}:${PORT}${BASE_PATH}${postOAuth2TokenDefinition.path}`,
295
+ headers: {
296
+ authorization: `basic ${Buffer.from('ali:open_sesame').toString(
297
+ 'base64',
298
+ )}`,
299
+ },
300
+ data: {
301
+ grant_type: 'password',
302
+ username: 'me@example.com',
303
+ password: 'udelawli',
304
+ scope: 'user',
305
+ },
306
+ validateStatus: () => true,
307
+ });
308
+
309
+ expect({
310
+ status,
311
+ headers: {
312
+ ...headers,
313
+ // Erasing the Date header that may be added by Axios :/
314
+ date: undefined,
315
+ },
316
+ data,
317
+ }).toMatchInlineSnapshot(`
318
+ Object {
319
+ "data": Object {
320
+ "access_token": "an_access_token",
321
+ "expiration_date": "2010-03-07T00:00:00.000Z",
322
+ "expires_in": 86400,
323
+ "refresh_token": "a_refresh_token",
324
+ "refresh_token_expiration_date": "2180-03-06T00:00:00.000Z",
325
+ "refresh_token_expires_in": 5364748800,
326
+ "token_type": "bearer",
327
+ },
328
+ "headers": Object {
329
+ "connection": "close",
330
+ "content-type": "application/json",
331
+ "date": undefined,
332
+ "transaction-id": "0",
333
+ "transfer-encoding": "chunked",
334
+ "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\"}",
335
+ },
336
+ "status": 200,
337
+ }
338
+ `);
339
+ expect({
340
+ checkApplicationCalls: checkApplication.mock.calls,
341
+ authenticationCheckCalls: authentication.check.mock.calls,
342
+ oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
343
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
344
+ oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
345
+ oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
346
+ oAuth2ClientCredentialsCheckCalls:
347
+ oAuth2ClientCredentials.check.mock.calls,
348
+ oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
349
+ oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
350
+ oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
351
+ }).toMatchSnapshot();
352
+ });
353
+ });
354
+
355
+ describe('with the refresh token flow', () => {
356
+ it('should produce new tokens', async () => {
357
+ time.mockReturnValue(Date.parse('2010-03-06T00:00:00Z'));
358
+ [
359
+ oAuth2AccessToken.check,
360
+ oAuth2ClientCredentials.check,
361
+ oAuth2Code.check,
362
+ oAuth2Code.create,
363
+ oAuth2Password.check,
364
+ ].forEach((mock) =>
365
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
366
+ );
367
+ authentication.check.mockResolvedValueOnce({
368
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
369
+ scope: 'user,oauth',
370
+ });
371
+ checkApplication.mockResolvedValueOnce(undefined);
372
+ oAuth2RefreshToken.check.mockResolvedValueOnce({
373
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
374
+ scope: 'user,auth',
375
+ authentication: {
376
+ userId: 1,
377
+ },
378
+ });
379
+ oAuth2AccessToken.create.mockResolvedValueOnce({
380
+ token: 'an_access_token',
381
+ expiresAt: Date.parse('2010-03-07T00:00:00Z'),
382
+ });
383
+ oAuth2RefreshToken.create.mockResolvedValueOnce({
384
+ token: 'a_refresh_token',
385
+ expiresAt: Date.parse('2180-03-06T00:00:00Z'),
386
+ });
387
+
388
+ const { status, headers, data } = await axios({
389
+ method: 'post',
390
+ url: `http://${HOST}:${PORT}${BASE_PATH}${postOAuth2TokenDefinition.path}`,
391
+ headers: {
392
+ authorization: `basic ${Buffer.from('ali:open_sesame').toString(
393
+ 'base64',
394
+ )}`,
395
+ },
396
+ data: {
397
+ grant_type: 'refresh_token',
398
+ refresh_token: 'a_refresh_token',
399
+ scope: 'user',
400
+ },
401
+ validateStatus: () => true,
402
+ });
403
+
404
+ expect({
405
+ status,
406
+ headers: {
407
+ ...headers,
408
+ // Erasing the Date header that may be added by Axios :/
409
+ date: undefined,
410
+ },
411
+ data,
412
+ }).toMatchInlineSnapshot(`
413
+ Object {
414
+ "data": Object {
415
+ "access_token": "an_access_token",
416
+ "expiration_date": "2010-03-07T00:00:00.000Z",
417
+ "expires_in": 86400,
418
+ "refresh_token": "a_refresh_token",
419
+ "refresh_token_expiration_date": "2180-03-06T00:00:00.000Z",
420
+ "refresh_token_expires_in": 5364748800,
421
+ "token_type": "bearer",
422
+ },
423
+ "headers": Object {
424
+ "connection": "close",
425
+ "content-type": "application/json",
426
+ "date": undefined,
427
+ "transaction-id": "1",
428
+ "transfer-encoding": "chunked",
429
+ "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\"}",
430
+ },
431
+ "status": 200,
432
+ }
433
+ `);
434
+ expect({
435
+ checkApplicationCalls: checkApplication.mock.calls,
436
+ authenticationCheckCalls: authentication.check.mock.calls,
437
+ oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
438
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
439
+ oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
440
+ oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
441
+ oAuth2ClientCredentialsCheckCalls:
442
+ oAuth2ClientCredentials.check.mock.calls,
443
+ oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
444
+ oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
445
+ oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
446
+ }).toMatchSnapshot();
447
+ });
448
+ });
449
+
450
+ describe('with the client credentials flow', () => {
451
+ it('should produce new tokens', async () => {
452
+ time.mockReturnValue(Date.parse('2010-03-06T00:00:00Z'));
453
+ [
454
+ oAuth2AccessToken.check,
455
+ oAuth2RefreshToken.check,
456
+ oAuth2Code.check,
457
+ oAuth2Code.create,
458
+ oAuth2Password.check,
459
+ ].forEach((mock) =>
460
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
461
+ );
462
+ authentication.check.mockResolvedValueOnce({
463
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
464
+ scope: 'user,oauth',
465
+ });
466
+ checkApplication.mockResolvedValueOnce(undefined);
467
+ oAuth2ClientCredentials.check.mockResolvedValueOnce({
468
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
469
+ scope: 'user,auth',
470
+ authentication: {
471
+ userId: 1,
472
+ },
473
+ });
474
+ oAuth2AccessToken.create.mockResolvedValueOnce({
475
+ token: 'an_access_token',
476
+ expiresAt: Date.parse('2010-03-07T00:00:00Z'),
477
+ });
478
+ oAuth2RefreshToken.create.mockResolvedValueOnce({
479
+ token: 'a_refresh_token',
480
+ expiresAt: Date.parse('2180-03-06T00:00:00Z'),
481
+ });
482
+
483
+ const { status, headers, data } = await axios({
484
+ method: 'post',
485
+ url: `http://${HOST}:${PORT}${BASE_PATH}${postOAuth2TokenDefinition.path}`,
486
+ headers: {
487
+ authorization: `basic ${Buffer.from('ali:open_sesame').toString(
488
+ 'base64',
489
+ )}`,
490
+ },
491
+ data: {
492
+ grant_type: 'client_credentials',
493
+ scope: 'user',
494
+ },
495
+ validateStatus: () => true,
496
+ });
497
+
498
+ expect({
499
+ status,
500
+ headers: {
501
+ ...headers,
502
+ // Erasing the Date header that may be added by Axios :/
503
+ date: undefined,
504
+ },
505
+ data,
506
+ }).toMatchInlineSnapshot(`
507
+ Object {
508
+ "data": Object {
509
+ "access_token": "an_access_token",
510
+ "expiration_date": "2010-03-07T00:00:00.000Z",
511
+ "expires_in": 86400,
512
+ "refresh_token": "a_refresh_token",
513
+ "refresh_token_expiration_date": "2180-03-06T00:00:00.000Z",
514
+ "refresh_token_expires_in": 5364748800,
515
+ "token_type": "bearer",
516
+ },
517
+ "headers": Object {
518
+ "connection": "close",
519
+ "content-type": "application/json",
520
+ "date": undefined,
521
+ "transaction-id": "2",
522
+ "transfer-encoding": "chunked",
523
+ "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\"}",
524
+ },
525
+ "status": 200,
526
+ }
527
+ `);
528
+ expect({
529
+ checkApplicationCalls: checkApplication.mock.calls,
530
+ authenticationCheckCalls: authentication.check.mock.calls,
531
+ oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
532
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
533
+ oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
534
+ oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
535
+ oAuth2ClientCredentialsCheckCalls:
536
+ oAuth2ClientCredentials.check.mock.calls,
537
+ oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
538
+ oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
539
+ oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
540
+ }).toMatchSnapshot();
541
+ });
542
+ });
543
+
544
+ describe('with the code flow', () => {
545
+ it('should build the authorization redirection', async () => {
546
+ time.mockReturnValue(Date.parse('2010-03-06T00:00:00Z'));
547
+ [
548
+ authentication.check,
549
+ oAuth2AccessToken.check,
550
+ oAuth2AccessToken.create,
551
+ oAuth2RefreshToken.check,
552
+ oAuth2RefreshToken.create,
553
+ oAuth2Code.check,
554
+ oAuth2Code.create,
555
+ oAuth2ClientCredentials.check,
556
+ oAuth2Password.check,
557
+ ].forEach((mock) =>
558
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
559
+ );
560
+ checkApplication.mockResolvedValueOnce({
561
+ redirectURI:
562
+ 'https://example.com/oauth2/callback?a_param=a_param_value',
563
+ });
564
+
565
+ const { status, headers, data } = await axios({
566
+ method: 'get',
567
+ url: `http://${HOST}:${PORT}${BASE_PATH}${getOAuth2AuthorizeDefinition.path}`,
568
+ params: {
569
+ response_type: 'code',
570
+ client_id: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
571
+ redirect_uri:
572
+ 'https://example.com/oauth2/callback?a_param=a_param_value',
573
+ scope: 'user',
574
+ state: 'xyz',
575
+ },
576
+ maxRedirects: 0,
577
+ validateStatus: () => true,
578
+ });
579
+
580
+ expect({
581
+ status,
582
+ headers: {
583
+ ...headers,
584
+ // Erasing the Date header that may be added by Axios :/
585
+ date: undefined,
586
+ },
587
+ data,
588
+ }).toMatchInlineSnapshot(`
589
+ Object {
590
+ "data": "",
591
+ "headers": Object {
592
+ "connection": "close",
593
+ "date": undefined,
594
+ "location": "https://auth.example.com/sign_in?type=code&redirect_uri=https%3A%2F%2Fexample.com%2Foauth2%2Fcallback%3Fa_param%3Da_param_value&scope=user&client_id=acdc41ce-acdc-41ce-acdc-41ceacdc41ce&state=xyz",
595
+ "transaction-id": "3",
596
+ "transfer-encoding": "chunked",
597
+ },
598
+ "status": 302,
599
+ }
600
+ `);
601
+ expect({
602
+ checkApplicationCalls: checkApplication.mock.calls,
603
+ authenticationCheckCalls: authentication.check.mock.calls,
604
+ oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
605
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
606
+ oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
607
+ oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
608
+ oAuth2ClientCredentialsCheckCalls:
609
+ oAuth2ClientCredentials.check.mock.calls,
610
+ oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
611
+ oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
612
+ oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
613
+ }).toMatchSnapshot();
614
+ });
615
+
616
+ it('should redirect with a code', async () => {
617
+ time.mockReturnValue(Date.parse('2010-03-06T00:00:00Z'));
618
+ [
619
+ oAuth2AccessToken.check,
620
+ oAuth2AccessToken.create,
621
+ oAuth2RefreshToken.check,
622
+ oAuth2RefreshToken.create,
623
+ oAuth2Code.check,
624
+ oAuth2ClientCredentials.check,
625
+ oAuth2Password.check,
626
+ ].forEach((mock) =>
627
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
628
+ );
629
+ authentication.check.mockResolvedValueOnce({
630
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
631
+ scope: 'user,oauth',
632
+ });
633
+ oAuth2Code.create.mockResolvedValueOnce('a_code');
634
+ checkApplication.mockResolvedValueOnce(undefined);
635
+ checkApplication.mockResolvedValueOnce({
636
+ redirectURI: 'http://redirect.example.com/yolo',
637
+ });
638
+
639
+ const { status, headers, data } = await axios({
640
+ method: 'post',
641
+ url: `http://${HOST}:${PORT}${BASE_PATH}${postOAuth2AcknowledgeDefinition.path}`,
642
+ headers: {
643
+ authorization: 'Bearer yolo',
644
+ },
645
+ data: {
646
+ responseType: 'code',
647
+ clientId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
648
+ redirectURI: 'http://redirect.example.com/yolo?a_param=a_value',
649
+ scope: 'user',
650
+ state: 'xyz',
651
+ acknowledged: true,
652
+ },
653
+ maxRedirects: 0,
654
+ validateStatus: () => true,
655
+ });
656
+
657
+ expect({
658
+ status,
659
+ headers: {
660
+ ...headers,
661
+ // Erasing the Date header that may be added by Axios :/
662
+ date: undefined,
663
+ },
664
+ data,
665
+ }).toMatchInlineSnapshot(`
666
+ Object {
667
+ "data": "",
668
+ "headers": Object {
669
+ "connection": "close",
670
+ "date": undefined,
671
+ "location": "http://redirect.example.com/yolo?a_param=a_value&client_id=acdc41ce-acdc-41ce-acdc-41ceacdc41ce&scope=user&state=xyz&redirect_uri=http%3A%2F%2Fredirect.example.com%2Fyolo%3Fa_param%3Da_value&code=a_code",
672
+ "transaction-id": "4",
673
+ "transfer-encoding": "chunked",
674
+ "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\"}",
675
+ },
676
+ "status": 302,
677
+ }
678
+ `);
679
+ expect({
680
+ checkApplicationCalls: checkApplication.mock.calls,
681
+ authenticationCheckCalls: authentication.check.mock.calls,
682
+ oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
683
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
684
+ oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
685
+ oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
686
+ oAuth2ClientCredentialsCheckCalls:
687
+ oAuth2ClientCredentials.check.mock.calls,
688
+ oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
689
+ oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
690
+ oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
691
+ }).toMatchSnapshot();
692
+ });
693
+
694
+ it('should produce new tokens', async () => {
695
+ time.mockReturnValue(Date.parse('2010-03-06T00:00:00Z'));
696
+ [
697
+ oAuth2AccessToken.check,
698
+ oAuth2RefreshToken.check,
699
+ oAuth2ClientCredentials.check,
700
+ oAuth2Password.check,
701
+ ].forEach((mock) =>
702
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
703
+ );
704
+ authentication.check.mockResolvedValueOnce({
705
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
706
+ scope: 'user,oauth',
707
+ });
708
+ checkApplication.mockResolvedValueOnce(undefined);
709
+ oAuth2Code.check.mockResolvedValueOnce({
710
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
711
+ scope: 'user,auth',
712
+ userId: 1,
713
+ });
714
+ oAuth2AccessToken.create.mockResolvedValueOnce({
715
+ token: 'an_access_token',
716
+ expiresAt: Date.parse('2010-03-07T00:00:00Z'),
717
+ });
718
+ oAuth2RefreshToken.create.mockResolvedValueOnce({
719
+ token: 'a_refresh_token',
720
+ expiresAt: Date.parse('2180-03-06T00:00:00Z'),
721
+ });
722
+
723
+ const { status, headers, data } = await axios({
724
+ method: 'post',
725
+ url: `http://${HOST}:${PORT}${BASE_PATH}${postOAuth2TokenDefinition.path}`,
726
+ headers: {
727
+ authorization: `basic ${Buffer.from('ali:open_sesame').toString(
728
+ 'base64',
729
+ )}`,
730
+ },
731
+ data: {
732
+ grant_type: 'authorization_code',
733
+ code: 'a_grant_code',
734
+ redirect_uri: 'http://redirect.example.com/yolo',
735
+ },
736
+ validateStatus: () => true,
737
+ });
738
+
739
+ expect({
740
+ status,
741
+ headers: {
742
+ ...headers,
743
+ // Erasing the Date header that may be added by Axios :/
744
+ date: undefined,
745
+ },
746
+ data,
747
+ }).toMatchInlineSnapshot(`
748
+ Object {
749
+ "data": Object {
750
+ "access_token": "an_access_token",
751
+ "expiration_date": "2010-03-07T00:00:00.000Z",
752
+ "expires_in": 86400,
753
+ "refresh_token": "a_refresh_token",
754
+ "refresh_token_expiration_date": "2180-03-06T00:00:00.000Z",
755
+ "refresh_token_expires_in": 5364748800,
756
+ "token_type": "bearer",
757
+ },
758
+ "headers": Object {
759
+ "connection": "close",
760
+ "content-type": "application/json",
761
+ "date": undefined,
762
+ "transaction-id": "5",
763
+ "transfer-encoding": "chunked",
764
+ "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\"}",
765
+ },
766
+ "status": 200,
767
+ }
768
+ `);
769
+ expect({
770
+ checkApplicationCalls: checkApplication.mock.calls,
771
+ authenticationCheckCalls: authentication.check.mock.calls,
772
+ oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
773
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
774
+ oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
775
+ oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
776
+ oAuth2ClientCredentialsCheckCalls:
777
+ oAuth2ClientCredentials.check.mock.calls,
778
+ oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
779
+ oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
780
+ oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
781
+ }).toMatchSnapshot();
782
+ });
783
+ });
784
+
785
+ describe('with the implicit flow', () => {
786
+ it('should build the authorization redirection', async () => {
787
+ time.mockReturnValue(Date.parse('2010-03-06T00:00:00Z'));
788
+ [
789
+ authentication.check,
790
+ oAuth2AccessToken.check,
791
+ oAuth2AccessToken.create,
792
+ oAuth2RefreshToken.check,
793
+ oAuth2RefreshToken.create,
794
+ oAuth2Code.check,
795
+ oAuth2Code.create,
796
+ oAuth2ClientCredentials.check,
797
+ oAuth2Password.check,
798
+ ].forEach((mock) =>
799
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
800
+ );
801
+ checkApplication.mockResolvedValueOnce({
802
+ redirectURI: 'http://redirect.example.com/yolo',
803
+ });
804
+
805
+ const { status, headers, data } = await axios({
806
+ method: 'get',
807
+ url: `http://${HOST}:${PORT}${BASE_PATH}${getOAuth2AuthorizeDefinition.path}`,
808
+ params: {
809
+ response_type: 'token',
810
+ client_id: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
811
+ redirect_uri: 'http://redirect.example.com/yolo',
812
+ scope: 'user',
813
+ state: 'xyz',
814
+ },
815
+ maxRedirects: 0,
816
+ validateStatus: () => true,
817
+ });
818
+
819
+ expect({
820
+ status,
821
+ headers: {
822
+ ...headers,
823
+ // Erasing the Date header that may be added by Axios :/
824
+ date: undefined,
825
+ },
826
+ data,
827
+ }).toMatchInlineSnapshot(`
828
+ Object {
829
+ "data": "",
830
+ "headers": Object {
831
+ "connection": "close",
832
+ "date": undefined,
833
+ "location": "https://auth.example.com/sign_in?type=token&redirect_uri=http%3A%2F%2Fredirect.example.com%2Fyolo&scope=user&client_id=acdc41ce-acdc-41ce-acdc-41ceacdc41ce&state=xyz",
834
+ "transaction-id": "6",
835
+ "transfer-encoding": "chunked",
836
+ },
837
+ "status": 302,
838
+ }
839
+ `);
840
+ expect({
841
+ checkApplicationCalls: checkApplication.mock.calls,
842
+ authenticationCheckCalls: authentication.check.mock.calls,
843
+ oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
844
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
845
+ oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
846
+ oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
847
+ oAuth2ClientCredentialsCheckCalls:
848
+ oAuth2ClientCredentials.check.mock.calls,
849
+ oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
850
+ oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
851
+ oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
852
+ }).toMatchSnapshot();
853
+ });
854
+
855
+ it('should redirect with a token', async () => {
856
+ time.mockReturnValue(Date.parse('2010-03-06T00:00:00Z'));
857
+ [
858
+ oAuth2AccessToken.check,
859
+ oAuth2RefreshToken.check,
860
+ oAuth2RefreshToken.create,
861
+ oAuth2Code.check,
862
+ oAuth2Code.create,
863
+ oAuth2ClientCredentials.check,
864
+ oAuth2Password.check,
865
+ ].forEach((mock) =>
866
+ mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
867
+ );
868
+ authentication.check.mockResolvedValueOnce({
869
+ applicationId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
870
+ scope: 'user,oauth',
871
+ });
872
+ oAuth2AccessToken.create.mockResolvedValueOnce({
873
+ token: 'an_access_token',
874
+ expiresAt: Date.parse('2010-03-07T00:00:00Z'),
875
+ });
876
+ checkApplication.mockResolvedValueOnce(undefined);
877
+ checkApplication.mockResolvedValueOnce({
878
+ redirectURI: 'http://redirect.example.com/yolo',
879
+ });
880
+
881
+ const { status, headers, data } = await axios({
882
+ method: 'post',
883
+ url: `http://${HOST}:${PORT}${BASE_PATH}${postOAuth2AcknowledgeDefinition.path}`,
884
+ headers: {
885
+ authorization: 'Bearer yolo',
886
+ },
887
+ data: {
888
+ responseType: 'token',
889
+ clientId: 'acdc41ce-acdc-41ce-acdc-41ceacdc41ce',
890
+ redirectURI: 'http://redirect.example.com/yolo?a_param=a_value',
891
+ scope: 'user',
892
+ state: 'xyz',
893
+ acknowledged: true,
894
+ },
895
+ maxRedirects: 0,
896
+ validateStatus: () => true,
897
+ });
898
+
899
+ expect({
900
+ status,
901
+ headers: {
902
+ ...headers,
903
+ // Erasing the Date header that may be added by Axios :/
904
+ date: undefined,
905
+ },
906
+ data,
907
+ }).toMatchInlineSnapshot(`
908
+ Object {
909
+ "data": "",
910
+ "headers": Object {
911
+ "connection": "close",
912
+ "date": undefined,
913
+ "location": "http://redirect.example.com/yolo?a_param=a_value&client_id=acdc41ce-acdc-41ce-acdc-41ceacdc41ce&scope=user&state=xyz&redirect_uri=http%3A%2F%2Fredirect.example.com%2Fyolo&access_token=an_access_token&token_type=bearer&expires_in=86400",
914
+ "transaction-id": "7",
915
+ "transfer-encoding": "chunked",
916
+ "x-authenticated": "{\\"applicationId\\":\\"acdc41ce-acdc-41ce-acdc-41ceacdc41ce\\",\\"scope\\":\\"user,oauth\\"}",
917
+ },
918
+ "status": 302,
919
+ }
920
+ `);
921
+ expect({
922
+ checkApplicationCalls: checkApplication.mock.calls,
923
+ authenticationCheckCalls: authentication.check.mock.calls,
924
+ oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
925
+ oAuth2AccessTokenCheckCalls: oAuth2AccessToken.check.mock.calls,
926
+ oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
927
+ oAuth2RefreshTokenCheckCalls: oAuth2RefreshToken.check.mock.calls,
928
+ oAuth2ClientCredentialsCheckCalls:
929
+ oAuth2ClientCredentials.check.mock.calls,
930
+ oAuth2CodeCheckCalls: oAuth2Code.check.mock.calls,
931
+ oAuth2CodeCreateCalls: oAuth2Code.create.mock.calls,
932
+ oAuth2PasswordCheckCalls: oAuth2Password.check.mock.calls,
933
+ }).toMatchSnapshot();
934
+ });
935
+ });
936
+ });