@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
|
@@ -4,10 +4,10 @@ import initGetOAuth2Authorize from './getOAuth2Authorize.js';
|
|
|
4
4
|
import { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/whook';
|
|
5
5
|
import { OAUTH2_ERRORS_DESCRIPTORS } from '../services/oAuth2Granters.js';
|
|
6
6
|
import { YError } from 'yerror';
|
|
7
|
-
import type
|
|
8
|
-
import type
|
|
9
|
-
import type
|
|
10
|
-
import type
|
|
7
|
+
import { type OAuth2CodeGranterService } from '../services/oAuth2CodeGranter.js';
|
|
8
|
+
import { type OAuth2TokenGranterService } from '../services/oAuth2TokenGranter.js';
|
|
9
|
+
import { type LogService } from 'common-services';
|
|
10
|
+
import { type OAuth2Options, type OAuth2GranterService } from '../index.js';
|
|
11
11
|
|
|
12
12
|
describe('getOAuth2Authorize', () => {
|
|
13
13
|
const OAUTH2: OAuth2Options = {
|
|
@@ -99,11 +99,13 @@ describe('getOAuth2Authorize', () => {
|
|
|
99
99
|
log,
|
|
100
100
|
});
|
|
101
101
|
const response = await getOAuth2Authorize({
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
102
|
+
query: {
|
|
103
|
+
response_type: 'code',
|
|
104
|
+
client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
105
|
+
redirect_uri: 'https://www.example.com',
|
|
106
|
+
scope: 'user',
|
|
107
|
+
state: 'bancal',
|
|
108
|
+
},
|
|
107
109
|
});
|
|
108
110
|
|
|
109
111
|
expect(response).toMatchInlineSnapshot(`
|
|
@@ -147,11 +149,13 @@ describe('getOAuth2Authorize', () => {
|
|
|
147
149
|
log,
|
|
148
150
|
});
|
|
149
151
|
const response = await getOAuth2Authorize({
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
152
|
+
query: {
|
|
153
|
+
response_type: 'yolo',
|
|
154
|
+
client_id: 'abbacaca-abba-caca-abba-cacaabbacaca',
|
|
155
|
+
redirect_uri: 'https://www.example.com',
|
|
156
|
+
scope: 'user',
|
|
157
|
+
state: 'bancal',
|
|
158
|
+
},
|
|
155
159
|
});
|
|
156
160
|
|
|
157
161
|
expect(response).toMatchInlineSnapshot(`
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { autoService, location } from 'knifecycle';
|
|
2
|
+
import camelCase from 'camelcase';
|
|
3
|
+
import { YError, printStackTrace } from 'yerror';
|
|
4
|
+
import {
|
|
5
|
+
refersTo,
|
|
6
|
+
type WhookRouteDefinition,
|
|
7
|
+
type WhookAPIParameterDefinition,
|
|
8
|
+
type WhookErrorsDescriptors,
|
|
9
|
+
type WhookErrorDescriptor,
|
|
10
|
+
} from '@whook/whook';
|
|
11
|
+
import {
|
|
12
|
+
type OAuth2Options,
|
|
13
|
+
type OAuth2GranterService,
|
|
14
|
+
} from '../services/oAuth2Granters.js';
|
|
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 = {
|
|
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
|
+
} as const satisfies WhookAPIParameterDefinition;
|
|
35
|
+
export const clientIdParameter = {
|
|
36
|
+
name: 'clientId',
|
|
37
|
+
parameter: {
|
|
38
|
+
in: 'query',
|
|
39
|
+
name: 'client_id',
|
|
40
|
+
required: true,
|
|
41
|
+
schema: {
|
|
42
|
+
type: 'string',
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
} as const satisfies WhookAPIParameterDefinition;
|
|
46
|
+
export const redirectURIParameter = {
|
|
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
|
+
} as const satisfies WhookAPIParameterDefinition;
|
|
59
|
+
export const scopeParameter = {
|
|
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
|
+
} as const satisfies WhookAPIParameterDefinition;
|
|
71
|
+
export const stateParameter = {
|
|
72
|
+
name: 'state',
|
|
73
|
+
parameter: {
|
|
74
|
+
in: 'query',
|
|
75
|
+
name: 'state',
|
|
76
|
+
required: true,
|
|
77
|
+
schema: {
|
|
78
|
+
type: 'string',
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
} as const satisfies WhookAPIParameterDefinition;
|
|
82
|
+
|
|
83
|
+
export const definition = {
|
|
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
|
+
refersTo(responseTypeParameter),
|
|
93
|
+
refersTo(clientIdParameter),
|
|
94
|
+
refersTo(redirectURIParameter),
|
|
95
|
+
refersTo(scopeParameter),
|
|
96
|
+
refersTo(stateParameter),
|
|
97
|
+
],
|
|
98
|
+
responses: {
|
|
99
|
+
'302': {
|
|
100
|
+
description: 'Redirects the user to the authorization interface.',
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
} as const satisfies WhookRouteDefinition;
|
|
105
|
+
|
|
106
|
+
// The OAuth2 standard uses snake case names so we are
|
|
107
|
+
// converting them to the project standards asap
|
|
108
|
+
export function camelCaseObjectProperties<T>(
|
|
109
|
+
object: Record<string, T>,
|
|
110
|
+
): Record<string, T> {
|
|
111
|
+
return Object.keys(object).reduce((camelCasedObject, key) => {
|
|
112
|
+
const newKey = key === 'redirect_uri' ? 'redirectURI' : camelCase(key);
|
|
113
|
+
|
|
114
|
+
camelCasedObject[newKey] = object[key];
|
|
115
|
+
return camelCasedObject;
|
|
116
|
+
}, {});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function setURLError(
|
|
120
|
+
url: URL,
|
|
121
|
+
err: YError | Error,
|
|
122
|
+
oAuth2Error: WhookErrorDescriptor,
|
|
123
|
+
): void {
|
|
124
|
+
url.searchParams.set('error', oAuth2Error.code);
|
|
125
|
+
if (oAuth2Error.description) {
|
|
126
|
+
url.searchParams.set(
|
|
127
|
+
'error_decription',
|
|
128
|
+
oAuth2Error.description.replace(/\$([0-9]+)/g, (_, paramIndex) => {
|
|
129
|
+
return ((err as YError).params || [])[parseInt(paramIndex, 10)];
|
|
130
|
+
}),
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async function initGetOAuth2Authorize({
|
|
136
|
+
OAUTH2,
|
|
137
|
+
ERRORS_DESCRIPTORS,
|
|
138
|
+
oAuth2Granters,
|
|
139
|
+
log,
|
|
140
|
+
}: {
|
|
141
|
+
OAUTH2: OAuth2Options;
|
|
142
|
+
ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
|
|
143
|
+
oAuth2Granters: OAuth2GranterService[];
|
|
144
|
+
log: LogService;
|
|
145
|
+
}) {
|
|
146
|
+
return async ({
|
|
147
|
+
query: {
|
|
148
|
+
response_type: responseType,
|
|
149
|
+
client_id: clientId,
|
|
150
|
+
redirect_uri: demandedRedirectURI = '',
|
|
151
|
+
scope: demandedScope = '',
|
|
152
|
+
state,
|
|
153
|
+
...authorizeParameters
|
|
154
|
+
},
|
|
155
|
+
}: {
|
|
156
|
+
query: {
|
|
157
|
+
response_type: string;
|
|
158
|
+
client_id: string;
|
|
159
|
+
redirect_uri?: string;
|
|
160
|
+
scope?: string;
|
|
161
|
+
state: string;
|
|
162
|
+
} & Record<string, unknown>;
|
|
163
|
+
}) => {
|
|
164
|
+
const url = new URL(OAUTH2.authenticateURL);
|
|
165
|
+
|
|
166
|
+
try {
|
|
167
|
+
const granter = oAuth2Granters.find(
|
|
168
|
+
(granter) =>
|
|
169
|
+
granter.authorizer &&
|
|
170
|
+
granter.authorizer.responseType === responseType,
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
if (!granter) {
|
|
174
|
+
throw new YError('E_UNKNOWN_AUTHORIZER_TYPE', responseType);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const { applicationId, redirectURI, scope } = await (
|
|
178
|
+
granter.authorizer as NonNullable<OAuth2GranterService['authorizer']>
|
|
179
|
+
).authorize(
|
|
180
|
+
{
|
|
181
|
+
clientId,
|
|
182
|
+
redirectURI: demandedRedirectURI,
|
|
183
|
+
scope: demandedScope,
|
|
184
|
+
},
|
|
185
|
+
camelCaseObjectProperties(authorizeParameters),
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
url.searchParams.set('type', responseType);
|
|
189
|
+
url.searchParams.set('redirect_uri', redirectURI);
|
|
190
|
+
url.searchParams.set('scope', scope);
|
|
191
|
+
url.searchParams.set('client_id', applicationId);
|
|
192
|
+
} catch (err) {
|
|
193
|
+
log('debug', '👫 - OAuth2 initialization error', (err as YError).code);
|
|
194
|
+
log('error-stack', printStackTrace(err as Error));
|
|
195
|
+
|
|
196
|
+
url.searchParams.set('redirect_uri', demandedRedirectURI);
|
|
197
|
+
setURLError(
|
|
198
|
+
url,
|
|
199
|
+
err as YError,
|
|
200
|
+
ERRORS_DESCRIPTORS[(err as YError).code] || ERRORS_DESCRIPTORS.E_OAUTH2,
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (state) {
|
|
205
|
+
url.searchParams.set('state', state);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return {
|
|
209
|
+
status: 302,
|
|
210
|
+
headers: {
|
|
211
|
+
location: url.href,
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export default location(autoService(initGetOAuth2Authorize), import.meta.url);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describe, test, jest, expect } from '@jest/globals';
|
|
2
2
|
import initPostAuthLogin from './postAuthLogin.js';
|
|
3
|
-
import type
|
|
3
|
+
import { type AuthCookiesService } from '../services/authCookies.js';
|
|
4
|
+
import { type WhookAuthenticationData } from '@whook/authorization';
|
|
4
5
|
|
|
5
6
|
describe('postAuthLogin', () => {
|
|
6
7
|
test('should work', async () => {
|
|
@@ -13,7 +14,7 @@ describe('postAuthLogin', () => {
|
|
|
13
14
|
ROOT_AUTHENTICATION_DATA: {
|
|
14
15
|
scope: 'user',
|
|
15
16
|
applicationId: 'root_app_id',
|
|
16
|
-
},
|
|
17
|
+
} as WhookAuthenticationData,
|
|
17
18
|
authCookies,
|
|
18
19
|
postOAuth2Token,
|
|
19
20
|
});
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { autoService, 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 WhookRouteDefinition } from '@whook/whook';
|
|
6
|
+
import {
|
|
7
|
+
type AuthCookiesService,
|
|
8
|
+
type AuthHandlersConfig,
|
|
9
|
+
} from '../services/authCookies.js';
|
|
10
|
+
|
|
11
|
+
export const definition = {
|
|
12
|
+
method: 'post',
|
|
13
|
+
path: `${AUTH_API_PREFIX}/login`,
|
|
14
|
+
config: {
|
|
15
|
+
environments: [],
|
|
16
|
+
},
|
|
17
|
+
operation: {
|
|
18
|
+
operationId: 'postAuthLogin',
|
|
19
|
+
summary: 'Logs a user in',
|
|
20
|
+
tags: ['auth'],
|
|
21
|
+
parameters: [],
|
|
22
|
+
requestBody: {
|
|
23
|
+
required: true,
|
|
24
|
+
content: {
|
|
25
|
+
'application/json': {
|
|
26
|
+
schema: {
|
|
27
|
+
type: 'object',
|
|
28
|
+
required: ['username', 'password'],
|
|
29
|
+
properties: {
|
|
30
|
+
username: { type: 'string' },
|
|
31
|
+
password: { type: 'string' },
|
|
32
|
+
scope: { type: 'string' },
|
|
33
|
+
remember: { type: 'boolean' },
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
responses: {
|
|
40
|
+
200: {
|
|
41
|
+
description: 'Successfuly logged in.',
|
|
42
|
+
content: {
|
|
43
|
+
'application/json': {
|
|
44
|
+
schema: {
|
|
45
|
+
type: 'object',
|
|
46
|
+
properties: {
|
|
47
|
+
access_token: { type: 'string' },
|
|
48
|
+
expiration_date: { type: 'string' },
|
|
49
|
+
expires_in: { type: 'string' },
|
|
50
|
+
token_type: { type: 'string' },
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
} as const satisfies WhookRouteDefinition;
|
|
59
|
+
|
|
60
|
+
async function initPostAuthLogin({
|
|
61
|
+
ROOT_AUTHENTICATION_DATA,
|
|
62
|
+
authCookies,
|
|
63
|
+
postOAuth2Token,
|
|
64
|
+
}: AuthHandlersConfig & {
|
|
65
|
+
authCookies: Pick<AuthCookiesService, 'build'>;
|
|
66
|
+
postOAuth2Token: Awaited<ReturnType<typeof initPostOauth2Token>>;
|
|
67
|
+
}) {
|
|
68
|
+
return async ({
|
|
69
|
+
body,
|
|
70
|
+
}: {
|
|
71
|
+
body: {
|
|
72
|
+
username: string;
|
|
73
|
+
password: string;
|
|
74
|
+
scope: string;
|
|
75
|
+
remember: boolean;
|
|
76
|
+
};
|
|
77
|
+
}) => {
|
|
78
|
+
try {
|
|
79
|
+
const response = await postOAuth2Token({
|
|
80
|
+
body: {
|
|
81
|
+
grant_type: 'password',
|
|
82
|
+
scope: body.scope,
|
|
83
|
+
username: body.username,
|
|
84
|
+
password: body.password,
|
|
85
|
+
},
|
|
86
|
+
authenticationData: ROOT_AUTHENTICATION_DATA,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
...response,
|
|
91
|
+
headers: {
|
|
92
|
+
...(response.headers || {}),
|
|
93
|
+
'Set-Cookie': authCookies.build(
|
|
94
|
+
response.status === 200 ? response.body : undefined,
|
|
95
|
+
{ session: !body.remember },
|
|
96
|
+
),
|
|
97
|
+
},
|
|
98
|
+
body: {
|
|
99
|
+
access_token: response.body.access_token,
|
|
100
|
+
expiration_date: response.body.expiration_date,
|
|
101
|
+
expires_in: response.body.expires_in,
|
|
102
|
+
token_type: response.body.token_type,
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
} catch (err) {
|
|
106
|
+
const newErr = YHTTPError.wrap(err as Error);
|
|
107
|
+
|
|
108
|
+
newErr.headers = {
|
|
109
|
+
...((err as YHTTPError).headers || {}),
|
|
110
|
+
'Set-Cookie': authCookies.build(),
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
throw newErr;
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export default location(autoService(initPostAuthLogin), import.meta.url);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, test, jest, expect } from '@jest/globals';
|
|
2
2
|
import initPostAuthLogout from './postAuthLogout.js';
|
|
3
|
-
import type
|
|
3
|
+
import { type AuthCookiesService } from '../services/authCookies.js';
|
|
4
4
|
|
|
5
5
|
describe('postAuthLogout', () => {
|
|
6
6
|
test('should work', async () => {
|
|
@@ -13,7 +13,7 @@ describe('postAuthLogout', () => {
|
|
|
13
13
|
|
|
14
14
|
authCookies.build.mockReturnValueOnce(['the_build_cookies']);
|
|
15
15
|
|
|
16
|
-
const response = await postAuthLogout(
|
|
16
|
+
const response = await postAuthLogout();
|
|
17
17
|
|
|
18
18
|
expect({
|
|
19
19
|
response,
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { autoService, location } from 'knifecycle';
|
|
2
|
+
import { AUTH_API_PREFIX } from '../services/authCookies.js';
|
|
3
|
+
import { type WhookRouteDefinition } from '@whook/whook';
|
|
4
|
+
import { type AuthCookiesService } from '../services/authCookies.js';
|
|
5
|
+
|
|
6
|
+
export const definition = {
|
|
7
|
+
method: 'post',
|
|
8
|
+
path: `${AUTH_API_PREFIX}/logout`,
|
|
9
|
+
config: {
|
|
10
|
+
environments: [],
|
|
11
|
+
},
|
|
12
|
+
operation: {
|
|
13
|
+
operationId: 'postAuthLogout',
|
|
14
|
+
summary: 'Logs a user out',
|
|
15
|
+
tags: ['auth'],
|
|
16
|
+
parameters: [],
|
|
17
|
+
responses: {
|
|
18
|
+
204: {
|
|
19
|
+
description: 'Successfully logged out!',
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
} as const satisfies WhookRouteDefinition;
|
|
24
|
+
|
|
25
|
+
async function initPostAuthLogout({
|
|
26
|
+
authCookies,
|
|
27
|
+
}: {
|
|
28
|
+
authCookies: Pick<AuthCookiesService, 'build'>;
|
|
29
|
+
}) {
|
|
30
|
+
return async () => ({
|
|
31
|
+
status: 204,
|
|
32
|
+
headers: {
|
|
33
|
+
'Set-Cookie': authCookies.build(),
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default location(autoService(initPostAuthLogout), import.meta.url);
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import { describe, test, jest, expect } from '@jest/globals';
|
|
3
3
|
import initPostAuthRefresh from './postAuthRefresh.js';
|
|
4
|
-
import type
|
|
4
|
+
import { type AuthCookiesService } from '../services/authCookies.js';
|
|
5
|
+
import { type WhookAuthenticationData } from '@whook/authorization';
|
|
5
6
|
|
|
6
7
|
describe('postAuthRefresh', () => {
|
|
7
8
|
test('should work', async () => {
|
|
@@ -14,7 +15,7 @@ describe('postAuthRefresh', () => {
|
|
|
14
15
|
ROOT_AUTHENTICATION_DATA: {
|
|
15
16
|
scope: 'user',
|
|
16
17
|
applicationId: 'root_app_id',
|
|
17
|
-
},
|
|
18
|
+
} as WhookAuthenticationData,
|
|
18
19
|
authCookies,
|
|
19
20
|
postOAuth2Token,
|
|
20
21
|
});
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { autoService, 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 {
|
|
6
|
+
refersTo,
|
|
7
|
+
type WhookRouteDefinition,
|
|
8
|
+
type WhookAPIParameterDefinition,
|
|
9
|
+
} from '@whook/whook';
|
|
10
|
+
import {
|
|
11
|
+
type AuthCookiesService,
|
|
12
|
+
type AuthHandlersConfig,
|
|
13
|
+
} from '../services/authCookies.js';
|
|
14
|
+
|
|
15
|
+
export const authCookieHeaderParameter = {
|
|
16
|
+
name: 'cookie',
|
|
17
|
+
example: 'access_token=an_access_token; refresh_token=a_refresh_token;',
|
|
18
|
+
parameter: {
|
|
19
|
+
name: 'cookie',
|
|
20
|
+
in: 'header',
|
|
21
|
+
required: false,
|
|
22
|
+
schema: {
|
|
23
|
+
type: 'string',
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
} as const satisfies WhookAPIParameterDefinition<string>;
|
|
27
|
+
|
|
28
|
+
export const definition = {
|
|
29
|
+
method: 'post',
|
|
30
|
+
path: `${AUTH_API_PREFIX}/refresh`,
|
|
31
|
+
config: {
|
|
32
|
+
environments: [],
|
|
33
|
+
},
|
|
34
|
+
operation: {
|
|
35
|
+
operationId: 'postAuthRefresh',
|
|
36
|
+
summary: 'Refreshs a user auth',
|
|
37
|
+
tags: ['auth'],
|
|
38
|
+
parameters: [refersTo(authCookieHeaderParameter)],
|
|
39
|
+
requestBody: {
|
|
40
|
+
required: true,
|
|
41
|
+
content: {
|
|
42
|
+
'application/json': {
|
|
43
|
+
schema: {
|
|
44
|
+
type: 'object',
|
|
45
|
+
required: [],
|
|
46
|
+
properties: {
|
|
47
|
+
scope: { type: 'string' },
|
|
48
|
+
remember: { type: 'boolean' },
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
responses: {
|
|
55
|
+
200: {
|
|
56
|
+
description: 'Successfuly refreshed.',
|
|
57
|
+
content: {
|
|
58
|
+
'application/json': {
|
|
59
|
+
schema: {
|
|
60
|
+
type: 'object',
|
|
61
|
+
properties: {
|
|
62
|
+
access_token: { type: 'string' },
|
|
63
|
+
expiration_date: { type: 'string' },
|
|
64
|
+
expires_in: { type: 'string' },
|
|
65
|
+
token_type: { type: 'string' },
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
} as const satisfies WhookRouteDefinition;
|
|
74
|
+
|
|
75
|
+
export default location(autoService(initPostAuthRefresh), import.meta.url);
|
|
76
|
+
|
|
77
|
+
async function initPostAuthRefresh({
|
|
78
|
+
ROOT_AUTHENTICATION_DATA,
|
|
79
|
+
authCookies,
|
|
80
|
+
postOAuth2Token,
|
|
81
|
+
}: AuthHandlersConfig & {
|
|
82
|
+
authCookies: AuthCookiesService;
|
|
83
|
+
postOAuth2Token: Awaited<ReturnType<typeof initPostOauth2Token>>;
|
|
84
|
+
}) {
|
|
85
|
+
return async ({
|
|
86
|
+
body,
|
|
87
|
+
cookie = '',
|
|
88
|
+
}: {
|
|
89
|
+
body: {
|
|
90
|
+
scope: string;
|
|
91
|
+
remember: boolean;
|
|
92
|
+
};
|
|
93
|
+
cookie?: string;
|
|
94
|
+
}) => {
|
|
95
|
+
const parsedCookies = authCookies.parse(cookie);
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
if (!parsedCookies.refresh_token) {
|
|
99
|
+
throw new YHTTPError(401, 'E_REFRESH_COOKIE', cookie);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const response = await postOAuth2Token({
|
|
103
|
+
body: {
|
|
104
|
+
grant_type: 'refresh_token',
|
|
105
|
+
scope: body.scope,
|
|
106
|
+
refresh_token: parsedCookies.refresh_token,
|
|
107
|
+
},
|
|
108
|
+
authenticationData: ROOT_AUTHENTICATION_DATA,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
...response,
|
|
113
|
+
headers: {
|
|
114
|
+
...(response.headers || {}),
|
|
115
|
+
'Set-Cookie': authCookies.build(
|
|
116
|
+
response.status === 200 ? response.body : undefined,
|
|
117
|
+
{ session: !body.remember },
|
|
118
|
+
),
|
|
119
|
+
},
|
|
120
|
+
body: {
|
|
121
|
+
access_token: response.body.access_token,
|
|
122
|
+
expiration_date: response.body.expiration_date,
|
|
123
|
+
expires_in: response.body.expires_in,
|
|
124
|
+
token_type: response.body.token_type,
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
} catch (err) {
|
|
128
|
+
const newErr = YHTTPError.wrap(err as Error);
|
|
129
|
+
|
|
130
|
+
newErr.headers = {
|
|
131
|
+
...((err as YHTTPError).headers || {}),
|
|
132
|
+
'Set-Cookie': authCookies.build(),
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
throw newErr;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|