@finueva/auth 0.18.0 → 0.18.1

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/dist/index.js CHANGED
@@ -2291,7 +2291,7 @@ const passkeyClient = () => {
2291
2291
  };
2292
2292
  };
2293
2293
  //#endregion
2294
- //#region ../../node_modules/.pnpm/@chiballc+utils@2.5.1_@hono+zod-openapi@1.5.1_hono@4.13.0_zod@4.4.3__hono@4.13.0_magic-_dfcdb59639d9fe73412dad0223379a88/node_modules/@chiballc/utils/dist/index.mjs
2294
+ //#region ../../node_modules/.pnpm/@chiballc+utils@2.5.1_@hono+zod-openapi@1.5.1_hono@4.13.0_zod@4.4.3__hono@4.13.0_magic-_80fa16dd1813631ddeb36d2ee29b8660/node_modules/@chiballc/utils/dist/index.mjs
2295
2295
  function isPromise(arg) {
2296
2296
  return arg && typeof arg.then === "function";
2297
2297
  }
@@ -3683,6 +3683,18 @@ function unique(items, key) {
3683
3683
  if (new Set(items.map(key)).size !== items.length) throw authorityUnavailable();
3684
3684
  return items;
3685
3685
  }
3686
+ function credentialSessionId(token) {
3687
+ const parts = token.split(".");
3688
+ if (parts.length !== 3 || !/^[A-Za-z0-9_-]+$/.test(parts[1])) return void 0;
3689
+ try {
3690
+ const base64 = parts[1].replaceAll("-", "+").replaceAll("_", "/");
3691
+ const decoded = atob(base64.padEnd(base64.length + (4 - base64.length % 4) % 4, "="));
3692
+ const payload = JSON.parse(decoded);
3693
+ return isObject(payload) && typeof payload.sid === "string" && payload.sid.length > 0 ? payload.sid : void 0;
3694
+ } catch {
3695
+ return;
3696
+ }
3697
+ }
3686
3698
  function isObject(value) {
3687
3699
  return typeof value === "object" && value !== null && !Array.isArray(value);
3688
3700
  }
@@ -4509,7 +4521,7 @@ function normalizeUserApiCredential(value, startedAt) {
4509
4521
  expiresAt: new Date(startedAt + value.expiresIn * 1e3)
4510
4522
  };
4511
4523
  }
4512
- function createUserApiCredentialCache(issue, now) {
4524
+ function createUserApiCredentialCache(issue, now, currentSessionId = () => void 0) {
4513
4525
  let generation = 0;
4514
4526
  let cached;
4515
4527
  let pending;
@@ -4519,6 +4531,12 @@ function createUserApiCredentialCache(issue, now) {
4519
4531
  });
4520
4532
  return {
4521
4533
  hasAuthority: () => Boolean(cached || pending),
4534
+ retainSession(sessionId) {
4535
+ if (cached?.sessionId === sessionId && !pending) return;
4536
+ generation += 1;
4537
+ cached = void 0;
4538
+ pending = void 0;
4539
+ },
4522
4540
  clear() {
4523
4541
  generation += 1;
4524
4542
  cached = void 0;
@@ -4542,10 +4560,14 @@ function createUserApiCredentialCache(issue, now) {
4542
4560
  const value = normalizeUserApiCredential(await issue(applicationId), startedAt);
4543
4561
  if (epoch !== generation) throw new AuthError("state_conflict", 409);
4544
4562
  if (now() < startedAt || value.expiresAt.getTime() <= now()) throw new AuthError("authentication_required", 401);
4563
+ const sessionId = credentialSessionId(value.token);
4564
+ const observed = currentSessionId();
4565
+ if (observed !== void 0 && sessionId !== void 0 && sessionId !== observed) throw new AuthError("authentication_required", 401);
4545
4566
  cached = {
4546
4567
  applicationId,
4547
4568
  token: value.token,
4548
- expiresAt: value.expiresAt.getTime()
4569
+ expiresAt: value.expiresAt.getTime(),
4570
+ sessionId
4549
4571
  };
4550
4572
  return value;
4551
4573
  })().finally(() => {
@@ -4578,7 +4600,7 @@ function createAuthClient(options) {
4578
4600
  method: "POST",
4579
4601
  headers: { "content-type": "application/json; charset=utf-8" },
4580
4602
  body: { applicationId }
4581
- }), now);
4603
+ }), now, () => sessionId);
4582
4604
  const betterAuth = createAuthClient$1({
4583
4605
  baseURL: authOrigin,
4584
4606
  basePath: "/api/v1/auth",
@@ -4740,9 +4762,12 @@ function createAuthClient(options) {
4740
4762
  credentialGeneration += 1;
4741
4763
  }
4742
4764
  function updateSession(session) {
4743
- const unboundCredential = !sessionId && (credential || credentialRequests.size > 0 || userApiCredentials.hasAuthority());
4744
4765
  const changedSession = sessionId === void 0 ? !sessionAuthorityObserved || session !== null : !session || session.id !== sessionId || session.user.id !== sessionUserId;
4745
- if (!session || unboundCredential || sessionId && (session.id !== sessionId || session.user.id !== sessionUserId)) clearCredential();
4766
+ if (!session || sessionId && (session.id !== sessionId || session.user.id !== sessionUserId)) clearCredential();
4767
+ else if (!sessionId) {
4768
+ if (credential && credential.sessionId !== session.id) credential = void 0;
4769
+ userApiCredentials.retainSession(session.id);
4770
+ }
4746
4771
  if (changedSession || recentAuthenticationStartAttempt && (recentAuthenticationStartAttempt.sessionId !== session?.id || recentAuthenticationStartAttempt.userId !== session?.user.id)) recentAuthenticationStartAttempt = void 0;
4747
4772
  if (changedSession) sessionAuthorityEpoch += 1;
4748
4773
  sessionAuthorityObserved = true;
@@ -5390,9 +5415,12 @@ function createAuthClient(options) {
5390
5415
  if (error) throw error;
5391
5416
  const nextCredential = normalizeDriveCredential(result, now());
5392
5417
  if (generation !== credentialGeneration) throw new AuthError("authentication_required", 401);
5418
+ const issuedFor = credentialSessionId(nextCredential.token);
5419
+ if (sessionId !== void 0 && issuedFor !== void 0 && issuedFor !== sessionId) throw new AuthError("authentication_required", 401);
5393
5420
  if (selectedWorkspaceKey === workspaceKey) credential = {
5394
5421
  key: workspaceKey,
5395
- value: nextCredential
5422
+ value: nextCredential,
5423
+ sessionId: issuedFor
5396
5424
  };
5397
5425
  return nextCredential;
5398
5426
  })().finally(() => {