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