@softure-ai/auth 0.2.13 → 0.2.17
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/CHANGELOG.md +31 -0
- package/README.md +16 -3
- package/dist/contract.d.ts +1 -1
- package/dist/contract.d.ts.map +1 -1
- package/dist/contract.js.map +1 -1
- package/dist/index.d.ts +11 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/messages/en.d.ts +1 -0
- package/dist/messages/en.d.ts.map +1 -1
- package/dist/messages/en.js +1 -0
- package/dist/messages/en.js.map +1 -1
- package/dist/messages/index.d.ts +2 -1
- package/dist/messages/index.d.ts.map +1 -1
- package/dist/next/context.js +1 -1
- package/dist/next/context.js.map +1 -1
- package/dist/options.d.ts +39 -11
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +32 -6
- package/dist/options.js.map +1 -1
- package/dist/server/oidc/accounts.d.ts +1 -1
- package/dist/server/oidc/accounts.d.ts.map +1 -1
- package/dist/server/oidc/accounts.js +7 -1
- package/dist/server/oidc/accounts.js.map +1 -1
- package/dist/server/oidc/allowlist.d.ts +1 -1
- package/dist/server/oidc/allowlist.d.ts.map +1 -1
- package/dist/server/oidc/allowlist.js +3 -1
- package/dist/server/oidc/allowlist.js.map +1 -1
- package/dist/server/options.d.ts +2 -1
- package/dist/server/options.d.ts.map +1 -1
- package/dist/server/options.js +17 -1
- package/dist/server/options.js.map +1 -1
- package/dist/server/switches.d.ts +5 -0
- package/dist/server/switches.d.ts.map +1 -1
- package/dist/server/switches.js +7 -0
- package/dist/server/switches.js.map +1 -1
- package/dist/testing/account.d.ts.map +1 -1
- package/dist/testing/account.js +2 -2
- package/dist/testing/account.js.map +1 -1
- package/dist/testing/github-provider.d.ts +2 -0
- package/dist/testing/github-provider.d.ts.map +1 -1
- package/dist/testing/github-provider.js +2 -1
- package/dist/testing/github-provider.js.map +1 -1
- package/module.json +2 -1
- package/package.json +12 -2
- package/src/contract.ts +2 -1
- package/src/index.ts +3 -1
- package/src/messages/en.ts +1 -0
- package/src/messages/index.ts +1 -1
- package/src/next/context.ts +1 -1
- package/src/options.ts +48 -9
- package/src/server/oidc/accounts.ts +7 -2
- package/src/server/oidc/allowlist.ts +4 -2
- package/src/server/options.ts +19 -3
- package/src/server/switches.ts +8 -0
- package/src/testing/account.ts +2 -2
- package/src/testing/github-provider.ts +4 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// The account an OpenID Connect identity signs in to. The provider's subject finds it (`auth.identities`), so an
|
|
2
2
|
// email change at the provider keeps it; a first sign-in links the account with the same verified email, or creates
|
|
3
3
|
// one with an unusable password when the provider may create accounts. One transaction, so two first sign-ins at
|
|
4
|
-
// once end with one account and one identity. Every sign-in that carries a display name stores it on the account.
|
|
4
|
+
// once end with one account and one identity. An `openSignUp` provider creates none while registration is closed. Every sign-in that carries a display name stores it on the account.
|
|
5
5
|
import { randomBytes } from "node:crypto";
|
|
6
6
|
import { err, ok, type Err, type Ok } from "@softure-ai/core";
|
|
7
7
|
import { and, eq } from "drizzle-orm";
|
|
@@ -11,6 +11,7 @@ import { AUTH_USER_COLUMNS, identities, users } from "../../schema.js";
|
|
|
11
11
|
import { getAuthOptions } from "../options.js";
|
|
12
12
|
import { hashPassword } from "../password.js";
|
|
13
13
|
import type { AuthContext } from "../sessions.js";
|
|
14
|
+
import { isRegistrationSwitchOn } from "../switches.js";
|
|
14
15
|
|
|
15
16
|
export interface OidcIdentity {
|
|
16
17
|
readonly subject: string;
|
|
@@ -20,11 +21,14 @@ export interface OidcIdentity {
|
|
|
20
21
|
readonly displayName: string | null;
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
export type OidcAccountResult = Ok<AuthUser> | Err<"auth.oidc_no_account">;
|
|
24
|
+
export type OidcAccountResult = Ok<AuthUser> | Err<"auth.oidc_no_account" | "auth.oidc_registration_closed">;
|
|
24
25
|
|
|
25
26
|
export async function resolveOidcAccount(ctx: AuthContext, provider: OidcProvider, identity: OidcIdentity): Promise<OidcAccountResult> {
|
|
26
27
|
const options = getAuthOptions(ctx.config);
|
|
27
28
|
const now = ctx.clock.now();
|
|
29
|
+
// An open provider creates accounts only while registration is open; read before the transaction, which holds the
|
|
30
|
+
// connection the switch reader would need on a single-connection database.
|
|
31
|
+
const isSignUpClosed = provider.openSignUp && (await isRegistrationSwitchOn(ctx));
|
|
28
32
|
|
|
29
33
|
return ctx.db.transaction(async (tx): Promise<OidcAccountResult> => {
|
|
30
34
|
const txCtx: AuthContext = { ...ctx, db: tx };
|
|
@@ -55,6 +59,7 @@ export async function resolveOidcAccount(ctx: AuthContext, provider: OidcProvide
|
|
|
55
59
|
let isCreated = false;
|
|
56
60
|
if (user === undefined) {
|
|
57
61
|
if (!provider.createAccounts) return err("auth.oidc_no_account");
|
|
62
|
+
if (isSignUpClosed) return err("auth.oidc_registration_closed");
|
|
58
63
|
// Nobody knows this password: the account signs in through its identity (or a reset link, when passwords are on).
|
|
59
64
|
const unusablePassword = await hashPassword(randomBytes(32).toString("base64url"), options.password.scrypt);
|
|
60
65
|
const [created] = await tx
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
// Who may sign in through a provider: a verified email listed in `allowedEmails` or of a domain in `allowedDomains
|
|
1
|
+
// Who may sign in through a provider: a verified email listed in `allowedEmails` or of a domain in `allowedDomains`, or
|
|
2
|
+
// any verified email when the provider has `openSignUp`.
|
|
2
3
|
import type { OidcProvider } from "../../options.js";
|
|
3
4
|
import type { IdentityClaims } from "./id-token.js";
|
|
4
5
|
|
|
@@ -12,8 +13,9 @@ export function getAllowedEmail(provider: OidcProvider, claims: IdentityClaims):
|
|
|
12
13
|
return isAllowedEmail(provider, email, claims.hostedDomain) ? email : null;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
/** Whether a verified, lowercased email passes the provider's allowlist. */
|
|
16
|
+
/** Whether a verified, lowercased email passes the provider's allowlist (every one does with `openSignUp`). */
|
|
16
17
|
export function isAllowedEmail(provider: OidcProvider, email: string, hostedDomain: string | null): boolean {
|
|
18
|
+
if (provider.openSignUp) return true;
|
|
17
19
|
if (provider.allowedEmails.includes(email)) return true;
|
|
18
20
|
const at = email.lastIndexOf("@");
|
|
19
21
|
if (at <= 0) return false;
|
package/src/server/options.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// The auth options of the running app, read from the configuration in the module context.
|
|
2
|
-
import { getModule, type SoftureConfig } from "@softure-ai/core";
|
|
3
|
-
import type
|
|
2
|
+
import { getModule, type SoftureConfig, type SoftureRuntime } from "@softure-ai/core";
|
|
3
|
+
import { resolveScryptParams, type AuthOptions, type ParsedAuthOptions } from "../options.js";
|
|
4
4
|
|
|
5
5
|
const MODULE_ID = "auth";
|
|
6
6
|
|
|
@@ -13,9 +13,25 @@ export function getAuthModule(config: SoftureConfig) {
|
|
|
13
13
|
return module;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
/** Per parsed options object and runtime, so `getAuthOptions` returns the same object on every call. */
|
|
17
|
+
const resolvedOptions = new WeakMap<object, Map<SoftureRuntime, AuthOptions>>();
|
|
18
|
+
|
|
19
|
+
/** The auth options of the running app, with the hash cost resolved for its runtime (`config.platform`). */
|
|
16
20
|
export function getAuthOptions(config: SoftureConfig): AuthOptions {
|
|
17
21
|
// The module factory parsed these options with authOptionsSchema.
|
|
18
|
-
|
|
22
|
+
const parsed = getAuthModule(config).options as ParsedAuthOptions;
|
|
23
|
+
const runtime = config.platform.runtime;
|
|
24
|
+
let byRuntime = resolvedOptions.get(parsed);
|
|
25
|
+
if (byRuntime === undefined) {
|
|
26
|
+
byRuntime = new Map();
|
|
27
|
+
resolvedOptions.set(parsed, byRuntime);
|
|
28
|
+
}
|
|
29
|
+
let resolved = byRuntime.get(runtime);
|
|
30
|
+
if (resolved === undefined) {
|
|
31
|
+
resolved = { ...parsed, password: { ...parsed.password, scrypt: resolveScryptParams(parsed.password.scrypt, runtime) } };
|
|
32
|
+
byRuntime.set(runtime, resolved);
|
|
33
|
+
}
|
|
34
|
+
return resolved;
|
|
19
35
|
}
|
|
20
36
|
|
|
21
37
|
const anchoredPatterns = new WeakMap<RegExp, RegExp>();
|
package/src/server/switches.ts
CHANGED
|
@@ -18,6 +18,14 @@ const OFF = new Set(["false", "0"]);
|
|
|
18
18
|
*/
|
|
19
19
|
export async function isRegistrationClosed(ctx: ModuleContext, env: Readonly<Record<string, string | undefined>> = process.env): Promise<boolean> {
|
|
20
20
|
if (!getAuthOptions(ctx.config).password.enabled) return true;
|
|
21
|
+
return isRegistrationSwitchOn(ctx, env);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The `auth.registration_closed` switch alone, whatever `password.enabled` says: an open provider sign-up
|
|
26
|
+
* (`openSignUp`) creates accounts while it is off, with or without the password form.
|
|
27
|
+
*/
|
|
28
|
+
export async function isRegistrationSwitchOn(ctx: ModuleContext, env: Readonly<Record<string, string | undefined>> = process.env): Promise<boolean> {
|
|
21
29
|
const reading = await readSwitch(ctx, REGISTRATION_CLOSED_SWITCH);
|
|
22
30
|
if (reading.kind === "value") return reading.isEnabled;
|
|
23
31
|
const override = env[REGISTRATION_CLOSED_ENV]?.trim().toLowerCase();
|
package/src/testing/account.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// request (`runHooks`), so fixture rows the app's hooks write (a profile, an entitlement) exist too.
|
|
4
4
|
import type { Queryable } from "@softure-ai/db";
|
|
5
5
|
import type { AuthUser } from "../contract.js";
|
|
6
|
-
import {
|
|
6
|
+
import { SCRYPT_DEFAULTS, type ScryptParams } from "../options.js";
|
|
7
7
|
import { userRoles, users } from "../schema.js";
|
|
8
8
|
import { getAuthOptions } from "../server/options.js";
|
|
9
9
|
import { hashPassword, isPasswordHash } from "../server/password.js";
|
|
@@ -11,7 +11,7 @@ import type { AuthContext } from "../server/sessions.js";
|
|
|
11
11
|
import { parseEmail } from "../server/validation.js";
|
|
12
12
|
|
|
13
13
|
/** Auth's own hash parameters when the app sets none (`password.scrypt` in `auth({ ... })`). */
|
|
14
|
-
const DEFAULT_SCRYPT: ScryptParams =
|
|
14
|
+
const DEFAULT_SCRYPT: ScryptParams = SCRYPT_DEFAULTS.node;
|
|
15
15
|
|
|
16
16
|
/** One hash per password and parameters for the whole test run: scrypt is the slow part of a big suite's setup. */
|
|
17
17
|
const hashCache = new Map<string, Promise<string>>();
|
|
@@ -20,6 +20,8 @@ export interface FakeGithubProviderOptions {
|
|
|
20
20
|
readonly allowedDomains?: readonly string[];
|
|
21
21
|
readonly allowedEmails?: readonly string[];
|
|
22
22
|
readonly createAccounts?: boolean;
|
|
23
|
+
/** Any verified email signs in (`openSignUp`); the default allowlist is then empty. */
|
|
24
|
+
readonly openSignUp?: boolean;
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
/** One of the account's emails, as `/user/emails` lists it. */
|
|
@@ -164,9 +166,10 @@ export function createFakeGithubProvider(options: FakeGithubProviderOptions = {}
|
|
|
164
166
|
name: "GitHub",
|
|
165
167
|
clientIdEnv: `${envPrefix}_CLIENT_ID`,
|
|
166
168
|
clientSecretEnv: `${envPrefix}_CLIENT_SECRET`,
|
|
167
|
-
allowedDomains: [...(options.allowedDomains ?? (options.allowedEmails === undefined ? ["example.com"] : []))],
|
|
169
|
+
allowedDomains: [...(options.allowedDomains ?? (options.allowedEmails === undefined && options.openSignUp !== true ? ["example.com"] : []))],
|
|
168
170
|
allowedEmails: [...(options.allowedEmails ?? [])],
|
|
169
171
|
createAccounts: options.createAccounts,
|
|
172
|
+
openSignUp: options.openSignUp,
|
|
170
173
|
},
|
|
171
174
|
env: { [`${envPrefix}_CLIENT_ID`]: clientId, [`${envPrefix}_CLIENT_SECRET`]: clientSecret },
|
|
172
175
|
fetch: fetchFake,
|