@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
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { AUTH_TEXTS, createAuthError } from "@jskit-ai/auth-core/server/authTexts";
|
|
2
|
+
import { AUTH_METHOD_PASSWORD_PROVIDER } from "@jskit-ai/auth-core/shared/authMethods";
|
|
3
|
+
import {
|
|
4
|
+
buildAuthMethodsStatusFromProviderIds,
|
|
5
|
+
buildAuthMethodsStatusFromSupabaseUser,
|
|
6
|
+
buildSecurityStatusFromAuthMethodsStatus
|
|
7
|
+
} from "./authMethodStatus.js";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The sign-in methods of the signed-in account: its password, whether it may
|
|
11
|
+
* sign in with it, and each linked OAuth provider, with the actions this app
|
|
12
|
+
* offers on them.
|
|
13
|
+
* @param {object} dependencies
|
|
14
|
+
* @param {import("../types.js").SupabaseConfig} dependencies.config
|
|
15
|
+
* @param {import("@jskit-ai/auth-core/server/types").AuthCapabilities} dependencies.capabilities
|
|
16
|
+
* @param {import("../types.js").SupabaseSessions} dependencies.sessions
|
|
17
|
+
* @param {import("../types.js").SupabaseProfileMirror} dependencies.profileMirror
|
|
18
|
+
* @param {import("../types.js").SupabaseProfiles} dependencies.profiles
|
|
19
|
+
*/
|
|
20
|
+
function createSupabaseSecurityStatus({ config, capabilities, sessions, profileMirror, profiles }) {
|
|
21
|
+
const { features } = capabilities;
|
|
22
|
+
const actions = Object.freeze({
|
|
23
|
+
changePassword: true,
|
|
24
|
+
// readable-check: allow setter-seam - not a slot: setPasswordEnabled says whether the security screen offers the toggle
|
|
25
|
+
setPasswordEnabled: features.password.methodToggle,
|
|
26
|
+
linkProvider: features.providerLinking.start,
|
|
27
|
+
unlinkProvider: features.providerLinking.unlink,
|
|
28
|
+
signOutOtherSessions: features.signOutOtherSessions
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
function buildSecurityStatus(authMethodsStatus) {
|
|
32
|
+
return buildSecurityStatusFromAuthMethodsStatus(authMethodsStatus, { actions });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function passwordSignInPolicyForUser(userId) {
|
|
36
|
+
const settings = await profiles.userSettingsRepository.ensureForUserId(userId);
|
|
37
|
+
return {
|
|
38
|
+
passwordSignInEnabled: settings?.passwordSignInEnabled !== false,
|
|
39
|
+
passwordSetupRequired: settings?.passwordSetupRequired === true
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The signed-in Supabase user, its app profile and its sign-in methods. */
|
|
44
|
+
async function currentAuthContext(request, options = {}) {
|
|
45
|
+
const current = await sessions.resolveCurrentSupabaseUser(request, options);
|
|
46
|
+
const profile = await profileMirror.syncProfileFromSupabaseUser(current.user, current.user?.email || "");
|
|
47
|
+
const passwordSignInPolicy = await passwordSignInPolicyForUser(profile.id);
|
|
48
|
+
const authMethodsStatus = buildAuthMethodsStatusFromSupabaseUser(current.user, {
|
|
49
|
+
...passwordSignInPolicy,
|
|
50
|
+
oauthProviders: config.oauth.providers
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
...current,
|
|
55
|
+
profile,
|
|
56
|
+
passwordSignInEnabled: passwordSignInPolicy.passwordSignInEnabled,
|
|
57
|
+
passwordSetupRequired: passwordSignInPolicy.passwordSetupRequired,
|
|
58
|
+
authMethodsStatus
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The security status of a dev auth session, or null for any other request. */
|
|
63
|
+
async function devAuthSecurityStatus(request) {
|
|
64
|
+
const devAuthResult = await sessions.authenticateDevAuthRequestFromCookies(request);
|
|
65
|
+
if (!devAuthResult) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (devAuthResult.authenticated !== true) {
|
|
70
|
+
throw createAuthError(AUTH_TEXTS.sessionRequired);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const passwordSignInPolicy = await passwordSignInPolicyForUser(devAuthResult.actor.id);
|
|
74
|
+
const authMethodsStatus = buildAuthMethodsStatusFromProviderIds([AUTH_METHOD_PASSWORD_PROVIDER], {
|
|
75
|
+
...passwordSignInPolicy,
|
|
76
|
+
oauthProviders: config.oauth.providers
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
return buildSecurityStatus(authMethodsStatus);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function getSecurityStatus(request) {
|
|
83
|
+
if (!request) {
|
|
84
|
+
const authMethodsStatus = buildAuthMethodsStatusFromProviderIds([AUTH_METHOD_PASSWORD_PROVIDER], {
|
|
85
|
+
passwordSignInEnabled: true,
|
|
86
|
+
passwordSetupRequired: false,
|
|
87
|
+
oauthProviders: config.oauth.providers
|
|
88
|
+
});
|
|
89
|
+
return buildSecurityStatus(authMethodsStatus);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const devAuthStatus = await devAuthSecurityStatus(request);
|
|
93
|
+
if (devAuthStatus) {
|
|
94
|
+
return devAuthStatus;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const current = await currentAuthContext(request);
|
|
98
|
+
return buildSecurityStatus(current.authMethodsStatus);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return Object.freeze({
|
|
102
|
+
buildSecurityStatus,
|
|
103
|
+
passwordSignInPolicyForUser,
|
|
104
|
+
currentAuthContext,
|
|
105
|
+
getSecurityStatus
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export { createSupabaseSecurityStatus };
|