@whook/oauth2 8.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +384 -0
- package/dist/handlers/getOAuth2Authorize.d.ts +26 -0
- package/dist/handlers/getOAuth2Authorize.js +192 -0
- package/dist/handlers/getOAuth2Authorize.js.map +1 -0
- package/dist/handlers/getOAuth2Authorize.mjs +169 -0
- package/dist/handlers/getOAuth2Authorize.mjs.map +1 -0
- package/dist/handlers/getOAuth2Authorize.test.d.ts +1 -0
- package/dist/handlers/getOAuth2Authorize.test.js +122 -0
- package/dist/handlers/getOAuth2Authorize.test.js.map +1 -0
- package/dist/handlers/getOAuth2Authorize.test.mjs +114 -0
- package/dist/handlers/getOAuth2Authorize.test.mjs.map +1 -0
- package/dist/handlers/postAuthLogin.d.ts +45 -0
- package/dist/handlers/postAuthLogin.js +125 -0
- package/dist/handlers/postAuthLogin.js.map +1 -0
- package/dist/handlers/postAuthLogin.mjs +108 -0
- package/dist/handlers/postAuthLogin.mjs.map +1 -0
- package/dist/handlers/postAuthLogin.test.d.ts +1 -0
- package/dist/handlers/postAuthLogin.test.js +90 -0
- package/dist/handlers/postAuthLogin.test.js.map +1 -0
- package/dist/handlers/postAuthLogin.test.mjs +85 -0
- package/dist/handlers/postAuthLogin.test.mjs.map +1 -0
- package/dist/handlers/postAuthLogout.d.ts +12 -0
- package/dist/handlers/postAuthLogout.js +46 -0
- package/dist/handlers/postAuthLogout.js.map +1 -0
- package/dist/handlers/postAuthLogout.mjs +33 -0
- package/dist/handlers/postAuthLogout.mjs.map +1 -0
- package/dist/handlers/postAuthLogout.test.d.ts +1 -0
- package/dist/handlers/postAuthLogout.test.js +35 -0
- package/dist/handlers/postAuthLogout.test.js.map +1 -0
- package/dist/handlers/postAuthLogout.test.mjs +30 -0
- package/dist/handlers/postAuthLogout.test.mjs.map +1 -0
- package/dist/handlers/postAuthRefresh.d.ts +45 -0
- package/dist/handlers/postAuthRefresh.js +140 -0
- package/dist/handlers/postAuthRefresh.js.map +1 -0
- package/dist/handlers/postAuthRefresh.mjs +122 -0
- package/dist/handlers/postAuthRefresh.mjs.map +1 -0
- package/dist/handlers/postAuthRefresh.test.d.ts +1 -0
- package/dist/handlers/postAuthRefresh.test.js +99 -0
- package/dist/handlers/postAuthRefresh.test.js.map +1 -0
- package/dist/handlers/postAuthRefresh.test.mjs +94 -0
- package/dist/handlers/postAuthRefresh.test.mjs.map +1 -0
- package/dist/handlers/postOAuth2Acknowledge.d.ts +28 -0
- package/dist/handlers/postOAuth2Acknowledge.js +165 -0
- package/dist/handlers/postOAuth2Acknowledge.js.map +1 -0
- package/dist/handlers/postOAuth2Acknowledge.mjs +150 -0
- package/dist/handlers/postOAuth2Acknowledge.mjs.map +1 -0
- package/dist/handlers/postOAuth2Acknowledge.test.d.ts +1 -0
- package/dist/handlers/postOAuth2Acknowledge.test.js +148 -0
- package/dist/handlers/postOAuth2Acknowledge.test.js.map +1 -0
- package/dist/handlers/postOAuth2Acknowledge.test.mjs +140 -0
- package/dist/handlers/postOAuth2Acknowledge.test.mjs.map +1 -0
- package/dist/handlers/postOAuth2Token.d.ts +36 -0
- package/dist/handlers/postOAuth2Token.js +263 -0
- package/dist/handlers/postOAuth2Token.js.map +1 -0
- package/dist/handlers/postOAuth2Token.mjs +242 -0
- package/dist/handlers/postOAuth2Token.mjs.map +1 -0
- package/dist/handlers/postOAuth2Token.test.d.ts +1 -0
- package/dist/handlers/postOAuth2Token.test.js +166 -0
- package/dist/handlers/postOAuth2Token.test.js.map +1 -0
- package/dist/handlers/postOAuth2Token.test.mjs +160 -0
- package/dist/handlers/postOAuth2Token.test.mjs.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +230 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +15 -0
- package/dist/index.mjs.map +1 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +759 -0
- package/dist/index.test.js.map +1 -0
- package/dist/index.test.mjs +748 -0
- package/dist/index.test.mjs.map +1 -0
- package/dist/services/authCookies.d.ts +26 -0
- package/dist/services/authCookies.js +74 -0
- package/dist/services/authCookies.js.map +1 -0
- package/dist/services/authCookies.mjs +58 -0
- package/dist/services/authCookies.mjs.map +1 -0
- package/dist/services/authCookies.test.d.ts +1 -0
- package/dist/services/authCookies.test.js +82 -0
- package/dist/services/authCookies.test.js.map +1 -0
- package/dist/services/authCookies.test.mjs +77 -0
- package/dist/services/authCookies.test.mjs.map +1 -0
- package/dist/services/oAuth2ClientCredentialsGranter.d.ts +17 -0
- package/dist/services/oAuth2ClientCredentialsGranter.js +56 -0
- package/dist/services/oAuth2ClientCredentialsGranter.js.map +1 -0
- package/dist/services/oAuth2ClientCredentialsGranter.mjs +41 -0
- package/dist/services/oAuth2ClientCredentialsGranter.mjs.map +1 -0
- package/dist/services/oAuth2CodeGranter.d.ts +17 -0
- package/dist/services/oAuth2CodeGranter.js +115 -0
- package/dist/services/oAuth2CodeGranter.js.map +1 -0
- package/dist/services/oAuth2CodeGranter.mjs +98 -0
- package/dist/services/oAuth2CodeGranter.mjs.map +1 -0
- package/dist/services/oAuth2CodeGranter.test.d.ts +1 -0
- package/dist/services/oAuth2CodeGranter.test.js +86 -0
- package/dist/services/oAuth2CodeGranter.test.js.map +1 -0
- package/dist/services/oAuth2CodeGranter.test.mjs +81 -0
- package/dist/services/oAuth2CodeGranter.test.mjs.map +1 -0
- package/dist/services/oAuth2Granters.d.ts +79 -0
- package/dist/services/oAuth2Granters.js +140 -0
- package/dist/services/oAuth2Granters.js.map +1 -0
- package/dist/services/oAuth2Granters.mjs +127 -0
- package/dist/services/oAuth2Granters.mjs.map +1 -0
- package/dist/services/oAuth2PasswordGranter.d.ts +16 -0
- package/dist/services/oAuth2PasswordGranter.js +55 -0
- package/dist/services/oAuth2PasswordGranter.js.map +1 -0
- package/dist/services/oAuth2PasswordGranter.mjs +40 -0
- package/dist/services/oAuth2PasswordGranter.mjs.map +1 -0
- package/dist/services/oAuth2RefreshTokenGranter.d.ts +15 -0
- package/dist/services/oAuth2RefreshTokenGranter.js +62 -0
- package/dist/services/oAuth2RefreshTokenGranter.js.map +1 -0
- package/dist/services/oAuth2RefreshTokenGranter.mjs +47 -0
- package/dist/services/oAuth2RefreshTokenGranter.mjs.map +1 -0
- package/dist/services/oAuth2TokenGranter.d.ts +15 -0
- package/dist/services/oAuth2TokenGranter.js +92 -0
- package/dist/services/oAuth2TokenGranter.js.map +1 -0
- package/dist/services/oAuth2TokenGranter.mjs +78 -0
- package/dist/services/oAuth2TokenGranter.mjs.map +1 -0
- package/package.json +205 -0
- package/src/__snapshots__/index.test.ts.snap +396 -0
- package/src/handlers/__snapshots__/getOAuth2Authorize.test.ts.snap +38 -0
- package/src/handlers/__snapshots__/postOAuth2Acknowledge.test.ts.snap +61 -0
- package/src/handlers/__snapshots__/postOAuth2Token.test.ts.snap +52 -0
- package/src/handlers/getOAuth2Authorize.test.ts +142 -0
- package/src/handlers/getOAuth2Authorize.ts +222 -0
- package/src/handlers/postAuthLogin.test.ts +88 -0
- package/src/handlers/postAuthLogin.ts +123 -0
- package/src/handlers/postAuthLogout.test.ts +33 -0
- package/src/handlers/postAuthLogout.ts +38 -0
- package/src/handlers/postAuthRefresh.test.ts +97 -0
- package/src/handlers/postAuthRefresh.ts +146 -0
- package/src/handlers/postOAuth2Acknowledge.test.ts +175 -0
- package/src/handlers/postOAuth2Acknowledge.ts +209 -0
- package/src/handlers/postOAuth2Token.test.ts +153 -0
- package/src/handlers/postOAuth2Token.ts +302 -0
- package/src/index.test.ts +936 -0
- package/src/index.ts +105 -0
- package/src/services/__snapshots__/oAuth2CodeGranter.test.ts.snap +49 -0
- package/src/services/authCookies.test.ts +93 -0
- package/src/services/authCookies.ts +82 -0
- package/src/services/oAuth2ClientCredentialsGranter.ts +76 -0
- package/src/services/oAuth2CodeGranter.test.ts +94 -0
- package/src/services/oAuth2CodeGranter.ts +133 -0
- package/src/services/oAuth2Granters.ts +274 -0
- package/src/services/oAuth2PasswordGranter.ts +75 -0
- package/src/services/oAuth2RefreshTokenGranter.ts +77 -0
- package/src/services/oAuth2TokenGranter.ts +105 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { autoHandler } from 'knifecycle';
|
|
2
|
+
import YHTTPError from 'yhttperror';
|
|
3
|
+
import initPostOauth2Token from './postOAuth2Token';
|
|
4
|
+
import { AUTH_API_PREFIX } from '../services/authCookies';
|
|
5
|
+
import type {
|
|
6
|
+
WhookAPIHandlerDefinition,
|
|
7
|
+
WhookAPIParameterDefinition,
|
|
8
|
+
} from '@whook/whook';
|
|
9
|
+
import type {
|
|
10
|
+
AuthCookiesService,
|
|
11
|
+
AuthHandlersConfig,
|
|
12
|
+
} from '../services/authCookies';
|
|
13
|
+
import type { AsyncReturnType } from 'type-fest';
|
|
14
|
+
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
15
|
+
|
|
16
|
+
export const authCookieHeaderParameter: WhookAPIParameterDefinition<string> = {
|
|
17
|
+
name: 'cookie',
|
|
18
|
+
example: 'access_token=an_access_token; refresh_token=a_refresh_token;',
|
|
19
|
+
parameter: {
|
|
20
|
+
name: 'cookie',
|
|
21
|
+
in: 'header',
|
|
22
|
+
required: false,
|
|
23
|
+
schema: {
|
|
24
|
+
type: 'string',
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const definition: WhookAPIHandlerDefinition = {
|
|
30
|
+
method: 'post',
|
|
31
|
+
path: `${AUTH_API_PREFIX}/refresh`,
|
|
32
|
+
operation: {
|
|
33
|
+
operationId: 'postAuthRefresh',
|
|
34
|
+
summary: 'Refreshs a user auth',
|
|
35
|
+
tags: ['auth'],
|
|
36
|
+
'x-whook': {
|
|
37
|
+
disabled: true,
|
|
38
|
+
},
|
|
39
|
+
parameters: [
|
|
40
|
+
{
|
|
41
|
+
$ref: `#/components/parameters/${authCookieHeaderParameter.name}`,
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
requestBody: {
|
|
45
|
+
required: true,
|
|
46
|
+
content: {
|
|
47
|
+
'application/json': {
|
|
48
|
+
schema: {
|
|
49
|
+
type: 'object',
|
|
50
|
+
required: [],
|
|
51
|
+
properties: {
|
|
52
|
+
scope: { type: 'string' },
|
|
53
|
+
remember: { type: 'boolean' },
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
responses: {
|
|
60
|
+
200: {
|
|
61
|
+
description: 'Successfuly refreshed.',
|
|
62
|
+
content: {
|
|
63
|
+
'application/json': {
|
|
64
|
+
schema: {
|
|
65
|
+
type: 'object',
|
|
66
|
+
properties: {
|
|
67
|
+
access_token: { type: 'string' },
|
|
68
|
+
expiration_date: { type: 'string' },
|
|
69
|
+
expires_in: { type: 'string' },
|
|
70
|
+
token_type: { type: 'string' },
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export default autoHandler(postAuthRefresh);
|
|
81
|
+
|
|
82
|
+
async function postAuthRefresh<
|
|
83
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
84
|
+
>(
|
|
85
|
+
{
|
|
86
|
+
ROOT_AUTHENTICATION_DATA,
|
|
87
|
+
authCookies,
|
|
88
|
+
postOAuth2Token,
|
|
89
|
+
}: AuthHandlersConfig<AUTHENTICATION_DATA> & {
|
|
90
|
+
authCookies: AuthCookiesService;
|
|
91
|
+
postOAuth2Token: AsyncReturnType<typeof initPostOauth2Token>;
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
body,
|
|
95
|
+
cookie = '',
|
|
96
|
+
}: {
|
|
97
|
+
body: {
|
|
98
|
+
scope: string;
|
|
99
|
+
remember: boolean;
|
|
100
|
+
};
|
|
101
|
+
cookie?: string;
|
|
102
|
+
},
|
|
103
|
+
) {
|
|
104
|
+
const parsedCookies = authCookies.parse(cookie);
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
if (!parsedCookies.refresh_token) {
|
|
108
|
+
throw new YHTTPError(401, 'E_REFRESH_COOKIE', cookie);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const response = await postOAuth2Token({
|
|
112
|
+
body: {
|
|
113
|
+
grant_type: 'refresh_token',
|
|
114
|
+
scope: body.scope,
|
|
115
|
+
refresh_token: parsedCookies.refresh_token,
|
|
116
|
+
},
|
|
117
|
+
authenticationData: ROOT_AUTHENTICATION_DATA,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
...response,
|
|
122
|
+
headers: {
|
|
123
|
+
...(response.headers || {}),
|
|
124
|
+
'Set-Cookie': authCookies.build(
|
|
125
|
+
response.status === 200 ? response.body : undefined,
|
|
126
|
+
{ session: !body.remember },
|
|
127
|
+
),
|
|
128
|
+
},
|
|
129
|
+
body: {
|
|
130
|
+
access_token: response.body.access_token,
|
|
131
|
+
expiration_date: response.body.expiration_date,
|
|
132
|
+
expires_in: response.body.expires_in,
|
|
133
|
+
token_type: response.body.token_type,
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
} catch (err) {
|
|
137
|
+
const newErr = YHTTPError.wrap(err);
|
|
138
|
+
|
|
139
|
+
newErr.headers = {
|
|
140
|
+
...(err.headers || {}),
|
|
141
|
+
'Set-Cookie': authCookies.build(),
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
throw newErr;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import initPostOAuth2Acknowledge from './postOAuth2Acknowledge';
|
|
2
|
+
import YError from 'yerror';
|
|
3
|
+
import { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';
|
|
4
|
+
import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters';
|
|
5
|
+
import type { OAuth2GranterService } from '../services/oAuth2Granters';
|
|
6
|
+
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
7
|
+
|
|
8
|
+
describe('postOAuth2Acknowledge', () => {
|
|
9
|
+
const ERRORS_DESCRIPTORS = {
|
|
10
|
+
...DEFAULT_ERRORS_DESCRIPTORS,
|
|
11
|
+
...OAUTH2_ERRORS_DESCRIPTORS,
|
|
12
|
+
};
|
|
13
|
+
const checkApplication = jest.fn();
|
|
14
|
+
const log = jest.fn();
|
|
15
|
+
const codeGranter = {
|
|
16
|
+
type: 'code',
|
|
17
|
+
authorizer: { responseType: 'code', authorize: jest.fn() },
|
|
18
|
+
acknowledger: { acknowledgmentType: 'code', acknowledge: jest.fn() },
|
|
19
|
+
authenticator: {
|
|
20
|
+
grantType: 'authorization_code',
|
|
21
|
+
authenticate: jest.fn(),
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
const tokenGranter = {
|
|
25
|
+
type: 'token',
|
|
26
|
+
authorizer: { responseType: 'token', authorize: jest.fn() },
|
|
27
|
+
acknowledger: { acknowledgmentType: 'token', acknowledge: jest.fn() },
|
|
28
|
+
};
|
|
29
|
+
const oAuth2Granters: OAuth2GranterService[] = [codeGranter, tokenGranter];
|
|
30
|
+
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
checkApplication.mockReset();
|
|
33
|
+
log.mockReset();
|
|
34
|
+
[
|
|
35
|
+
codeGranter.authorizer.authorize,
|
|
36
|
+
codeGranter.acknowledger.acknowledge,
|
|
37
|
+
codeGranter.authenticator.authenticate,
|
|
38
|
+
tokenGranter.authorizer.authorize,
|
|
39
|
+
tokenGranter.acknowledger.acknowledge,
|
|
40
|
+
].forEach((mock) => mock.mockReset());
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('should redirect', async () => {
|
|
44
|
+
[
|
|
45
|
+
codeGranter.authorizer.authorize,
|
|
46
|
+
codeGranter.authenticator.authenticate,
|
|
47
|
+
tokenGranter.authorizer.authorize,
|
|
48
|
+
tokenGranter.acknowledger.acknowledge,
|
|
49
|
+
].forEach((mock) =>
|
|
50
|
+
mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
|
|
51
|
+
);
|
|
52
|
+
codeGranter.acknowledger.acknowledge.mockResolvedValueOnce({
|
|
53
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
54
|
+
redirectURI: 'http://lol',
|
|
55
|
+
scope: 'user',
|
|
56
|
+
userId: 1,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const postOAuth2Acknowledge = await initPostOAuth2Acknowledge<
|
|
60
|
+
BaseAuthenticationData & {
|
|
61
|
+
userId: number;
|
|
62
|
+
}
|
|
63
|
+
>({
|
|
64
|
+
ERRORS_DESCRIPTORS,
|
|
65
|
+
oAuth2Granters,
|
|
66
|
+
checkApplication,
|
|
67
|
+
log,
|
|
68
|
+
});
|
|
69
|
+
const response = await postOAuth2Acknowledge({
|
|
70
|
+
authenticationData: {
|
|
71
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
72
|
+
scope: 'auth',
|
|
73
|
+
userId: 1,
|
|
74
|
+
},
|
|
75
|
+
body: {
|
|
76
|
+
responseType: 'code',
|
|
77
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
78
|
+
redirectURI: 'https://www.example.com',
|
|
79
|
+
scope: 'user',
|
|
80
|
+
state: 'bancal',
|
|
81
|
+
acknowledged: true,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
expect({
|
|
85
|
+
response,
|
|
86
|
+
}).toMatchInlineSnapshot(`
|
|
87
|
+
Object {
|
|
88
|
+
"response": Object {
|
|
89
|
+
"headers": Object {
|
|
90
|
+
"location": "https://www.example.com/?client_id=abbacaca-abba-caca-abba-cacaabbacaca&scope=user&state=bancal&redirect_uri=http%3A%2F%2Flol&user_id=1",
|
|
91
|
+
},
|
|
92
|
+
"status": 302,
|
|
93
|
+
},
|
|
94
|
+
}
|
|
95
|
+
`);
|
|
96
|
+
expect({
|
|
97
|
+
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
|
|
98
|
+
checkApplicationCalls: checkApplication.mock.calls,
|
|
99
|
+
codeGranterAuthorizerAuthorizeCalls:
|
|
100
|
+
codeGranter.authorizer.authorize.mock.calls,
|
|
101
|
+
codeGranterAcknowledgerAcknowledgeCalls:
|
|
102
|
+
codeGranter.acknowledger.acknowledge.mock.calls,
|
|
103
|
+
codeGranterAuthenticatorAuthenticateCalls:
|
|
104
|
+
codeGranter.authenticator.authenticate.mock.calls,
|
|
105
|
+
tokenGranterAuthorizerAuthorizeCalls:
|
|
106
|
+
tokenGranter.authorizer.authorize.mock.calls,
|
|
107
|
+
tokenGranterAcknowledgerAcknowledgeCalls:
|
|
108
|
+
tokenGranter.acknowledger.acknowledge.mock.calls,
|
|
109
|
+
}).toMatchSnapshot();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('should redirect errors too', async () => {
|
|
113
|
+
[
|
|
114
|
+
codeGranter.authorizer.authorize,
|
|
115
|
+
codeGranter.authenticator.authenticate,
|
|
116
|
+
tokenGranter.authorizer.authorize,
|
|
117
|
+
tokenGranter.acknowledger.acknowledge,
|
|
118
|
+
codeGranter.acknowledger.acknowledge,
|
|
119
|
+
].forEach((mock) =>
|
|
120
|
+
mock.mockRejectedValueOnce(new YError('E_NOT_SUPPOSED_TO_BE_HERE')),
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
const postOAuth2Acknowledge = await initPostOAuth2Acknowledge<
|
|
124
|
+
BaseAuthenticationData & {
|
|
125
|
+
userId: number;
|
|
126
|
+
}
|
|
127
|
+
>({
|
|
128
|
+
ERRORS_DESCRIPTORS,
|
|
129
|
+
oAuth2Granters,
|
|
130
|
+
checkApplication,
|
|
131
|
+
log,
|
|
132
|
+
});
|
|
133
|
+
const response = await postOAuth2Acknowledge({
|
|
134
|
+
authenticationData: {
|
|
135
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
136
|
+
scope: 'auth',
|
|
137
|
+
userId: 1,
|
|
138
|
+
},
|
|
139
|
+
body: {
|
|
140
|
+
responseType: 'yolo',
|
|
141
|
+
clientId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
142
|
+
redirectURI: 'https://www.example.com',
|
|
143
|
+
scope: 'user',
|
|
144
|
+
state: 'bancal',
|
|
145
|
+
acknowledged: true,
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
expect({
|
|
149
|
+
response,
|
|
150
|
+
}).toMatchInlineSnapshot(`
|
|
151
|
+
Object {
|
|
152
|
+
"response": Object {
|
|
153
|
+
"headers": Object {
|
|
154
|
+
"location": "https://www.example.com/?error=unsupported_response_type&error_decription=Type+%22yolo%22+not+supported.",
|
|
155
|
+
},
|
|
156
|
+
"status": 302,
|
|
157
|
+
},
|
|
158
|
+
}
|
|
159
|
+
`);
|
|
160
|
+
expect({
|
|
161
|
+
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
|
|
162
|
+
checkApplicationCalls: checkApplication.mock.calls,
|
|
163
|
+
codeGranterAuthorizerAuthorizeCalls:
|
|
164
|
+
codeGranter.authorizer.authorize.mock.calls,
|
|
165
|
+
codeGranterAcknowledgerAcknowledgeCalls:
|
|
166
|
+
codeGranter.acknowledger.acknowledge.mock.calls,
|
|
167
|
+
codeGranterAuthenticatorAuthenticateCalls:
|
|
168
|
+
codeGranter.authenticator.authenticate.mock.calls,
|
|
169
|
+
tokenGranterAuthorizerAuthorizeCalls:
|
|
170
|
+
tokenGranter.authorizer.authorize.mock.calls,
|
|
171
|
+
tokenGranterAcknowledgerAcknowledgeCalls:
|
|
172
|
+
tokenGranter.acknowledger.acknowledge.mock.calls,
|
|
173
|
+
}).toMatchSnapshot();
|
|
174
|
+
});
|
|
175
|
+
});
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { autoHandler } from 'knifecycle';
|
|
2
|
+
import YError from 'yerror';
|
|
3
|
+
import { setURLError } from './getOAuth2Authorize';
|
|
4
|
+
import type {
|
|
5
|
+
WhookAPIHandlerDefinition,
|
|
6
|
+
WhookErrorsDescriptors,
|
|
7
|
+
WhookAPIOperationConfig,
|
|
8
|
+
} from '@whook/whook';
|
|
9
|
+
import type {
|
|
10
|
+
CheckApplicationService,
|
|
11
|
+
OAuth2GranterService,
|
|
12
|
+
} from '../services/oAuth2Granters';
|
|
13
|
+
import type { LogService } from 'common-services';
|
|
14
|
+
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
15
|
+
|
|
16
|
+
/* Architecture Note #2: OAuth2 acknowledge
|
|
17
|
+
This endpoint is to be used by the authentication SSR frontend
|
|
18
|
+
to acknowlege that the user accepted the client request in it.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export const definition: WhookAPIHandlerDefinition = {
|
|
22
|
+
method: 'post',
|
|
23
|
+
path: '/oauth2/acknowledge',
|
|
24
|
+
operation: {
|
|
25
|
+
operationId: 'postOAuth2Acknowledge',
|
|
26
|
+
summary: `Implements the logic that allow the authentication frontend
|
|
27
|
+
to get the [Redirection Endpoint](https://tools.ietf.org/html/rfc6749#section-3.1.2).`,
|
|
28
|
+
tags: ['oauth2'],
|
|
29
|
+
'x-whook': {
|
|
30
|
+
private: true,
|
|
31
|
+
} as WhookAPIOperationConfig,
|
|
32
|
+
requestBody: {
|
|
33
|
+
required: true,
|
|
34
|
+
content: {
|
|
35
|
+
'application/json': {
|
|
36
|
+
schema: {
|
|
37
|
+
type: 'object',
|
|
38
|
+
required: [
|
|
39
|
+
'responseType',
|
|
40
|
+
'clientId',
|
|
41
|
+
'redirectURI',
|
|
42
|
+
'scope',
|
|
43
|
+
'state',
|
|
44
|
+
'acknowledged',
|
|
45
|
+
],
|
|
46
|
+
properties: {
|
|
47
|
+
responseType: {
|
|
48
|
+
type: 'string',
|
|
49
|
+
},
|
|
50
|
+
clientId: {
|
|
51
|
+
type: 'string',
|
|
52
|
+
},
|
|
53
|
+
redirectURI: {
|
|
54
|
+
type: 'string',
|
|
55
|
+
pattern: '^https?://',
|
|
56
|
+
format: 'uri',
|
|
57
|
+
},
|
|
58
|
+
scope: {
|
|
59
|
+
type: 'string',
|
|
60
|
+
description:
|
|
61
|
+
'See https://tools.ietf.org/html/rfc6749#section-3.3',
|
|
62
|
+
},
|
|
63
|
+
state: {
|
|
64
|
+
type: 'string',
|
|
65
|
+
},
|
|
66
|
+
acknowledged: {
|
|
67
|
+
type: 'boolean',
|
|
68
|
+
description:
|
|
69
|
+
'Wether the user acknowledged the delegation or not.',
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
responses: {
|
|
77
|
+
'201': {
|
|
78
|
+
description: 'Redirection endpoint URI computed.',
|
|
79
|
+
content: {
|
|
80
|
+
'application/json': {
|
|
81
|
+
schema: {
|
|
82
|
+
type: 'object',
|
|
83
|
+
required: ['redirectURI'],
|
|
84
|
+
properties: {
|
|
85
|
+
redirectURI: {
|
|
86
|
+
type: 'string',
|
|
87
|
+
pattern: '^https?://',
|
|
88
|
+
format: 'uri',
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export default autoHandler(postOAuth2Acknowledge);
|
|
100
|
+
|
|
101
|
+
async function postOAuth2Acknowledge<
|
|
102
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
103
|
+
>(
|
|
104
|
+
{
|
|
105
|
+
ERRORS_DESCRIPTORS,
|
|
106
|
+
oAuth2Granters,
|
|
107
|
+
checkApplication,
|
|
108
|
+
log,
|
|
109
|
+
}: {
|
|
110
|
+
ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
|
|
111
|
+
oAuth2Granters: OAuth2GranterService[];
|
|
112
|
+
checkApplication: CheckApplicationService;
|
|
113
|
+
log: LogService;
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
authenticationData,
|
|
117
|
+
body: {
|
|
118
|
+
responseType,
|
|
119
|
+
clientId,
|
|
120
|
+
redirectURI,
|
|
121
|
+
scope: demandedScope,
|
|
122
|
+
state,
|
|
123
|
+
acknowledged = false,
|
|
124
|
+
...additionalParameters
|
|
125
|
+
},
|
|
126
|
+
}: {
|
|
127
|
+
authenticationData: AUTHENTICATION_DATA;
|
|
128
|
+
body: {
|
|
129
|
+
responseType: string;
|
|
130
|
+
clientId: string;
|
|
131
|
+
redirectURI: string;
|
|
132
|
+
scope: string;
|
|
133
|
+
state: string;
|
|
134
|
+
acknowledged: boolean;
|
|
135
|
+
[name: string]: unknown;
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
) {
|
|
139
|
+
if (!authenticationData) {
|
|
140
|
+
throw new YError('E_UNAUTHORIZED');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Here we check the applicationId has the right to authenticate a user
|
|
144
|
+
// with the special type 'root'
|
|
145
|
+
await checkApplication({
|
|
146
|
+
applicationId: authenticationData.applicationId,
|
|
147
|
+
type: 'root',
|
|
148
|
+
scope: '',
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const url = new URL(redirectURI);
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
if (!acknowledged) {
|
|
155
|
+
throw new YError('E_ACCESS_DENIED', clientId);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const granter = oAuth2Granters.find(
|
|
159
|
+
(granter) =>
|
|
160
|
+
granter.acknowledger &&
|
|
161
|
+
granter.acknowledger.acknowledgmentType === responseType,
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
if (!granter) {
|
|
165
|
+
throw new YError('E_UNKNOWN_ACKNOWLEDGOR_TYPE', responseType);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const { applicationId, scope, ...additionalProperties } =
|
|
169
|
+
await granter.acknowledger.acknowledge(
|
|
170
|
+
authenticationData,
|
|
171
|
+
{
|
|
172
|
+
clientId,
|
|
173
|
+
redirectURI,
|
|
174
|
+
scope: demandedScope,
|
|
175
|
+
},
|
|
176
|
+
additionalParameters,
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
url.searchParams.set('client_id', applicationId);
|
|
180
|
+
url.searchParams.set('scope', scope);
|
|
181
|
+
url.searchParams.set('state', state);
|
|
182
|
+
Object.keys(additionalProperties).forEach((key) =>
|
|
183
|
+
url.searchParams.set(snakeCase(key), additionalProperties[key] as string),
|
|
184
|
+
);
|
|
185
|
+
} catch (err) {
|
|
186
|
+
log('debug', '👫 - OAuth2 acknowledge error', err.code);
|
|
187
|
+
log('debug-stack', err.stack);
|
|
188
|
+
|
|
189
|
+
setURLError(
|
|
190
|
+
url,
|
|
191
|
+
err,
|
|
192
|
+
ERRORS_DESCRIPTORS[err.code] || ERRORS_DESCRIPTORS.E_OAUTH2,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return {
|
|
197
|
+
status: 302,
|
|
198
|
+
headers: {
|
|
199
|
+
location: url.href,
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function snakeCase(s: string): string {
|
|
205
|
+
return s
|
|
206
|
+
.split(/(?=(?<![A-Z])[A-Z])|[^a-zA-Z]+/)
|
|
207
|
+
.map((s) => s.toLowerCase())
|
|
208
|
+
.join('_');
|
|
209
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import initPostOAuth2Token from './postOAuth2Token';
|
|
2
|
+
import YError from 'yerror';
|
|
3
|
+
import type { OAuth2GranterService } from '..';
|
|
4
|
+
|
|
5
|
+
describe('postOAuth2Token', () => {
|
|
6
|
+
const log = jest.fn();
|
|
7
|
+
const time = jest.fn();
|
|
8
|
+
const oAuth2AccessToken = { create: jest.fn(), check: jest.fn() };
|
|
9
|
+
const oAuth2RefreshToken = { create: jest.fn(), check: jest.fn() };
|
|
10
|
+
const codeGranter = {
|
|
11
|
+
type: 'code',
|
|
12
|
+
authorizer: { responseType: 'code', authorize: jest.fn() },
|
|
13
|
+
acknowledger: { acknowledgmentType: 'code', acknowledge: jest.fn() },
|
|
14
|
+
authenticator: {
|
|
15
|
+
grantType: 'authorization_code',
|
|
16
|
+
authenticate: jest.fn(),
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
const passwordGranter = {
|
|
20
|
+
type: 'password',
|
|
21
|
+
authenticator: {
|
|
22
|
+
grantType: 'password',
|
|
23
|
+
authenticate: jest.fn(),
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
const clientCredentialsGranter = {
|
|
27
|
+
type: 'client_credentials',
|
|
28
|
+
authenticator: {
|
|
29
|
+
grantType: 'client_credentials',
|
|
30
|
+
authenticate: jest.fn(),
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
const tokenGranter = {
|
|
34
|
+
type: 'token',
|
|
35
|
+
authorizer: { responseType: 'token', authorize: jest.fn() },
|
|
36
|
+
acknowledger: { acknowledgmentType: 'token', acknowledge: jest.fn() },
|
|
37
|
+
};
|
|
38
|
+
const refreshTokenGranter = {
|
|
39
|
+
type: 'refresh',
|
|
40
|
+
authenticator: {
|
|
41
|
+
grantType: 'refresh',
|
|
42
|
+
authenticate: jest.fn(),
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
const oAuth2Granters: OAuth2GranterService[] = [
|
|
46
|
+
codeGranter,
|
|
47
|
+
tokenGranter,
|
|
48
|
+
passwordGranter,
|
|
49
|
+
refreshTokenGranter,
|
|
50
|
+
clientCredentialsGranter,
|
|
51
|
+
];
|
|
52
|
+
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
log.mockReset();
|
|
55
|
+
time.mockReset();
|
|
56
|
+
oAuth2AccessToken.create.mockReset();
|
|
57
|
+
oAuth2AccessToken.check.mockReset();
|
|
58
|
+
oAuth2RefreshToken.create.mockReset();
|
|
59
|
+
oAuth2RefreshToken.check.mockReset();
|
|
60
|
+
codeGranter.authorizer.authorize.mockReset();
|
|
61
|
+
codeGranter.acknowledger.acknowledge.mockReset();
|
|
62
|
+
codeGranter.authenticator.authenticate.mockReset();
|
|
63
|
+
tokenGranter.authorizer.authorize.mockReset();
|
|
64
|
+
tokenGranter.acknowledger.acknowledge.mockReset();
|
|
65
|
+
passwordGranter.authenticator.authenticate.mockReset();
|
|
66
|
+
refreshTokenGranter.authenticator.authenticate.mockReset();
|
|
67
|
+
clientCredentialsGranter.authenticator.authenticate.mockReset();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('should create a token', async () => {
|
|
71
|
+
time.mockReturnValue(new Date('2010-03-06T00:00:00Z').getTime());
|
|
72
|
+
oAuth2AccessToken.create.mockResolvedValueOnce({
|
|
73
|
+
token: 'an_access_token',
|
|
74
|
+
expiresAt: Date.parse('2010-03-07T00:00:00Z'),
|
|
75
|
+
});
|
|
76
|
+
oAuth2RefreshToken.create.mockResolvedValueOnce({
|
|
77
|
+
token: 'a_refresh_token',
|
|
78
|
+
expiresAt: Date.parse('2180-03-06T00:00:00Z'),
|
|
79
|
+
});
|
|
80
|
+
codeGranter.authorizer.authorize.mockResolvedValueOnce({
|
|
81
|
+
applicationId: '',
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const postOAuth2Token = await initPostOAuth2Token({
|
|
85
|
+
oAuth2Granters,
|
|
86
|
+
oAuth2AccessToken,
|
|
87
|
+
oAuth2RefreshToken,
|
|
88
|
+
time,
|
|
89
|
+
log,
|
|
90
|
+
});
|
|
91
|
+
const response = await postOAuth2Token({
|
|
92
|
+
authenticationData: {
|
|
93
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
94
|
+
scope: 'user',
|
|
95
|
+
},
|
|
96
|
+
body: {
|
|
97
|
+
grant_type: 'authorization_code',
|
|
98
|
+
code: '007',
|
|
99
|
+
client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
100
|
+
redirect_uri: 'http://lol',
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
expect({
|
|
104
|
+
response,
|
|
105
|
+
oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
|
|
106
|
+
oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
|
|
107
|
+
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
|
|
108
|
+
}).toMatchSnapshot();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('should fail with a bad grant type', async () => {
|
|
112
|
+
const postOAuth2Token = await initPostOAuth2Token({
|
|
113
|
+
oAuth2Granters,
|
|
114
|
+
oAuth2AccessToken,
|
|
115
|
+
oAuth2RefreshToken,
|
|
116
|
+
time,
|
|
117
|
+
log,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
await postOAuth2Token({
|
|
122
|
+
authenticationData: {
|
|
123
|
+
applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
124
|
+
scope: 'user',
|
|
125
|
+
},
|
|
126
|
+
body: {
|
|
127
|
+
grant_type: 'yolo',
|
|
128
|
+
code: '007',
|
|
129
|
+
client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
130
|
+
redirect_uri: 'http://lol',
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
throw new YError('E_UNEXPECTED_SUCCESS');
|
|
134
|
+
} catch (err) {
|
|
135
|
+
expect({
|
|
136
|
+
errorCode: err.code,
|
|
137
|
+
errorParams: err.params,
|
|
138
|
+
}).toMatchInlineSnapshot(`
|
|
139
|
+
Object {
|
|
140
|
+
"errorCode": "E_UNKNOWN_AUTHENTICATOR_TYPE",
|
|
141
|
+
"errorParams": Array [
|
|
142
|
+
"yolo",
|
|
143
|
+
],
|
|
144
|
+
}
|
|
145
|
+
`);
|
|
146
|
+
expect({
|
|
147
|
+
oAuth2AccessTokenCreateCalls: oAuth2AccessToken.create.mock.calls,
|
|
148
|
+
oAuth2RefreshTokenCreateCalls: oAuth2RefreshToken.create.mock.calls,
|
|
149
|
+
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
|
|
150
|
+
}).toMatchSnapshot();
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
});
|