@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,274 @@
|
|
|
1
|
+
import { initializer } from 'knifecycle';
|
|
2
|
+
import { DEFAULT_ERROR_URI, DEFAULT_HELP_URI } from '@whook/whook';
|
|
3
|
+
import type { WhookErrorsDescriptors } from '@whook/whook';
|
|
4
|
+
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
5
|
+
|
|
6
|
+
export const OAUTH2_ERRORS_DESCRIPTORS: WhookErrorsDescriptors = {
|
|
7
|
+
E_UNKNOWN_AUTHORIZER_TYPE: {
|
|
8
|
+
code: 'unsupported_response_type',
|
|
9
|
+
status: 400,
|
|
10
|
+
description: `The type "$0" is not supported.`,
|
|
11
|
+
uri: DEFAULT_ERROR_URI,
|
|
12
|
+
help: DEFAULT_HELP_URI,
|
|
13
|
+
},
|
|
14
|
+
E_UNKNOWN_APPLICATION_ID: {
|
|
15
|
+
code: 'unauthorized_client',
|
|
16
|
+
status: 400,
|
|
17
|
+
description: `The client id "$0" is unknown.`,
|
|
18
|
+
uri: DEFAULT_ERROR_URI,
|
|
19
|
+
help: DEFAULT_HELP_URI,
|
|
20
|
+
},
|
|
21
|
+
E_UNAUTHORIZED_APPLICATION_ID: {
|
|
22
|
+
code: 'unauthorized_client',
|
|
23
|
+
status: 401,
|
|
24
|
+
description: `The client id "$0" is not allowed.`,
|
|
25
|
+
uri: DEFAULT_ERROR_URI,
|
|
26
|
+
help: DEFAULT_HELP_URI,
|
|
27
|
+
},
|
|
28
|
+
E_APPLICATION_DISABLED: {
|
|
29
|
+
code: 'unauthorized_client',
|
|
30
|
+
status: 401,
|
|
31
|
+
description: `The client id "$0" is disabled.`,
|
|
32
|
+
uri: DEFAULT_ERROR_URI,
|
|
33
|
+
help: DEFAULT_HELP_URI,
|
|
34
|
+
},
|
|
35
|
+
E_BAD_REDIRECT_URI: {
|
|
36
|
+
code: 'invalid_request',
|
|
37
|
+
status: 400,
|
|
38
|
+
description: `The redirect uri "$0" is not allowed.`,
|
|
39
|
+
uri: DEFAULT_ERROR_URI,
|
|
40
|
+
help: DEFAULT_HELP_URI,
|
|
41
|
+
},
|
|
42
|
+
E_UNAUTHORIZED_SCOPE: {
|
|
43
|
+
code: 'invalid_scope',
|
|
44
|
+
status: 403,
|
|
45
|
+
description: `The scope "$0" is not allowed.`,
|
|
46
|
+
uri: DEFAULT_ERROR_URI,
|
|
47
|
+
help: DEFAULT_HELP_URI,
|
|
48
|
+
},
|
|
49
|
+
E_UNAUTHORIZED_GRANT_TYPE: {
|
|
50
|
+
code: 'invalid_request',
|
|
51
|
+
status: 403,
|
|
52
|
+
description: `The response type "$0" is not allowed for this application.`,
|
|
53
|
+
uri: DEFAULT_ERROR_URI,
|
|
54
|
+
help: DEFAULT_HELP_URI,
|
|
55
|
+
},
|
|
56
|
+
E_UNAUTHORIZED_CLIENT: {
|
|
57
|
+
code: 'invalid_client',
|
|
58
|
+
status: 401,
|
|
59
|
+
uri: DEFAULT_ERROR_URI,
|
|
60
|
+
help: DEFAULT_HELP_URI,
|
|
61
|
+
},
|
|
62
|
+
E_BAD_USERNAME: {
|
|
63
|
+
code: 'invalid_request',
|
|
64
|
+
status: 400,
|
|
65
|
+
description: 'User does not exists.',
|
|
66
|
+
uri: DEFAULT_ERROR_URI,
|
|
67
|
+
help: DEFAULT_HELP_URI,
|
|
68
|
+
},
|
|
69
|
+
E_BAD_PASSWORD: {
|
|
70
|
+
code: 'invalid_request',
|
|
71
|
+
status: 400,
|
|
72
|
+
description: 'Bad password sent for that user.',
|
|
73
|
+
uri: DEFAULT_ERROR_URI,
|
|
74
|
+
help: DEFAULT_HELP_URI,
|
|
75
|
+
},
|
|
76
|
+
E_BAD_REFRESH_TOKEN: {
|
|
77
|
+
code: 'invalid_request',
|
|
78
|
+
status: 400,
|
|
79
|
+
description: 'The refresh token is expired/invalid.',
|
|
80
|
+
uri: DEFAULT_ERROR_URI,
|
|
81
|
+
help: DEFAULT_HELP_URI,
|
|
82
|
+
},
|
|
83
|
+
E_BAD_AUTHORIZATION_CODE: {
|
|
84
|
+
code: 'invalid_request',
|
|
85
|
+
status: 400,
|
|
86
|
+
description: 'The authorization code is invalid or expired.',
|
|
87
|
+
uri: DEFAULT_ERROR_URI,
|
|
88
|
+
help: DEFAULT_HELP_URI,
|
|
89
|
+
},
|
|
90
|
+
E_UNKNOWN_ACKNOWLEDGOR_TYPE: {
|
|
91
|
+
code: 'unsupported_response_type',
|
|
92
|
+
status: 400,
|
|
93
|
+
description: `Type "$0" not supported.`,
|
|
94
|
+
uri: DEFAULT_ERROR_URI,
|
|
95
|
+
help: DEFAULT_HELP_URI,
|
|
96
|
+
},
|
|
97
|
+
E_ACCESS_DENIED: {
|
|
98
|
+
code: 'access_denied',
|
|
99
|
+
status: 403,
|
|
100
|
+
description: 'The user denied access to your application.',
|
|
101
|
+
uri: DEFAULT_ERROR_URI,
|
|
102
|
+
help: DEFAULT_HELP_URI,
|
|
103
|
+
},
|
|
104
|
+
E_UNKNOWN_AUTHENTICATION_TYPE: {
|
|
105
|
+
code: 'unsupported_grant_type',
|
|
106
|
+
status: 400,
|
|
107
|
+
uri: DEFAULT_ERROR_URI,
|
|
108
|
+
help: DEFAULT_HELP_URI,
|
|
109
|
+
},
|
|
110
|
+
E_OAUTH2: {
|
|
111
|
+
code: 'server_error',
|
|
112
|
+
status: 500,
|
|
113
|
+
uri: DEFAULT_ERROR_URI,
|
|
114
|
+
help: DEFAULT_HELP_URI,
|
|
115
|
+
},
|
|
116
|
+
E_REFRESH_COOKIE: {
|
|
117
|
+
code: 'invalid_request',
|
|
118
|
+
status: 400,
|
|
119
|
+
description: `Could not find any refresh_token value in the cookie header ($0).`,
|
|
120
|
+
uri: DEFAULT_ERROR_URI,
|
|
121
|
+
help: DEFAULT_HELP_URI,
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export type OAuth2CodeService<
|
|
126
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
127
|
+
CODE = string,
|
|
128
|
+
> = {
|
|
129
|
+
create: (
|
|
130
|
+
authenticationData: AUTHENTICATION_DATA,
|
|
131
|
+
redirectURI: string,
|
|
132
|
+
additionalParameters: { [name: string]: unknown },
|
|
133
|
+
) => Promise<CODE>;
|
|
134
|
+
check: (
|
|
135
|
+
authenticationData: AUTHENTICATION_DATA,
|
|
136
|
+
code: CODE,
|
|
137
|
+
redirectURI: string,
|
|
138
|
+
) => Promise<
|
|
139
|
+
AUTHENTICATION_DATA & {
|
|
140
|
+
redirectURI: string;
|
|
141
|
+
} & { [name: string]: unknown }
|
|
142
|
+
>;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export type OAuth2PasswordService<
|
|
146
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
147
|
+
USERNAME = string,
|
|
148
|
+
PASSWORD = string,
|
|
149
|
+
> = {
|
|
150
|
+
check: (
|
|
151
|
+
authenticationData: AUTHENTICATION_DATA,
|
|
152
|
+
username: USERNAME,
|
|
153
|
+
password: PASSWORD,
|
|
154
|
+
demandedScope: AUTHENTICATION_DATA['scope'],
|
|
155
|
+
) => Promise<AUTHENTICATION_DATA>;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
export type OAuth2AccessTokenService<
|
|
159
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
160
|
+
TOKEN = string,
|
|
161
|
+
> = {
|
|
162
|
+
create: (
|
|
163
|
+
authenticationData: AUTHENTICATION_DATA,
|
|
164
|
+
tokenAuthenticationData: AUTHENTICATION_DATA,
|
|
165
|
+
additionalParameters?: { [name: string]: unknown },
|
|
166
|
+
) => Promise<{
|
|
167
|
+
token: TOKEN;
|
|
168
|
+
expiresAt: number;
|
|
169
|
+
}>;
|
|
170
|
+
check: (
|
|
171
|
+
authenticationData: AUTHENTICATION_DATA,
|
|
172
|
+
token: TOKEN,
|
|
173
|
+
scope?: AUTHENTICATION_DATA['scope'],
|
|
174
|
+
) => Promise<AUTHENTICATION_DATA>;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export type OAuth2RefreshTokenService<
|
|
178
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
179
|
+
TOKEN = string,
|
|
180
|
+
> = OAuth2AccessTokenService<AUTHENTICATION_DATA, TOKEN>;
|
|
181
|
+
|
|
182
|
+
export type CheckApplicationService = {
|
|
183
|
+
(context: {
|
|
184
|
+
applicationId: string;
|
|
185
|
+
type: string;
|
|
186
|
+
scope: string;
|
|
187
|
+
redirectURI?: string;
|
|
188
|
+
}): Promise<{
|
|
189
|
+
applicationId: string;
|
|
190
|
+
type: string;
|
|
191
|
+
scope: string;
|
|
192
|
+
redirectURI: string;
|
|
193
|
+
}>;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
export type OAuth2GranterService<
|
|
197
|
+
AUTHORIZE_PARAMETERS extends Record<string, unknown> = Record<
|
|
198
|
+
string,
|
|
199
|
+
unknown
|
|
200
|
+
>,
|
|
201
|
+
ACKNOWLEDGE_PARAMETERS extends Record<string, unknown> = Record<
|
|
202
|
+
string,
|
|
203
|
+
unknown
|
|
204
|
+
>,
|
|
205
|
+
GRANT_PARAMETERS extends Record<string, unknown> = Record<string, unknown>,
|
|
206
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
207
|
+
> = {
|
|
208
|
+
type: string;
|
|
209
|
+
authorizer?: {
|
|
210
|
+
responseType: string;
|
|
211
|
+
authorize: (
|
|
212
|
+
context: {
|
|
213
|
+
clientId: AUTHENTICATION_DATA['applicationId'];
|
|
214
|
+
redirectURI: string;
|
|
215
|
+
scope: AUTHENTICATION_DATA['scope'];
|
|
216
|
+
},
|
|
217
|
+
authorizeParameters?: AUTHORIZE_PARAMETERS,
|
|
218
|
+
) => Promise<{
|
|
219
|
+
applicationId: AUTHENTICATION_DATA['applicationId'];
|
|
220
|
+
redirectURI: string;
|
|
221
|
+
scope: AUTHENTICATION_DATA['scope'];
|
|
222
|
+
}>;
|
|
223
|
+
};
|
|
224
|
+
acknowledger?: {
|
|
225
|
+
acknowledgmentType: string;
|
|
226
|
+
acknowledge: (
|
|
227
|
+
authenticationData: AUTHENTICATION_DATA,
|
|
228
|
+
acknowledgeParameters: {
|
|
229
|
+
clientId: AUTHENTICATION_DATA['applicationId'];
|
|
230
|
+
redirectURI: string;
|
|
231
|
+
scope: AUTHENTICATION_DATA['applicationId'];
|
|
232
|
+
},
|
|
233
|
+
additionalParameters: ACKNOWLEDGE_PARAMETERS,
|
|
234
|
+
) => Promise<
|
|
235
|
+
AUTHENTICATION_DATA & {
|
|
236
|
+
redirectURI: string;
|
|
237
|
+
[name: string]: unknown;
|
|
238
|
+
}
|
|
239
|
+
>;
|
|
240
|
+
};
|
|
241
|
+
authenticator?: {
|
|
242
|
+
grantType: string;
|
|
243
|
+
authenticate: (
|
|
244
|
+
grantParameters: GRANT_PARAMETERS,
|
|
245
|
+
authenticationData: AUTHENTICATION_DATA,
|
|
246
|
+
) => Promise<AUTHENTICATION_DATA>;
|
|
247
|
+
};
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
export type OAuth2Options = {
|
|
251
|
+
authenticateURL: string;
|
|
252
|
+
defaultToClientScope?: boolean;
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
export type OAuth2Config = {
|
|
256
|
+
OAUTH2: OAuth2Options;
|
|
257
|
+
ERRORS_DESCRIPTORS: WhookErrorsDescriptors;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
export default initializer(
|
|
261
|
+
{
|
|
262
|
+
name: 'oAuth2Granters',
|
|
263
|
+
type: 'service',
|
|
264
|
+
inject: [
|
|
265
|
+
'oAuth2ClientCredentialsGranter',
|
|
266
|
+
'oAuth2CodeGranter',
|
|
267
|
+
'oAuth2PasswordGranter',
|
|
268
|
+
'oAuth2RefreshTokenGranter',
|
|
269
|
+
'oAuth2TokenGranter',
|
|
270
|
+
],
|
|
271
|
+
singleton: true,
|
|
272
|
+
},
|
|
273
|
+
async (services) => Object.keys(services).map((key) => services[key]),
|
|
274
|
+
);
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { autoService } from 'knifecycle';
|
|
2
|
+
import { noop } from '@whook/whook';
|
|
3
|
+
import YError from 'yerror';
|
|
4
|
+
import type { LogService } from 'common-services';
|
|
5
|
+
import type {
|
|
6
|
+
OAuth2GranterService,
|
|
7
|
+
OAuth2PasswordService,
|
|
8
|
+
CheckApplicationService,
|
|
9
|
+
} from './oAuth2Granters';
|
|
10
|
+
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
11
|
+
|
|
12
|
+
export type OAuth2PasswordGranterDependencies = {
|
|
13
|
+
oAuth2Password: OAuth2PasswordService;
|
|
14
|
+
checkApplication: CheckApplicationService;
|
|
15
|
+
log?: LogService;
|
|
16
|
+
};
|
|
17
|
+
export type OAuth2PasswordGranterParameters = {
|
|
18
|
+
username: string;
|
|
19
|
+
password: string;
|
|
20
|
+
scope?: string;
|
|
21
|
+
};
|
|
22
|
+
export type OAuth2PasswordGranterService<
|
|
23
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
24
|
+
> = OAuth2GranterService<
|
|
25
|
+
Record<string, unknown>,
|
|
26
|
+
Record<string, unknown>,
|
|
27
|
+
OAuth2PasswordGranterParameters,
|
|
28
|
+
AUTHENTICATION_DATA
|
|
29
|
+
>;
|
|
30
|
+
|
|
31
|
+
export default autoService(initOAuth2PasswordGranter);
|
|
32
|
+
|
|
33
|
+
// Resource Owner Password Credentials Grant
|
|
34
|
+
// https://tools.ietf.org/html/rfc6749#section-4.3
|
|
35
|
+
async function initOAuth2PasswordGranter({
|
|
36
|
+
oAuth2Password,
|
|
37
|
+
checkApplication,
|
|
38
|
+
log = noop,
|
|
39
|
+
}: OAuth2PasswordGranterDependencies): Promise<OAuth2PasswordGranterService> {
|
|
40
|
+
const authenticateWithPassword: OAuth2PasswordGranterService['authenticator']['authenticate'] =
|
|
41
|
+
async (
|
|
42
|
+
{ username, password, scope: demandedScope = '' },
|
|
43
|
+
authenticationData,
|
|
44
|
+
) => {
|
|
45
|
+
// The client must be authenticated
|
|
46
|
+
if (!authenticationData) {
|
|
47
|
+
throw new YError('E_UNAUTHORIZED');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
await checkApplication({
|
|
51
|
+
applicationId: authenticationData.applicationId,
|
|
52
|
+
type: 'password',
|
|
53
|
+
scope: demandedScope,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const finalAuthenticationData = await oAuth2Password.check(
|
|
57
|
+
authenticationData,
|
|
58
|
+
username,
|
|
59
|
+
password,
|
|
60
|
+
demandedScope,
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
return finalAuthenticationData;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
log('debug', '👫 - OAuth2PasswordGranter Service Initialized!');
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
type: 'password',
|
|
70
|
+
authenticator: {
|
|
71
|
+
grantType: 'password',
|
|
72
|
+
authenticate: authenticateWithPassword,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { autoService } from 'knifecycle';
|
|
2
|
+
import { noop } from '@whook/whook';
|
|
3
|
+
import YError from 'yerror';
|
|
4
|
+
import type { LogService } from 'common-services';
|
|
5
|
+
import type {
|
|
6
|
+
OAuth2GranterService,
|
|
7
|
+
CheckApplicationService,
|
|
8
|
+
OAuth2RefreshTokenService,
|
|
9
|
+
} from './oAuth2Granters';
|
|
10
|
+
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
11
|
+
|
|
12
|
+
export type OAuth2RefreshTokenGranterDependencies = {
|
|
13
|
+
checkApplication: CheckApplicationService;
|
|
14
|
+
oAuth2RefreshToken: OAuth2RefreshTokenService;
|
|
15
|
+
log?: LogService;
|
|
16
|
+
};
|
|
17
|
+
export type OAuth2RefreshTokenGranterParameters = {
|
|
18
|
+
refreshToken: string;
|
|
19
|
+
scope?: string;
|
|
20
|
+
};
|
|
21
|
+
export type OAuth2RefreshTokenGranterService<
|
|
22
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
23
|
+
> = OAuth2GranterService<
|
|
24
|
+
Record<string, unknown>,
|
|
25
|
+
Record<string, unknown>,
|
|
26
|
+
OAuth2RefreshTokenGranterParameters,
|
|
27
|
+
AUTHENTICATION_DATA
|
|
28
|
+
>;
|
|
29
|
+
|
|
30
|
+
export default autoService(initOAuth2RefreshTokenGranter);
|
|
31
|
+
|
|
32
|
+
// Refresh Token Grant
|
|
33
|
+
// https://tools.ietf.org/html/rfc6749#page-47
|
|
34
|
+
async function initOAuth2RefreshTokenGranter({
|
|
35
|
+
checkApplication,
|
|
36
|
+
oAuth2RefreshToken,
|
|
37
|
+
log = noop,
|
|
38
|
+
}: OAuth2RefreshTokenGranterDependencies): Promise<OAuth2RefreshTokenGranterService> {
|
|
39
|
+
const authenticateWithRefreshToken: OAuth2RefreshTokenGranterService['authenticator']['authenticate'] =
|
|
40
|
+
async ({ refreshToken, scope: demandedScope }, authenticationData) => {
|
|
41
|
+
try {
|
|
42
|
+
// The client must be authenticated
|
|
43
|
+
if (!authenticationData) {
|
|
44
|
+
throw new YError('E_UNAUTHORIZED');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
await checkApplication({
|
|
48
|
+
applicationId: authenticationData.applicationId,
|
|
49
|
+
type: 'refresh',
|
|
50
|
+
scope: demandedScope,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const newAuthenticationData = await oAuth2RefreshToken.check(
|
|
54
|
+
authenticationData,
|
|
55
|
+
refreshToken,
|
|
56
|
+
demandedScope,
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
return newAuthenticationData;
|
|
60
|
+
} catch (err) {
|
|
61
|
+
if (err.code === 'E_BAD_TOKEN') {
|
|
62
|
+
throw YError.wrap(err, 'E_BAD_REFRESH_TOKEN');
|
|
63
|
+
}
|
|
64
|
+
throw err;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
log('debug', '👫 - OAuth2RefreshTokenGranter Service Initialized!');
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
type: 'refresh',
|
|
72
|
+
authenticator: {
|
|
73
|
+
grantType: 'refresh_token',
|
|
74
|
+
authenticate: authenticateWithRefreshToken,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { autoService } from 'knifecycle';
|
|
2
|
+
import { noop } from '@whook/whook';
|
|
3
|
+
import type { LogService, TimeService } from 'common-services';
|
|
4
|
+
import type {
|
|
5
|
+
OAuth2GranterService,
|
|
6
|
+
OAuth2AccessTokenService,
|
|
7
|
+
CheckApplicationService,
|
|
8
|
+
OAuth2CodeService,
|
|
9
|
+
} from './oAuth2Granters';
|
|
10
|
+
import type { BaseAuthenticationData } from '@whook/authorization';
|
|
11
|
+
|
|
12
|
+
export const IMPLICIT_GRANTER_TYPE = 'token';
|
|
13
|
+
|
|
14
|
+
export type OAuth2TokenGranterDependencies = {
|
|
15
|
+
oAuth2Code: OAuth2CodeService;
|
|
16
|
+
checkApplication: CheckApplicationService;
|
|
17
|
+
oAuth2AccessToken: OAuth2AccessTokenService;
|
|
18
|
+
time?: TimeService;
|
|
19
|
+
log?: LogService;
|
|
20
|
+
};
|
|
21
|
+
export type OAuth2TokenGranterParameters = Record<string, unknown>;
|
|
22
|
+
export type OAuth2TokenGranterService<
|
|
23
|
+
AUTHENTICATION_DATA extends BaseAuthenticationData = BaseAuthenticationData,
|
|
24
|
+
> = OAuth2GranterService<
|
|
25
|
+
Record<string, unknown>,
|
|
26
|
+
Record<string, unknown>,
|
|
27
|
+
OAuth2TokenGranterParameters,
|
|
28
|
+
AUTHENTICATION_DATA
|
|
29
|
+
>;
|
|
30
|
+
|
|
31
|
+
export default autoService(initOAuth2TokenGranter);
|
|
32
|
+
|
|
33
|
+
// Implicit Grant
|
|
34
|
+
// https://tools.ietf.org/html/rfc6749#section-4.2
|
|
35
|
+
async function initOAuth2TokenGranter({
|
|
36
|
+
checkApplication,
|
|
37
|
+
oAuth2AccessToken,
|
|
38
|
+
time = Date.now.bind(Date),
|
|
39
|
+
log = noop,
|
|
40
|
+
}: OAuth2TokenGranterDependencies): Promise<OAuth2TokenGranterService> {
|
|
41
|
+
const authorizeWithToken: OAuth2TokenGranterService['authorizer']['authorize'] =
|
|
42
|
+
async ({ clientId, redirectURI, scope }) => {
|
|
43
|
+
const { redirectURI: finalRedirectURI } = await checkApplication({
|
|
44
|
+
applicationId: clientId,
|
|
45
|
+
type: IMPLICIT_GRANTER_TYPE,
|
|
46
|
+
redirectURI,
|
|
47
|
+
scope,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
applicationId: clientId,
|
|
52
|
+
redirectURI: finalRedirectURI,
|
|
53
|
+
scope,
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// Access Token Response:
|
|
58
|
+
// https://tools.ietf.org/html/rfc6749#section-4.2.2
|
|
59
|
+
const acknowledgeWithToken: OAuth2GranterService<OAuth2TokenGranterParameters>['acknowledger']['acknowledge'] =
|
|
60
|
+
async (
|
|
61
|
+
authenticationData,
|
|
62
|
+
{ clientId, redirectURI, scope: providedScope },
|
|
63
|
+
additionalParameters,
|
|
64
|
+
) => {
|
|
65
|
+
const { redirectURI: finalRedirectURI } = await checkApplication({
|
|
66
|
+
applicationId: clientId,
|
|
67
|
+
redirectURI,
|
|
68
|
+
scope: providedScope,
|
|
69
|
+
type: IMPLICIT_GRANTER_TYPE,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const { token: accessToken, expiresAt: accessTokenExpiresAt } =
|
|
73
|
+
await oAuth2AccessToken.create(
|
|
74
|
+
authenticationData,
|
|
75
|
+
{
|
|
76
|
+
applicationId: clientId,
|
|
77
|
+
scope: providedScope,
|
|
78
|
+
},
|
|
79
|
+
additionalParameters,
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
applicationId: clientId,
|
|
84
|
+
redirectURI: finalRedirectURI,
|
|
85
|
+
scope: providedScope,
|
|
86
|
+
accessToken,
|
|
87
|
+
tokenType: 'bearer',
|
|
88
|
+
expiresIn: Math.round((accessTokenExpiresAt - time()) / 1000),
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
log('debug', '👫 - OAuth2TokenGranter Service Initialized!');
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
type: IMPLICIT_GRANTER_TYPE,
|
|
96
|
+
authorizer: {
|
|
97
|
+
responseType: 'token',
|
|
98
|
+
authorize: authorizeWithToken,
|
|
99
|
+
},
|
|
100
|
+
acknowledger: {
|
|
101
|
+
acknowledgmentType: 'token',
|
|
102
|
+
acknowledge: acknowledgeWithToken,
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
}
|