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