@jskit-ai/auth-provider-supabase-core 0.2.4 → 0.2.6
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/locales/en.json +3 -19
- package/package.json +3 -3
- package/patterns/supabase-auth/example/package.json +1 -1
- package/src/server/lib/accountFlows.js +66 -86
- package/src/server/lib/authProfileNames.js +9 -12
- package/src/server/lib/authenticatedProfile.js +1 -2
- package/src/server/lib/config.js +90 -0
- package/src/server/lib/devAuthBootstrap.js +34 -57
- package/src/server/lib/flowGuards.js +1 -27
- package/src/server/lib/index.js +1 -1
- package/src/server/lib/oauthFlows.js +65 -68
- package/src/server/lib/{passwordSecurityFlows.js → passwordFlows.js} +68 -112
- package/src/server/lib/profileMirror.js +204 -0
- package/src/server/lib/profileModes.js +80 -0
- package/src/server/lib/securityStatus.js +109 -0
- package/src/server/lib/service.js +87 -1092
- package/src/server/lib/sessions.js +494 -0
- package/src/server/lib/supabaseClients.js +28 -0
- package/src/server/providers/AuthSupabaseProvider.js +19 -98
- package/src/server/types.js +21 -0
- package/test/auth.provider.supabase.test.js +155 -144
- package/test/codedTexts.test.js +3 -5
- package/test/devAuthBootstrap.test.js +37 -34
- package/test/index.test.js +3 -3
- package/src/server/lib/standaloneProfileSyncService.js +0 -97
- package/src/server/lib/test-utils.js +0 -44
package/locales/en.json
CHANGED
|
@@ -2,16 +2,11 @@
|
|
|
2
2
|
"jskit": {
|
|
3
3
|
"auth": {
|
|
4
4
|
"supabase": {
|
|
5
|
-
"config": {
|
|
6
|
-
"missing": "Auth provider is not configured.",
|
|
7
|
-
"passwordSettingsMissing": "Password sign-in settings repository is not configured."
|
|
8
|
-
},
|
|
9
5
|
"request": {
|
|
10
6
|
"failed": "Authentication request could not be processed."
|
|
11
7
|
},
|
|
12
8
|
"register": {
|
|
13
9
|
"emailTaken": "Email is already registered.",
|
|
14
|
-
"noUser": "Supabase sign-up did not return a user.",
|
|
15
10
|
"confirmationSent": "If an account exists for that email, a confirmation email has been sent."
|
|
16
11
|
},
|
|
17
12
|
"login": {
|
|
@@ -50,38 +45,27 @@
|
|
|
50
45
|
"currentUnverified": "Unable to verify current password.",
|
|
51
46
|
"signInDisabled": "Password sign-in is disabled for this account."
|
|
52
47
|
},
|
|
53
|
-
"session": {
|
|
54
|
-
"emailUnresolved": "Authenticated user email could not be resolved."
|
|
55
|
-
},
|
|
56
48
|
"methods": {
|
|
57
49
|
"lastEnabled": "At least one sign-in method must remain enabled.",
|
|
58
50
|
"enabledNotBoolean": "Enabled must be a boolean.",
|
|
59
|
-
"passwordUnresolved": "Password method configuration could not be resolved.",
|
|
60
51
|
"passwordNotSet": "Set a password before enabling password sign-in."
|
|
61
52
|
},
|
|
62
53
|
"link": {
|
|
63
54
|
"alreadyLinked": "This sign-in method is already linked.",
|
|
64
55
|
"manualLinkingDisabled": "Provider linking is disabled in Supabase. Enable Manual Linking in Supabase Auth settings to link or unlink providers.",
|
|
65
56
|
"lastIdentity": "At least one linked sign-in method must remain available.",
|
|
66
|
-
"notLinked": "This sign-in method is not currently linked."
|
|
67
|
-
"unsupported": "Supabase client does not support identity linking in this environment."
|
|
57
|
+
"notLinked": "This sign-in method is not currently linked."
|
|
68
58
|
},
|
|
69
59
|
"unlink": {
|
|
70
|
-
"unsupported": "Supabase client does not support identity unlinking in this environment.",
|
|
71
60
|
"notLinked": "{provider} is not linked to this account.",
|
|
72
61
|
"identityUnresolved": "Linked identity details could not be resolved for this provider."
|
|
73
62
|
},
|
|
74
63
|
"profile": {
|
|
75
|
-
"invalid": "{context} is invalid.",
|
|
76
64
|
"updateFailed": "Unable to update profile details.",
|
|
77
|
-
"
|
|
78
|
-
"emailConflict": "This email is already registered with another sign-in method. Sign in with that method, then link this provider in Settings > Security.",
|
|
79
|
-
"payloadIncomplete": "Auth provider user payload is missing required fields."
|
|
65
|
+
"emailConflict": "This email is already registered with another sign-in method. Sign in with that method, then link this provider in Settings > Security."
|
|
80
66
|
},
|
|
81
67
|
"dev": {
|
|
82
|
-
"sessionInvalid": "Dev session is invalid."
|
|
83
|
-
"findByIdMissing": "Dev auth bootstrap requires internal.repository.user-profiles.findById().",
|
|
84
|
-
"findByEmailMissing": "Dev auth bootstrap requires internal.repository.user-profiles.findByEmail() for email lookup."
|
|
68
|
+
"sessionInvalid": "Dev session is invalid."
|
|
85
69
|
}
|
|
86
70
|
}
|
|
87
71
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jskit-ai/auth-provider-supabase-core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "node --test --test-concurrency=1"
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
}
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|
|
63
|
-
"@jskit-ai/auth-core": "0.2.
|
|
64
|
-
"@jskit-ai/kernel": "0.2.
|
|
63
|
+
"@jskit-ai/auth-core": "0.2.4",
|
|
64
|
+
"@jskit-ai/kernel": "0.2.4"
|
|
65
65
|
}
|
|
66
66
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { AppError } from "@jskit-ai/kernel/server/runtime/errors";
|
|
2
1
|
import { AUTH_TEXTS, authFieldError, createAuthError } from "@jskit-ai/auth-core/server/authTexts";
|
|
3
2
|
import { authRegisterBodyValidator } from "@jskit-ai/auth-core/shared/commands/authRegisterCommand";
|
|
4
3
|
import { authRegisterConfirmationResendBodyValidator } from "@jskit-ai/auth-core/shared/commands/authRegisterConfirmationResendCommand";
|
|
@@ -9,6 +8,17 @@ import {
|
|
|
9
8
|
requireAuthUser,
|
|
10
9
|
requireAuthUserSession
|
|
11
10
|
} from "./flowGuards.js";
|
|
11
|
+
import {
|
|
12
|
+
isTransientSupabaseError,
|
|
13
|
+
isUserNotFoundLikeAuthError,
|
|
14
|
+
mapAuthError,
|
|
15
|
+
mapOtpVerifyError,
|
|
16
|
+
mapProfileUpdateError,
|
|
17
|
+
validationError
|
|
18
|
+
} from "./authErrorMappers.js";
|
|
19
|
+
import { displayNameFromEmail } from "./authProfileNames.js";
|
|
20
|
+
import { normalizeReturnToPath } from "@jskit-ai/auth-core/server/utils";
|
|
21
|
+
import { buildOtpLoginRedirectUrl } from "./authRedirectUrls.js";
|
|
12
22
|
|
|
13
23
|
// The answer is the same whether or not an account exists for the email.
|
|
14
24
|
const CONFIRMATION_SENT_RESULT = Object.freeze({
|
|
@@ -25,80 +35,54 @@ const OTP_SENT_RESULT = Object.freeze({
|
|
|
25
35
|
messageParams: Object.freeze({})
|
|
26
36
|
});
|
|
27
37
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function createAccountFlows(deps) {
|
|
42
|
-
const {
|
|
43
|
-
ensureConfigured,
|
|
44
|
-
validationError,
|
|
45
|
-
getSupabaseClient,
|
|
46
|
-
displayNameFromEmail,
|
|
47
|
-
mapAuthError,
|
|
48
|
-
syncProfileFromSupabaseUser,
|
|
49
|
-
resolvePasswordSignInPolicyForUserId,
|
|
50
|
-
otpLoginRedirectUrl,
|
|
51
|
-
buildOtpLoginRedirectUrl,
|
|
52
|
-
appPublicUrl,
|
|
53
|
-
isTransientSupabaseError,
|
|
54
|
-
isUserNotFoundLikeAuthError,
|
|
55
|
-
mapOtpVerifyError,
|
|
56
|
-
setSessionFromRequestCookies,
|
|
57
|
-
mapProfileUpdateError,
|
|
58
|
-
normalizeReturnToPath = normalizeLocalReturnToPath
|
|
59
|
-
} = deps;
|
|
60
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Registering, signing in with a password or a one-time code, and renaming
|
|
40
|
+
* an account through Supabase.
|
|
41
|
+
* @param {object} dependencies
|
|
42
|
+
* @param {import("../types.js").SupabaseConfig} dependencies.config
|
|
43
|
+
* @param {import("../types.js").SupabaseClients} dependencies.clients
|
|
44
|
+
* @param {import("../types.js").SupabaseSessions} dependencies.sessions
|
|
45
|
+
* @param {import("../types.js").SupabaseProfileMirror} dependencies.profileMirror
|
|
46
|
+
* @param {import("../types.js").SupabaseSecurityStatus} dependencies.securityStatus
|
|
47
|
+
*/
|
|
48
|
+
function createSupabaseAccountFlows({ config, clients, sessions, profileMirror, securityStatus }) {
|
|
61
49
|
function resolveOtpEmailRedirectTo(returnToValue) {
|
|
62
|
-
const returnTo = normalizeReturnToPath(returnToValue, { fallback: "" });
|
|
50
|
+
const returnTo = normalizeReturnToPath(returnToValue, { fallback: "", allowedOrigins: config.allowedReturnToOrigins });
|
|
63
51
|
if (!returnTo) {
|
|
64
|
-
return otpLoginRedirectUrl;
|
|
52
|
+
return config.otpLoginRedirectUrl;
|
|
65
53
|
}
|
|
66
54
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
// Fall through to the pre-built URL fallback below.
|
|
75
|
-
}
|
|
55
|
+
try {
|
|
56
|
+
return buildOtpLoginRedirectUrl({
|
|
57
|
+
appPublicUrl: config.appPublicUrl,
|
|
58
|
+
returnTo
|
|
59
|
+
});
|
|
60
|
+
} catch {
|
|
61
|
+
// Fall through to the pre-built URL fallback below.
|
|
76
62
|
}
|
|
77
63
|
|
|
78
64
|
try {
|
|
79
|
-
const redirectUrl = new URL(String(otpLoginRedirectUrl || ""));
|
|
65
|
+
const redirectUrl = new URL(String(config.otpLoginRedirectUrl || ""));
|
|
80
66
|
redirectUrl.searchParams.set("returnTo", returnTo);
|
|
81
67
|
return redirectUrl.toString();
|
|
82
68
|
} catch {
|
|
83
|
-
return otpLoginRedirectUrl;
|
|
69
|
+
return config.otpLoginRedirectUrl;
|
|
84
70
|
}
|
|
85
71
|
}
|
|
86
72
|
|
|
87
73
|
function resolveRegisterEmailRedirectTo() {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
// Fall through to the pre-built URL fallback below.
|
|
95
|
-
}
|
|
74
|
+
try {
|
|
75
|
+
return buildOtpLoginRedirectUrl({
|
|
76
|
+
appPublicUrl: config.appPublicUrl
|
|
77
|
+
});
|
|
78
|
+
} catch {
|
|
79
|
+
// Fall through to the pre-built URL fallback below.
|
|
96
80
|
}
|
|
97
|
-
return otpLoginRedirectUrl;
|
|
81
|
+
return config.otpLoginRedirectUrl;
|
|
98
82
|
}
|
|
99
83
|
|
|
100
84
|
async function register(payload) {
|
|
101
|
-
ensureConfigured();
|
|
85
|
+
clients.ensureConfigured();
|
|
102
86
|
|
|
103
87
|
const result = authRegisterBodyValidator.schema.create(payload);
|
|
104
88
|
if (Object.keys(result.errors).length > 0) {
|
|
@@ -106,7 +90,7 @@ function createAccountFlows(deps) {
|
|
|
106
90
|
}
|
|
107
91
|
const parsed = result.validatedObject;
|
|
108
92
|
|
|
109
|
-
const supabase =
|
|
93
|
+
const supabase = clients.createSupabaseClient();
|
|
110
94
|
const response = await supabase.auth.signUp({
|
|
111
95
|
email: parsed.email,
|
|
112
96
|
password: parsed.password,
|
|
@@ -123,10 +107,10 @@ function createAccountFlows(deps) {
|
|
|
123
107
|
}
|
|
124
108
|
|
|
125
109
|
if (!response.data?.user) {
|
|
126
|
-
throw new
|
|
110
|
+
throw new Error("Supabase sign-up did not return a user.");
|
|
127
111
|
}
|
|
128
112
|
|
|
129
|
-
const profile = await syncProfileFromSupabaseUser(response.data.user, parsed.email);
|
|
113
|
+
const profile = await profileMirror.syncProfileFromSupabaseUser(response.data.user, parsed.email);
|
|
130
114
|
|
|
131
115
|
if (!response.data.session) {
|
|
132
116
|
return {
|
|
@@ -145,7 +129,7 @@ function createAccountFlows(deps) {
|
|
|
145
129
|
}
|
|
146
130
|
|
|
147
131
|
async function resendRegisterConfirmation(payload) {
|
|
148
|
-
ensureConfigured();
|
|
132
|
+
clients.ensureConfigured();
|
|
149
133
|
|
|
150
134
|
const result = authRegisterConfirmationResendBodyValidator.schema.create(payload);
|
|
151
135
|
if (Object.keys(result.errors).length > 0) {
|
|
@@ -153,7 +137,7 @@ function createAccountFlows(deps) {
|
|
|
153
137
|
}
|
|
154
138
|
const parsed = result.validatedObject;
|
|
155
139
|
|
|
156
|
-
const supabase =
|
|
140
|
+
const supabase = clients.createSupabaseClient();
|
|
157
141
|
const emailRedirectTo = resolveRegisterEmailRedirectTo();
|
|
158
142
|
let response;
|
|
159
143
|
try {
|
|
@@ -188,7 +172,7 @@ function createAccountFlows(deps) {
|
|
|
188
172
|
}
|
|
189
173
|
|
|
190
174
|
async function login(payload) {
|
|
191
|
-
ensureConfigured();
|
|
175
|
+
clients.ensureConfigured();
|
|
192
176
|
|
|
193
177
|
const result = authLoginPasswordBodyValidator.schema.create(payload);
|
|
194
178
|
if (Object.keys(result.errors).length > 0) {
|
|
@@ -196,7 +180,7 @@ function createAccountFlows(deps) {
|
|
|
196
180
|
}
|
|
197
181
|
const parsed = result.validatedObject;
|
|
198
182
|
|
|
199
|
-
const supabase =
|
|
183
|
+
const supabase = clients.createSupabaseClient();
|
|
200
184
|
const response = await supabase.auth.signInWithPassword({
|
|
201
185
|
email: parsed.email,
|
|
202
186
|
password: parsed.password
|
|
@@ -204,8 +188,8 @@ function createAccountFlows(deps) {
|
|
|
204
188
|
|
|
205
189
|
const { user, session } = requireAuthUserSession(response, mapAuthError, 401);
|
|
206
190
|
|
|
207
|
-
const profile = await syncProfileFromSupabaseUser(user, parsed.email);
|
|
208
|
-
const passwordSignInPolicy = await
|
|
191
|
+
const profile = await profileMirror.syncProfileFromSupabaseUser(user, parsed.email);
|
|
192
|
+
const passwordSignInPolicy = await securityStatus.passwordSignInPolicyForUser(profile.id);
|
|
209
193
|
if (!passwordSignInPolicy.passwordSignInEnabled) {
|
|
210
194
|
throw createAuthError(AUTH_TEXTS.invalidCredentials);
|
|
211
195
|
}
|
|
@@ -217,7 +201,7 @@ function createAccountFlows(deps) {
|
|
|
217
201
|
}
|
|
218
202
|
|
|
219
203
|
async function requestOtpLogin(payload) {
|
|
220
|
-
ensureConfigured();
|
|
204
|
+
clients.ensureConfigured();
|
|
221
205
|
|
|
222
206
|
const result = authLoginOtpRequestBodyValidator.schema.create(payload);
|
|
223
207
|
if (Object.keys(result.errors).length > 0) {
|
|
@@ -226,7 +210,7 @@ function createAccountFlows(deps) {
|
|
|
226
210
|
const parsed = result.validatedObject;
|
|
227
211
|
|
|
228
212
|
const emailRedirectTo = resolveOtpEmailRedirectTo(parsed.returnTo);
|
|
229
|
-
const supabase =
|
|
213
|
+
const supabase = clients.createSupabaseClient();
|
|
230
214
|
let response;
|
|
231
215
|
try {
|
|
232
216
|
response = await supabase.auth.signInWithOtp({
|
|
@@ -260,9 +244,9 @@ function createAccountFlows(deps) {
|
|
|
260
244
|
}
|
|
261
245
|
|
|
262
246
|
async function verifyOtpLogin(payload) {
|
|
263
|
-
ensureConfigured();
|
|
247
|
+
clients.ensureConfigured();
|
|
264
248
|
|
|
265
|
-
const result = authLoginOtpVerifyBodyValidator.schema.
|
|
249
|
+
const result = authLoginOtpVerifyBodyValidator.schema.create(payload);
|
|
266
250
|
if (Object.keys(result.errors).length > 0) {
|
|
267
251
|
throw validationError(result.errors);
|
|
268
252
|
}
|
|
@@ -270,7 +254,7 @@ function createAccountFlows(deps) {
|
|
|
270
254
|
const fieldErrors = {};
|
|
271
255
|
const token = String(parsed.token || "").trim();
|
|
272
256
|
const tokenHash = String(parsed.tokenHash || "").trim();
|
|
273
|
-
const type = String(parsed.type || "email").trim().toLowerCase();
|
|
257
|
+
const type = /** @type {import("@supabase/supabase-js").EmailOtpType} */ (String(parsed.type || "email").trim().toLowerCase());
|
|
274
258
|
|
|
275
259
|
if (!token && !tokenHash) {
|
|
276
260
|
fieldErrors.token = { code: "auth.supabase.otp.codeRequired", message: "One-time code is required." };
|
|
@@ -284,7 +268,7 @@ function createAccountFlows(deps) {
|
|
|
284
268
|
throw validationError(fieldErrors);
|
|
285
269
|
}
|
|
286
270
|
|
|
287
|
-
const supabase =
|
|
271
|
+
const supabase = clients.createSupabaseClient();
|
|
288
272
|
let response;
|
|
289
273
|
try {
|
|
290
274
|
if (tokenHash) {
|
|
@@ -305,7 +289,7 @@ function createAccountFlows(deps) {
|
|
|
305
289
|
|
|
306
290
|
const { user, session } = requireAuthUserSession(response, mapOtpVerifyError);
|
|
307
291
|
|
|
308
|
-
const profile = await syncProfileFromSupabaseUser(user, user.email || parsed.email);
|
|
292
|
+
const profile = await profileMirror.syncProfileFromSupabaseUser(user, user.email || parsed.email);
|
|
309
293
|
|
|
310
294
|
return {
|
|
311
295
|
profile,
|
|
@@ -313,22 +297,18 @@ function createAccountFlows(deps) {
|
|
|
313
297
|
};
|
|
314
298
|
}
|
|
315
299
|
|
|
316
|
-
async function updateDisplayName(request, payload
|
|
317
|
-
ensureConfigured();
|
|
300
|
+
async function updateDisplayName(request, payload) {
|
|
301
|
+
clients.ensureConfigured();
|
|
318
302
|
|
|
319
|
-
const normalizedDisplayName = String(
|
|
320
|
-
payload && typeof payload === "object" && !Array.isArray(payload)
|
|
321
|
-
? payload.displayName
|
|
322
|
-
: payload
|
|
323
|
-
).trim();
|
|
303
|
+
const normalizedDisplayName = String(payload.displayName || "").trim();
|
|
324
304
|
if (!normalizedDisplayName) {
|
|
325
305
|
throw validationError({
|
|
326
306
|
displayName: authFieldError(AUTH_TEXTS.displayNameRequired)
|
|
327
307
|
});
|
|
328
308
|
}
|
|
329
309
|
|
|
330
|
-
const supabase =
|
|
331
|
-
const sessionResponse = await setSessionFromRequestCookies(request, {
|
|
310
|
+
const supabase = clients.createSupabaseClient();
|
|
311
|
+
const sessionResponse = await sessions.setSessionFromRequestCookies(request, {
|
|
332
312
|
supabaseClient: supabase
|
|
333
313
|
});
|
|
334
314
|
|
|
@@ -345,7 +325,7 @@ function createAccountFlows(deps) {
|
|
|
345
325
|
|
|
346
326
|
const { user } = requireAuthUser(updateResponse, mapProfileUpdateError);
|
|
347
327
|
|
|
348
|
-
const profile = await syncProfileFromSupabaseUser(user, user.email);
|
|
328
|
+
const profile = await profileMirror.syncProfileFromSupabaseUser(user, user.email);
|
|
349
329
|
|
|
350
330
|
return {
|
|
351
331
|
profile,
|
|
@@ -353,14 +333,14 @@ function createAccountFlows(deps) {
|
|
|
353
333
|
};
|
|
354
334
|
}
|
|
355
335
|
|
|
356
|
-
return {
|
|
336
|
+
return Object.freeze({
|
|
357
337
|
register,
|
|
358
338
|
resendRegisterConfirmation,
|
|
359
339
|
login,
|
|
360
340
|
requestOtpLogin,
|
|
361
341
|
verifyOtpLogin,
|
|
362
342
|
updateDisplayName
|
|
363
|
-
};
|
|
343
|
+
});
|
|
364
344
|
}
|
|
365
345
|
|
|
366
|
-
export {
|
|
346
|
+
export { createSupabaseAccountFlows };
|
|
@@ -3,8 +3,14 @@ function displayNameFromEmail(email) {
|
|
|
3
3
|
return local.slice(0, 120);
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
/**
|
|
7
|
+
* The display name in a Supabase user's or its token claims' metadata, else
|
|
8
|
+
* one made from the email.
|
|
9
|
+
* @param {{ user_metadata?: { display_name?: unknown } } | null | undefined} source
|
|
10
|
+
* @param {string} fallbackEmail
|
|
11
|
+
*/
|
|
12
|
+
function resolveDisplayName(source, fallbackEmail) {
|
|
13
|
+
const metadataDisplayName = String(source?.user_metadata?.display_name || "").trim();
|
|
8
14
|
if (metadataDisplayName) {
|
|
9
15
|
return metadataDisplayName.slice(0, 120);
|
|
10
16
|
}
|
|
@@ -12,13 +18,4 @@ function resolveDisplayName(supabaseUser, fallbackEmail) {
|
|
|
12
18
|
return displayNameFromEmail(fallbackEmail);
|
|
13
19
|
}
|
|
14
20
|
|
|
15
|
-
|
|
16
|
-
const metadataDisplayName = String(claims?.user_metadata?.display_name || "").trim();
|
|
17
|
-
if (metadataDisplayName) {
|
|
18
|
-
return metadataDisplayName.slice(0, 120);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
return displayNameFromEmail(fallbackEmail);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export { displayNameFromEmail, resolveDisplayName, resolveDisplayNameFromClaims };
|
|
21
|
+
export { displayNameFromEmail, resolveDisplayName };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createSchema } from "json-rest-schema";
|
|
2
|
-
import { AppError } from "@jskit-ai/kernel/server/runtime/errors";
|
|
3
2
|
import { RECORD_ID_PATTERN } from "@jskit-ai/kernel/shared/validators";
|
|
4
3
|
|
|
5
4
|
const authenticatedProfileSchema = createSchema({
|
|
@@ -69,7 +68,7 @@ function requireAuthenticatedProfile(profileLike, { context = "authenticated pro
|
|
|
69
68
|
|
|
70
69
|
const { validatedObject, errors } = authenticatedProfileSchema.create(candidate);
|
|
71
70
|
if (Object.keys(errors).length > 0) {
|
|
72
|
-
throw new
|
|
71
|
+
throw new Error(`${context} is invalid.`);
|
|
73
72
|
}
|
|
74
73
|
|
|
75
74
|
return validatedObject;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { assertDevAuthPolicy } from "@jskit-ai/auth-core/server/devAuth";
|
|
2
|
+
import { normalizeObject } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
|
+
import { resolveAllowedOriginsFromSurfaceDefinitions } from "@jskit-ai/kernel/shared/support/returnToPath";
|
|
4
|
+
import { buildOtpLoginRedirectUrl, buildPasswordResetRedirectUrl } from "./authRedirectUrls.js";
|
|
5
|
+
import { resolveDevAuthConfig } from "./devAuthBootstrap.js";
|
|
6
|
+
import { buildOAuthProviderCatalogResponse, resolveSupabaseOAuthProviderCatalog } from "./oauthProviderCatalog.js";
|
|
7
|
+
import { SUPABASE_PROVIDER_ID } from "./profileMirror.js";
|
|
8
|
+
|
|
9
|
+
const PROFILE_MODES = Object.freeze(["provider", "users"]);
|
|
10
|
+
const DEFAULT_JWT_AUDIENCE = "authenticated";
|
|
11
|
+
|
|
12
|
+
function splitCsv(value) {
|
|
13
|
+
return String(value || "").split(",").map((entry) => entry.trim()).filter(Boolean);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The OAuth providers the app offers: AUTH_OAUTH_PROVIDERS, else config.auth.oauth. */
|
|
17
|
+
function resolveOAuthCatalog(env, auth) {
|
|
18
|
+
const oauth = normalizeObject(auth.oauth);
|
|
19
|
+
const envProviders = splitCsv(env.AUTH_OAUTH_PROVIDERS);
|
|
20
|
+
const configuredProviders = Array.isArray(oauth.providers)
|
|
21
|
+
? oauth.providers.map((entry) => String(entry || "").trim()).filter(Boolean)
|
|
22
|
+
: splitCsv(oauth.providers);
|
|
23
|
+
return resolveSupabaseOAuthProviderCatalog({
|
|
24
|
+
oauthProviders: envProviders.length > 0 ? envProviders : configuredProviders,
|
|
25
|
+
oauthDefaultProvider: String(env.AUTH_OAUTH_DEFAULT_PROVIDER || "").trim() || String(oauth.defaultProvider || "").trim(),
|
|
26
|
+
oauthProviderQueryParams: normalizeObject(oauth.queryParams)
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function resolveProfileMode(auth) {
|
|
31
|
+
const mode = String(auth.profileMode || "provider").trim().toLowerCase();
|
|
32
|
+
if (!PROFILE_MODES.includes(mode)) {
|
|
33
|
+
throw new Error(`Unsupported config.auth.profileMode "${mode}". Supported values: ${PROFILE_MODES.join(", ")}.`);
|
|
34
|
+
}
|
|
35
|
+
return mode;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Refuses an AUTH_PROVIDER that names another provider than this installed one.
|
|
40
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} env
|
|
41
|
+
*/
|
|
42
|
+
function assertSelectedProvider(env) {
|
|
43
|
+
const selected = String(env.AUTH_PROVIDER || "").trim().toLowerCase();
|
|
44
|
+
if (selected && selected !== SUPABASE_PROVIDER_ID) {
|
|
45
|
+
throw new Error(`AUTH_PROVIDER is "${selected}", but the installed auth provider is Supabase.`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Supabase auth's settings, read once from the app's config and Env: the
|
|
51
|
+
* project, where profiles live, the app's addresses, its OAuth providers and
|
|
52
|
+
* dev auth. Production is worked out once, by auth-core's dev auth policy.
|
|
53
|
+
* @param {object} sources
|
|
54
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeConfig} sources.config
|
|
55
|
+
* @param {import("@jskit-ai/kernel/server/types").RuntimeEnv} sources.env
|
|
56
|
+
*/
|
|
57
|
+
function resolveSupabaseConfig({ config, env }) {
|
|
58
|
+
const auth = normalizeObject(config.auth);
|
|
59
|
+
const profileMode = resolveProfileMode(auth);
|
|
60
|
+
const supabaseUrl = String(env.AUTH_SUPABASE_URL || env.SUPABASE_URL || "").trim();
|
|
61
|
+
const supabasePublishableKey = String(env.AUTH_SUPABASE_PUBLISHABLE_KEY || env.SUPABASE_PUBLISHABLE_KEY || "").trim();
|
|
62
|
+
const jwtAudience = String(env.AUTH_JWT_AUDIENCE || "").trim() || DEFAULT_JWT_AUDIENCE;
|
|
63
|
+
const appPublicUrl = String(env.APP_PUBLIC_URL || "").trim();
|
|
64
|
+
const oauth = resolveOAuthCatalog(env, auth);
|
|
65
|
+
const devAuth = resolveDevAuthConfig(env, jwtAudience);
|
|
66
|
+
assertDevAuthPolicy(devAuth);
|
|
67
|
+
if (devAuth.enabled && profileMode !== "users") {
|
|
68
|
+
throw new Error('Dev auth signs in as an app user, so AUTH_DEV_BYPASS_ENABLED=true requires config.auth.profileMode "users".');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return Object.freeze({
|
|
72
|
+
profileMode,
|
|
73
|
+
supabaseUrl,
|
|
74
|
+
supabasePublishableKey,
|
|
75
|
+
configured: Boolean(supabaseUrl && supabasePublishableKey),
|
|
76
|
+
jwtAudience,
|
|
77
|
+
isProduction: devAuth.isProduction,
|
|
78
|
+
appPublicUrl,
|
|
79
|
+
allowedReturnToOrigins: resolveAllowedOriginsFromSurfaceDefinitions(config.surfaceDefinitions || {}, {
|
|
80
|
+
seedOrigins: [appPublicUrl]
|
|
81
|
+
}),
|
|
82
|
+
passwordResetRedirectUrl: buildPasswordResetRedirectUrl({ appPublicUrl }),
|
|
83
|
+
otpLoginRedirectUrl: buildOtpLoginRedirectUrl({ appPublicUrl }),
|
|
84
|
+
oauth,
|
|
85
|
+
oauthCatalog: Object.freeze(buildOAuthProviderCatalogResponse(oauth)),
|
|
86
|
+
devAuth
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export { assertSelectedProvider, resolveSupabaseConfig };
|