@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,222 @@
|
|
|
1
|
+
import { autoHandler } from 'knifecycle';
|
|
2
|
+
import camelCase from 'camelcase';
|
|
3
|
+
import YError from 'yerror';
|
|
4
|
+
import type {
|
|
5
|
+
WhookAPIHandlerDefinition,
|
|
6
|
+
WhookAPIParameterDefinition,
|
|
7
|
+
WhookResponse,
|
|
8
|
+
WhookErrorsDescriptors,
|
|
9
|
+
WhookErrorDescriptor,
|
|
10
|
+
} from '@whook/whook';
|
|
11
|
+
import type {
|
|
12
|
+
OAuth2Options,
|
|
13
|
+
OAuth2GranterService,
|
|
14
|
+
} from '../services/oAuth2Granters';
|
|
15
|
+
import type { LogService } from 'common-services';
|
|
16
|
+
|
|
17
|
+
/* Architecture Note #1: OAuth2 authorize
|
|
18
|
+
This endpoint simply redirect the user to the authentication
|
|
19
|
+
server page by first checking the application details are
|
|
20
|
+
fine.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export const responseTypeParameter: WhookAPIParameterDefinition = {
|
|
24
|
+
name: 'responseType',
|
|
25
|
+
parameter: {
|
|
26
|
+
in: 'query',
|
|
27
|
+
name: 'response_type',
|
|
28
|
+
required: true,
|
|
29
|
+
schema: {
|
|
30
|
+
type: 'string',
|
|
31
|
+
enum: ['code', 'token'],
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
export const clientIdParameter: WhookAPIParameterDefinition = {
|
|
36
|
+
name: 'clientId',
|
|
37
|
+
parameter: {
|
|
38
|
+
in: 'query',
|
|
39
|
+
name: 'client_id',
|
|
40
|
+
required: true,
|
|
41
|
+
schema: {
|
|
42
|
+
type: 'string',
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
export const redirectURIParameter: WhookAPIParameterDefinition = {
|
|
47
|
+
name: 'redirectURI',
|
|
48
|
+
parameter: {
|
|
49
|
+
in: 'query',
|
|
50
|
+
name: 'redirect_uri',
|
|
51
|
+
required: false,
|
|
52
|
+
schema: {
|
|
53
|
+
type: 'string',
|
|
54
|
+
pattern: '^https?://',
|
|
55
|
+
format: 'uri',
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
export const scopeParameter: WhookAPIParameterDefinition = {
|
|
60
|
+
name: 'scope',
|
|
61
|
+
parameter: {
|
|
62
|
+
in: 'query',
|
|
63
|
+
name: 'scope',
|
|
64
|
+
description: 'See https://tools.ietf.org/html/rfc6749#section-3.3',
|
|
65
|
+
required: false,
|
|
66
|
+
schema: {
|
|
67
|
+
type: 'string',
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
export const stateParameter: WhookAPIParameterDefinition = {
|
|
72
|
+
name: 'state',
|
|
73
|
+
parameter: {
|
|
74
|
+
in: 'query',
|
|
75
|
+
name: 'state',
|
|
76
|
+
required: true,
|
|
77
|
+
schema: {
|
|
78
|
+
type: 'string',
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const definition: WhookAPIHandlerDefinition = {
|
|
84
|
+
method: 'get',
|
|
85
|
+
path: '/oauth2/authorize',
|
|
86
|
+
operation: {
|
|
87
|
+
operationId: 'getOAuth2Authorize',
|
|
88
|
+
summary: `Implements the [Authorization Endpoint](https://tools.ietf.org/html/rfc6749#section-3.1)
|
|
89
|
+
as defined per the OAuth2 RFC.`,
|
|
90
|
+
tags: ['oauth2'],
|
|
91
|
+
parameters: [
|
|
92
|
+
{
|
|
93
|
+
$ref: `#/components/parameters/${responseTypeParameter.name}`,
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
$ref: `#/components/parameters/${clientIdParameter.name}`,
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
$ref: `#/components/parameters/${redirectURIParameter.name}`,
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
$ref: `#/components/parameters/${scopeParameter.name}`,
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
$ref: `#/components/parameters/${stateParameter.name}`,
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
responses: {
|
|
109
|
+
'302': {
|
|
110
|
+
description: 'Redirects the user to the authorization interface.',
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export default autoHandler(getOAuth2Authorize);
|
|
117
|
+
|
|
118
|
+
async function getOAuth2Authorize(
|
|
119
|
+
{
|
|
120
|
+
OAUTH2,
|
|
121
|
+
ERRORS_DESCRIPTORS,
|
|
122
|
+
oAuth2Granters,
|
|
123
|
+
log,
|
|
124
|
+
}: {
|
|
125
|
+
OAUTH2: OAuth2Options;
|
|
126
|
+
ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
|
|
127
|
+
oAuth2Granters: OAuth2GranterService[];
|
|
128
|
+
log: LogService;
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
response_type: responseType,
|
|
132
|
+
client_id: clientId,
|
|
133
|
+
redirect_uri: demandedRedirectURI = '',
|
|
134
|
+
scope: demandedScope = '',
|
|
135
|
+
state,
|
|
136
|
+
...authorizeParameters
|
|
137
|
+
}: {
|
|
138
|
+
response_type: string;
|
|
139
|
+
client_id: string;
|
|
140
|
+
redirect_uri?: string;
|
|
141
|
+
scope?: string;
|
|
142
|
+
state: string;
|
|
143
|
+
} & Record<string, unknown>,
|
|
144
|
+
): Promise<WhookResponse> {
|
|
145
|
+
const url = new URL(OAUTH2.authenticateURL);
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
const granter = oAuth2Granters.find(
|
|
149
|
+
(granter) =>
|
|
150
|
+
granter.authorizer && granter.authorizer.responseType === responseType,
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
if (!granter) {
|
|
154
|
+
throw new YError('E_UNKNOWN_AUTHORIZER_TYPE', responseType);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const { applicationId, redirectURI, scope } =
|
|
158
|
+
await granter.authorizer.authorize(
|
|
159
|
+
{
|
|
160
|
+
clientId,
|
|
161
|
+
redirectURI: demandedRedirectURI,
|
|
162
|
+
scope: demandedScope,
|
|
163
|
+
},
|
|
164
|
+
camelCaseObjectProperties(authorizeParameters),
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
url.searchParams.set('type', responseType);
|
|
168
|
+
url.searchParams.set('redirect_uri', redirectURI);
|
|
169
|
+
url.searchParams.set('scope', scope);
|
|
170
|
+
url.searchParams.set('client_id', applicationId);
|
|
171
|
+
} catch (err) {
|
|
172
|
+
log('debug', '👫 - OAuth2 initialization error', err.code);
|
|
173
|
+
log('debug-stack', err.stack);
|
|
174
|
+
|
|
175
|
+
url.searchParams.set('redirect_uri', demandedRedirectURI);
|
|
176
|
+
setURLError(
|
|
177
|
+
url,
|
|
178
|
+
err,
|
|
179
|
+
ERRORS_DESCRIPTORS[err.code] || ERRORS_DESCRIPTORS.E_OAUTH2,
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (state) {
|
|
184
|
+
url.searchParams.set('state', state);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
status: 302,
|
|
189
|
+
headers: {
|
|
190
|
+
location: url.href,
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// The OAuth2 standard uses snake case names so we are
|
|
196
|
+
// converting them to the project standards asap
|
|
197
|
+
export function camelCaseObjectProperties<T>(
|
|
198
|
+
object: Record<string, T>,
|
|
199
|
+
): Record<string, T> {
|
|
200
|
+
return Object.keys(object).reduce((camelCasedObject, key) => {
|
|
201
|
+
const newKey = key === 'redirect_uri' ? 'redirectURI' : camelCase(key);
|
|
202
|
+
|
|
203
|
+
camelCasedObject[newKey] = object[key];
|
|
204
|
+
return camelCasedObject;
|
|
205
|
+
}, {});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function setURLError(
|
|
209
|
+
url: URL,
|
|
210
|
+
err: YError | Error,
|
|
211
|
+
oAuth2Error: WhookErrorDescriptor,
|
|
212
|
+
): void {
|
|
213
|
+
url.searchParams.set('error', oAuth2Error.code);
|
|
214
|
+
if (oAuth2Error.description) {
|
|
215
|
+
url.searchParams.set(
|
|
216
|
+
'error_decription',
|
|
217
|
+
oAuth2Error.description.replace(/\$([0-9]+)/g, (_, paramIndex) => {
|
|
218
|
+
return ((err as YError).params || [])[parseInt(paramIndex, 10)];
|
|
219
|
+
}),
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import initPostAuthLogin from './postAuthLogin';
|
|
2
|
+
|
|
3
|
+
describe('postAuthLogin', () => {
|
|
4
|
+
test('should work', async () => {
|
|
5
|
+
const authCookies = {
|
|
6
|
+
build: jest.fn(),
|
|
7
|
+
};
|
|
8
|
+
const postOAuth2Token = jest.fn();
|
|
9
|
+
const postAuthLogin = await initPostAuthLogin({
|
|
10
|
+
ROOT_AUTHENTICATION_DATA: {
|
|
11
|
+
scope: 'user',
|
|
12
|
+
applicationId: 'root_app_id',
|
|
13
|
+
},
|
|
14
|
+
authCookies,
|
|
15
|
+
postOAuth2Token,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
authCookies.build.mockReturnValueOnce('the_build_cookies');
|
|
19
|
+
postOAuth2Token.mockResolvedValueOnce({
|
|
20
|
+
status: 200,
|
|
21
|
+
body: {
|
|
22
|
+
access_token: 'an_access_token',
|
|
23
|
+
expiration_date: '2020-02-02T20:22:02Z',
|
|
24
|
+
expires_in: 3600,
|
|
25
|
+
token_type: 'bearer',
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const response = await postAuthLogin({
|
|
30
|
+
body: {
|
|
31
|
+
username: 'a_username',
|
|
32
|
+
password: 'a_password',
|
|
33
|
+
remember: false,
|
|
34
|
+
scope: 'user',
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
expect({
|
|
39
|
+
response,
|
|
40
|
+
authCookiesBuildCalls: authCookies.build.mock.calls,
|
|
41
|
+
postOAuth2TokenCalls: postOAuth2Token.mock.calls,
|
|
42
|
+
}).toMatchInlineSnapshot(`
|
|
43
|
+
Object {
|
|
44
|
+
"authCookiesBuildCalls": Array [
|
|
45
|
+
Array [
|
|
46
|
+
Object {
|
|
47
|
+
"access_token": "an_access_token",
|
|
48
|
+
"expiration_date": "2020-02-02T20:22:02Z",
|
|
49
|
+
"expires_in": 3600,
|
|
50
|
+
"token_type": "bearer",
|
|
51
|
+
},
|
|
52
|
+
Object {
|
|
53
|
+
"session": true,
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
],
|
|
57
|
+
"postOAuth2TokenCalls": Array [
|
|
58
|
+
Array [
|
|
59
|
+
Object {
|
|
60
|
+
"authenticationData": Object {
|
|
61
|
+
"applicationId": "root_app_id",
|
|
62
|
+
"scope": "user",
|
|
63
|
+
},
|
|
64
|
+
"body": Object {
|
|
65
|
+
"grant_type": "password",
|
|
66
|
+
"password": "a_password",
|
|
67
|
+
"scope": "user",
|
|
68
|
+
"username": "a_username",
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
],
|
|
73
|
+
"response": Object {
|
|
74
|
+
"body": Object {
|
|
75
|
+
"access_token": "an_access_token",
|
|
76
|
+
"expiration_date": "2020-02-02T20:22:02Z",
|
|
77
|
+
"expires_in": 3600,
|
|
78
|
+
"token_type": "bearer",
|
|
79
|
+
},
|
|
80
|
+
"headers": Object {
|
|
81
|
+
"Set-Cookie": "the_build_cookies",
|
|
82
|
+
},
|
|
83
|
+
"status": 200,
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
`);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
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 { WhookAPIHandlerDefinition } from '@whook/whook';
|
|
6
|
+
import type {
|
|
7
|
+
AuthCookiesService,
|
|
8
|
+
AuthHandlersConfig,
|
|
9
|
+
} from '../services/authCookies';
|
|
10
|
+
import type { AsyncReturnType } from 'type-fest';
|
|
11
|
+
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
12
|
+
|
|
13
|
+
export const definition: WhookAPIHandlerDefinition = {
|
|
14
|
+
method: 'post',
|
|
15
|
+
path: `${AUTH_API_PREFIX}/login`,
|
|
16
|
+
operation: {
|
|
17
|
+
operationId: 'postAuthLogin',
|
|
18
|
+
summary: 'Logs a user in',
|
|
19
|
+
tags: ['auth'],
|
|
20
|
+
'x-whook': {
|
|
21
|
+
disabled: true,
|
|
22
|
+
},
|
|
23
|
+
parameters: [],
|
|
24
|
+
requestBody: {
|
|
25
|
+
required: true,
|
|
26
|
+
content: {
|
|
27
|
+
'application/json': {
|
|
28
|
+
schema: {
|
|
29
|
+
type: 'object',
|
|
30
|
+
required: ['username', 'password'],
|
|
31
|
+
properties: {
|
|
32
|
+
username: { type: 'string' },
|
|
33
|
+
password: { type: 'string' },
|
|
34
|
+
scope: { type: 'string' },
|
|
35
|
+
remember: { type: 'boolean' },
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
responses: {
|
|
42
|
+
200: {
|
|
43
|
+
description: 'Successfuly logged in.',
|
|
44
|
+
content: {
|
|
45
|
+
'application/json': {
|
|
46
|
+
schema: {
|
|
47
|
+
type: 'object',
|
|
48
|
+
properties: {
|
|
49
|
+
access_token: { type: 'string' },
|
|
50
|
+
expiration_date: { type: 'string' },
|
|
51
|
+
expires_in: { type: 'string' },
|
|
52
|
+
token_type: { type: 'string' },
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export default autoHandler(postAuthLogin);
|
|
63
|
+
|
|
64
|
+
async function postAuthLogin<
|
|
65
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
66
|
+
>(
|
|
67
|
+
{
|
|
68
|
+
ROOT_AUTHENTICATION_DATA,
|
|
69
|
+
authCookies,
|
|
70
|
+
postOAuth2Token,
|
|
71
|
+
}: AuthHandlersConfig<AUTHENTICATION_DATA> & {
|
|
72
|
+
authCookies: Pick<AuthCookiesService, 'build'>;
|
|
73
|
+
postOAuth2Token: AsyncReturnType<typeof initPostOauth2Token>;
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
body,
|
|
77
|
+
}: {
|
|
78
|
+
body: {
|
|
79
|
+
username: string;
|
|
80
|
+
password: string;
|
|
81
|
+
scope: string;
|
|
82
|
+
remember: boolean;
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
) {
|
|
86
|
+
try {
|
|
87
|
+
const response = await postOAuth2Token({
|
|
88
|
+
body: {
|
|
89
|
+
grant_type: 'password',
|
|
90
|
+
scope: body.scope,
|
|
91
|
+
username: body.username,
|
|
92
|
+
password: body.password,
|
|
93
|
+
},
|
|
94
|
+
authenticationData: ROOT_AUTHENTICATION_DATA,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
...response,
|
|
99
|
+
headers: {
|
|
100
|
+
...(response.headers || {}),
|
|
101
|
+
'Set-Cookie': authCookies.build(
|
|
102
|
+
response.status === 200 ? response.body : undefined,
|
|
103
|
+
{ session: !body.remember },
|
|
104
|
+
),
|
|
105
|
+
},
|
|
106
|
+
body: {
|
|
107
|
+
access_token: response.body.access_token,
|
|
108
|
+
expiration_date: response.body.expiration_date,
|
|
109
|
+
expires_in: response.body.expires_in,
|
|
110
|
+
token_type: response.body.token_type,
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
} catch (err) {
|
|
114
|
+
const newErr = YHTTPError.wrap(err);
|
|
115
|
+
|
|
116
|
+
newErr.headers = {
|
|
117
|
+
...(err.headers || {}),
|
|
118
|
+
'Set-Cookie': authCookies.build(),
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
throw newErr;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import initPostAuthLogout from './postAuthLogout';
|
|
2
|
+
|
|
3
|
+
describe('postAuthLogout', () => {
|
|
4
|
+
test('should work', async () => {
|
|
5
|
+
const authCookies = {
|
|
6
|
+
build: jest.fn(),
|
|
7
|
+
};
|
|
8
|
+
const postAuthLogout = await initPostAuthLogout({
|
|
9
|
+
authCookies,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
authCookies.build.mockReturnValueOnce('the_build_cookies');
|
|
13
|
+
|
|
14
|
+
const response = await postAuthLogout({});
|
|
15
|
+
|
|
16
|
+
expect({
|
|
17
|
+
response,
|
|
18
|
+
authCookiesBuildCalls: authCookies.build.mock.calls,
|
|
19
|
+
}).toMatchInlineSnapshot(`
|
|
20
|
+
Object {
|
|
21
|
+
"authCookiesBuildCalls": Array [
|
|
22
|
+
Array [],
|
|
23
|
+
],
|
|
24
|
+
"response": Object {
|
|
25
|
+
"headers": Object {
|
|
26
|
+
"Set-Cookie": "the_build_cookies",
|
|
27
|
+
},
|
|
28
|
+
"status": 204,
|
|
29
|
+
},
|
|
30
|
+
}
|
|
31
|
+
`);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { autoHandler } from 'knifecycle';
|
|
2
|
+
import { AUTH_API_PREFIX } from '../services/authCookies';
|
|
3
|
+
import type { WhookAPIHandlerDefinition } from '@whook/whook';
|
|
4
|
+
import type { AuthCookiesService } from '../services/authCookies';
|
|
5
|
+
|
|
6
|
+
export const definition: WhookAPIHandlerDefinition = {
|
|
7
|
+
method: 'post',
|
|
8
|
+
path: `${AUTH_API_PREFIX}/logout`,
|
|
9
|
+
operation: {
|
|
10
|
+
operationId: 'postAuthLogout',
|
|
11
|
+
summary: 'Logs a user out',
|
|
12
|
+
tags: ['auth'],
|
|
13
|
+
'x-whook': {
|
|
14
|
+
disabled: true,
|
|
15
|
+
},
|
|
16
|
+
parameters: [],
|
|
17
|
+
responses: {
|
|
18
|
+
204: {
|
|
19
|
+
description: 'Successfuly logged out!',
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default autoHandler(postAuthLogout);
|
|
26
|
+
|
|
27
|
+
async function postAuthLogout({
|
|
28
|
+
authCookies,
|
|
29
|
+
}: {
|
|
30
|
+
authCookies: Pick<AuthCookiesService, 'build'>;
|
|
31
|
+
}) {
|
|
32
|
+
return {
|
|
33
|
+
status: 204,
|
|
34
|
+
headers: {
|
|
35
|
+
'Set-Cookie': authCookies.build(),
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import initPostAuthRefresh from './postAuthRefresh';
|
|
2
|
+
|
|
3
|
+
describe('postAuthRefresh', () => {
|
|
4
|
+
test('should work', async () => {
|
|
5
|
+
const authCookies = {
|
|
6
|
+
parse: jest.fn(),
|
|
7
|
+
build: jest.fn(),
|
|
8
|
+
};
|
|
9
|
+
const postOAuth2Token = jest.fn();
|
|
10
|
+
const postAuthRefresh = await initPostAuthRefresh({
|
|
11
|
+
ROOT_AUTHENTICATION_DATA: {
|
|
12
|
+
scope: 'user',
|
|
13
|
+
applicationId: 'root_app_id',
|
|
14
|
+
},
|
|
15
|
+
authCookies,
|
|
16
|
+
postOAuth2Token,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
authCookies.parse.mockReturnValueOnce({
|
|
20
|
+
access_token: 'an_access_token',
|
|
21
|
+
refresh_token: 'a_refresh_token',
|
|
22
|
+
});
|
|
23
|
+
authCookies.build.mockReturnValueOnce('the_build_cookies');
|
|
24
|
+
postOAuth2Token.mockResolvedValueOnce({
|
|
25
|
+
status: 200,
|
|
26
|
+
body: {
|
|
27
|
+
access_token: 'an_access_token',
|
|
28
|
+
expiration_date: '2020-02-02T20:22:02Z',
|
|
29
|
+
expires_in: 3600,
|
|
30
|
+
token_type: 'bearer',
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const response = await postAuthRefresh({
|
|
35
|
+
cookie: 'a_given_cookie',
|
|
36
|
+
body: {
|
|
37
|
+
remember: false,
|
|
38
|
+
scope: 'user',
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
expect({
|
|
43
|
+
response,
|
|
44
|
+
authCookiesParseCalls: authCookies.parse.mock.calls,
|
|
45
|
+
authCookiesBuildCalls: authCookies.build.mock.calls,
|
|
46
|
+
postOAuth2TokenCalls: postOAuth2Token.mock.calls,
|
|
47
|
+
}).toMatchInlineSnapshot(`
|
|
48
|
+
Object {
|
|
49
|
+
"authCookiesBuildCalls": Array [
|
|
50
|
+
Array [
|
|
51
|
+
Object {
|
|
52
|
+
"access_token": "an_access_token",
|
|
53
|
+
"expiration_date": "2020-02-02T20:22:02Z",
|
|
54
|
+
"expires_in": 3600,
|
|
55
|
+
"token_type": "bearer",
|
|
56
|
+
},
|
|
57
|
+
Object {
|
|
58
|
+
"session": true,
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
],
|
|
62
|
+
"authCookiesParseCalls": Array [
|
|
63
|
+
Array [
|
|
64
|
+
"a_given_cookie",
|
|
65
|
+
],
|
|
66
|
+
],
|
|
67
|
+
"postOAuth2TokenCalls": Array [
|
|
68
|
+
Array [
|
|
69
|
+
Object {
|
|
70
|
+
"authenticationData": Object {
|
|
71
|
+
"applicationId": "root_app_id",
|
|
72
|
+
"scope": "user",
|
|
73
|
+
},
|
|
74
|
+
"body": Object {
|
|
75
|
+
"grant_type": "refresh_token",
|
|
76
|
+
"refresh_token": "a_refresh_token",
|
|
77
|
+
"scope": "user",
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
],
|
|
82
|
+
"response": Object {
|
|
83
|
+
"body": Object {
|
|
84
|
+
"access_token": "an_access_token",
|
|
85
|
+
"expiration_date": "2020-02-02T20:22:02Z",
|
|
86
|
+
"expires_in": 3600,
|
|
87
|
+
"token_type": "bearer",
|
|
88
|
+
},
|
|
89
|
+
"headers": Object {
|
|
90
|
+
"Set-Cookie": "the_build_cookies",
|
|
91
|
+
},
|
|
92
|
+
"status": 200,
|
|
93
|
+
},
|
|
94
|
+
}
|
|
95
|
+
`);
|
|
96
|
+
});
|
|
97
|
+
});
|