@jskit-ai/auth-provider-local-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.
@@ -3,12 +3,15 @@ import fs from "node:fs/promises";
3
3
  import os from "node:os";
4
4
  import path from "node:path";
5
5
  import test from "node:test";
6
- import { createActionProvider } from "@jskit-ai/kernel/server/actions";
6
+ import { ActionProvider } from "@jskit-ai/kernel/server/actions";
7
+ import { EventProvider } from "@jskit-ai/kernel/server/runtime";
7
8
  import { createCapabilityRuntime, defineProvider } from "@jskit-ai/kernel/shared/capabilities";
8
9
  import { createAuthExtensions } from "@jskit-ai/auth-core/server/authExtensions";
10
+ import { silentLogger } from "@jskit-ai/kernel/server/runtime/logger";
9
11
  import { DEV_AUTH_SECRET_HEADER } from "@jskit-ai/auth-core/server/devAuth";
10
12
  import { AuthFeature } from "@jskit-ai/auth-core/server/providers/AuthFeature";
11
13
  import { AuthLocalProvider } from "../src/server/providers/AuthLocalProvider.js";
14
+ import { localServiceOptions } from "../test-support/localService.js";
12
15
  import {
13
16
  createLocalAuthRegisterHookDecorator,
14
17
  createLocalAuthService,
@@ -92,12 +95,6 @@ async function createStartedApp({
92
95
  debug() {}
93
96
  };
94
97
  if (typeof configureExtensions === "function") configureExtensions(extensions, runtimeLogger);
95
- if (profileProjector) {
96
- extensions.registerProfileProjector({
97
- projectorId: "test.profile",
98
- ...profileProjector
99
- });
100
- }
101
98
  if (invitationContextResolver) {
102
99
  extensions.registerInvitationContextResolver({
103
100
  resolverId: "test.invitation",
@@ -130,13 +127,14 @@ async function createStartedApp({
130
127
  "runtime.logger": runtimeLogger,
131
128
  "auth.extensions": extensions,
132
129
  "auth.local.backend": backend,
130
+ ...(profileProjector ? { "auth.profile.projector": profileProjector } : {}),
133
131
  ...(passwordStrategy ? { "auth.local.password-strategy": passwordStrategy } : {}),
134
132
  ...(recoverySender !== null ? { "auth.local.recovery-sender": recoverySender } : {}),
135
133
  };
136
134
  const runtime = createCapabilityRuntime({
137
135
  inputs,
138
136
  providers: [
139
- createActionProvider(),
137
+ EventProvider, ActionProvider,
140
138
  AuthLocalProvider,
141
139
  AuthFeature,
142
140
  probe
@@ -211,9 +209,9 @@ test("local file auth login-as issues native cookies for an existing user", asyn
211
209
  password: "correct horse battery staple"
212
210
  });
213
211
 
214
- const impersonated = await authService.devLoginAs(createDevAuthExchangeRequest(), {
212
+ const impersonated = await authService.devLoginAs({
215
213
  email: "ADA@EXAMPLE.COM"
216
- });
214
+ }, createDevAuthExchangeRequest());
217
215
  assert.equal(impersonated.actor.email, "ada@example.com");
218
216
  assert.equal(impersonated.session.purpose, "dev-auth");
219
217
  assert.equal(authService.getCapabilities().features.devLoginAs, true);
@@ -266,9 +264,9 @@ test("local login-as never invokes the mutating profile projector", async () =>
266
264
  findCalls = 0;
267
265
  syncCalls = 0;
268
266
 
269
- const impersonated = await authService.devLoginAs(createDevAuthExchangeRequest(), {
267
+ const impersonated = await authService.devLoginAs({
270
268
  email: "ada@example.com"
271
- });
269
+ }, createDevAuthExchangeRequest());
272
270
  const reply = createReplyFixture();
273
271
  authService.writeSessionCookies(reply, impersonated.session);
274
272
  const authenticated = await authService.authenticateRequest(createLocalAuthRequest({
@@ -365,7 +363,7 @@ test("local login-as rejects missing, disabled, and unprojected users", async ()
365
363
  "unprojected@example.com"
366
364
  ]) {
367
365
  await assert.rejects(
368
- () => authService.devLoginAs(createDevAuthExchangeRequest(), { email }),
366
+ () => authService.devLoginAs({ email }, createDevAuthExchangeRequest()),
369
367
  /User not found/
370
368
  );
371
369
  }
@@ -382,21 +380,21 @@ test("local login-as rejects missing, incorrect, and non-loopback exchange autho
382
380
  });
383
381
 
384
382
  await assert.rejects(
385
- () => authService.devLoginAs(createLocalAuthRequest(), { email: "ada@example.com" }),
383
+ () => authService.devLoginAs({ email: "ada@example.com" }, createLocalAuthRequest()),
386
384
  /not authorized/
387
385
  );
388
386
  await assert.rejects(
389
- () => authService.devLoginAs(createLocalAuthRequest({
387
+ () => authService.devLoginAs({ email: "ada@example.com" }, createLocalAuthRequest({
390
388
  headers: {
391
389
  [DEV_AUTH_SECRET_HEADER]: "wrong-secret"
392
390
  }
393
- }), { email: "ada@example.com" }),
391
+ })),
394
392
  /not authorized/
395
393
  );
396
394
  await assert.rejects(
397
- () => authService.devLoginAs(createDevAuthExchangeRequest({
395
+ () => authService.devLoginAs({ email: "ada@example.com" }, createDevAuthExchangeRequest({
398
396
  remoteAddress: "203.0.113.5"
399
- }), { email: "ada@example.com" }),
397
+ })),
400
398
  /localhost/
401
399
  );
402
400
  });
@@ -446,6 +444,7 @@ test("local auth provider applies auth service decorators for blocking and non-b
446
444
  extensions.registerServiceDecorator(createLocalAuthRegisterHookDecorator({
447
445
  decoratorId: "test.auth.local.blockingHook",
448
446
  order: 10,
447
+ logger: runtimeLogger,
449
448
  hook: {
450
449
  hookId: "permissions",
451
450
  blocking: true,
@@ -489,6 +488,7 @@ test("local auth register hook decorators require explicit blocking mode", () =>
489
488
  assert.throws(
490
489
  () =>
491
490
  createLocalAuthRegisterHookDecorator({
491
+ logger: silentLogger,
492
492
  hook: {
493
493
  hookId: "invalid",
494
494
  async handle() {}
@@ -499,6 +499,7 @@ test("local auth register hook decorators require explicit blocking mode", () =>
499
499
  assert.throws(
500
500
  () =>
501
501
  createLocalAuthRegisterHookDecorator({
502
+ logger: silentLogger,
502
503
  hook: {
503
504
  hookId: "invalid",
504
505
  blocking: false
@@ -508,6 +509,20 @@ test("local auth register hook decorators require explicit blocking mode", () =>
508
509
  );
509
510
  });
510
511
 
512
+ test("local auth register hook decorators require a logger for failed non-blocking hooks", () => {
513
+ assert.throws(
514
+ () =>
515
+ createLocalAuthRegisterHookDecorator({
516
+ hook: {
517
+ hookId: "audit",
518
+ blocking: false,
519
+ async handle() {}
520
+ }
521
+ }),
522
+ /createLocalAuthRegisterHookDecorator requires a logger/
523
+ );
524
+ });
525
+
511
526
  test("local auth provider accepts a custom password strategy capability", async () => {
512
527
  const passwordStrategy = {
513
528
  prefix: "strategy",
@@ -623,13 +638,8 @@ test("local auth login verifies password and creates session in one backend tran
623
638
  }
624
639
  };
625
640
  const authService = createLocalAuthService({
626
- backend: wrappedBackend,
627
- config: {
628
- sessionSecret: "test-secret",
629
- nodeEnv: "test",
630
- recoveryDevOutput: "response",
631
- appPublicUrl: "http://localhost:5173"
632
- }
641
+ ...localServiceOptions(),
642
+ backend: wrappedBackend
633
643
  });
634
644
 
635
645
  const result = await authService.login({
@@ -646,6 +656,7 @@ test("local auth service accepts a custom stored-password format", async () => {
646
656
  const passwordRecords = [];
647
657
  const sessions = [];
648
658
  const authService = createLocalAuthService({
659
+ ...localServiceOptions(),
649
660
  backend: {
650
661
  async withTransaction(callback) {
651
662
  return callback({
@@ -729,12 +740,6 @@ test("local auth service accepts a custom stored-password format", async () => {
729
740
  });
730
741
  }
731
742
  },
732
- config: {
733
- sessionSecret: "test-secret",
734
- nodeEnv: "test",
735
- recoveryDevOutput: "response",
736
- appPublicUrl: "http://localhost:5173"
737
- },
738
743
  passwordStrategy: {
739
744
  async verifyPassword(password, record) {
740
745
  return record === `custom:existing@example.com:${password}`;
@@ -808,10 +813,10 @@ test("local auth password strategy handles register, reset, login, and change pa
808
813
  authService.writeSessionCookies(recoveryReply, recovery.session);
809
814
  await authService.resetPassword(
810
815
  {
811
- cookies: recoveryReply.cookies
816
+ password: "reset password value"
812
817
  },
813
818
  {
814
- password: "reset password value"
819
+ cookies: recoveryReply.cookies
815
820
  }
816
821
  );
817
822
 
@@ -867,10 +872,10 @@ test("local auth provider completes recovery through a recovery-scoped session",
867
872
  () =>
868
873
  authService.resetPassword(
869
874
  {
870
- cookies: normalReply.cookies
875
+ password: "not allowed value"
871
876
  },
872
877
  {
873
- password: "not allowed value"
878
+ cookies: normalReply.cookies
874
879
  }
875
880
  ),
876
881
  /Recovery session required/
@@ -898,10 +903,10 @@ test("local auth provider completes recovery through a recovery-scoped session",
898
903
 
899
904
  await authService.resetPassword(
900
905
  {
901
- cookies: reply.cookies
906
+ password: "updated password value"
902
907
  },
903
908
  {
904
- password: "updated password value"
909
+ cookies: reply.cookies
905
910
  }
906
911
  );
907
912
 
@@ -945,10 +950,10 @@ test("local auth reset invalidates other outstanding recovery tokens for the use
945
950
  authService.writeSessionCookies(reply, recovery.session);
946
951
  await authService.resetPassword(
947
952
  {
948
- cookies: reply.cookies
953
+ password: "updated password value"
949
954
  },
950
955
  {
951
- password: "updated password value"
956
+ cookies: reply.cookies
952
957
  }
953
958
  );
954
959
 
@@ -1015,6 +1020,9 @@ test("local auth provider projects app profile when auth.profile.projector is in
1015
1020
  const projectedProfiles = [];
1016
1021
  const { authService } = await createStartedApp({
1017
1022
  profileProjector: {
1023
+ async findByIdentity() {
1024
+ return null;
1025
+ },
1018
1026
  async syncIdentityProfile(profile) {
1019
1027
  projectedProfiles.push(profile);
1020
1028
  return {
@@ -1058,6 +1066,9 @@ test("local auth provider passes resolved invitation context into profile projec
1058
1066
  }
1059
1067
  },
1060
1068
  profileProjector: {
1069
+ async findByIdentity() {
1070
+ return null;
1071
+ },
1061
1072
  async syncIdentityProfile(profile, options) {
1062
1073
  projectionCalls.push({ profile, options });
1063
1074
  return {
@@ -0,0 +1,38 @@
1
+ import os from "node:os";
2
+ import { invitationAsGiven } from "@jskit-ai/auth-core/server/authExtensions";
3
+ import { noAppProfiles } from "@jskit-ai/auth-core/server/noAppProfiles";
4
+ import { silentLogger } from "@jskit-ai/kernel/server/runtime/logger";
5
+ import { resolveLocalAuthConfig } from "../src/server/lib/config.js";
6
+ import { createLoggedRecoverySender } from "../src/server/lib/passwordFlows.js";
7
+ import { scryptPasswordStrategy } from "../src/server/lib/passwords.js";
8
+
9
+ /**
10
+ * What a test passes to createLocalAuthService besides its backend: a complete
11
+ * configuration resolved from a test environment, and each collaborator's default.
12
+ * @param {object} [options]
13
+ * @param {Record<string, string>} [options.env] Environment values over the test ones.
14
+ * @param {boolean} [options.emailRecovery] True when the test passes its own recoverySender.
15
+ */
16
+ function localServiceOptions({ env = {}, emailRecovery = false } = {}) {
17
+ return {
18
+ config: resolveLocalAuthConfig({
19
+ appRoot: os.tmpdir(),
20
+ env: {
21
+ NODE_ENV: "test",
22
+ AUTH_LOCAL_SESSION_SECRET: "test-session-secret",
23
+ APP_PUBLIC_URL: "http://localhost:5173",
24
+ AUTH_LOCAL_RECOVERY_DEV_OUTPUT: "response",
25
+ ...env
26
+ },
27
+ fileBackend: false,
28
+ emailRecovery
29
+ }),
30
+ invitationContextResolver: invitationAsGiven,
31
+ logger: silentLogger,
32
+ passwordStrategy: scryptPasswordStrategy,
33
+ profileProjector: noAppProfiles,
34
+ recoverySender: createLoggedRecoverySender(silentLogger)
35
+ };
36
+ }
37
+
38
+ export { localServiceOptions };
@@ -3,6 +3,7 @@ import http from "node:http";
3
3
  import { once } from "node:events";
4
4
  import { followAuthorization, startMockIssuer } from "../../auth-provider-oidc-core/test-support/mockIssuer.js";
5
5
  import { createLocalAuthService } from "../src/server/lib/service.js";
6
+ import { localServiceOptions } from "./localService.js";
6
7
 
7
8
  const APP_URL = "http://localhost:5173";
8
9
  const REDIRECT_URI = `${APP_URL}/auth/login`;
@@ -106,7 +107,7 @@ async function startFakeGitHub({ user = { id: 4242, login: "ada", name: "Ada Lov
106
107
  apiUrl: origin
107
108
  },
108
109
  async close() {
109
- server.closeAllConnections?.();
110
+ server.closeAllConnections();
110
111
  server.close();
111
112
  await once(server, "close");
112
113
  }
@@ -117,16 +118,20 @@ function oidcProviderEntry(issuer, { id = "google", label = "Google" } = {}) {
117
118
  return { id, label, ...CLIENT, issuer: issuer.issuer };
118
119
  }
119
120
 
120
- function createService({ backend, oauthProviders, profileProjector = null }) {
121
+ /**
122
+ * The local auth service of a sign-in test, whose OAuth providers are the mock ones.
123
+ * @param {object} dependencies
124
+ * @param {import("../src/server/types.js").AuthLocalBackend} dependencies.backend
125
+ * @param {import("../src/server/types.js").AuthLocalOAuthProvider[]} dependencies.oauthProviders
126
+ * @param {import("@jskit-ai/auth-core/server/types").AuthProfileProjector} dependencies.profileProjector
127
+ */
128
+ function createService({ backend, oauthProviders, profileProjector }) {
129
+ const options = localServiceOptions({ env: { APP_PUBLIC_URL: APP_URL } });
121
130
  return createLocalAuthService({
131
+ ...options,
122
132
  backend,
123
- profileProjector,
124
- config: {
125
- sessionSecret: "oauth-test-session-secret",
126
- nodeEnv: "test",
127
- appPublicUrl: APP_URL,
128
- oauthProviders
129
- }
133
+ config: { ...options.config, oauthProviders, oauthEnabled: oauthProviders.length > 0 },
134
+ profileProjector
130
135
  });
131
136
  }
132
137
 
@@ -137,6 +142,7 @@ function sessionCookies(session) {
137
142
  /**
138
143
  * Starts a sign-in (or, with `link`, a link for the signed-in `cookies`), lets
139
144
  * the provider approve it, and returns the callback input and browser request.
145
+ * @param {ReturnType<typeof createService>} service
140
146
  */
141
147
  async function approveAtProvider(service, { provider = "google", returnTo = "/home", cookies = {}, link = false } = {}) {
142
148
  const started = link