@authyon/server 0.2.0-beta.30 → 0.2.0-beta.32

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/bff.cjs CHANGED
@@ -307,7 +307,7 @@ function loginInput(body) {
307
307
  return {
308
308
  password: inputString(body, "password"),
309
309
  ...body.email !== void 0 ? { email: inputString(body, "email") } : { username: inputString(body, "username") },
310
- ...body.organizationSlug !== void 0 ? { organizationSlug: inputString(body, "organizationSlug") } : {},
310
+ ...body.tenantSlug !== void 0 ? { tenantSlug: inputString(body, "tenantSlug") } : body.organizationSlug !== void 0 ? { tenantSlug: inputString(body, "organizationSlug") } : {},
311
311
  sessionPersistence: sessionPersistence(body)
312
312
  };
313
313
  }
@@ -455,11 +455,11 @@ var BffSessionManager = class {
455
455
  });
456
456
  this.refreshAhead = positiveDuration(options.refreshAheadMs ?? DEFAULT_REFRESH_AHEAD_MS);
457
457
  }
458
- async create(tokens, organizationSlug, sessionPersistence2 = "standard") {
458
+ async create(tokens, tenantSlug, sessionPersistence2 = "standard") {
459
459
  try {
460
460
  const user = await this.verifiedProfile(tokens);
461
- if (organizationSlug !== void 0 && user.organization?.slug !== organizationSlug) {
462
- throw new BffSessionError("session.organization_mismatch", 502);
461
+ if (tenantSlug !== void 0 && user.tenant?.slug !== tenantSlug) {
462
+ throw new BffSessionError("session.tenant_mismatch", 502);
463
463
  }
464
464
  const lifetime = this.sessionLifetime(sessionPersistence2);
465
465
  const record = {
@@ -480,15 +480,19 @@ var BffSessionManager = class {
480
480
  async read(id) {
481
481
  return this.readCurrent(id, 0);
482
482
  }
483
+ /** @deprecated Use `switchTenant`. */
483
484
  async switchOrganization(id, slug) {
485
+ return this.switchTenant(id, slug);
486
+ }
487
+ async switchTenant(id, slug) {
484
488
  return this.update(id, async (lease) => {
485
489
  const refreshed = await this.refreshIfNeeded(lease);
486
490
  if (!refreshed && !await this.options.provider.validate(lease.record.tokens.accessToken)) {
487
491
  throw new BffSessionError("session.revoked", 401, true);
488
492
  }
489
493
  await this.rotate(lease, {
490
- issue: () => this.issueOrganizationTokens(lease.record.tokens.accessToken, slug),
491
- accepts: (user) => user.id === lease.record.user.id && user.organization?.slug === slug
494
+ issue: () => this.issueTenantTokens(lease.record.tokens.accessToken, slug),
495
+ accepts: (user) => user.id === lease.record.user.id && user.tenant?.slug === slug
492
496
  });
493
497
  });
494
498
  }
@@ -571,7 +575,7 @@ var BffSessionManager = class {
571
575
  if (!this.needsRefresh(lease.record)) return false;
572
576
  await this.rotate(lease, {
573
577
  issue: () => this.options.provider.refresh(lease.record.tokens.refreshToken),
574
- accepts: (user) => user.id === lease.record.user.id && user.organization?.id === lease.record.user.organization?.id
578
+ accepts: (user) => user.id === lease.record.user.id && user.tenant?.id === lease.record.user.tenant?.id
575
579
  });
576
580
  return true;
577
581
  }
@@ -581,7 +585,7 @@ var BffSessionManager = class {
581
585
  tokens = await operation.issue();
582
586
  } catch (error) {
583
587
  if (error instanceof AuthyonError && error.status === 429) throw error;
584
- if (error instanceof BffSessionError && error.code === "organization.forbidden") throw error;
588
+ if (error instanceof BffSessionError && error.code === "tenant.forbidden") throw error;
585
589
  if (rejectedCredential(error)) throw error;
586
590
  if (error instanceof BffSessionError && error.clearCookie) throw error;
587
591
  throw new BffSessionError("session.rotation_interrupted", 503, true);
@@ -597,12 +601,15 @@ var BffSessionManager = class {
597
601
  throw new BffSessionError("session.rotation_interrupted", 503, true);
598
602
  }
599
603
  }
600
- async issueOrganizationTokens(accessToken, slug) {
604
+ async issueTenantTokens(accessToken, slug) {
605
+ const provider = this.options.provider;
606
+ const issue = provider.switchTenant ?? provider.switchOrganization;
607
+ if (!issue) throw new BffSessionError("provider.unsupported_operation", 501);
601
608
  try {
602
- return await this.options.provider.switchOrganization(accessToken, slug);
609
+ return await issue.call(provider, accessToken, slug);
603
610
  } catch (error) {
604
611
  if (error instanceof AuthyonError && error.status === 403)
605
- throw new BffSessionError("organization.forbidden", 403);
612
+ throw new BffSessionError("tenant.forbidden", 403);
606
613
  throw error;
607
614
  }
608
615
  }
@@ -613,10 +620,11 @@ var BffSessionManager = class {
613
620
  if (!await this.options.provider.validate(tokens.accessToken))
614
621
  throw new BffSessionError("session.revoked", 401, true);
615
622
  const user = await this.options.provider.profile(tokens.accessToken);
623
+ const tenant = user.tenant ?? user.organization ?? null;
616
624
  return {
617
625
  id: user.id,
618
626
  email: user.email,
619
- organization: user.organization ? { id: user.organization.id, slug: user.organization.slug } : null
627
+ tenant: tenant ? { id: tenant.id, slug: tenant.slug } : null
620
628
  };
621
629
  }
622
630
  async revokeUnstoredTokens(tokens) {
@@ -629,8 +637,12 @@ var BffSessionManager = class {
629
637
  };
630
638
 
631
639
  // src/bff/handlers.ts
640
+ function publicUser(user) {
641
+ const { id, email, tenant } = user;
642
+ return { id, email, tenant, organization: tenant };
643
+ }
632
644
  function publicSession(record) {
633
- return { authenticated: true, user: record.user, expiresAt: record.expiresAt };
645
+ return { authenticated: true, user: publicUser(record.user), expiresAt: record.expiresAt };
634
646
  }
635
647
  function createBffSession(options) {
636
648
  const http = createBffHttp(options);
@@ -658,8 +670,8 @@ function createBffSession(options) {
658
670
  }
659
671
  };
660
672
  }
661
- async function finishLogin(tokens, organizationSlug, sessionPersistence2) {
662
- const { id, record } = await manager.create(tokens, organizationSlug, sessionPersistence2);
673
+ async function finishLogin(tokens, tenantSlug, sessionPersistence2) {
674
+ const { id, record } = await manager.create(tokens, tenantSlug, sessionPersistence2);
663
675
  return http.response(publicSession(record), { cookie: http.cookie(id, record.expiresAt) });
664
676
  }
665
677
  async function assertSignedOut(request) {
@@ -673,6 +685,16 @@ function createBffSession(options) {
673
685
  }
674
686
  throw new BffSessionError("session.already_authenticated", 409);
675
687
  }
688
+ const switchTenant = handler("POST", async (request) => {
689
+ const body = await readBffBody(request);
690
+ const slug = inputString(
691
+ body,
692
+ body.tenantSlug === void 0 && body.organizationSlug !== void 0 ? "organizationSlug" : "tenantSlug"
693
+ );
694
+ return http.response(
695
+ publicSession(await manager.switchTenant(requiredSessionId(request), slug))
696
+ );
697
+ });
676
698
  return {
677
699
  login: handler("POST", async (request) => {
678
700
  await assertSignedOut(request);
@@ -685,7 +707,7 @@ function createBffSession(options) {
685
707
  methods: result.methods
686
708
  });
687
709
  }
688
- return finishLogin(result, input.organizationSlug, input.sessionPersistence);
710
+ return finishLogin(result, input.tenantSlug, input.sessionPersistence);
689
711
  }),
690
712
  verifyTwoFactor: handler("POST", async (request) => {
691
713
  await assertSignedOut(request);
@@ -707,12 +729,10 @@ function createBffSession(options) {
707
729
  "GET",
708
730
  async (request) => http.response(publicSession(await manager.read(requiredSessionId(request))))
709
731
  ),
710
- switchOrganization: handler("POST", async (request) => {
711
- const slug = inputString(await readBffBody(request), "organizationSlug");
712
- return http.response(
713
- publicSession(await manager.switchOrganization(requiredSessionId(request), slug))
714
- );
715
- }),
732
+ /** Body `{ tenantSlug }` (the deprecated `{ organizationSlug }` is still read). */
733
+ switchTenant,
734
+ /** @deprecated Mount `switchTenant`; this is the same handler. */
735
+ switchOrganization: switchTenant,
716
736
  logout: handler("POST", async (request) => {
717
737
  const id = http.sessionId(request);
718
738
  if (id) {
@@ -730,7 +750,7 @@ function createBffSession(options) {
730
750
  const record = await manager.read(requiredSessionId(request));
731
751
  return {
732
752
  accessToken: record.tokens.accessToken,
733
- user: record.user,
753
+ user: publicUser(record.user),
734
754
  expiresAt: record.expiresAt
735
755
  };
736
756
  },
@@ -988,12 +1008,12 @@ function readChallenge(value) {
988
1008
  }
989
1009
  function readProfile(value) {
990
1010
  const user = object(value);
991
- const activeOrganization = user.activeOrganization ?? user.tenant;
992
- const organization = activeOrganization == null ? null : object(activeOrganization);
1011
+ const active = user.tenant ?? user.activeTenant ?? user.activeOrganization;
1012
+ const tenant = active == null ? null : object(active);
993
1013
  return {
994
1014
  id: requiredString(user.id),
995
1015
  email: requiredString(user.email),
996
- organization: organization ? { id: requiredString(organization.id), slug: requiredString(organization.slug) } : null
1016
+ tenant: tenant ? { id: requiredString(tenant.id), slug: requiredString(tenant.slug) } : null
997
1017
  };
998
1018
  }
999
1019
  function createAuthyonBffProvider(options) {
@@ -1009,6 +1029,8 @@ function createAuthyonBffProvider(options) {
1009
1029
  return {
1010
1030
  async login(input) {
1011
1031
  const credentials = { ...input };
1032
+ const tenantSlug = input.tenantSlug ?? input.organizationSlug;
1033
+ delete credentials.tenantSlug;
1012
1034
  delete credentials.organizationSlug;
1013
1035
  delete credentials.sessionPersistence;
1014
1036
  const response = object(
@@ -1017,7 +1039,7 @@ function createAuthyonBffProvider(options) {
1017
1039
  headers,
1018
1040
  body: {
1019
1041
  ...credentials,
1020
- ...input.organizationSlug ? { tenantSlug: input.organizationSlug } : {}
1042
+ ...tenantSlug ? { tenantSlug } : {}
1021
1043
  }
1022
1044
  })
1023
1045
  );
@@ -1045,7 +1067,7 @@ function createAuthyonBffProvider(options) {
1045
1067
  async logout(refreshToken) {
1046
1068
  await http.request("/auth/logout", { method: "POST", headers, body: { refreshToken } });
1047
1069
  },
1048
- async switchOrganization(accessToken, slug) {
1070
+ async switchTenant(accessToken, slug) {
1049
1071
  return readTokens(
1050
1072
  await http.request("/auth/switch-tenant", {
1051
1073
  method: "POST",
@@ -1121,12 +1143,18 @@ function isObject(value) {
1121
1143
  function isFiniteNumber(value) {
1122
1144
  return typeof value === "number" && Number.isFinite(value);
1123
1145
  }
1124
- function isOrganization(value) {
1146
+ function isTenant(value) {
1125
1147
  return value === null || isObject(value) && typeof value.id === "string" && typeof value.slug === "string";
1126
1148
  }
1149
+ function upgradeLegacyRecord(value) {
1150
+ if (!isObject(value) || !isObject(value.user)) return value;
1151
+ if (value.user.tenant !== void 0 || value.user.organization === void 0) return value;
1152
+ const { organization, ...user } = value.user;
1153
+ return { ...value, user: { ...user, tenant: organization } };
1154
+ }
1127
1155
  function isSessionRecord(value) {
1128
1156
  if (!isObject(value) || !isObject(value.tokens) || !isObject(value.user)) return false;
1129
- return typeof value.revision === "string" && typeof value.tokens.accessToken === "string" && typeof value.tokens.refreshToken === "string" && isFiniteNumber(value.tokens.expiresAt) && typeof value.user.id === "string" && typeof value.user.email === "string" && isOrganization(value.user.organization) && (value.sessionPersistence === void 0 || value.sessionPersistence === "standard" || value.sessionPersistence === "remembered") && isFiniteNumber(value.expiresAt) && isFiniteNumber(value.idleExpiresAt) && (value.busyUntil === null || isFiniteNumber(value.busyUntil));
1157
+ return typeof value.revision === "string" && typeof value.tokens.accessToken === "string" && typeof value.tokens.refreshToken === "string" && isFiniteNumber(value.tokens.expiresAt) && typeof value.user.id === "string" && typeof value.user.email === "string" && isTenant(value.user.tenant) && (value.sessionPersistence === void 0 || value.sessionPersistence === "standard" || value.sessionPersistence === "remembered") && isFiniteNumber(value.expiresAt) && isFiniteNumber(value.idleExpiresAt) && (value.busyUntil === null || isFiniteNumber(value.busyUntil));
1130
1158
  }
1131
1159
  function createRedisBffSessionStore(options) {
1132
1160
  if (options.encryptionKey.byteLength !== 32) {
@@ -1145,7 +1173,7 @@ function createRedisBffSessionStore(options) {
1145
1173
  contentEncryptionAlgorithms: ["A256GCM"],
1146
1174
  audience: prefix + key
1147
1175
  });
1148
- const record = payload.session;
1176
+ const record = upgradeLegacyRecord(payload.session);
1149
1177
  if (!isSessionRecord(record) || record.revision !== envelope.revision) {
1150
1178
  throw new Error("invalid session record");
1151
1179
  }
package/dist/bff.d.cts CHANGED
@@ -1,10 +1,13 @@
1
- import { H as HttpAdapter, a as HttpLoggerOptions, A as AuthyonServerClient } from './authyonServerClient-C_iWckru.cjs';
1
+ import { H as HttpAdapter, a as HttpLoggerOptions, A as AuthyonServerClient } from './authyonServerClient-DqY5OtxL.cjs';
2
2
  import 'jose';
3
3
 
4
4
  interface BffLoginInput {
5
5
  email?: string;
6
6
  username?: string;
7
7
  password: string;
8
+ /** Tenant to scope the new session to. */
9
+ tenantSlug?: string;
10
+ /** @deprecated Use `tenantSlug`. */
8
11
  organizationSlug?: string;
9
12
  sessionPersistence?: BffSessionPersistence;
10
13
  }
@@ -32,7 +35,16 @@ interface BffTokens {
32
35
  interface BffUser {
33
36
  id: string;
34
37
  email: string;
35
- organization: {
38
+ /** The tenant the session is scoped to, or `null`. */
39
+ tenant: {
40
+ id: string;
41
+ slug: string;
42
+ } | null;
43
+ /**
44
+ * @deprecated Use `tenant`. Still read from custom providers and still sent
45
+ * to the browser as a mirror of `tenant`; removed in a later release.
46
+ */
47
+ organization?: {
36
48
  id: string;
37
49
  slug: string;
38
50
  } | null;
@@ -52,7 +64,14 @@ interface BffAuthProvider {
52
64
  resendTwoFactorEmail?(challengeToken: string): Promise<void>;
53
65
  refresh(refreshToken: string): Promise<BffTokens>;
54
66
  logout(refreshToken: string): Promise<void>;
55
- switchOrganization(accessToken: string, slug: string): Promise<BffTokens>;
67
+ /**
68
+ * Issues tokens scoped to another tenant. Optional only so providers written
69
+ * against the old name keep working: one of `switchTenant` or the deprecated
70
+ * `switchOrganization` must exist, or switching answers 501.
71
+ */
72
+ switchTenant?(accessToken: string, slug: string): Promise<BffTokens>;
73
+ /** @deprecated Implement `switchTenant`. */
74
+ switchOrganization?(accessToken: string, slug: string): Promise<BffTokens>;
56
75
  validate(accessToken: string): Promise<boolean>;
57
76
  profile(accessToken: string): Promise<BffUser>;
58
77
  }
@@ -98,12 +117,26 @@ declare function createBffSession(options: BffSessionOptions): {
98
117
  /** Same guards as `verifyTwoFactor`; never creates a session or sets a cookie. */
99
118
  resendTwoFactorEmail: (request: Request) => Promise<Response>;
100
119
  session: (request: Request) => Promise<Response>;
120
+ /** Body `{ tenantSlug }` (the deprecated `{ organizationSlug }` is still read). */
121
+ switchTenant: (request: Request) => Promise<Response>;
122
+ /** @deprecated Mount `switchTenant`; this is the same handler. */
101
123
  switchOrganization: (request: Request) => Promise<Response>;
102
124
  logout: (request: Request) => Promise<Response>;
103
125
  /** Server-only credential for downstream calls. Never serialize this result to the browser. */
104
126
  requireSession(request: Request): Promise<{
105
127
  accessToken: string;
106
- user: BffUser;
128
+ user: {
129
+ id: string;
130
+ email: string;
131
+ tenant: {
132
+ id: string;
133
+ slug: string;
134
+ } | null;
135
+ organization: {
136
+ id: string;
137
+ slug: string;
138
+ } | null;
139
+ };
107
140
  expiresAt: number;
108
141
  }>;
109
142
  errorResponse: (error: unknown) => Response;
package/dist/bff.d.ts CHANGED
@@ -1,10 +1,13 @@
1
- import { H as HttpAdapter, a as HttpLoggerOptions, A as AuthyonServerClient } from './authyonServerClient-C_iWckru.js';
1
+ import { H as HttpAdapter, a as HttpLoggerOptions, A as AuthyonServerClient } from './authyonServerClient-DqY5OtxL.js';
2
2
  import 'jose';
3
3
 
4
4
  interface BffLoginInput {
5
5
  email?: string;
6
6
  username?: string;
7
7
  password: string;
8
+ /** Tenant to scope the new session to. */
9
+ tenantSlug?: string;
10
+ /** @deprecated Use `tenantSlug`. */
8
11
  organizationSlug?: string;
9
12
  sessionPersistence?: BffSessionPersistence;
10
13
  }
@@ -32,7 +35,16 @@ interface BffTokens {
32
35
  interface BffUser {
33
36
  id: string;
34
37
  email: string;
35
- organization: {
38
+ /** The tenant the session is scoped to, or `null`. */
39
+ tenant: {
40
+ id: string;
41
+ slug: string;
42
+ } | null;
43
+ /**
44
+ * @deprecated Use `tenant`. Still read from custom providers and still sent
45
+ * to the browser as a mirror of `tenant`; removed in a later release.
46
+ */
47
+ organization?: {
36
48
  id: string;
37
49
  slug: string;
38
50
  } | null;
@@ -52,7 +64,14 @@ interface BffAuthProvider {
52
64
  resendTwoFactorEmail?(challengeToken: string): Promise<void>;
53
65
  refresh(refreshToken: string): Promise<BffTokens>;
54
66
  logout(refreshToken: string): Promise<void>;
55
- switchOrganization(accessToken: string, slug: string): Promise<BffTokens>;
67
+ /**
68
+ * Issues tokens scoped to another tenant. Optional only so providers written
69
+ * against the old name keep working: one of `switchTenant` or the deprecated
70
+ * `switchOrganization` must exist, or switching answers 501.
71
+ */
72
+ switchTenant?(accessToken: string, slug: string): Promise<BffTokens>;
73
+ /** @deprecated Implement `switchTenant`. */
74
+ switchOrganization?(accessToken: string, slug: string): Promise<BffTokens>;
56
75
  validate(accessToken: string): Promise<boolean>;
57
76
  profile(accessToken: string): Promise<BffUser>;
58
77
  }
@@ -98,12 +117,26 @@ declare function createBffSession(options: BffSessionOptions): {
98
117
  /** Same guards as `verifyTwoFactor`; never creates a session or sets a cookie. */
99
118
  resendTwoFactorEmail: (request: Request) => Promise<Response>;
100
119
  session: (request: Request) => Promise<Response>;
120
+ /** Body `{ tenantSlug }` (the deprecated `{ organizationSlug }` is still read). */
121
+ switchTenant: (request: Request) => Promise<Response>;
122
+ /** @deprecated Mount `switchTenant`; this is the same handler. */
101
123
  switchOrganization: (request: Request) => Promise<Response>;
102
124
  logout: (request: Request) => Promise<Response>;
103
125
  /** Server-only credential for downstream calls. Never serialize this result to the browser. */
104
126
  requireSession(request: Request): Promise<{
105
127
  accessToken: string;
106
- user: BffUser;
128
+ user: {
129
+ id: string;
130
+ email: string;
131
+ tenant: {
132
+ id: string;
133
+ slug: string;
134
+ } | null;
135
+ organization: {
136
+ id: string;
137
+ slug: string;
138
+ } | null;
139
+ };
107
140
  expiresAt: number;
108
141
  }>;
109
142
  errorResponse: (error: unknown) => Response;
package/dist/bff.js CHANGED
@@ -147,7 +147,7 @@ function loginInput(body) {
147
147
  return {
148
148
  password: inputString(body, "password"),
149
149
  ...body.email !== void 0 ? { email: inputString(body, "email") } : { username: inputString(body, "username") },
150
- ...body.organizationSlug !== void 0 ? { organizationSlug: inputString(body, "organizationSlug") } : {},
150
+ ...body.tenantSlug !== void 0 ? { tenantSlug: inputString(body, "tenantSlug") } : body.organizationSlug !== void 0 ? { tenantSlug: inputString(body, "organizationSlug") } : {},
151
151
  sessionPersistence: sessionPersistence(body)
152
152
  };
153
153
  }
@@ -295,11 +295,11 @@ var BffSessionManager = class {
295
295
  });
296
296
  this.refreshAhead = positiveDuration(options.refreshAheadMs ?? DEFAULT_REFRESH_AHEAD_MS);
297
297
  }
298
- async create(tokens, organizationSlug, sessionPersistence2 = "standard") {
298
+ async create(tokens, tenantSlug, sessionPersistence2 = "standard") {
299
299
  try {
300
300
  const user = await this.verifiedProfile(tokens);
301
- if (organizationSlug !== void 0 && user.organization?.slug !== organizationSlug) {
302
- throw new BffSessionError("session.organization_mismatch", 502);
301
+ if (tenantSlug !== void 0 && user.tenant?.slug !== tenantSlug) {
302
+ throw new BffSessionError("session.tenant_mismatch", 502);
303
303
  }
304
304
  const lifetime = this.sessionLifetime(sessionPersistence2);
305
305
  const record = {
@@ -320,15 +320,19 @@ var BffSessionManager = class {
320
320
  async read(id) {
321
321
  return this.readCurrent(id, 0);
322
322
  }
323
+ /** @deprecated Use `switchTenant`. */
323
324
  async switchOrganization(id, slug) {
325
+ return this.switchTenant(id, slug);
326
+ }
327
+ async switchTenant(id, slug) {
324
328
  return this.update(id, async (lease) => {
325
329
  const refreshed = await this.refreshIfNeeded(lease);
326
330
  if (!refreshed && !await this.options.provider.validate(lease.record.tokens.accessToken)) {
327
331
  throw new BffSessionError("session.revoked", 401, true);
328
332
  }
329
333
  await this.rotate(lease, {
330
- issue: () => this.issueOrganizationTokens(lease.record.tokens.accessToken, slug),
331
- accepts: (user) => user.id === lease.record.user.id && user.organization?.slug === slug
334
+ issue: () => this.issueTenantTokens(lease.record.tokens.accessToken, slug),
335
+ accepts: (user) => user.id === lease.record.user.id && user.tenant?.slug === slug
332
336
  });
333
337
  });
334
338
  }
@@ -411,7 +415,7 @@ var BffSessionManager = class {
411
415
  if (!this.needsRefresh(lease.record)) return false;
412
416
  await this.rotate(lease, {
413
417
  issue: () => this.options.provider.refresh(lease.record.tokens.refreshToken),
414
- accepts: (user) => user.id === lease.record.user.id && user.organization?.id === lease.record.user.organization?.id
418
+ accepts: (user) => user.id === lease.record.user.id && user.tenant?.id === lease.record.user.tenant?.id
415
419
  });
416
420
  return true;
417
421
  }
@@ -421,7 +425,7 @@ var BffSessionManager = class {
421
425
  tokens = await operation.issue();
422
426
  } catch (error) {
423
427
  if (error instanceof AuthyonError && error.status === 429) throw error;
424
- if (error instanceof BffSessionError && error.code === "organization.forbidden") throw error;
428
+ if (error instanceof BffSessionError && error.code === "tenant.forbidden") throw error;
425
429
  if (rejectedCredential(error)) throw error;
426
430
  if (error instanceof BffSessionError && error.clearCookie) throw error;
427
431
  throw new BffSessionError("session.rotation_interrupted", 503, true);
@@ -437,12 +441,15 @@ var BffSessionManager = class {
437
441
  throw new BffSessionError("session.rotation_interrupted", 503, true);
438
442
  }
439
443
  }
440
- async issueOrganizationTokens(accessToken, slug) {
444
+ async issueTenantTokens(accessToken, slug) {
445
+ const provider = this.options.provider;
446
+ const issue = provider.switchTenant ?? provider.switchOrganization;
447
+ if (!issue) throw new BffSessionError("provider.unsupported_operation", 501);
441
448
  try {
442
- return await this.options.provider.switchOrganization(accessToken, slug);
449
+ return await issue.call(provider, accessToken, slug);
443
450
  } catch (error) {
444
451
  if (error instanceof AuthyonError && error.status === 403)
445
- throw new BffSessionError("organization.forbidden", 403);
452
+ throw new BffSessionError("tenant.forbidden", 403);
446
453
  throw error;
447
454
  }
448
455
  }
@@ -453,10 +460,11 @@ var BffSessionManager = class {
453
460
  if (!await this.options.provider.validate(tokens.accessToken))
454
461
  throw new BffSessionError("session.revoked", 401, true);
455
462
  const user = await this.options.provider.profile(tokens.accessToken);
463
+ const tenant = user.tenant ?? user.organization ?? null;
456
464
  return {
457
465
  id: user.id,
458
466
  email: user.email,
459
- organization: user.organization ? { id: user.organization.id, slug: user.organization.slug } : null
467
+ tenant: tenant ? { id: tenant.id, slug: tenant.slug } : null
460
468
  };
461
469
  }
462
470
  async revokeUnstoredTokens(tokens) {
@@ -469,8 +477,12 @@ var BffSessionManager = class {
469
477
  };
470
478
 
471
479
  // src/bff/handlers.ts
480
+ function publicUser(user) {
481
+ const { id, email, tenant } = user;
482
+ return { id, email, tenant, organization: tenant };
483
+ }
472
484
  function publicSession(record) {
473
- return { authenticated: true, user: record.user, expiresAt: record.expiresAt };
485
+ return { authenticated: true, user: publicUser(record.user), expiresAt: record.expiresAt };
474
486
  }
475
487
  function createBffSession(options) {
476
488
  const http = createBffHttp(options);
@@ -498,8 +510,8 @@ function createBffSession(options) {
498
510
  }
499
511
  };
500
512
  }
501
- async function finishLogin(tokens, organizationSlug, sessionPersistence2) {
502
- const { id, record } = await manager.create(tokens, organizationSlug, sessionPersistence2);
513
+ async function finishLogin(tokens, tenantSlug, sessionPersistence2) {
514
+ const { id, record } = await manager.create(tokens, tenantSlug, sessionPersistence2);
503
515
  return http.response(publicSession(record), { cookie: http.cookie(id, record.expiresAt) });
504
516
  }
505
517
  async function assertSignedOut(request) {
@@ -513,6 +525,16 @@ function createBffSession(options) {
513
525
  }
514
526
  throw new BffSessionError("session.already_authenticated", 409);
515
527
  }
528
+ const switchTenant = handler("POST", async (request) => {
529
+ const body = await readBffBody(request);
530
+ const slug = inputString(
531
+ body,
532
+ body.tenantSlug === void 0 && body.organizationSlug !== void 0 ? "organizationSlug" : "tenantSlug"
533
+ );
534
+ return http.response(
535
+ publicSession(await manager.switchTenant(requiredSessionId(request), slug))
536
+ );
537
+ });
516
538
  return {
517
539
  login: handler("POST", async (request) => {
518
540
  await assertSignedOut(request);
@@ -525,7 +547,7 @@ function createBffSession(options) {
525
547
  methods: result.methods
526
548
  });
527
549
  }
528
- return finishLogin(result, input.organizationSlug, input.sessionPersistence);
550
+ return finishLogin(result, input.tenantSlug, input.sessionPersistence);
529
551
  }),
530
552
  verifyTwoFactor: handler("POST", async (request) => {
531
553
  await assertSignedOut(request);
@@ -547,12 +569,10 @@ function createBffSession(options) {
547
569
  "GET",
548
570
  async (request) => http.response(publicSession(await manager.read(requiredSessionId(request))))
549
571
  ),
550
- switchOrganization: handler("POST", async (request) => {
551
- const slug = inputString(await readBffBody(request), "organizationSlug");
552
- return http.response(
553
- publicSession(await manager.switchOrganization(requiredSessionId(request), slug))
554
- );
555
- }),
572
+ /** Body `{ tenantSlug }` (the deprecated `{ organizationSlug }` is still read). */
573
+ switchTenant,
574
+ /** @deprecated Mount `switchTenant`; this is the same handler. */
575
+ switchOrganization: switchTenant,
556
576
  logout: handler("POST", async (request) => {
557
577
  const id = http.sessionId(request);
558
578
  if (id) {
@@ -570,7 +590,7 @@ function createBffSession(options) {
570
590
  const record = await manager.read(requiredSessionId(request));
571
591
  return {
572
592
  accessToken: record.tokens.accessToken,
573
- user: record.user,
593
+ user: publicUser(record.user),
574
594
  expiresAt: record.expiresAt
575
595
  };
576
596
  },
@@ -616,12 +636,12 @@ function readChallenge(value) {
616
636
  }
617
637
  function readProfile(value) {
618
638
  const user = object(value);
619
- const activeOrganization = user.activeOrganization ?? user.tenant;
620
- const organization = activeOrganization == null ? null : object(activeOrganization);
639
+ const active = user.tenant ?? user.activeTenant ?? user.activeOrganization;
640
+ const tenant = active == null ? null : object(active);
621
641
  return {
622
642
  id: requiredString(user.id),
623
643
  email: requiredString(user.email),
624
- organization: organization ? { id: requiredString(organization.id), slug: requiredString(organization.slug) } : null
644
+ tenant: tenant ? { id: requiredString(tenant.id), slug: requiredString(tenant.slug) } : null
625
645
  };
626
646
  }
627
647
  function createAuthyonBffProvider(options) {
@@ -637,6 +657,8 @@ function createAuthyonBffProvider(options) {
637
657
  return {
638
658
  async login(input) {
639
659
  const credentials = { ...input };
660
+ const tenantSlug = input.tenantSlug ?? input.organizationSlug;
661
+ delete credentials.tenantSlug;
640
662
  delete credentials.organizationSlug;
641
663
  delete credentials.sessionPersistence;
642
664
  const response = object(
@@ -645,7 +667,7 @@ function createAuthyonBffProvider(options) {
645
667
  headers,
646
668
  body: {
647
669
  ...credentials,
648
- ...input.organizationSlug ? { tenantSlug: input.organizationSlug } : {}
670
+ ...tenantSlug ? { tenantSlug } : {}
649
671
  }
650
672
  })
651
673
  );
@@ -673,7 +695,7 @@ function createAuthyonBffProvider(options) {
673
695
  async logout(refreshToken) {
674
696
  await http.request("/auth/logout", { method: "POST", headers, body: { refreshToken } });
675
697
  },
676
- async switchOrganization(accessToken, slug) {
698
+ async switchTenant(accessToken, slug) {
677
699
  return readTokens(
678
700
  await http.request("/auth/switch-tenant", {
679
701
  method: "POST",
@@ -749,12 +771,18 @@ function isObject(value) {
749
771
  function isFiniteNumber(value) {
750
772
  return typeof value === "number" && Number.isFinite(value);
751
773
  }
752
- function isOrganization(value) {
774
+ function isTenant(value) {
753
775
  return value === null || isObject(value) && typeof value.id === "string" && typeof value.slug === "string";
754
776
  }
777
+ function upgradeLegacyRecord(value) {
778
+ if (!isObject(value) || !isObject(value.user)) return value;
779
+ if (value.user.tenant !== void 0 || value.user.organization === void 0) return value;
780
+ const { organization, ...user } = value.user;
781
+ return { ...value, user: { ...user, tenant: organization } };
782
+ }
755
783
  function isSessionRecord(value) {
756
784
  if (!isObject(value) || !isObject(value.tokens) || !isObject(value.user)) return false;
757
- return typeof value.revision === "string" && typeof value.tokens.accessToken === "string" && typeof value.tokens.refreshToken === "string" && isFiniteNumber(value.tokens.expiresAt) && typeof value.user.id === "string" && typeof value.user.email === "string" && isOrganization(value.user.organization) && (value.sessionPersistence === void 0 || value.sessionPersistence === "standard" || value.sessionPersistence === "remembered") && isFiniteNumber(value.expiresAt) && isFiniteNumber(value.idleExpiresAt) && (value.busyUntil === null || isFiniteNumber(value.busyUntil));
785
+ return typeof value.revision === "string" && typeof value.tokens.accessToken === "string" && typeof value.tokens.refreshToken === "string" && isFiniteNumber(value.tokens.expiresAt) && typeof value.user.id === "string" && typeof value.user.email === "string" && isTenant(value.user.tenant) && (value.sessionPersistence === void 0 || value.sessionPersistence === "standard" || value.sessionPersistence === "remembered") && isFiniteNumber(value.expiresAt) && isFiniteNumber(value.idleExpiresAt) && (value.busyUntil === null || isFiniteNumber(value.busyUntil));
758
786
  }
759
787
  function createRedisBffSessionStore(options) {
760
788
  if (options.encryptionKey.byteLength !== 32) {
@@ -773,7 +801,7 @@ function createRedisBffSessionStore(options) {
773
801
  contentEncryptionAlgorithms: ["A256GCM"],
774
802
  audience: prefix + key
775
803
  });
776
- const record = payload.session;
804
+ const record = upgradeLegacyRecord(payload.session);
777
805
  if (!isSessionRecord(record) || record.revision !== envelope.revision) {
778
806
  throw new Error("invalid session record");
779
807
  }