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