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