@jskit-ai/auth-provider-supabase-core 0.2.5 → 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
|
@@ -8,38 +8,32 @@ import {
|
|
|
8
8
|
requireAuthSession,
|
|
9
9
|
requireAuthUserSession
|
|
10
10
|
} from "./flowGuards.js";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
buildSecurityStatusFromAuthMethodsStatus,
|
|
36
|
-
authMethodPasswordProvider,
|
|
37
|
-
buildAuthMethodsStatusFromProviderIds,
|
|
38
|
-
resolveDevAuthSecurityStatus
|
|
39
|
-
} = deps;
|
|
40
|
-
|
|
11
|
+
import { AUTH_METHOD_PASSWORD_ID } from "@jskit-ai/auth-core/shared/authMethods";
|
|
12
|
+
import { normalizeEmail } from "@jskit-ai/auth-core/server/utils";
|
|
13
|
+
import {
|
|
14
|
+
mapAuthError,
|
|
15
|
+
mapCurrentPasswordError,
|
|
16
|
+
mapPasswordUpdateError,
|
|
17
|
+
mapRecoveryError,
|
|
18
|
+
validationError
|
|
19
|
+
} from "./authErrorMappers.js";
|
|
20
|
+
import { buildAuthMethodsStatusFromSupabaseUser, findAuthMethodById } from "./authMethodStatus.js";
|
|
21
|
+
import { buildDisabledPasswordSecret } from "./authSecrets.js";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Password recovery, changing a password and turning password sign-in on or
|
|
25
|
+
* off, through Supabase.
|
|
26
|
+
* @param {object} dependencies
|
|
27
|
+
* @param {import("../types.js").SupabaseConfig} dependencies.config
|
|
28
|
+
* @param {import("../types.js").SupabaseClients} dependencies.clients
|
|
29
|
+
* @param {import("../types.js").SupabaseSessions} dependencies.sessions
|
|
30
|
+
* @param {import("../types.js").SupabaseProfileMirror} dependencies.profileMirror
|
|
31
|
+
* @param {import("../types.js").SupabaseSecurityStatus} dependencies.securityStatus
|
|
32
|
+
* @param {import("../types.js").SupabaseProfiles} dependencies.profiles
|
|
33
|
+
*/
|
|
34
|
+
function createSupabasePasswordFlows({ config, clients, sessions, profileMirror, securityStatus, profiles }) {
|
|
41
35
|
async function requestPasswordReset(payload) {
|
|
42
|
-
ensureConfigured();
|
|
36
|
+
clients.ensureConfigured();
|
|
43
37
|
|
|
44
38
|
const result = authPasswordResetRequestBodyValidator.schema.create(payload);
|
|
45
39
|
if (Object.keys(result.errors).length > 0) {
|
|
@@ -47,8 +41,8 @@ function createPasswordSecurityFlows(deps) {
|
|
|
47
41
|
}
|
|
48
42
|
const parsed = result.validatedObject;
|
|
49
43
|
|
|
50
|
-
const supabase =
|
|
51
|
-
const options = { redirectTo: passwordResetRedirectUrl };
|
|
44
|
+
const supabase = clients.createSupabaseClient();
|
|
45
|
+
const options = { redirectTo: config.passwordResetRedirectUrl };
|
|
52
46
|
let response;
|
|
53
47
|
try {
|
|
54
48
|
response = await supabase.auth.resetPasswordForEmail(parsed.email, options);
|
|
@@ -71,7 +65,7 @@ function createPasswordSecurityFlows(deps) {
|
|
|
71
65
|
}
|
|
72
66
|
|
|
73
67
|
async function completePasswordRecovery(payload) {
|
|
74
|
-
ensureConfigured();
|
|
68
|
+
clients.ensureConfigured();
|
|
75
69
|
|
|
76
70
|
const result = authPasswordRecoveryCompleteBodyValidator.schema.patch(payload);
|
|
77
71
|
if (Object.keys(result.errors).length > 0) {
|
|
@@ -110,7 +104,7 @@ function createPasswordSecurityFlows(deps) {
|
|
|
110
104
|
throw validationError(fieldErrors);
|
|
111
105
|
}
|
|
112
106
|
|
|
113
|
-
const supabase =
|
|
107
|
+
const supabase = clients.createSupabaseClient();
|
|
114
108
|
let response;
|
|
115
109
|
try {
|
|
116
110
|
if (hasCode) {
|
|
@@ -137,7 +131,7 @@ function createPasswordSecurityFlows(deps) {
|
|
|
137
131
|
|
|
138
132
|
const { user, session } = requireAuthUserSession(response, () =>
|
|
139
133
|
new AppError(401, "Recovery link is invalid or has expired.", { code: "auth.supabase.recovery.linkInvalid" }));
|
|
140
|
-
const profile = await syncProfileFromSupabaseUser(user, user.email);
|
|
134
|
+
const profile = await profileMirror.syncProfileFromSupabaseUser(user, user.email);
|
|
141
135
|
|
|
142
136
|
return {
|
|
143
137
|
profile,
|
|
@@ -148,8 +142,8 @@ function createPasswordSecurityFlows(deps) {
|
|
|
148
142
|
};
|
|
149
143
|
}
|
|
150
144
|
|
|
151
|
-
async function resetPassword(
|
|
152
|
-
ensureConfigured();
|
|
145
|
+
async function resetPassword(payload, request) {
|
|
146
|
+
clients.ensureConfigured();
|
|
153
147
|
|
|
154
148
|
const result = authPasswordResetBodyValidator.schema.create(payload);
|
|
155
149
|
if (Object.keys(result.errors).length > 0) {
|
|
@@ -157,15 +151,15 @@ function createPasswordSecurityFlows(deps) {
|
|
|
157
151
|
}
|
|
158
152
|
const parsed = result.validatedObject;
|
|
159
153
|
|
|
160
|
-
const supabase =
|
|
161
|
-
const sessionResponse = await setRecoverySessionFromRequestCookies(request, {
|
|
154
|
+
const supabase = clients.createSupabaseClient();
|
|
155
|
+
const sessionResponse = await sessions.setRecoverySessionFromRequestCookies(request, {
|
|
162
156
|
supabaseClient: supabase
|
|
163
157
|
});
|
|
164
|
-
const profile = await syncProfileFromSupabaseUser(
|
|
158
|
+
const profile = await profileMirror.syncProfileFromSupabaseUser(
|
|
165
159
|
sessionResponse.data.user,
|
|
166
160
|
sessionResponse.data.user?.email || ""
|
|
167
161
|
);
|
|
168
|
-
const passwordSignInPolicy = await
|
|
162
|
+
const passwordSignInPolicy = await securityStatus.passwordSignInPolicyForUser(profile.id);
|
|
169
163
|
if (!passwordSignInPolicy.passwordSignInEnabled) {
|
|
170
164
|
throw new AppError(409, "Password sign-in is disabled for this account.", { code: "auth.supabase.password.signInDisabled" });
|
|
171
165
|
}
|
|
@@ -181,8 +175,8 @@ function createPasswordSecurityFlows(deps) {
|
|
|
181
175
|
|
|
182
176
|
const { user } = requireAuthUser(updateResponse, mapPasswordUpdateError);
|
|
183
177
|
|
|
184
|
-
const updatedProfile = await syncProfileFromSupabaseUser(user, user.email);
|
|
185
|
-
await
|
|
178
|
+
const updatedProfile = await profileMirror.syncProfileFromSupabaseUser(user, user.email);
|
|
179
|
+
await profiles.userSettingsRepository.updatePasswordSetupRequired(updatedProfile.id, false);
|
|
186
180
|
|
|
187
181
|
const signOutResponse = await supabase.auth.signOut({
|
|
188
182
|
scope: "local"
|
|
@@ -194,23 +188,23 @@ function createPasswordSecurityFlows(deps) {
|
|
|
194
188
|
}
|
|
195
189
|
|
|
196
190
|
async function changePassword(request, payload) {
|
|
197
|
-
ensureConfigured();
|
|
191
|
+
clients.ensureConfigured();
|
|
198
192
|
|
|
199
|
-
const currentPassword = String(payload
|
|
200
|
-
const newPassword = String(payload
|
|
201
|
-
const requireCurrentPassword = payload
|
|
202
|
-
const supabase =
|
|
203
|
-
const sessionResponse = await setSessionFromRequestCookies(request, {
|
|
193
|
+
const currentPassword = String(payload.currentPassword || "");
|
|
194
|
+
const newPassword = String(payload.newPassword || "");
|
|
195
|
+
const requireCurrentPassword = payload.requireCurrentPassword !== false;
|
|
196
|
+
const supabase = clients.createSupabaseClient();
|
|
197
|
+
const sessionResponse = await sessions.setSessionFromRequestCookies(request, {
|
|
204
198
|
supabaseClient: supabase
|
|
205
199
|
});
|
|
206
200
|
|
|
207
201
|
const email = normalizeEmail(sessionResponse.data.user?.email || "");
|
|
208
202
|
if (!email) {
|
|
209
|
-
throw new
|
|
203
|
+
throw new Error("Authenticated user email could not be resolved.");
|
|
210
204
|
}
|
|
211
205
|
|
|
212
206
|
if (requireCurrentPassword) {
|
|
213
|
-
const verificationClient =
|
|
207
|
+
const verificationClient = clients.createSupabaseClient();
|
|
214
208
|
let verifyResponse;
|
|
215
209
|
try {
|
|
216
210
|
verifyResponse = await verificationClient.auth.signInWithPassword({
|
|
@@ -235,8 +229,8 @@ function createPasswordSecurityFlows(deps) {
|
|
|
235
229
|
|
|
236
230
|
const { user } = requireAuthUser(updateResponse, mapPasswordUpdateError);
|
|
237
231
|
|
|
238
|
-
const profile = await syncProfileFromSupabaseUser(user, user.email);
|
|
239
|
-
await
|
|
232
|
+
const profile = await profileMirror.syncProfileFromSupabaseUser(user, user.email);
|
|
233
|
+
await profiles.userSettingsRepository.updatePasswordSetupRequired(profile.id, false);
|
|
240
234
|
|
|
241
235
|
return {
|
|
242
236
|
profile,
|
|
@@ -244,28 +238,26 @@ function createPasswordSecurityFlows(deps) {
|
|
|
244
238
|
};
|
|
245
239
|
}
|
|
246
240
|
|
|
247
|
-
async function setPasswordSignInEnabled(request, payload
|
|
248
|
-
ensureConfigured();
|
|
241
|
+
async function setPasswordSignInEnabled(request, payload) {
|
|
242
|
+
clients.ensureConfigured();
|
|
249
243
|
|
|
250
|
-
if (typeof payload
|
|
244
|
+
if (typeof payload.enabled !== "boolean") {
|
|
251
245
|
throw validationError({
|
|
252
246
|
enabled: { code: "auth.supabase.methods.enabledNotBoolean", message: "Enabled must be a boolean." }
|
|
253
247
|
});
|
|
254
248
|
}
|
|
255
249
|
|
|
256
|
-
const supabase =
|
|
257
|
-
await setSessionFromRequestCookies(request, {
|
|
250
|
+
const supabase = clients.createSupabaseClient();
|
|
251
|
+
await sessions.setSessionFromRequestCookies(request, {
|
|
258
252
|
supabaseClient: supabase
|
|
259
253
|
});
|
|
260
|
-
const current = await
|
|
254
|
+
const current = await securityStatus.currentAuthContext(request, {
|
|
261
255
|
supabaseClient: supabase
|
|
262
256
|
});
|
|
263
|
-
const passwordMethod = findAuthMethodById(current.authMethodsStatus,
|
|
257
|
+
const passwordMethod = findAuthMethodById(current.authMethodsStatus, AUTH_METHOD_PASSWORD_ID);
|
|
264
258
|
|
|
265
259
|
if (!passwordMethod) {
|
|
266
|
-
throw new
|
|
267
|
-
code: "auth.supabase.methods.passwordUnresolved"
|
|
268
|
-
});
|
|
260
|
+
throw new Error("Password method configuration could not be resolved.");
|
|
269
261
|
}
|
|
270
262
|
|
|
271
263
|
if (payload.enabled && !passwordMethod.configured) {
|
|
@@ -296,71 +288,35 @@ function createPasswordSecurityFlows(deps) {
|
|
|
296
288
|
}
|
|
297
289
|
|
|
298
290
|
if (updateResponse?.data?.user) {
|
|
299
|
-
await syncProfileFromSupabaseUser(updateResponse.data.user, updateResponse.data.user.email);
|
|
291
|
+
await profileMirror.syncProfileFromSupabaseUser(updateResponse.data.user, updateResponse.data.user.email);
|
|
300
292
|
}
|
|
301
293
|
}
|
|
302
294
|
|
|
303
295
|
const passwordSignInOptions = !payload.enabled && passwordMethod.configured ? { passwordSetupRequired: true } : {};
|
|
304
|
-
const
|
|
296
|
+
const updatedSettings = await profiles.userSettingsRepository.updatePasswordSignInEnabled(
|
|
305
297
|
current.profile.id,
|
|
306
298
|
payload.enabled,
|
|
307
299
|
passwordSignInOptions
|
|
308
300
|
);
|
|
309
|
-
const nextAuthMethodsStatus = buildAuthMethodsStatusFromSupabaseUser(current.user,
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
async function signOutOtherSessions(request) {
|
|
317
|
-
ensureConfigured();
|
|
318
|
-
const supabase = getSupabaseClient();
|
|
319
|
-
await setSessionFromRequestCookies(request, {
|
|
320
|
-
supabaseClient: supabase
|
|
321
|
-
});
|
|
322
|
-
const response = await supabase.auth.signOut({
|
|
323
|
-
scope: "others"
|
|
301
|
+
const nextAuthMethodsStatus = buildAuthMethodsStatusFromSupabaseUser(current.user, {
|
|
302
|
+
passwordSignInEnabled: updatedSettings.passwordSignInEnabled !== false,
|
|
303
|
+
passwordSetupRequired: updatedSettings.passwordSetupRequired === true,
|
|
304
|
+
oauthProviders: config.oauth.providers
|
|
324
305
|
});
|
|
325
306
|
|
|
326
|
-
if (response.error) {
|
|
327
|
-
throw mapAuthError(response.error, Number(response.error?.status || 400));
|
|
328
|
-
}
|
|
329
|
-
|
|
330
307
|
return {
|
|
331
|
-
|
|
308
|
+
securityStatus: securityStatus.buildSecurityStatus(nextAuthMethodsStatus)
|
|
332
309
|
};
|
|
333
310
|
}
|
|
334
311
|
|
|
335
|
-
|
|
336
|
-
if (!request) {
|
|
337
|
-
const authMethodsStatus = buildAuthMethodsStatusFromProviderIds([authMethodPasswordProvider], {
|
|
338
|
-
passwordSignInEnabled: true,
|
|
339
|
-
passwordSetupRequired: false
|
|
340
|
-
});
|
|
341
|
-
return buildSecurityStatusFromAuthMethodsStatus(authMethodsStatus);
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
if (typeof resolveDevAuthSecurityStatus === "function") {
|
|
345
|
-
const devAuthSecurityStatus = await resolveDevAuthSecurityStatus(request);
|
|
346
|
-
if (devAuthSecurityStatus) {
|
|
347
|
-
return devAuthSecurityStatus;
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
const current = await resolveCurrentAuthContext(request);
|
|
352
|
-
return buildSecurityStatusFromAuthMethodsStatus(current.authMethodsStatus);
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
return {
|
|
312
|
+
return Object.freeze({
|
|
356
313
|
requestPasswordReset,
|
|
357
314
|
completePasswordRecovery,
|
|
358
315
|
resetPassword,
|
|
359
316
|
changePassword,
|
|
360
|
-
setPasswordSignInEnabled
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
};
|
|
317
|
+
// readable-check: allow setter-seam - not a slot: setPasswordSignInEnabled is an auth.service operation
|
|
318
|
+
setPasswordSignInEnabled
|
|
319
|
+
});
|
|
364
320
|
}
|
|
365
321
|
|
|
366
|
-
export {
|
|
322
|
+
export { createSupabasePasswordFlows };
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { AppError } from "@jskit-ai/kernel/server/runtime/errors";
|
|
2
|
+
import { normalizeAuthActor, normalizeAuthResult } from "@jskit-ai/auth-core/server/authActor";
|
|
3
|
+
import { normalizeEmail } from "@jskit-ai/auth-core/server/utils";
|
|
4
|
+
import { resolveDisplayName } from "./authProfileNames.js";
|
|
5
|
+
import { requireAuthenticatedProfile } from "./authenticatedProfile.js";
|
|
6
|
+
|
|
7
|
+
/** The provider id of Supabase auth: in its capabilities, its profiles' identity and AUTH_PROVIDER. */
|
|
8
|
+
const SUPABASE_PROVIDER_ID = "supabase";
|
|
9
|
+
/** The code users-core's profiles repository gives an email that another sign-in already uses. */
|
|
10
|
+
const USER_PROFILE_EMAIL_CONFLICT_CODE = "USER_PROFILE_EMAIL_CONFLICT";
|
|
11
|
+
const AUTH_PROVIDER_ID_PATTERN = /^[a-z][a-z0-9_-]{1,63}$/;
|
|
12
|
+
|
|
13
|
+
/** A profile's auth provider id, or SUPABASE_PROVIDER_ID when it names none or an invalid one. */
|
|
14
|
+
function normalizeAuthProviderId(value) {
|
|
15
|
+
const normalized = String(value || "")
|
|
16
|
+
.trim()
|
|
17
|
+
.toLowerCase();
|
|
18
|
+
return AUTH_PROVIDER_ID_PATTERN.test(normalized) ? normalized : SUPABASE_PROVIDER_ID;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function normalizeProviderUserId(value) {
|
|
22
|
+
return String(value || "").trim();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The app profile mirrored from a Supabase user or its token claims, and the
|
|
27
|
+
* actor an auth result carries for it.
|
|
28
|
+
* @param {object} dependencies
|
|
29
|
+
* @param {import("../types.js").SupabaseConfig} dependencies.config
|
|
30
|
+
* @param {import("../types.js").SupabaseProfiles} dependencies.profiles
|
|
31
|
+
*/
|
|
32
|
+
function createSupabaseProfileMirror({ config, profiles }) {
|
|
33
|
+
const profileProjectionEnabled = config.profileMode === "users";
|
|
34
|
+
const { projector } = profiles;
|
|
35
|
+
|
|
36
|
+
function requireSynchronizedProfile(profile) {
|
|
37
|
+
return requireAuthenticatedProfile(profile, {
|
|
38
|
+
context: "authentication profile"
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function buildNormalizedIdentityKey(identityLike) {
|
|
43
|
+
const source = identityLike && typeof identityLike === "object" ? identityLike : {};
|
|
44
|
+
const authProvider = normalizeAuthProviderId(source.authProvider);
|
|
45
|
+
const authProviderUserSid = normalizeProviderUserId(source.authProviderUserSid);
|
|
46
|
+
|
|
47
|
+
if (!authProviderUserSid) {
|
|
48
|
+
throw new TypeError("Profile identity is missing required fields.");
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
authProvider,
|
|
53
|
+
authProviderUserSid
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function buildNormalizedIdentityProfile(profileLike) {
|
|
58
|
+
const source = profileLike && typeof profileLike === "object" ? profileLike : {};
|
|
59
|
+
const identity = buildNormalizedIdentityKey(source);
|
|
60
|
+
const email = normalizeEmail(source.email || "");
|
|
61
|
+
const displayName = String(source.displayName || "").trim();
|
|
62
|
+
|
|
63
|
+
if (!email || !displayName) {
|
|
64
|
+
throw new TypeError("Profile identity is missing required fields.");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
authProvider: identity.authProvider,
|
|
69
|
+
authProviderUserSid: identity.authProviderUserSid,
|
|
70
|
+
email,
|
|
71
|
+
displayName
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function findProfileByIdentity(identityProfile, options = {}) {
|
|
76
|
+
const normalized = buildNormalizedIdentityKey(identityProfile);
|
|
77
|
+
return projector.findByIdentity(
|
|
78
|
+
{
|
|
79
|
+
authProvider: normalized.authProvider,
|
|
80
|
+
authProviderUserSid: normalized.authProviderUserSid
|
|
81
|
+
},
|
|
82
|
+
options
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* With syncProfile: false the caller asks only who is signed in, so the
|
|
88
|
+
* existing profile is read instead of synchronized.
|
|
89
|
+
*/
|
|
90
|
+
async function syncProfileMirror(nextProfile, { syncProfile = true } = {}) {
|
|
91
|
+
if (!syncProfile) {
|
|
92
|
+
return findProfileByIdentity(nextProfile);
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
const normalized = buildNormalizedIdentityProfile(nextProfile);
|
|
96
|
+
const synchronizedProfile = await projector.syncIdentityProfile(normalized);
|
|
97
|
+
return requireSynchronizedProfile(synchronizedProfile);
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (String(error?.code || "") === USER_PROFILE_EMAIL_CONFLICT_CODE) {
|
|
100
|
+
throw new AppError(
|
|
101
|
+
409,
|
|
102
|
+
"This email is already registered with another sign-in method. Sign in with that method, then link this provider in Settings > Security.",
|
|
103
|
+
{ code: "auth.supabase.profile.emailConflict" }
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function syncProfileFromSupabaseUser(supabaseUser, fallbackEmail, profileOptions = {}) {
|
|
112
|
+
const supabaseUserId = normalizeProviderUserId(supabaseUser?.id);
|
|
113
|
+
const email = normalizeEmail(supabaseUser?.email || fallbackEmail);
|
|
114
|
+
|
|
115
|
+
if (!supabaseUserId || !email) {
|
|
116
|
+
throw new Error("Auth provider user payload is missing required fields.");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return syncProfileMirror({
|
|
120
|
+
authProvider: SUPABASE_PROVIDER_ID,
|
|
121
|
+
authProviderUserSid: supabaseUserId,
|
|
122
|
+
email,
|
|
123
|
+
displayName: resolveDisplayName(supabaseUser, email)
|
|
124
|
+
}, profileOptions);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function buildActorFromProfile(profile) {
|
|
128
|
+
return normalizeAuthActor(
|
|
129
|
+
{
|
|
130
|
+
provider: profile?.authProvider || SUPABASE_PROVIDER_ID,
|
|
131
|
+
providerUserId: profile?.authProviderUserSid || profile?.id,
|
|
132
|
+
email: profile?.email,
|
|
133
|
+
displayName: profile?.displayName,
|
|
134
|
+
appUserId: profileProjectionEnabled ? profile?.id : null,
|
|
135
|
+
profileSource: profile?.profileSource || (profileProjectionEnabled ? "users" : "auth-provider")
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
provider: SUPABASE_PROVIDER_ID,
|
|
139
|
+
profileSource: profileProjectionEnabled ? "users" : "auth-provider"
|
|
140
|
+
}
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function withActor(result) {
|
|
145
|
+
if (!result || typeof result !== "object") {
|
|
146
|
+
return result;
|
|
147
|
+
}
|
|
148
|
+
if (result.authenticated !== true && !result.profile) {
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
const actor = buildActorFromProfile(result.profile);
|
|
152
|
+
if (!actor) {
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
const { profile: _profile, ...authResult } = result;
|
|
156
|
+
return normalizeAuthResult({
|
|
157
|
+
...authResult,
|
|
158
|
+
actor
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function withActorResult(operation) {
|
|
163
|
+
return async (...args) => withActor(await operation(...args));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async function syncProfileFromJwtClaims(claims, { syncProfile = true } = {}) {
|
|
167
|
+
const supabaseUserId = normalizeProviderUserId(claims?.sub);
|
|
168
|
+
if (!supabaseUserId) {
|
|
169
|
+
throw new AppError(401, "Token is missing subject claim.", { code: "auth.supabase.token.subjectMissing" });
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const existing = await findProfileByIdentity({
|
|
173
|
+
authProvider: SUPABASE_PROVIDER_ID,
|
|
174
|
+
authProviderUserSid: supabaseUserId
|
|
175
|
+
});
|
|
176
|
+
if (!syncProfile) {
|
|
177
|
+
return existing;
|
|
178
|
+
}
|
|
179
|
+
const emailFromToken = normalizeEmail(claims?.email || "");
|
|
180
|
+
|
|
181
|
+
if (!emailFromToken) {
|
|
182
|
+
if (existing) {
|
|
183
|
+
return existing;
|
|
184
|
+
}
|
|
185
|
+
throw new AppError(401, "Token is missing email claim.", { code: "auth.supabase.token.emailMissing" });
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return syncProfileMirror({
|
|
189
|
+
authProvider: SUPABASE_PROVIDER_ID,
|
|
190
|
+
authProviderUserSid: supabaseUserId,
|
|
191
|
+
email: emailFromToken,
|
|
192
|
+
displayName: resolveDisplayName(claims, emailFromToken)
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return Object.freeze({
|
|
197
|
+
syncProfileFromSupabaseUser,
|
|
198
|
+
syncProfileFromJwtClaims,
|
|
199
|
+
withActor,
|
|
200
|
+
withActorResult
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export { SUPABASE_PROVIDER_ID, createSupabaseProfileMirror };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { normalizeEmail } from "@jskit-ai/auth-core/server/utils";
|
|
2
|
+
import { normalizeObject } from "@jskit-ai/kernel/shared/support/normalize";
|
|
3
|
+
import { SUPABASE_PROVIDER_ID } from "./profileMirror.js";
|
|
4
|
+
|
|
5
|
+
/** In profile mode "provider", the profile is the Supabase user itself: its id, email and name. */
|
|
6
|
+
function createProviderProfileProjector() {
|
|
7
|
+
async function findByIdentity() {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async function syncIdentityProfile(profileLike) {
|
|
12
|
+
const profile = normalizeObject(profileLike);
|
|
13
|
+
const authProviderUserSid = String(profile.authProviderUserSid || "").trim();
|
|
14
|
+
const email = normalizeEmail(profile.email || "");
|
|
15
|
+
if (!authProviderUserSid || !email) {
|
|
16
|
+
throw new TypeError("Provider identity profile requires authProviderUserSid and email.");
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
id: authProviderUserSid,
|
|
20
|
+
authProvider: String(profile.authProvider || SUPABASE_PROVIDER_ID).trim() || SUPABASE_PROVIDER_ID,
|
|
21
|
+
authProviderUserSid,
|
|
22
|
+
email,
|
|
23
|
+
displayName: String(profile.displayName || email.split("@")[0] || "User").trim(),
|
|
24
|
+
profileSource: "auth-provider"
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return Object.freeze({
|
|
29
|
+
findByIdentity,
|
|
30
|
+
syncIdentityProfile
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Profile mode "provider" has no app users, so dev auth, which signs in as one, finds none. */
|
|
35
|
+
const PROVIDER_MODE_USER_PROFILES = Object.freeze({
|
|
36
|
+
async findById() {
|
|
37
|
+
return null;
|
|
38
|
+
},
|
|
39
|
+
async findByEmail() {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
/** Profile mode "provider" keeps no password settings: every account may sign in with its password. */
|
|
45
|
+
const PROVIDER_MODE_USER_SETTINGS = Object.freeze({
|
|
46
|
+
async ensureForUserId() {
|
|
47
|
+
return { passwordSignInEnabled: true, passwordSetupRequired: false };
|
|
48
|
+
},
|
|
49
|
+
async updatePasswordSignInEnabled() {
|
|
50
|
+
throw new Error("Password sign-in settings repository is not configured.");
|
|
51
|
+
},
|
|
52
|
+
async updatePasswordSetupRequired() {}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* config.auth.profileMode "provider" (the default): Supabase holds the
|
|
57
|
+
* profile, and the app keeps no users of its own.
|
|
58
|
+
* @type {import("../types.js").SupabaseProfiles}
|
|
59
|
+
*/
|
|
60
|
+
const PROVIDER_MODE_PROFILES = Object.freeze({
|
|
61
|
+
projector: createProviderProfileProjector(),
|
|
62
|
+
userProfilesRepository: PROVIDER_MODE_USER_PROFILES,
|
|
63
|
+
userSettingsRepository: PROVIDER_MODE_USER_SETTINGS
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* config.auth.profileMode "users": users-core keeps an app user for each
|
|
68
|
+
* Supabase user, with its password sign-in settings.
|
|
69
|
+
* @param {import("@jskit-ai/users-core/server/types").UsersIdentity} identity
|
|
70
|
+
* @returns {import("../types.js").SupabaseProfiles}
|
|
71
|
+
*/
|
|
72
|
+
function createUsersModeProfiles(identity) {
|
|
73
|
+
return Object.freeze({
|
|
74
|
+
projector: identity.profileProjector,
|
|
75
|
+
userProfilesRepository: identity.repositories.userProfiles,
|
|
76
|
+
userSettingsRepository: identity.repositories.userSettings
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export { PROVIDER_MODE_PROFILES, createUsersModeProfiles };
|