@jskit-ai/auth-provider-local-core 0.2.5 → 0.2.7

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,86 @@
1
+ import { AUTH_METHOD_KIND_OAUTH, buildOAuthMethodId } from "@jskit-ai/auth-core/shared/authMethods";
2
+ import { buildSecurityStatusFromAuthMethodsStatus } from "@jskit-ai/auth-core/shared/authSecurityStatus";
3
+ import { AUTH_TEXTS, createAuthError } from "@jskit-ai/auth-core/server/authTexts";
4
+ import { hasPassword } from "./accounts.js";
5
+
6
+ /**
7
+ * The sign-in methods of an account: its password and each configured provider.
8
+ * @param {object} dependencies
9
+ * @param {import("../types.js").AuthLocalBackend} dependencies.backend
10
+ * @param {import("../types.js").AuthLocalConfig} dependencies.config
11
+ * @param {Map<string, import("../types.js").AuthLocalOAuthProvider>} dependencies.oauthProvidersById
12
+ * @param {import("../types.js").AuthLocalSessions} dependencies.sessions
13
+ */
14
+ function createLocalSecurityStatus({ backend, config, oauthProvidersById, sessions }) {
15
+ const { oauthProviders, oauthEnabled } = config;
16
+
17
+ async function securityStatusForUser(userId) {
18
+ const { user, identities } = await backend.withTransaction(async (tx) => ({
19
+ user: await tx.users.findById(userId),
20
+ identities: oauthEnabled ? await tx.identities.listForUser(userId) : []
21
+ }));
22
+ const passwordSet = hasPassword(user);
23
+ // Identities of a provider the app no longer configures cannot sign in.
24
+ const linkedProviders = new Set(identities.map((identity) => identity.provider).filter((id) => oauthProvidersById.has(id)));
25
+ const signInMethodCount = (passwordSet ? 1 : 0) + linkedProviders.size;
26
+ const methods = [
27
+ {
28
+ id: "password",
29
+ kind: "password",
30
+ provider: "email",
31
+ label: "Password",
32
+ configured: passwordSet,
33
+ enabled: passwordSet,
34
+ canDisable: false,
35
+ supportsSecretUpdate: true,
36
+ requiresCurrentPassword: false
37
+ },
38
+ // canEnable links the provider and canDisable unlinks it, as for Supabase.
39
+ ...oauthProviders.map((provider) => {
40
+ const linked = linkedProviders.has(provider.id);
41
+ return {
42
+ id: buildOAuthMethodId(provider.id),
43
+ kind: AUTH_METHOD_KIND_OAUTH,
44
+ provider: provider.id,
45
+ label: provider.label,
46
+ configured: linked,
47
+ enabled: linked,
48
+ canEnable: !linked,
49
+ canDisable: linked && signInMethodCount > 1,
50
+ supportsSecretUpdate: false,
51
+ requiresCurrentPassword: false
52
+ };
53
+ })
54
+ ];
55
+ return buildSecurityStatusFromAuthMethodsStatus(
56
+ {
57
+ methods,
58
+ minimumEnabledMethods: 1,
59
+ enabledMethodsCount: signInMethodCount
60
+ },
61
+ {
62
+ actions: {
63
+ changePassword: passwordSet,
64
+ signOutOtherSessions: true,
65
+ linkProvider: oauthEnabled,
66
+ unlinkProvider: oauthEnabled
67
+ }
68
+ }
69
+ );
70
+ }
71
+
72
+ async function getSecurityStatus(request) {
73
+ const authResult = await sessions.authenticateRequest(request);
74
+ if (authResult.authenticated !== true) {
75
+ throw createAuthError(AUTH_TEXTS.sessionRequired);
76
+ }
77
+ return securityStatusForUser(authResult.actor.providerUserId);
78
+ }
79
+
80
+ return Object.freeze({
81
+ securityStatusForUser,
82
+ getSecurityStatus
83
+ });
84
+ }
85
+
86
+ export { createLocalSecurityStatus };