@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.
@@ -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 };