@authyon/auth 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/index.js CHANGED
@@ -481,6 +481,38 @@ function memberQuery(options = {}) {
481
481
  var ORDER_FIELDS = ["name", "createdAt", "updatedAt"];
482
482
  var MAX_FILTERS = 10;
483
483
  var MAX_FILTER_LENGTH = 512;
484
+ var MAX_PERMISSIONS = 10;
485
+ var MAX_PERMISSION_LENGTH = 256;
486
+ function permissionQuery(options = {}) {
487
+ const { permission, permissionMatch } = options;
488
+ const query = {};
489
+ if (permissionMatch !== void 0) {
490
+ if (permissionMatch !== "all" && permissionMatch !== "any")
491
+ throw new RangeError('Authyon: permissionMatch must be "all" or "any".');
492
+ query.permissionMatch = permissionMatch;
493
+ }
494
+ if (permission === void 0) return query;
495
+ if (!Array.isArray(permission)) throw new RangeError("Authyon: permission must be an array.");
496
+ const values = permission.map((p) => typeof p === "string" ? p.trim() : "");
497
+ if (values.some((p) => p.length === 0))
498
+ throw new RangeError("Authyon: a permission value may not be empty.");
499
+ if (values.length > MAX_PERMISSIONS)
500
+ throw new RangeError(`Authyon: at most ${MAX_PERMISSIONS} permission values per request.`);
501
+ for (const p of values) {
502
+ if (p.length > MAX_PERMISSION_LENGTH)
503
+ throw new RangeError(
504
+ `Authyon: a permission holds at most ${MAX_PERMISSION_LENGTH} characters.`
505
+ );
506
+ if (p.includes("*"))
507
+ throw new RangeError(`Authyon: name a concrete permission, not a wildcard (got "${p}").`);
508
+ }
509
+ if (values.length > 0) query.permission = values;
510
+ return query;
511
+ }
512
+ function tenantListQuery(options = {}) {
513
+ const { permission, permissionMatch, ...page } = options;
514
+ return { ...memberQuery(page), ...permissionQuery({ permission, permissionMatch }) };
515
+ }
484
516
  function orderByParam(order) {
485
517
  if (order === void 0) return void 0;
486
518
  const field = typeof order === "string" ? order : order?.field;
@@ -492,8 +524,11 @@ function orderByParam(order) {
492
524
  return `${field}:${direction}`;
493
525
  }
494
526
  function subTenantQuery(options = {}) {
495
- const { filter, orderBy, ...page } = options;
496
- const query = { ...memberQuery(page) };
527
+ const { filter, orderBy, permission, permissionMatch, ...page } = options;
528
+ const query = {
529
+ ...memberQuery(page),
530
+ ...permissionQuery({ permission, permissionMatch })
531
+ };
497
532
  if (filter !== void 0) {
498
533
  if (!Array.isArray(filter)) throw new RangeError("Authyon: filter must be an array.");
499
534
  const present = filter.filter((f) => typeof f === "string" && f.trim().length > 0);
@@ -516,6 +551,15 @@ function subTenantQuery(options = {}) {
516
551
  return query;
517
552
  }
518
553
 
554
+ // ../../internal/core/http/tenantLookup.ts
555
+ var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iu;
556
+ function tenantLookupPath(idOrSlug) {
557
+ const value = typeof idOrSlug === "string" ? idOrSlug.trim() : "";
558
+ if (!value) throw new RangeError("Authyon: a tenant id or slug is required.");
559
+ if (value.length > 64) throw new RangeError("Authyon: a tenant slug has at most 64 characters.");
560
+ return UUID.test(value) ? `/auth/tenants/${encodeURIComponent(value)}` : `/auth/tenants/by-slug/${encodeURIComponent(value.toLowerCase())}`;
561
+ }
562
+
519
563
  // src/client/authyonClient.ts
520
564
  var FALLBACK_EXPIRES_IN = 1800;
521
565
  function readTokens(raw) {
@@ -624,54 +668,75 @@ var AuthyonClient = class {
624
668
  return { redirectUri: done?.redirectUri ?? null };
625
669
  }
626
670
  };
627
- // ── Organization ─────────────────────────────────────────────────────────
628
- this.organization = {
671
+ // ── Tenant ─────────────────────────────────────────────────────────
672
+ this.tenants = {
629
673
  /**
630
- * GET /auth/tenants — filterable list of organization memberships.
674
+ * GET /auth/tenants — filterable list of tenant memberships.
631
675
  *
632
- * Also lists the organizations below them that the user may switch into
676
+ * Also lists the tenants below them that the user may switch into
633
677
  * without being a member, marked `inherited` — only the ones that actually
634
- * carry `subtenants:tree:switch`, so the switcher never offers an
635
- * organization the switch would then refuse. `depth` indents the list into
678
+ * carry `subtenants:tree:switch`, so the switcher never offers a
679
+ * tenant the switch would then refuse. `depth` indents the list into
636
680
  * the tree it describes.
681
+ *
682
+ * `search`, `skip`/`take` and the permission filter (`permission`,
683
+ * `permissionMatch`) narrow it — see `TenantListParams`. The answer stays
684
+ * a plain array. A bad option raises `RangeError` before any request.
685
+ * APIs older than 2026-11-27 ignored every option and always answered the
686
+ * whole list.
637
687
  */
638
- list: (params = {}) => this.request(appendQuery("/auth/tenants", params), { bearer: true }),
688
+ list: (params = {}) => this.request(appendQuery("/auth/tenants", tenantListQuery(params)), { bearer: true }),
639
689
  /**
640
- * POST /auth/tenants — creates an organization owned by the signed-in
641
- * user (only available when self-service organization creation is
690
+ * POST /auth/tenants — creates a tenant owned by the signed-in
691
+ * user (only available when self-service tenant creation is
642
692
  * enabled for the environment).
643
693
  *
644
694
  * `parentTenantId` nests it under one the user already belongs to, which
645
695
  * needs `subtenants:tree:create` there. The creator lands as the effective
646
696
  * owner either way, with `tenants:settings:manage`,
647
697
  * `tenants:members:invite` and `tenants:members:remove` granted directly
648
- * inside the new organization.
698
+ * inside the new tenant.
649
699
  */
650
700
  create: (params = {}) => this.request("/auth/tenants", { method: "POST", bearer: true, body: params }),
651
- /** GET /auth/tenants/{organizationId} — fetch one of the user's organizations by id. */
652
- get: (organizationId) => this.request(`/auth/tenants/${encodeURIComponent(organizationId)}`, { bearer: true }),
653
701
  /**
654
- * PATCH /auth/tenants/{organizationId} — renames the organization.
702
+ * GET /auth/tenants/{tenantId} — one tenant by id: the user's own,
703
+ * or one below it with `subtenants:tree:read` on an ancestor.
704
+ */
705
+ get: (tenantId) => this.request(`/auth/tenants/${encodeURIComponent(tenantId)}`, { bearer: true }),
706
+ /**
707
+ * One tenant by id **or** slug — a UUID goes to `GET /auth/tenants/{id}`,
708
+ * anything else to `GET /auth/tenants/by-slug/{slug}`. Same access rule and
709
+ * same answer as `get`, so a page that only has the slug from its URL
710
+ * doesn't have to page `subTenants.list` to find it.
711
+ *
712
+ * By slug, every refusal answers `tenant.not_found` (a slug is guessable,
713
+ * so "exists, not yours" is never told apart from "doesn't exist"). An
714
+ * empty value raises `RangeError` before any request. The slug route needs
715
+ * the Authyon API of 2026-11-26 or newer.
716
+ */
717
+ find: (idOrSlug) => this.request(tenantLookupPath(idOrSlug), { bearer: true }),
718
+ /**
719
+ * PATCH /auth/tenants/{tenantId} — renames the tenant.
655
720
  *
656
721
  * Requires the `tenants:settings:manage` custom permission on it, or
657
- * `subtenants:tree:manage` on an organization above it. The value was
722
+ * `subtenants:tree:manage` on a tenant above it. The value was
658
723
  * spelled `tenants:manage` before 2026-10-31 — two segments, which the API
659
724
  * could not store, so the endpoint refused everyone who had been granted
660
725
  * it.
661
726
  */
662
- rename: (organizationId, name) => this.request(`/auth/tenants/${encodeURIComponent(organizationId)}`, {
727
+ rename: (tenantId, name) => this.request(`/auth/tenants/${encodeURIComponent(tenantId)}`, {
663
728
  method: "PATCH",
664
729
  bearer: true,
665
730
  body: { name }
666
731
  }),
667
732
  /**
668
733
  * POST /auth/switch-tenant — issues a fresh token scoped to the new
669
- * organization.
734
+ * tenant.
670
735
  *
671
- * Membership is enough for one of the user's own organizations. Descending
672
- * into one below it takes `subtenants:tree:switch` at the organization they
736
+ * Membership is enough for one of the user's own tenants. Descending
737
+ * into one below it takes `subtenants:tree:switch` at the tenant they
673
738
  * belong to — deliberately not the same permission as administering a
674
- * sub-organization from the outside, since operating AS it is the larger of
739
+ * sub-tenant from the outside, since operating AS it is the larger of
675
740
  * the two powers. The grants then travel from the node the user belongs to,
676
741
  * and the token says so in its `tenant.via`.
677
742
  *
@@ -679,61 +744,58 @@ var AuthyonClient = class {
679
744
  * `tenant.ancestor_disabled`, and `tenant.path_stale` when the tree moved
680
745
  * under a session — sign-in again rather than retrying.
681
746
  */
682
- switch: (organizationSlug) => this.request("/auth/switch-tenant", {
747
+ switch: (tenantSlug) => this.request("/auth/switch-tenant", {
683
748
  method: "POST",
684
749
  bearer: true,
685
- body: { tenantSlug: organizationSlug }
750
+ body: { tenantSlug }
686
751
  }).then((data) => this.setSession({ tokens: readTokens(data) }, "refreshed")).then((session) => this.hydrateUser(session)),
687
- /** The organization the current session is scoped to, from the cached session — no network call. */
688
- current: () => this.getSession()?.user?.activeOrganization ?? null,
752
+ /** The tenant the current session is scoped to, from the cached session — no network call. */
753
+ current: () => this.getSession()?.user?.activeTenant ?? null,
689
754
  members: {
690
755
  /**
691
- * GET /auth/tenants/{organizationId}/members — paginated list of an
692
- * organization's members, newest first, in the `Paged<T>` envelope every
756
+ * GET /auth/tenants/{tenantId}/members — paginated list of an
757
+ * tenant's members, newest first, in the `Paged<T>` envelope every
693
758
  * other `skip`/`take` endpoint returns. `search` matches email, username
694
759
  * or name; at most 100 characters, and `take` 1-100, checked before any
695
760
  * request.
696
761
  */
697
- list: (organizationId, params = {}) => this.request(
698
- appendQuery(
699
- `/auth/tenants/${encodeURIComponent(organizationId)}/members`,
700
- memberQuery(params)
701
- ),
762
+ list: (tenantId, params = {}) => this.request(
763
+ appendQuery(`/auth/tenants/${encodeURIComponent(tenantId)}/members`, memberQuery(params)),
702
764
  { bearer: true }
703
765
  ),
704
766
  /**
705
- * POST /auth/tenants/{organizationId}/members — add an existing
767
+ * POST /auth/tenants/{tenantId}/members — add an existing
706
768
  * environment user by e-mail (no e-mail is sent). To invite an address
707
- * that may not have an account yet, use `organization.invites.create`.
769
+ * that may not have an account yet, use `tenants.invites.create`.
708
770
  */
709
- invite: (organizationId, params) => this.request(`/auth/tenants/${encodeURIComponent(organizationId)}/members`, {
771
+ invite: (tenantId, params) => this.request(`/auth/tenants/${encodeURIComponent(tenantId)}/members`, {
710
772
  method: "POST",
711
773
  bearer: true,
712
774
  body: params
713
775
  }),
714
- /** DELETE /auth/tenants/{organizationId}/members/{userId} — remove a member. */
715
- remove: (organizationId, userId) => this.request(
716
- `/auth/tenants/${encodeURIComponent(organizationId)}/members/${encodeURIComponent(userId)}`,
776
+ /** DELETE /auth/tenants/{tenantId}/members/{userId} — remove a member. */
777
+ remove: (tenantId, userId) => this.request(
778
+ `/auth/tenants/${encodeURIComponent(tenantId)}/members/${encodeURIComponent(userId)}`,
717
779
  { method: "DELETE", bearer: true }
718
780
  )
719
781
  },
720
782
  /**
721
783
  * Invites by e-mail or by link. Managing them requires the
722
- * `tenants:members:invite` permission on the organization; `preview` and
784
+ * `tenants:members:invite` permission on the tenant; `preview` and
723
785
  * `accept` are public — the token in the link is the credential.
724
786
  */
725
787
  invites: {
726
- /** GET /auth/tenants/{organizationId}/invites — every invite with its status, newest first. */
727
- list: (organizationId) => this.request(`/auth/tenants/${encodeURIComponent(organizationId)}/invites`, {
788
+ /** GET /auth/tenants/{tenantId}/invites — every invite with its status, newest first. */
789
+ list: (tenantId) => this.request(`/auth/tenants/${encodeURIComponent(tenantId)}/invites`, {
728
790
  bearer: true
729
791
  }),
730
792
  /**
731
- * POST /auth/tenants/{organizationId}/invites — invite an address.
793
+ * POST /auth/tenants/{tenantId}/invites — invite an address.
732
794
  * Always returns `acceptUrl`; with `sendEmail: false` Authyon does not
733
795
  * mail it and you deliver the link yourself. The link is valid for 7
734
796
  * days, once, and can't be read again later.
735
797
  */
736
- create: (organizationId, params) => this.request(`/auth/tenants/${encodeURIComponent(organizationId)}/invites`, {
798
+ create: (tenantId, params) => this.request(`/auth/tenants/${encodeURIComponent(tenantId)}/invites`, {
737
799
  method: "POST",
738
800
  bearer: true,
739
801
  body: {
@@ -744,17 +806,17 @@ var AuthyonClient = class {
744
806
  }
745
807
  }),
746
808
  /**
747
- * POST /auth/tenants/{organizationId}/invites/{inviteId}/resend — issue
809
+ * POST /auth/tenants/{tenantId}/invites/{inviteId}/resend — issue
748
810
  * a new link (the previous one stops working), mailing it unless
749
811
  * `sendEmail` is `false`.
750
812
  */
751
- resend: (organizationId, inviteId, params = {}) => this.request(
752
- `/auth/tenants/${encodeURIComponent(organizationId)}/invites/${encodeURIComponent(inviteId)}/resend`,
813
+ resend: (tenantId, inviteId, params = {}) => this.request(
814
+ `/auth/tenants/${encodeURIComponent(tenantId)}/invites/${encodeURIComponent(inviteId)}/resend`,
753
815
  { method: "POST", bearer: true, body: { sendEmail: params.sendEmail ?? true } }
754
816
  ),
755
- /** DELETE /auth/tenants/{organizationId}/invites/{inviteId} — revoke a pending invite. */
756
- revoke: (organizationId, inviteId) => this.request(
757
- `/auth/tenants/${encodeURIComponent(organizationId)}/invites/${encodeURIComponent(inviteId)}`,
817
+ /** DELETE /auth/tenants/{tenantId}/invites/{inviteId} — revoke a pending invite. */
818
+ revoke: (tenantId, inviteId) => this.request(
819
+ `/auth/tenants/${encodeURIComponent(tenantId)}/invites/${encodeURIComponent(inviteId)}`,
758
820
  { method: "DELETE", bearer: true }
759
821
  ),
760
822
  /**
@@ -764,41 +826,41 @@ var AuthyonClient = class {
764
826
  */
765
827
  preview: (token) => this.request("/auth/tenant-invites/preview", { method: "POST", body: { token } }),
766
828
  /**
767
- * POST /auth/tenant-invites/accept — public. Joins the organization; for
829
+ * POST /auth/tenant-invites/accept — public. Joins the tenant; for
768
830
  * an address with no account, also creates it (password required) with
769
831
  * the e-mail already confirmed. Does not sign the user in.
770
832
  */
771
833
  accept: (params) => this.request("/auth/tenant-invites/accept", { method: "POST", body: params })
772
834
  },
773
835
  /**
774
- * The direct, active sub-organizations of an organization. One level per
836
+ * The direct, active sub-tenants of a tenant. One level per
775
837
  * call: each row's `childCount` says where the next call is worth making,
776
838
  * which is what a lazily expanded tree asks for.
777
839
  *
778
- * Needs `subtenants:tree:read` on the organization or on one above it —
779
- * even on the user's own organization, since listing the children is
840
+ * Needs `subtenants:tree:read` on the tenant or on one above it —
841
+ * even on the user's own tenant, since listing the children is
780
842
  * reading below it. Paged like every other `skip`/`take` listing, with
781
843
  * `search` (name, slug, description and public metadata), `filter` (public
782
844
  * scope only — build them with `tenantFilter`) and `orderBy`
783
845
  * (`name` | `createdAt` | `updatedAt`, ascending or descending). A bad
784
846
  * option raises `RangeError` before any request.
785
847
  *
786
- * Not the same list as `organization.list()`, which is the switcher's and
848
+ * Not the same list as `tenants.list()`, which is the switcher's and
787
849
  * only brings what `subtenants:tree:switch` lets the user enter. Requires
788
850
  * the Authyon API of 2026-11-26 or newer.
789
851
  */
790
- subOrganizations: {
791
- /** GET /auth/tenants/{organizationId}/subtenants. */
792
- list: (organizationId, params = {}) => this.request(
852
+ subTenants: {
853
+ /** GET /auth/tenants/{tenantId}/subtenants. */
854
+ list: (tenantId, params = {}) => this.request(
793
855
  appendQuery(
794
- `/auth/tenants/${encodeURIComponent(organizationId)}/subtenants`,
856
+ `/auth/tenants/${encodeURIComponent(tenantId)}/subtenants`,
795
857
  subTenantQuery(params)
796
858
  ),
797
859
  { bearer: true }
798
860
  ),
799
861
  /**
800
- * GET /auth/tenants/subtenants — the same, for the organization the
801
- * session is scoped to (`organization.current()`, the token's
862
+ * GET /auth/tenants/subtenants — the same, for the tenant the
863
+ * session is scoped to (`tenants.current()`, the token's
802
864
  * `tenant_id`). Same permission rule; answers `tenant.no_active_tenant`
803
865
  * when the session never switched into one.
804
866
  */
@@ -807,12 +869,19 @@ var AuthyonClient = class {
807
869
  })
808
870
  },
809
871
  roles: {
810
- /** GET /auth/tenants/{organizationId}/roles — roles available in the organization. */
811
- list: (organizationId) => this.request(`/auth/tenants/${encodeURIComponent(organizationId)}/roles`, {
872
+ /** GET /auth/tenants/{tenantId}/roles — roles available in the tenant. */
873
+ list: (tenantId) => this.request(`/auth/tenants/${encodeURIComponent(tenantId)}/roles`, {
812
874
  bearer: true
813
875
  })
814
876
  }
815
877
  };
878
+ /**
879
+ * @deprecated Use `tenants` — the same methods, named after the API's own
880
+ * vocabulary (`tenants.subTenants` instead of `subOrganizations`). Kept so
881
+ * code written against 0.2.0-beta.30 and earlier keeps working; removed in a
882
+ * later release.
883
+ */
884
+ this.organization = { ...this.tenants, subOrganizations: this.tenants.subTenants };
816
885
  // ── Two-factor management (authenticated) ────────────────────────────────
817
886
  this.twoFactor = {
818
887
  /** GET /auth/2fa/status — enrolled methods and recovery code count. */
@@ -1055,8 +1124,9 @@ var AuthyonClient = class {
1055
1124
  * 2FA challenge to complete via `verifyTwoFactor()`.
1056
1125
  */
1057
1126
  async login(params) {
1058
- const { organizationSlug, ...rest } = params;
1059
- const body = organizationSlug ? { ...rest, tenantSlug: organizationSlug } : rest;
1127
+ const { organizationSlug, tenantSlug, ...rest } = params;
1128
+ const slug = tenantSlug ?? organizationSlug;
1129
+ const body = slug ? { ...rest, tenantSlug: slug } : rest;
1060
1130
  const data = await this.request("/auth/login", { method: "POST", body });
1061
1131
  if (data.twoFactor) {
1062
1132
  return { twoFactorRequired: true, ...data.twoFactor };
@@ -1157,11 +1227,15 @@ var AuthyonClient = class {
1157
1227
  }
1158
1228
  };
1159
1229
  function normalizeUser(raw) {
1160
- const { tenant, tenants, activeTenant, ...rest } = raw;
1230
+ const { tenant, tenants, activeTenant, organizations, activeOrganization, ...rest } = raw;
1231
+ const list = tenants ?? organizations;
1232
+ const active = tenant ?? activeTenant ?? activeOrganization ?? null;
1161
1233
  return {
1162
1234
  ...rest,
1163
- organizations: raw.organizations ?? tenants,
1164
- activeOrganization: tenant ?? raw.activeOrganization ?? activeTenant ?? null
1235
+ tenants: list,
1236
+ activeTenant: active,
1237
+ organizations: list,
1238
+ activeOrganization: active
1165
1239
  };
1166
1240
  }
1167
1241
  function createClient(options) {
@@ -1,7 +1,7 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode } from 'react';
3
- import { ak as User, a2 as SessionControllerOptions, A as AuthyonClient, a4 as SessionSnapshot, a1 as Session, f as AbilitySubject, t as AuthyonAbilityOptions, r as AuthyonAbility } from '../ability-CmJEviJj.cjs';
4
- export { w as AuthyonSessionController, a5 as SessionSnapshotListener, a6 as SessionStatus } from '../ability-CmJEviJj.cjs';
3
+ import { aw as User, a5 as SessionControllerOptions, A as AuthyonClient, a7 as SessionSnapshot, a4 as Session, f as AbilitySubject, u as AuthyonAbilityOptions, s as AuthyonAbility } from '../ability-DBh_yZ5o.cjs';
4
+ export { x as AuthyonSessionController, a8 as SessionSnapshotListener, a9 as SessionStatus } from '../ability-DBh_yZ5o.cjs';
5
5
 
6
6
  interface AuthyonProviderProps<TUser extends User = User> extends SessionControllerOptions {
7
7
  client: AuthyonClient;
@@ -1,7 +1,7 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode } from 'react';
3
- import { ak as User, a2 as SessionControllerOptions, A as AuthyonClient, a4 as SessionSnapshot, a1 as Session, f as AbilitySubject, t as AuthyonAbilityOptions, r as AuthyonAbility } from '../ability-CmJEviJj.js';
4
- export { w as AuthyonSessionController, a5 as SessionSnapshotListener, a6 as SessionStatus } from '../ability-CmJEviJj.js';
3
+ import { aw as User, a5 as SessionControllerOptions, A as AuthyonClient, a7 as SessionSnapshot, a4 as Session, f as AbilitySubject, u as AuthyonAbilityOptions, s as AuthyonAbility } from '../ability-DBh_yZ5o.js';
4
+ export { x as AuthyonSessionController, a8 as SessionSnapshotListener, a9 as SessionStatus } from '../ability-DBh_yZ5o.js';
5
5
 
6
6
  interface AuthyonProviderProps<TUser extends User = User> extends SessionControllerOptions {
7
7
  client: AuthyonClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@authyon/auth",
3
- "version": "0.2.0-beta.30",
3
+ "version": "0.2.0-beta.32",
4
4
  "description": "Authyon SDK for browsers — auth, sessions, multi-tenant and 2FA for vanilla JS/TS.",
5
5
  "license": "MIT",
6
6
  "type": "module",