@jskit-ai/auth-provider-local-core 0.2.2 → 0.2.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/auth-provider-local-core",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "test": "node --test --test-concurrency=1"
@@ -61,7 +61,7 @@
61
61
  }
62
62
  },
63
63
  "peerDependencies": {
64
- "@jskit-ai/auth-core": "0.2.1",
65
- "@jskit-ai/kernel": "0.2.1"
64
+ "@jskit-ai/auth-core": "0.2.2",
65
+ "@jskit-ai/kernel": "0.2.2"
66
66
  }
67
67
  }
@@ -167,21 +167,26 @@ async function findExistingAppProfile(user, profileProjector) {
167
167
  }
168
168
  if (typeof profileProjector.findByIdentity !== "function") {
169
169
  throw new Error(
170
- "auth.profile.projector.findByIdentity() is required for dev auth impersonation."
170
+ "auth.profile.projector.findByIdentity() is required for dev auth impersonation and profile lookups without sync."
171
171
  );
172
172
  }
173
173
  const profile = await profileProjector.findByIdentity(buildProfile(user));
174
174
  return profile ? requireProfileResult(profile, "findByIdentity") : null;
175
175
  }
176
176
 
177
- async function buildSessionAuthPayload({ devAuth, profileProjector, request, session, user }) {
178
- if (!isDevAuthSession(session)) {
177
+ // A dev session, or a caller that asks only who is signed in (syncProfile:
178
+ // false), reads the existing app profile instead of synchronizing it.
179
+ async function buildSessionAuthPayload({ devAuth, profileProjector, request, session, user, syncProfile = true }) {
180
+ const devSession = isDevAuthSession(session);
181
+ if (!devSession && syncProfile) {
179
182
  return buildAuthPayload({ user, session: null, profileProjector });
180
183
  }
181
- try {
182
- ensureDevAuthRuntimeAvailable(devAuth, request);
183
- } catch {
184
- return null;
184
+ if (devSession) {
185
+ try {
186
+ ensureDevAuthRuntimeAvailable(devAuth, request);
187
+ } catch {
188
+ return null;
189
+ }
185
190
  }
186
191
  const appProfile = await findExistingAppProfile(user, profileProjector);
187
192
  return profileProjector && !appProfile
@@ -507,7 +512,7 @@ function createLocalAuthService({
507
512
  });
508
513
  }
509
514
 
510
- async function authenticateRequest(request) {
515
+ async function authenticateRequest(request, { syncProfile = true } = {}) {
511
516
  const cookies = safeRequestCookies(request);
512
517
  const accessToken = String(cookies[ACCESS_TOKEN_COOKIE] || "").trim();
513
518
  const refreshToken = String(cookies[REFRESH_TOKEN_COOKIE] || "").trim();
@@ -532,7 +537,8 @@ function createLocalAuthService({
532
537
  profileProjector,
533
538
  request,
534
539
  session: resolved.session,
535
- user: resolved.user
540
+ user: resolved.user,
541
+ syncProfile
536
542
  });
537
543
  if (!authPayload) {
538
544
  return unauthenticatedAuthResult(true);
@@ -567,7 +573,8 @@ function createLocalAuthService({
567
573
  profileProjector,
568
574
  request,
569
575
  session: resolved.session,
570
- user: resolved.user
576
+ user: resolved.user,
577
+ syncProfile
571
578
  });
572
579
  if (!authPayload) {
573
580
  return unauthenticatedAuthResult(true);
@@ -281,6 +281,54 @@ test("local login-as never invokes the mutating profile projector", async () =>
281
281
  assert.equal(syncCalls, 0);
282
282
  });
283
283
 
284
+ test("local authentication without profile sync reads the existing profile", async () => {
285
+ const projectedProfile = {
286
+ id: "app-user-ada",
287
+ authProvider: "local",
288
+ authProviderUserSid: "",
289
+ displayName: "Ada",
290
+ email: "ada@example.com"
291
+ };
292
+ let existingProfile = projectedProfile;
293
+ let syncCalls = 0;
294
+ const { authService } = await createStartedApp({
295
+ profileProjector: {
296
+ async findByIdentity() {
297
+ return existingProfile;
298
+ },
299
+ async syncIdentityProfile(profile) {
300
+ syncCalls += 1;
301
+ return { ...projectedProfile, authProviderUserSid: profile.authProviderUserSid };
302
+ }
303
+ }
304
+ });
305
+ const registered = await authService.register({
306
+ displayName: "Ada",
307
+ email: "ada@example.com",
308
+ password: "correct horse battery staple"
309
+ });
310
+ const reply = createReplyFixture();
311
+ authService.writeSessionCookies(reply, registered.session);
312
+ const signedIn = createLocalAuthRequest({ cookies: reply.cookies });
313
+ const refreshOnly = createLocalAuthRequest({
314
+ cookies: { jskit_local_refresh_token: reply.cookies.jskit_local_refresh_token }
315
+ });
316
+ syncCalls = 0;
317
+
318
+ for (const request of [signedIn, refreshOnly]) {
319
+ const checked = await authService.authenticateRequest(request, { syncProfile: false });
320
+ assert.equal(checked.authenticated, true);
321
+ assert.equal(checked.actor.id, "app-user-ada");
322
+ }
323
+ assert.equal(syncCalls, 0);
324
+
325
+ existingProfile = null;
326
+ const unprojected = await authService.authenticateRequest(signedIn, { syncProfile: false });
327
+ assert.equal(unprojected.authenticated, false, "A login without an app profile is not signed in without sync");
328
+ assert.equal((await authService.authenticateRequest(signedIn)).actor.id, "app-user-ada");
329
+ assert.equal(syncCalls, 1);
330
+ });
331
+
284
332
  test("local login-as rejects missing, disabled, and unprojected users", async () => {
285
333
  const { authService, backend } = await createStartedApp({
286
334
  env: devAuthEnv(),