@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.
- package/README.md +106 -70
- package/dist/handlers/getOAuth2Authorize.d.ts +8 -6
- package/dist/handlers/getOAuth2Authorize.js +122 -165
- package/dist/handlers/getOAuth2Authorize.js.map +1 -1
- package/dist/handlers/getOAuth2Authorize.test.js +117 -103
- package/dist/handlers/getOAuth2Authorize.test.js.map +1 -1
- package/dist/handlers/postAuthLogin.d.ts +3 -3
- package/dist/handlers/postAuthLogin.js +83 -120
- package/dist/handlers/postAuthLogin.js.map +1 -1
- package/dist/handlers/postAuthLogin.test.js +42 -44
- package/dist/handlers/postAuthLogin.test.js.map +1 -1
- package/dist/handlers/postAuthLogout.d.ts +1 -1
- package/dist/handlers/postAuthLogout.js +26 -42
- package/dist/handlers/postAuthLogout.js.map +1 -1
- package/dist/handlers/postAuthLogout.test.js +19 -21
- package/dist/handlers/postAuthLogout.test.js.map +1 -1
- package/dist/handlers/postAuthRefresh.d.ts +4 -4
- package/dist/handlers/postAuthRefresh.js +93 -131
- package/dist/handlers/postAuthRefresh.js.map +1 -1
- package/dist/handlers/postAuthRefresh.test.js +47 -49
- package/dist/handlers/postAuthRefresh.test.js.map +1 -1
- package/dist/handlers/postOAuth2Acknowledge.d.ts +11 -8
- package/dist/handlers/postOAuth2Acknowledge.js +119 -151
- package/dist/handlers/postOAuth2Acknowledge.js.map +1 -1
- package/dist/handlers/postOAuth2Acknowledge.test.js +139 -125
- package/dist/handlers/postOAuth2Acknowledge.test.js.map +1 -1
- package/dist/handlers/postOAuth2Token.d.ts +4 -4
- package/dist/handlers/postOAuth2Token.js +200 -232
- package/dist/handlers/postOAuth2Token.js.map +1 -1
- package/dist/handlers/postOAuth2Token.test.js +156 -153
- package/dist/handlers/postOAuth2Token.test.js.map +1 -1
- package/dist/index.d.ts +16 -16
- package/dist/index.js +14 -229
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +697 -620
- package/dist/index.test.js.map +1 -1
- package/dist/services/authCookies.d.ts +8 -4
- package/dist/services/authCookies.js +40 -71
- package/dist/services/authCookies.js.map +1 -1
- package/dist/services/authCookies.test.js +57 -62
- package/dist/services/authCookies.test.js.map +1 -1
- package/dist/services/oAuth2ClientCredentialsGranter.d.ts +2 -2
- package/dist/services/oAuth2ClientCredentialsGranter.js +34 -52
- package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -1
- package/dist/services/oAuth2CodeGranter.d.ts +1 -1
- package/dist/services/oAuth2CodeGranter.js +66 -109
- package/dist/services/oAuth2CodeGranter.js.map +1 -1
- package/dist/services/oAuth2CodeGranter.test.js +65 -64
- package/dist/services/oAuth2CodeGranter.test.js.map +1 -1
- package/dist/services/oAuth2Granters.d.ts +22 -19
- package/dist/services/oAuth2Granters.js +131 -138
- package/dist/services/oAuth2Granters.js.map +1 -1
- package/dist/services/oAuth2PasswordGranter.d.ts +1 -1
- package/dist/services/oAuth2PasswordGranter.js +27 -52
- package/dist/services/oAuth2PasswordGranter.js.map +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.d.ts +1 -1
- package/dist/services/oAuth2RefreshTokenGranter.js +34 -58
- package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -1
- package/dist/services/oAuth2TokenGranter.d.ts +1 -1
- package/dist/services/oAuth2TokenGranter.js +50 -86
- package/dist/services/oAuth2TokenGranter.js.map +1 -1
- package/package.json +54 -96
- package/src/__snapshots__/index.test.ts.snap +23 -22
- package/src/handlers/__snapshots__/postOAuth2Acknowledge.test.ts.snap +1 -1
- package/src/handlers/getOAuth2Authorize.test.ts +49 -13
- package/src/handlers/getOAuth2Authorize.ts +17 -16
- package/src/handlers/postAuthLogin.test.ts +9 -5
- package/src/handlers/postAuthLogin.ts +8 -7
- package/src/handlers/postAuthLogout.test.ts +8 -4
- package/src/handlers/postAuthLogout.ts +2 -2
- package/src/handlers/postAuthRefresh.test.ts +10 -6
- package/src/handlers/postAuthRefresh.ts +8 -7
- package/src/handlers/postOAuth2Acknowledge.test.ts +83 -35
- package/src/handlers/postOAuth2Acknowledge.ts +48 -30
- package/src/handlers/postOAuth2Token.test.ts +79 -19
- package/src/handlers/postOAuth2Token.ts +7 -7
- package/src/index.test.ts +116 -72
- package/src/index.ts +21 -19
- package/src/services/authCookies.test.ts +2 -2
- package/src/services/authCookies.ts +10 -5
- package/src/services/oAuth2ClientCredentialsGranter.ts +28 -24
- package/src/services/oAuth2CodeGranter.test.ts +42 -18
- package/src/services/oAuth2CodeGranter.ts +68 -62
- package/src/services/oAuth2Granters.ts +59 -29
- package/src/services/oAuth2PasswordGranter.ts +25 -24
- package/src/services/oAuth2RefreshTokenGranter.ts +30 -26
- package/src/services/oAuth2TokenGranter.ts +45 -43
- package/dist/handlers/getOAuth2Authorize.mjs +0 -160
- package/dist/handlers/getOAuth2Authorize.mjs.map +0 -1
- package/dist/handlers/getOAuth2Authorize.test.mjs +0 -114
- package/dist/handlers/getOAuth2Authorize.test.mjs.map +0 -1
- package/dist/handlers/postAuthLogin.mjs +0 -108
- package/dist/handlers/postAuthLogin.mjs.map +0 -1
- package/dist/handlers/postAuthLogin.test.mjs +0 -85
- package/dist/handlers/postAuthLogin.test.mjs.map +0 -1
- package/dist/handlers/postAuthLogout.mjs +0 -33
- package/dist/handlers/postAuthLogout.mjs.map +0 -1
- package/dist/handlers/postAuthLogout.test.mjs +0 -30
- package/dist/handlers/postAuthLogout.test.mjs.map +0 -1
- package/dist/handlers/postAuthRefresh.mjs +0 -121
- package/dist/handlers/postAuthRefresh.mjs.map +0 -1
- package/dist/handlers/postAuthRefresh.test.mjs +0 -94
- package/dist/handlers/postAuthRefresh.test.mjs.map +0 -1
- package/dist/handlers/postOAuth2Acknowledge.mjs +0 -150
- package/dist/handlers/postOAuth2Acknowledge.mjs.map +0 -1
- package/dist/handlers/postOAuth2Acknowledge.test.mjs +0 -140
- package/dist/handlers/postOAuth2Acknowledge.test.mjs.map +0 -1
- package/dist/handlers/postOAuth2Token.mjs +0 -231
- package/dist/handlers/postOAuth2Token.mjs.map +0 -1
- package/dist/handlers/postOAuth2Token.test.mjs +0 -160
- package/dist/handlers/postOAuth2Token.test.mjs.map +0 -1
- package/dist/index.mjs +0 -15
- package/dist/index.mjs.map +0 -1
- package/dist/index.test.mjs +0 -748
- package/dist/index.test.mjs.map +0 -1
- package/dist/services/authCookies.mjs +0 -58
- package/dist/services/authCookies.mjs.map +0 -1
- package/dist/services/authCookies.test.mjs +0 -77
- package/dist/services/authCookies.test.mjs.map +0 -1
- package/dist/services/oAuth2ClientCredentialsGranter.mjs +0 -41
- package/dist/services/oAuth2ClientCredentialsGranter.mjs.map +0 -1
- package/dist/services/oAuth2CodeGranter.mjs +0 -98
- package/dist/services/oAuth2CodeGranter.mjs.map +0 -1
- package/dist/services/oAuth2CodeGranter.test.mjs +0 -81
- package/dist/services/oAuth2CodeGranter.test.mjs.map +0 -1
- package/dist/services/oAuth2Granters.mjs +0 -127
- package/dist/services/oAuth2Granters.mjs.map +0 -1
- package/dist/services/oAuth2PasswordGranter.mjs +0 -40
- package/dist/services/oAuth2PasswordGranter.mjs.map +0 -1
- package/dist/services/oAuth2RefreshTokenGranter.mjs +0 -47
- package/dist/services/oAuth2RefreshTokenGranter.mjs.map +0 -1
- package/dist/services/oAuth2TokenGranter.mjs +0 -78
- package/dist/services/oAuth2TokenGranter.mjs.map +0 -1
package/dist/index.test.js
CHANGED
|
@@ -1,225 +1,256 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
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
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
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
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
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
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
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
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
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
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
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
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
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
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
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
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
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
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
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
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
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
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
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
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
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
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
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
|