@alepha/react 0.12.0 → 0.13.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/dist/auth/chunk-DhGyd7sr.js +28 -0
- package/dist/auth/index.browser.js +394 -114
- package/dist/auth/index.browser.js.map +1 -1
- package/dist/auth/index.cjs +80 -1927
- package/dist/auth/index.cjs.map +1 -1
- package/dist/auth/index.d.cts +1130 -420
- package/dist/auth/index.d.ts +1130 -420
- package/dist/auth/index.js +72 -1918
- package/dist/auth/index.js.map +1 -1
- package/dist/core/chunk-DhGyd7sr.js +28 -0
- package/dist/core/index.browser.js +79 -79
- package/dist/core/index.browser.js.map +1 -1
- package/dist/core/index.cjs +89 -85
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.cts +1654 -154
- package/dist/core/index.d.ts +1654 -154
- package/dist/core/index.js +79 -79
- package/dist/core/index.js.map +1 -1
- package/dist/form/chunk-DhGyd7sr.js +28 -0
- package/dist/form/index.cjs +28 -8
- package/dist/form/index.cjs.map +1 -1
- package/dist/form/index.d.cts +215 -7
- package/dist/form/index.d.ts +215 -7
- package/dist/form/index.js +18 -3
- package/dist/form/index.js.map +1 -1
- package/dist/head/chunk-DhGyd7sr.js +28 -0
- package/dist/head/index.browser.js +385 -59
- package/dist/head/index.browser.js.map +1 -1
- package/dist/head/index.cjs +12 -8
- package/dist/head/index.cjs.map +1 -1
- package/dist/head/index.d.cts +1230 -24
- package/dist/head/index.d.ts +1230 -29
- package/dist/head/index.js +2 -2
- package/dist/head/index.js.map +1 -1
- package/dist/i18n/chunk-DhGyd7sr.js +28 -0
- package/dist/i18n/index.cjs +33 -20
- package/dist/i18n/index.cjs.map +1 -1
- package/dist/i18n/index.d.cts +282 -13
- package/dist/i18n/index.d.ts +282 -13
- package/dist/i18n/index.js +23 -14
- package/dist/i18n/index.js.map +1 -1
- package/dist/websocket/index.cjs +21 -8
- package/dist/websocket/index.cjs.map +1 -1
- package/dist/websocket/index.js +11 -2
- package/dist/websocket/index.js.map +1 -1
- package/package.json +7 -6
- package/src/auth/index.browser.ts +3 -6
- package/src/auth/index.shared.ts +0 -1
- package/src/auth/index.ts +3 -16
- package/src/auth/providers/ReactAuthProvider.ts +1 -614
- package/src/auth/services/ReactAuth.ts +6 -17
- package/src/core/descriptors/$page.ts +1 -1
- package/src/core/index.browser.ts +1 -0
- package/src/core/index.native.ts +21 -0
- package/src/core/index.shared-router.ts +15 -0
- package/src/core/index.shared.ts +0 -14
- package/src/core/index.ts +1 -0
- package/src/core/services/ReactRouter.ts +2 -2
- package/src/form/errors/FormValidationError.ts +20 -0
- package/src/form/hooks/useForm.ts +1 -1
- package/src/form/index.ts +1 -0
- package/src/head/providers/BrowserHeadProvider.ts +1 -1
- package/src/i18n/descriptors/$dictionary.ts +7 -3
- package/src/i18n/providers/I18nProvider.ts +9 -10
- package/src/websocket/hooks/useRoom.tsx +21 -2
- package/dist/auth/index.d.cts.map +0 -1
- package/dist/auth/index.d.ts.map +0 -1
- package/dist/core/index.d.cts.map +0 -1
- package/dist/core/index.d.ts.map +0 -1
- package/dist/form/index.d.cts.map +0 -1
- package/dist/form/index.d.ts.map +0 -1
- package/dist/head/index.d.cts.map +0 -1
- package/dist/head/index.d.ts.map +0 -1
- package/dist/i18n/index.d.cts.map +0 -1
- package/dist/i18n/index.d.ts.map +0 -1
- package/dist/websocket/index.d.cts.map +0 -1
- package/dist/websocket/index.d.ts.map +0 -1
- package/src/auth/descriptors/$auth.ts +0 -436
- package/src/auth/descriptors/$authApple.ts +0 -8
- package/src/auth/descriptors/$authGithub.ts +0 -81
- package/src/auth/descriptors/$authGoogle.ts +0 -38
- package/src/auth/errors/SessionExpiredError.ts +0 -6
- package/src/auth/schemas/tokenResponseSchema.ts +0 -11
- package/src/auth/schemas/tokensSchema.ts +0 -21
- package/src/auth/schemas/userinfoResponseSchema.ts +0 -10
|
@@ -1,57 +1,7 @@
|
|
|
1
|
-
import { $hook, $inject, Alepha
|
|
2
|
-
import { DateTimeProvider } from "alepha/datetime";
|
|
3
|
-
import { $logger } from "alepha/logger";
|
|
4
|
-
import { SecurityError, type UserAccount } from "alepha/security";
|
|
5
|
-
import { $route, BadRequestError, UnauthorizedError } from "alepha/server";
|
|
6
|
-
import {
|
|
7
|
-
$cookie,
|
|
8
|
-
type Cookies,
|
|
9
|
-
ServerCookiesProvider,
|
|
10
|
-
} from "alepha/server/cookies";
|
|
11
|
-
import { ServerLinksProvider } from "alepha/server/links";
|
|
12
|
-
import {
|
|
13
|
-
authorizationCodeGrant,
|
|
14
|
-
buildAuthorizationUrl,
|
|
15
|
-
buildEndSessionUrl,
|
|
16
|
-
calculatePKCECodeChallenge,
|
|
17
|
-
randomPKCECodeVerifier,
|
|
18
|
-
randomState,
|
|
19
|
-
} from "openid-client";
|
|
20
|
-
import { $auth, type AuthDescriptor } from "../descriptors/$auth.ts";
|
|
21
|
-
import { tokenResponseSchema } from "../schemas/tokenResponseSchema.ts";
|
|
22
|
-
import { type Tokens, tokensSchema } from "../schemas/tokensSchema.ts";
|
|
23
|
-
import { userinfoResponseSchema } from "../schemas/userinfoResponseSchema.ts";
|
|
24
|
-
import { ReactAuth } from "../services/ReactAuth.ts";
|
|
1
|
+
import { $hook, $inject, Alepha } from "alepha";
|
|
25
2
|
|
|
26
3
|
export class ReactAuthProvider {
|
|
27
|
-
protected readonly log = $logger();
|
|
28
4
|
protected readonly alepha = $inject(Alepha);
|
|
29
|
-
protected readonly serverCookiesProvider = $inject(ServerCookiesProvider);
|
|
30
|
-
protected readonly dateTimeProvider = $inject(DateTimeProvider);
|
|
31
|
-
protected readonly serverLinksProvider = $inject(ServerLinksProvider);
|
|
32
|
-
protected readonly reactAuth = $inject(ReactAuth);
|
|
33
|
-
|
|
34
|
-
protected readonly authorizationCode = $cookie({
|
|
35
|
-
name: "authorizationCode",
|
|
36
|
-
ttl: [15, "minutes"],
|
|
37
|
-
httpOnly: true,
|
|
38
|
-
schema: t.object({
|
|
39
|
-
provider: t.text(),
|
|
40
|
-
codeVerifier: t.optional(t.text({ size: "long" })),
|
|
41
|
-
redirectUri: t.optional(t.text({ size: "long" })),
|
|
42
|
-
state: t.optional(t.text()),
|
|
43
|
-
nonce: t.optional(t.text()),
|
|
44
|
-
}),
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
public readonly tokens = $cookie({
|
|
48
|
-
name: "tokens",
|
|
49
|
-
ttl: [30, "days"],
|
|
50
|
-
httpOnly: true,
|
|
51
|
-
compress: true,
|
|
52
|
-
encrypt: true,
|
|
53
|
-
schema: tokensSchema,
|
|
54
|
-
});
|
|
55
5
|
|
|
56
6
|
public readonly onRender = $hook({
|
|
57
7
|
on: "react:server:render:begin",
|
|
@@ -63,567 +13,4 @@ export class ReactAuthProvider {
|
|
|
63
13
|
}
|
|
64
14
|
},
|
|
65
15
|
});
|
|
66
|
-
|
|
67
|
-
public get identities(): Array<AuthDescriptor> {
|
|
68
|
-
return this.alepha
|
|
69
|
-
.descriptors($auth)
|
|
70
|
-
.filter((auth) => !auth.options.disabled);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
protected readonly configure = $hook({
|
|
74
|
-
on: "configure",
|
|
75
|
-
handler: async () => {
|
|
76
|
-
for (const identity of this.identities) {
|
|
77
|
-
await identity.prepare();
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
protected getAccessTokens(tokens: Tokens) {
|
|
83
|
-
const idp = this.provider(tokens.provider);
|
|
84
|
-
|
|
85
|
-
if (
|
|
86
|
-
"oidc" in idp.options &&
|
|
87
|
-
!("realm" in idp.options) &&
|
|
88
|
-
idp.options.oidc?.useIdToken
|
|
89
|
-
) {
|
|
90
|
-
return tokens.id_token;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return tokens.access_token;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Fill request headers with access token from cookies or fallback to provider's fallback function.
|
|
98
|
-
*/
|
|
99
|
-
protected readonly onRequest = $hook({
|
|
100
|
-
on: "server:onRequest",
|
|
101
|
-
after: this.serverCookiesProvider,
|
|
102
|
-
handler: async ({ request }) => {
|
|
103
|
-
const cookies = request.cookies;
|
|
104
|
-
|
|
105
|
-
// [feature] forward cookies to request headers
|
|
106
|
-
if (cookies) {
|
|
107
|
-
const tokens = await this.cookiesToTokens(cookies);
|
|
108
|
-
if (tokens) {
|
|
109
|
-
request.headers.authorization = `Bearer ${this.getAccessTokens(tokens)}`;
|
|
110
|
-
this.log.trace("Access token set in request headers", {
|
|
111
|
-
provider: tokens.provider,
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// [feature] support for auth providers with fallback
|
|
117
|
-
if (!request.headers.authorization) {
|
|
118
|
-
for (const provider of this.identities) {
|
|
119
|
-
if (!("realm" in provider.options) && !!provider.options.fallback) {
|
|
120
|
-
const token = await provider.options.fallback();
|
|
121
|
-
if (token) {
|
|
122
|
-
request.headers.authorization = `Bearer ${token}`;
|
|
123
|
-
break;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
},
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Convert cookies to tokens.
|
|
133
|
-
* If the tokens are expired, try to refresh them using the refresh token.
|
|
134
|
-
*/
|
|
135
|
-
protected async cookiesToTokens(
|
|
136
|
-
cookies: Cookies,
|
|
137
|
-
): Promise<Tokens | undefined> {
|
|
138
|
-
const tokens = this.tokens.get({ cookies });
|
|
139
|
-
if (!tokens) {
|
|
140
|
-
// no cookie, no tokens
|
|
141
|
-
this.log.trace("No tokens found in cookies");
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
this.log.trace("Tokens found in cookies", {
|
|
146
|
-
expires_in: tokens.expires_in,
|
|
147
|
-
issued_at: tokens.issued_at,
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
// check if tokens are expired
|
|
151
|
-
const refreshedTokens = await this.refreshTokens(tokens);
|
|
152
|
-
if (!refreshedTokens) {
|
|
153
|
-
this.tokens.del({ cookies });
|
|
154
|
-
// 08/25: exception here will go to Server error handler, not the React one
|
|
155
|
-
// better to remove cookie & session and let the page handle 401 Unauthorized
|
|
156
|
-
//throw new SessionExpiredError("Session expired. Please login again.");
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (refreshedTokens.access_token !== tokens.access_token) {
|
|
161
|
-
this.setTokens(refreshedTokens, cookies);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
return refreshedTokens;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
protected async refreshTokens(tokens: Tokens): Promise<Tokens | undefined> {
|
|
168
|
-
if (tokens.expires_in && tokens.issued_at) {
|
|
169
|
-
const gracePeriodSec = 10;
|
|
170
|
-
const expiresAt = tokens.issued_at + (tokens.expires_in - gracePeriodSec);
|
|
171
|
-
|
|
172
|
-
if (expiresAt < this.dateTimeProvider.now().unix()) {
|
|
173
|
-
this.log.trace("Tokens are expired");
|
|
174
|
-
|
|
175
|
-
// oh no, it is expired
|
|
176
|
-
if (tokens.refresh_token) {
|
|
177
|
-
this.log.trace("Trying to refresh tokens using refresh token");
|
|
178
|
-
// but has refresh token!
|
|
179
|
-
try {
|
|
180
|
-
const provider = this.provider(tokens);
|
|
181
|
-
const result = await provider.refresh(
|
|
182
|
-
tokens.refresh_token,
|
|
183
|
-
tokens.access_token,
|
|
184
|
-
);
|
|
185
|
-
const newTokens = {
|
|
186
|
-
...result,
|
|
187
|
-
provider: tokens.provider,
|
|
188
|
-
issued_at: this.dateTimeProvider.now().unix(),
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
this.log.debug("Tokens refreshed successfully");
|
|
192
|
-
|
|
193
|
-
return newTokens;
|
|
194
|
-
} catch (e) {
|
|
195
|
-
this.log.warn("Failed to refresh token", e);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
// session expired and no (valid) refresh token
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (!tokens.issued_at && tokens.access_token) {
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
return tokens;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// -------------------------------------------------------------------------------------------------------------------
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Get user information.
|
|
215
|
-
*/
|
|
216
|
-
public readonly userinfo = $route({
|
|
217
|
-
path: ReactAuth.path.userinfo,
|
|
218
|
-
schema: {
|
|
219
|
-
response: userinfoResponseSchema,
|
|
220
|
-
},
|
|
221
|
-
handler: async ({ user, headers, cookies }) => {
|
|
222
|
-
const tokens = this.tokens.get({ cookies });
|
|
223
|
-
if (tokens) {
|
|
224
|
-
const provider = this.provider(tokens);
|
|
225
|
-
if (!("realm" in provider.options)) {
|
|
226
|
-
const user = await provider.user(tokens);
|
|
227
|
-
const api = await this.serverLinksProvider.getUserApiLinks({
|
|
228
|
-
authorization: headers.authorization,
|
|
229
|
-
user,
|
|
230
|
-
});
|
|
231
|
-
return {
|
|
232
|
-
api,
|
|
233
|
-
user,
|
|
234
|
-
};
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const api = await this.serverLinksProvider.getUserApiLinks({
|
|
239
|
-
authorization: headers.authorization,
|
|
240
|
-
user,
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
return {
|
|
244
|
-
api,
|
|
245
|
-
user,
|
|
246
|
-
};
|
|
247
|
-
},
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Refresh a token for internal providers.
|
|
252
|
-
*/
|
|
253
|
-
public readonly refresh = $route({
|
|
254
|
-
path: ReactAuth.path.refresh,
|
|
255
|
-
method: "POST",
|
|
256
|
-
schema: {
|
|
257
|
-
query: t.object({
|
|
258
|
-
provider: t.text(),
|
|
259
|
-
}),
|
|
260
|
-
body: t.object({
|
|
261
|
-
refresh_token: t.text({
|
|
262
|
-
size: "rich",
|
|
263
|
-
}),
|
|
264
|
-
access_token: t.optional(
|
|
265
|
-
t.text({
|
|
266
|
-
size: "rich",
|
|
267
|
-
description:
|
|
268
|
-
"Required if provider has stateless refresh token on credentials mode",
|
|
269
|
-
}),
|
|
270
|
-
),
|
|
271
|
-
}),
|
|
272
|
-
response: tokensSchema,
|
|
273
|
-
},
|
|
274
|
-
handler: async ({ query, body, cookies }) => {
|
|
275
|
-
const provider = this.provider(query);
|
|
276
|
-
|
|
277
|
-
const tokens = {
|
|
278
|
-
provider: query.provider,
|
|
279
|
-
...(await provider.refresh(body.refresh_token, body.access_token)),
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
// for web applications, we store tokens in cookies
|
|
283
|
-
this.setTokens(tokens, cookies);
|
|
284
|
-
|
|
285
|
-
return tokens;
|
|
286
|
-
},
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Login for local password-based authentication.
|
|
291
|
-
*/
|
|
292
|
-
public readonly token = $route({
|
|
293
|
-
path: ReactAuth.path.token,
|
|
294
|
-
method: "POST",
|
|
295
|
-
schema: {
|
|
296
|
-
query: t.object({
|
|
297
|
-
provider: t.text(),
|
|
298
|
-
}),
|
|
299
|
-
body: t.object({
|
|
300
|
-
username: t.text(),
|
|
301
|
-
password: t.text(),
|
|
302
|
-
}),
|
|
303
|
-
response: tokenResponseSchema,
|
|
304
|
-
},
|
|
305
|
-
handler: async ({ query, body, cookies }) => {
|
|
306
|
-
const provider = this.provider(query);
|
|
307
|
-
const realm = "realm" in provider.options && provider.options.realm;
|
|
308
|
-
if (!realm) {
|
|
309
|
-
throw new SecurityError(
|
|
310
|
-
`Auth provider '${query.provider}' does not support password grant`,
|
|
311
|
-
);
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
const credentials =
|
|
315
|
-
"credentials" in provider.options && provider.options.credentials;
|
|
316
|
-
|
|
317
|
-
if (!credentials) {
|
|
318
|
-
throw new SecurityError(
|
|
319
|
-
`Auth provider '${query.provider}' does not support password grant`,
|
|
320
|
-
);
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
let user: UserAccount;
|
|
324
|
-
try {
|
|
325
|
-
user = await credentials.account(body);
|
|
326
|
-
} catch (e) {
|
|
327
|
-
throw new UnauthorizedError(`Failed to authenticate user`, {
|
|
328
|
-
cause: e,
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
const tokens = {
|
|
333
|
-
provider: query.provider,
|
|
334
|
-
...(await realm.createToken(user)),
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
// for web applications, we store tokens in cookies
|
|
338
|
-
this.setTokens(tokens, cookies);
|
|
339
|
-
|
|
340
|
-
const api = await this.serverLinksProvider.getUserApiLinks({
|
|
341
|
-
user,
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
// mobile apps require this
|
|
345
|
-
return {
|
|
346
|
-
...tokens,
|
|
347
|
-
user,
|
|
348
|
-
api,
|
|
349
|
-
};
|
|
350
|
-
},
|
|
351
|
-
});
|
|
352
|
-
|
|
353
|
-
/**
|
|
354
|
-
* Oauth2/OIDC login route.
|
|
355
|
-
*/
|
|
356
|
-
public readonly login = $route({
|
|
357
|
-
path: ReactAuth.path.login,
|
|
358
|
-
schema: {
|
|
359
|
-
query: t.object({
|
|
360
|
-
provider: t.text(),
|
|
361
|
-
redirect_uri: t.optional(t.text({ size: "rich" })),
|
|
362
|
-
}),
|
|
363
|
-
},
|
|
364
|
-
handler: async ({ query, url, reply }) => {
|
|
365
|
-
const provider = this.provider(query);
|
|
366
|
-
const oauth = provider.oauth;
|
|
367
|
-
if (!oauth) {
|
|
368
|
-
throw new SecurityError(
|
|
369
|
-
`Auth provider '${query.provider}' does not support OAuth2`,
|
|
370
|
-
);
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
const scope = provider.scope;
|
|
374
|
-
let redirect_uri = provider.redirect_uri || ReactAuth.path.callback;
|
|
375
|
-
if (redirect_uri.startsWith("/")) {
|
|
376
|
-
redirect_uri = `${url.protocol}//${url.host}${redirect_uri}`;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
const oidc = "oidc" in provider.options && provider.options.oidc;
|
|
380
|
-
|
|
381
|
-
if (!oauth.serverMetadata().supportsPKCE()) {
|
|
382
|
-
const state = randomState();
|
|
383
|
-
const parameters: Record<string, string> = {
|
|
384
|
-
redirect_uri,
|
|
385
|
-
state,
|
|
386
|
-
};
|
|
387
|
-
|
|
388
|
-
if (oidc) {
|
|
389
|
-
parameters.nonce = randomState();
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
if (scope) {
|
|
393
|
-
parameters.scope = scope;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
this.authorizationCode.set({
|
|
397
|
-
state,
|
|
398
|
-
nonce: parameters.nonce,
|
|
399
|
-
redirectUri: query.redirect_uri ?? "/",
|
|
400
|
-
provider: query.provider,
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
reply.redirect(buildAuthorizationUrl(oauth, parameters).toString());
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
const codeVerifier = randomPKCECodeVerifier();
|
|
408
|
-
const codeChallenge = await calculatePKCECodeChallenge(codeVerifier);
|
|
409
|
-
|
|
410
|
-
const parameters: Record<string, string> = {
|
|
411
|
-
redirect_uri,
|
|
412
|
-
code_challenge: codeChallenge,
|
|
413
|
-
code_challenge_method: "S256",
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
if (scope) {
|
|
417
|
-
parameters.scope = scope;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
this.authorizationCode.set({
|
|
421
|
-
codeVerifier,
|
|
422
|
-
redirectUri: query.redirect_uri ?? "/",
|
|
423
|
-
provider: query.provider,
|
|
424
|
-
});
|
|
425
|
-
|
|
426
|
-
reply.redirect(buildAuthorizationUrl(oauth, parameters).toString());
|
|
427
|
-
},
|
|
428
|
-
});
|
|
429
|
-
|
|
430
|
-
/**
|
|
431
|
-
* Callback for OAuth2/OIDC providers.
|
|
432
|
-
* It handles the authorization code flow and retrieves the access token.
|
|
433
|
-
*/
|
|
434
|
-
public readonly callback = $route({
|
|
435
|
-
path: ReactAuth.path.callback,
|
|
436
|
-
handler: async ({ url, reply, cookies }) => {
|
|
437
|
-
const authorizationCode = this.authorizationCode.get({ cookies });
|
|
438
|
-
if (!authorizationCode) {
|
|
439
|
-
throw new BadRequestError("Missing code verifier");
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
const provider = this.provider(authorizationCode);
|
|
443
|
-
const oauth = provider.oauth;
|
|
444
|
-
if (!oauth) {
|
|
445
|
-
throw new SecurityError(
|
|
446
|
-
`Auth provider '${provider.name}' does not support OAuth2`,
|
|
447
|
-
);
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
const redirectUri = authorizationCode.redirectUri ?? "/";
|
|
451
|
-
|
|
452
|
-
const externalTokens = await authorizationCodeGrant(oauth, url, {
|
|
453
|
-
pkceCodeVerifier: authorizationCode.codeVerifier,
|
|
454
|
-
expectedState: authorizationCode.state,
|
|
455
|
-
expectedNonce: authorizationCode.nonce,
|
|
456
|
-
})
|
|
457
|
-
.then((tokens) => ({
|
|
458
|
-
issued_at: this.dateTimeProvider.now().unix(),
|
|
459
|
-
provider: provider.name,
|
|
460
|
-
...tokens,
|
|
461
|
-
}))
|
|
462
|
-
.catch((e) => {
|
|
463
|
-
this.log.error("Failed to get access token", e);
|
|
464
|
-
throw new SecurityError("Failed to get access token", {
|
|
465
|
-
cause: e,
|
|
466
|
-
});
|
|
467
|
-
});
|
|
468
|
-
|
|
469
|
-
this.authorizationCode.del({ cookies });
|
|
470
|
-
|
|
471
|
-
const realm = "realm" in provider.options && provider.options.realm;
|
|
472
|
-
|
|
473
|
-
// external, full OIDC System (e.g. Keycloak, Auth0)
|
|
474
|
-
if (!realm) {
|
|
475
|
-
this.setTokens(externalTokens, cookies);
|
|
476
|
-
reply.redirect(redirectUri);
|
|
477
|
-
return;
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
// internal, we need to create our own tokens
|
|
481
|
-
|
|
482
|
-
const user = await provider.user(externalTokens);
|
|
483
|
-
const tokens = await realm.createToken(user);
|
|
484
|
-
|
|
485
|
-
this.setTokens(
|
|
486
|
-
{
|
|
487
|
-
...tokens,
|
|
488
|
-
issued_at: this.dateTimeProvider.now().unix(),
|
|
489
|
-
provider: provider.name,
|
|
490
|
-
},
|
|
491
|
-
cookies,
|
|
492
|
-
);
|
|
493
|
-
|
|
494
|
-
reply.redirect(redirectUri);
|
|
495
|
-
},
|
|
496
|
-
});
|
|
497
|
-
|
|
498
|
-
/**
|
|
499
|
-
* Logout route for OAuth2/OIDC providers.
|
|
500
|
-
*/
|
|
501
|
-
public readonly logout = $route({
|
|
502
|
-
path: ReactAuth.path.logout,
|
|
503
|
-
method: "GET",
|
|
504
|
-
schema: {
|
|
505
|
-
query: t.object({
|
|
506
|
-
post_logout_redirect_uri: t.optional(t.text()),
|
|
507
|
-
}),
|
|
508
|
-
},
|
|
509
|
-
handler: async ({ query, reply, cookies }) => {
|
|
510
|
-
const redirect = query.post_logout_redirect_uri ?? "/";
|
|
511
|
-
const tokens = this.tokens.get({ cookies });
|
|
512
|
-
if (!tokens) {
|
|
513
|
-
reply.redirect(redirect);
|
|
514
|
-
return;
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
const provider = this.provider(tokens.provider);
|
|
518
|
-
|
|
519
|
-
this.tokens.del({ cookies });
|
|
520
|
-
|
|
521
|
-
// for internal providers, we can delete the session - if available
|
|
522
|
-
if ("realm" in provider.options && tokens.refresh_token) {
|
|
523
|
-
const onDeleteSession =
|
|
524
|
-
provider.options.realm.options.settings?.onDeleteSession;
|
|
525
|
-
if (onDeleteSession) {
|
|
526
|
-
try {
|
|
527
|
-
await onDeleteSession(tokens.refresh_token);
|
|
528
|
-
} catch (e) {
|
|
529
|
-
this.log.error("Failed to delete session", e);
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
const oauth = provider.oauth;
|
|
535
|
-
if (!oauth) {
|
|
536
|
-
reply.redirect(redirect);
|
|
537
|
-
return;
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
const params = new URLSearchParams();
|
|
541
|
-
const idToken = tokens?.id_token;
|
|
542
|
-
|
|
543
|
-
params.set("post_logout_redirect_uri", redirect);
|
|
544
|
-
if (idToken) {
|
|
545
|
-
params.set("id_token_hint", idToken);
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
const customLogoutUri =
|
|
549
|
-
"oidc" in provider.options
|
|
550
|
-
? provider.options.oidc?.logoutUri
|
|
551
|
-
: undefined;
|
|
552
|
-
|
|
553
|
-
if (customLogoutUri) {
|
|
554
|
-
reply.redirect(`${customLogoutUri}?${params}`);
|
|
555
|
-
return;
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
if (!oauth.serverMetadata().end_session_endpoint) {
|
|
559
|
-
// await tokenRevocation(
|
|
560
|
-
// oauth,
|
|
561
|
-
// tokens?.refresh_token ?? tokens.access_token,
|
|
562
|
-
// );
|
|
563
|
-
reply.redirect(redirect);
|
|
564
|
-
return;
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
reply.redirect(buildEndSessionUrl(oauth, params).toString());
|
|
568
|
-
},
|
|
569
|
-
});
|
|
570
|
-
|
|
571
|
-
protected provider(opts: string | { provider: string }): AuthDescriptor {
|
|
572
|
-
const name = typeof opts === "string" ? opts : opts.provider;
|
|
573
|
-
const identity = this.identities.find((identity) => identity.name === name);
|
|
574
|
-
|
|
575
|
-
if (!identity) {
|
|
576
|
-
throw new SecurityError(`Auth provider '${name}' not found`);
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
return identity;
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
protected setTokens(tokens: Tokens, cookies?: Cookies): void {
|
|
583
|
-
const exp =
|
|
584
|
-
tokens.refresh_token_expires_in ||
|
|
585
|
-
tokens.refresh_expires_in ||
|
|
586
|
-
tokens.expires_in;
|
|
587
|
-
|
|
588
|
-
const ttl = exp
|
|
589
|
-
? this.dateTimeProvider.duration(exp, "seconds")
|
|
590
|
-
: undefined;
|
|
591
|
-
|
|
592
|
-
this.tokens.set(tokens, {
|
|
593
|
-
cookies,
|
|
594
|
-
ttl,
|
|
595
|
-
});
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
export interface OAuth2Profile {
|
|
600
|
-
sub: string; // Subject - unique ID per user (required by OpenID)
|
|
601
|
-
email?: string;
|
|
602
|
-
name?: string;
|
|
603
|
-
given_name?: string;
|
|
604
|
-
family_name?: string;
|
|
605
|
-
middle_name?: string;
|
|
606
|
-
nickname?: string;
|
|
607
|
-
preferred_username?: string;
|
|
608
|
-
profile?: string;
|
|
609
|
-
picture?: string;
|
|
610
|
-
website?: string;
|
|
611
|
-
email_verified?: boolean;
|
|
612
|
-
gender?: string;
|
|
613
|
-
birthdate?: string; // ISO 8601: YYYY-MM-DD
|
|
614
|
-
zoneinfo?: string;
|
|
615
|
-
locale?: string;
|
|
616
|
-
phone_number?: string;
|
|
617
|
-
phone_number_verified?: boolean;
|
|
618
|
-
address?: {
|
|
619
|
-
formatted?: string;
|
|
620
|
-
street_address?: string;
|
|
621
|
-
locality?: string;
|
|
622
|
-
region?: string;
|
|
623
|
-
postal_code?: string;
|
|
624
|
-
country?: string;
|
|
625
|
-
};
|
|
626
|
-
updated_at?: number; // seconds since epoch
|
|
627
|
-
// Allow additional fields (provider-specific)
|
|
628
|
-
[key: string]: unknown;
|
|
629
16
|
}
|
|
@@ -3,9 +3,7 @@ import { $hook, $inject, Alepha } from "alepha";
|
|
|
3
3
|
import { $logger } from "alepha/logger";
|
|
4
4
|
import type { UserAccountToken } from "alepha/security";
|
|
5
5
|
import { HttpClient } from "alepha/server";
|
|
6
|
-
import { tokenResponseSchema } from "
|
|
7
|
-
import type { Tokens } from "../schemas/tokensSchema.ts";
|
|
8
|
-
import { userinfoResponseSchema } from "../schemas/userinfoResponseSchema.ts";
|
|
6
|
+
import { alephaServerAuthRoutes, tokenResponseSchema, type Tokens, userinfoResponseSchema } from "alepha/server/auth";
|
|
9
7
|
|
|
10
8
|
/**
|
|
11
9
|
* Browser, SSR friendly, service to handle authentication.
|
|
@@ -15,15 +13,6 @@ export class ReactAuth {
|
|
|
15
13
|
protected readonly alepha = $inject(Alepha);
|
|
16
14
|
protected readonly httpClient = $inject(HttpClient);
|
|
17
15
|
|
|
18
|
-
static path = {
|
|
19
|
-
login: "/oauth/login",
|
|
20
|
-
callback: "/oauth/callback",
|
|
21
|
-
logout: "/oauth/logout",
|
|
22
|
-
token: "/_auth/token",
|
|
23
|
-
refresh: "/_auth/refresh",
|
|
24
|
-
userinfo: "/_auth/userinfo",
|
|
25
|
-
};
|
|
26
|
-
|
|
27
16
|
protected readonly onBeginTransition = $hook({
|
|
28
17
|
on: "react:transition:begin",
|
|
29
18
|
handler: async (event) => {
|
|
@@ -55,7 +44,7 @@ export class ReactAuth {
|
|
|
55
44
|
}
|
|
56
45
|
|
|
57
46
|
public async ping() {
|
|
58
|
-
const { data } = await this.httpClient.fetch(
|
|
47
|
+
const { data } = await this.httpClient.fetch(alephaServerAuthRoutes.userinfo, {
|
|
59
48
|
schema: { response: userinfoResponseSchema },
|
|
60
49
|
});
|
|
61
50
|
|
|
@@ -77,7 +66,7 @@ export class ReactAuth {
|
|
|
77
66
|
): Promise<Tokens> {
|
|
78
67
|
if (options.username || options.password) {
|
|
79
68
|
const { data } = await this.httpClient.fetch(
|
|
80
|
-
`${options.hostname || ""}${
|
|
69
|
+
`${options.hostname || ""}${alephaServerAuthRoutes.token}?provider=${provider}`,
|
|
81
70
|
{
|
|
82
71
|
method: "POST",
|
|
83
72
|
body: JSON.stringify({
|
|
@@ -103,7 +92,7 @@ export class ReactAuth {
|
|
|
103
92
|
? window.location.origin + browser.transitioning.to
|
|
104
93
|
: window.location.href);
|
|
105
94
|
|
|
106
|
-
const href = `${window.location.origin}${
|
|
95
|
+
const href = `${window.location.origin}${alephaServerAuthRoutes.login}?provider=${provider}&redirect_uri=${encodeURIComponent(redirect)}`;
|
|
107
96
|
|
|
108
97
|
if (browser.transitioning) {
|
|
109
98
|
throw new Redirection(href);
|
|
@@ -114,11 +103,11 @@ export class ReactAuth {
|
|
|
114
103
|
}
|
|
115
104
|
|
|
116
105
|
throw new Redirection(
|
|
117
|
-
`${
|
|
106
|
+
`${alephaServerAuthRoutes.login}?provider=${provider}&redirect_uri=${options.redirect || "/"}`,
|
|
118
107
|
);
|
|
119
108
|
}
|
|
120
109
|
|
|
121
110
|
public logout() {
|
|
122
|
-
window.location.href = `${
|
|
111
|
+
window.location.href = `${alephaServerAuthRoutes.logout}?post_logout_redirect_uri=${encodeURIComponent(window.location.origin)}`;
|
|
123
112
|
}
|
|
124
113
|
}
|
|
@@ -61,7 +61,7 @@ import { ReactPageService } from "../services/ReactPageService.ts";
|
|
|
61
61
|
* const userProfile = $page({
|
|
62
62
|
* path: "/users/:id",
|
|
63
63
|
* schema: {
|
|
64
|
-
* params: t.object({ id: t.
|
|
64
|
+
* params: t.object({ id: t.integer() }),
|
|
65
65
|
* query: t.object({ tab: t.optional(t.text()) })
|
|
66
66
|
* },
|
|
67
67
|
* resolve: async ({ params }) => {
|
|
@@ -13,6 +13,7 @@ import { ReactRouter } from "./services/ReactRouter.ts";
|
|
|
13
13
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
14
14
|
|
|
15
15
|
export * from "./index.shared.ts";
|
|
16
|
+
export * from "./index.shared-router.ts";
|
|
16
17
|
export * from "./providers/ReactBrowserProvider.ts";
|
|
17
18
|
export * from "./providers/ReactBrowserRouterProvider.ts";
|
|
18
19
|
export * from "./providers/ReactPageProvider.ts";
|