@jskit-ai/auth-provider-local-core 0.2.35 → 0.2.37

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.
Files changed (36) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/locales/en.json +9 -1
  3. package/package.json +8 -6
  4. package/src/server/lib/accounts.js +41 -11
  5. package/src/server/lib/config.js +70 -51
  6. package/src/server/lib/deliveries.js +4 -2
  7. package/src/server/lib/emailChange.js +183 -0
  8. package/src/server/lib/emailConfirmation.js +5 -22
  9. package/src/server/lib/fileBackend.js +40 -2
  10. package/src/server/lib/fileRecords.js +17 -7
  11. package/src/server/lib/firstAccountSetup.js +4 -4
  12. package/src/server/lib/index.js +2 -0
  13. package/src/server/lib/oauthFlows.js +18 -7
  14. package/src/server/lib/passSignIn.js +16 -6
  15. package/src/server/lib/passwordFlows.js +7 -23
  16. package/src/server/lib/registerHookDecorator.js +4 -4
  17. package/src/server/lib/service.js +30 -9
  18. package/src/server/lib/sessions.js +97 -19
  19. package/src/server/providers/AuthLocalProvider.js +64 -16
  20. package/src/server/types.js +75 -10
  21. package/test/accountEnumeration.test.js +12 -3
  22. package/test/codedTexts.test.js +1 -1
  23. package/test/cookieSameSite.test.js +1 -1
  24. package/test/emailChange.test.js +37 -0
  25. package/test/firstAccountSetup.test.js +6 -6
  26. package/test/oauthSignIn.test.js +14 -2
  27. package/test/passSignIn.test.js +1 -1
  28. package/test/providerRuntime.test.js +250 -95
  29. package/test/secondFactor.test.js +6 -6
  30. package/test/sessionMetadata.test.js +38 -0
  31. package/test/signInEvents.test.js +206 -0
  32. package/test/signInThrottle.test.js +1 -1
  33. package/test-support/emailChangeScenario.js +139 -0
  34. package/test-support/localService.js +10 -4
  35. package/test-support/passSignInScenario.js +10 -4
  36. package/test-support/sessionMetadataScenario.js +76 -0
@@ -139,16 +139,25 @@ test("a failed sign-in checks one password, whether or not its email has one to
139
139
  return scryptPasswordStrategy.verifyPassword(password, record);
140
140
  }
141
141
  };
142
- const { service } = await startService(t, { passwordStrategy });
142
+ const { backend, service } = await startService(t, { passwordStrategy });
143
143
  await service.register({ email: "ada@example.com", password: PASSWORD });
144
+ const password = await scryptPasswordStrategy.hashPassword(PASSWORD);
145
+ await backend.withTransaction((tx) =>
146
+ tx.users.create({ id: "usr_off", email: "off@example.com", displayName: "Off", password, disabled: true }));
144
147
 
145
- for (const email of ["ada@example.com", "nobody@example.com", "NOBODY@example.com", "other@example.com"]) {
148
+ for (const email of ["ada@example.com", "nobody@example.com", "NOBODY@example.com", "other@example.com", "off@example.com"]) {
146
149
  checks.length = 0;
147
- await assert.rejects(service.login({ email, password: "a wrong password" }), { status: 401 }, email);
150
+ await assert.rejects(service.login({ email, password: "a wrong password" }),
151
+ { status: 401, code: "auth.login.invalidCredentials" }, email);
148
152
  assert.deepEqual(checks, ["a wrong password"], email);
149
153
  }
150
154
  // The right password still signs in, with one check.
151
155
  checks.length = 0;
152
156
  assert.equal((await service.login({ email: "ada@example.com", password: PASSWORD })).actor.email, "ada@example.com");
153
157
  assert.deepEqual(checks, [PASSWORD]);
158
+ // A disabled account's right password, and only that, is told the account is disabled.
159
+ checks.length = 0;
160
+ await assert.rejects(service.login({ email: "off@example.com", password: PASSWORD }),
161
+ { status: 403, code: "auth.local.login.disabled", message: "This account is disabled." });
162
+ assert.deepEqual(checks, [PASSWORD]);
154
163
  });
@@ -67,7 +67,7 @@ test("local auth errors carry codes whose English is the thrown message", async
67
67
  await assert.rejects(() => confirming.confirmRegistration({ token, password: "wrong password" }),
68
68
  (error) => assertCoded(error, "auth.local.register.confirmationPasswordInvalid"));
69
69
 
70
- const disabled = await createService(t, { NODE_ENV: "production", AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "" });
70
+ const disabled = await createService(t, { APP_ENV: "production", NODE_ENV: "production", AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "" });
71
71
  await assert.rejects(() => disabled.requestPasswordReset({ email: "ada@example.com" }),
72
72
  (error) => assertCoded(error, "auth.local.recovery.notConfigured"));
73
73
  });
@@ -80,7 +80,7 @@ test("AUTH_COOKIE_SAME_SITE=strict makes every local auth cookie and its clearin
80
80
  test("an AUTH_COOKIE_SAME_SITE other than lax or strict stops local auth's startup", () => {
81
81
  const resolve = (value) => resolveLocalAuthConfig({
82
82
  appRoot: os.tmpdir(),
83
- env: { NODE_ENV: "test", AUTH_LOCAL_SESSION_SECRET: "test-session-secret", AUTH_COOKIE_SAME_SITE: value },
83
+ env: { APP_ENV: "test", AUTH_LOCAL_SESSION_SECRET: "test-session-secret", AUTH_COOKIE_SAME_SITE: value },
84
84
  fileBackend: false,
85
85
  emailRecovery: false
86
86
  });
@@ -0,0 +1,37 @@
1
+ import assert from "node:assert/strict";
2
+ import fs from "node:fs/promises";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import test from "node:test";
6
+ import { createLocalFileBackend } from "../src/server/lib/index.js";
7
+ import { emailChangeService, exerciseEmailChange } from "../test-support/emailChangeScenario.js";
8
+
9
+ async function fileBackend(t) {
10
+ const storeDir = await fs.mkdtemp(path.join(os.tmpdir(), "jskit-auth-local-email-change-"));
11
+ t.after(() => fs.rm(storeDir, { recursive: true, force: true }));
12
+ return { backend: createLocalFileBackend({ storeDir }), storeDir };
13
+ }
14
+
15
+ test("an account's email changes once the link sent to the new address is used, and the old address is told (file backend)", async (t) => {
16
+ const { backend, storeDir } = await fileBackend(t);
17
+ const { adaId } = await exerciseEmailChange(t, backend);
18
+ // Another server on the same store reads the change.
19
+ const reread = createLocalFileBackend({ storeDir });
20
+ assert.equal((await reread.withTransaction((tx) => tx.users.findById(adaId))).email, "ada.second@example.com");
21
+ });
22
+
23
+ test("a waiting change is kept in the file store, and an account stored before it read as having none", async (t) => {
24
+ const { backend, storeDir } = await fileBackend(t);
25
+ const { service } = emailChangeService(backend);
26
+ const ada = await service.register({ email: "ada@example.com", password: "correct horse battery staple" });
27
+ await service.requestEmailChangeForUser({ userId: ada.actor.providerUserId, email: "ada.new@example.com" });
28
+ const reread = createLocalFileBackend({ storeDir });
29
+ assert.equal((await reread.withTransaction((tx) => tx.users.findById(ada.actor.providerUserId))).pendingEmail, "ada.new@example.com");
30
+
31
+ const usersFile = path.join(storeDir, "users.passwd");
32
+ const line = (await fs.readFile(usersFile, "utf8")).trim();
33
+ // The twelve fields an account had before it kept a waiting change.
34
+ await fs.writeFile(usersFile, `${line.split(":").slice(0, 12).join(":")}\n`);
35
+ const older = createLocalFileBackend({ storeDir });
36
+ assert.equal((await older.withTransaction((tx) => tx.users.findById(ada.actor.providerUserId))).pendingEmail, "");
37
+ });
@@ -31,7 +31,7 @@ async function startServer(storeDir, { env = {}, confirmationSender } = {}) {
31
31
  const logger = { info() {}, warn: (fields, message) => warnings.push({ fields, message }), error() {}, debug() {} };
32
32
  const options = localServiceOptions({
33
33
  env: {
34
- NODE_ENV: "production",
34
+ APP_ENV: "production", NODE_ENV: "production",
35
35
  AUTH_LOCAL_STORE_DIR: storeDir,
36
36
  AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "",
37
37
  APP_PUBLIC_URL: "https://app.example.test",
@@ -149,7 +149,7 @@ test("with email confirmation, the first account the setup code admits signs in
149
149
 
150
150
  test("development's first account, which needs no setup code, still confirms its email", async (t) => {
151
151
  const storeDir = await temporaryStoreDir(t);
152
- const { service } = await startServer(storeDir, { env: { NODE_ENV: "development", AUTH_LOCAL_EMAIL_CONFIRMATION: "" } });
152
+ const { service } = await startServer(storeDir, { env: { APP_ENV: "development", AUTH_LOCAL_EMAIL_CONFIRMATION: "" } });
153
153
  const first = await service.register({ email: "dev@example.com", password: PASSWORD });
154
154
  assert.equal(first.requiresEmailConfirmation, true);
155
155
  assert.ok(first.confirmationUrl);
@@ -157,7 +157,7 @@ test("development's first account, which needs no setup code, still confirms its
157
157
 
158
158
  test("development asks for no setup code", async (t) => {
159
159
  const storeDir = await temporaryStoreDir(t);
160
- const { service, warnings } = await startServer(storeDir, { env: { NODE_ENV: "development" } });
160
+ const { service, warnings } = await startServer(storeDir, { env: { APP_ENV: "development" } });
161
161
  assert.deepEqual(warnings, []);
162
162
  assert.equal(await codeFile(storeDir), null);
163
163
  assert.equal(waitingForFirstAccount(service), false);
@@ -167,7 +167,7 @@ test("development asks for no setup code", async (t) => {
167
167
  test("a service composed without a started setup checks for an account on its first registration", async (t) => {
168
168
  const storeDir = await temporaryStoreDir(t);
169
169
  const options = localServiceOptions({
170
- env: { NODE_ENV: "production", AUTH_LOCAL_STORE_DIR: storeDir, AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "" }
170
+ env: { APP_ENV: "production", NODE_ENV: "production", AUTH_LOCAL_STORE_DIR: storeDir, AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "" }
171
171
  });
172
172
  const service = createLocalAuthService({ ...options, backend: createLocalFileBackend({ storeDir }) });
173
173
  await assert.rejects(service.register({ email: "owner@example.com", password: PASSWORD }), codeRequired);
@@ -212,8 +212,8 @@ test("a setup code supplied in AUTH_LOCAL_SETUP_CODE is asked for at every start
212
212
 
213
213
  test("a supplied setup code needs 20 letters and digits in production, and development ignores it", () => {
214
214
  assert.throws(
215
- () => localServiceOptions({ env: { NODE_ENV: "production", AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "", AUTH_LOCAL_SETUP_CODE: "short-code-1234" } }),
215
+ () => localServiceOptions({ env: { APP_ENV: "production", NODE_ENV: "production", AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "", AUTH_LOCAL_SETUP_CODE: "short-code-1234" } }),
216
216
  /AUTH_LOCAL_SETUP_CODE must have at least 20 letters and digits\./u
217
217
  );
218
- assert.equal(localServiceOptions({ env: { NODE_ENV: "development", AUTH_LOCAL_SETUP_CODE: "short" } }).config.setupCode, "");
218
+ assert.equal(localServiceOptions({ env: { APP_ENV: "development", AUTH_LOCAL_SETUP_CODE: "short" } }).config.setupCode, "");
219
219
  });
@@ -12,6 +12,7 @@ import {
12
12
  createLocalAuthRegisterHookDecorator,
13
13
  createLocalFileBackend
14
14
  } from "../src/server/lib/index.js";
15
+ import { NO_PASSWORD } from "../src/server/lib/accounts.js";
15
16
  import { sha256Base64url } from "../src/server/lib/tokens.js";
16
17
  import { resolveLocalAuthConfig } from "../src/server/lib/config.js";
17
18
  import {
@@ -153,7 +154,7 @@ test("while a production server waits for its first account, a Google sign-in ca
153
154
  const codes = [];
154
155
  const firstAccountSetup = createFirstAccountSetup({
155
156
  backend,
156
- config: { isProduction: true, storeDir: backend.storeDir },
157
+ config: { isDeployed: true, storeDir: backend.storeDir },
157
158
  logger: { warn: (fields) => codes.push(fields.firstAccountSetupCode) }
158
159
  });
159
160
  await firstAccountSetup.start();
@@ -198,6 +199,17 @@ test("a verified email that matches an existing account signs it in and links Go
198
199
  assert.equal(again.actor.email, issuer.account.email);
199
200
  });
200
201
 
202
+ test("a provider sign-in of a disabled account is told the account is disabled, and links nothing", async (t) => {
203
+ const { issuer, backend, service } = await googleSetup(t);
204
+ await backend.withTransaction((tx) => tx.users.create({
205
+ id: "usr_off", email: issuer.account.email, displayName: "Off", password: NO_PASSWORD, disabled: true
206
+ }));
207
+ const { input, request } = await approveAtProvider(service);
208
+ await assert.rejects(service.oauthComplete(input, request),
209
+ { statusCode: 403, code: "auth.local.login.disabled", message: "This account is disabled." });
210
+ assert.deepEqual(await backend.withTransaction((tx) => tx.identities.listForUser("usr_off")), []);
211
+ });
212
+
201
213
  test("a callback with the wrong state, another browser's attempt, or a used attempt is refused", async (t) => {
202
214
  const { backend, service } = await googleSetup(t);
203
215
  const { input, request } = await approveAtProvider(service);
@@ -379,7 +391,7 @@ test("returnTo stays on the app's own origin", async (t) => {
379
391
  });
380
392
 
381
393
  test("a provider appears only when its client id and secret are both set", () => {
382
- const base = { NODE_ENV: "test", AUTH_LOCAL_SESSION_SECRET: "secret", APP_PUBLIC_URL: APP_URL };
394
+ const base = { APP_ENV: "test", AUTH_LOCAL_SESSION_SECRET: "secret", APP_PUBLIC_URL: APP_URL };
383
395
  const config = (env) => resolveLocalAuthConfig({
384
396
  appRoot: os.tmpdir(),
385
397
  env: { ...base, ...env },
@@ -89,7 +89,7 @@ async function startProvider(t, storeDir, env, capabilities = {}) {
89
89
  const runtime = createCapabilityRuntime({
90
90
  inputs: {
91
91
  "runtime.app-root": storeDir,
92
- "runtime.env": { NODE_ENV: "test", AUTH_LOCAL_STORE_DIR: storeDir, APP_PUBLIC_URL: "http://localhost:5173", ...env },
92
+ "runtime.env": { APP_ENV: "test", AUTH_LOCAL_STORE_DIR: storeDir, APP_PUBLIC_URL: "http://localhost:5173", ...env },
93
93
  "runtime.logger": silentLogger,
94
94
  "auth.extensions": createAuthExtensions(),
95
95
  ...capabilities