cauthy-auth 0.28.1 → 0.31.0

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.
Files changed (46) hide show
  1. package/README.md +61 -3
  2. package/dist/{cauthy-CTVg4kvo.d.mts → cauthy-2jtP2LVQ.d.mts} +19 -1
  3. package/dist/{cauthy-BAwAJChs.d.ts → cauthy-CKfvvv_i.d.ts} +19 -1
  4. package/dist/cli.js +163 -59
  5. package/dist/client.d.mts +91 -10
  6. package/dist/client.d.ts +91 -10
  7. package/dist/client.js +39 -3
  8. package/dist/client.js.map +1 -1
  9. package/dist/client.mjs +39 -3
  10. package/dist/client.mjs.map +1 -1
  11. package/dist/express.d.mts +2 -2
  12. package/dist/express.d.ts +2 -2
  13. package/dist/fastify.d.mts +2 -2
  14. package/dist/fastify.d.ts +2 -2
  15. package/dist/hono.d.mts +2 -2
  16. package/dist/hono.d.ts +2 -2
  17. package/dist/index.d.mts +2 -2
  18. package/dist/index.d.ts +2 -2
  19. package/dist/index.js +387 -148
  20. package/dist/index.js.map +1 -1
  21. package/dist/index.mjs +385 -148
  22. package/dist/index.mjs.map +1 -1
  23. package/dist/next.d.mts +2 -2
  24. package/dist/next.d.ts +2 -2
  25. package/dist/react.d.mts +275 -23
  26. package/dist/react.d.ts +275 -23
  27. package/dist/react.js +312 -78
  28. package/dist/react.js.map +1 -1
  29. package/dist/react.mjs +311 -78
  30. package/dist/react.mjs.map +1 -1
  31. package/dist/styles.css +8 -0
  32. package/dist/svelte.d.mts +2 -2
  33. package/dist/svelte.d.ts +2 -2
  34. package/dist/svelte.js +53 -4
  35. package/dist/svelte.js.map +1 -1
  36. package/dist/svelte.mjs +53 -4
  37. package/dist/svelte.mjs.map +1 -1
  38. package/dist/{system-CVBW_AV8.d.mts → system-BrrJsrFW.d.mts} +321 -109
  39. package/dist/{system-Bn8uckvQ.d.ts → system-BvvoNs2y.d.ts} +321 -109
  40. package/dist/vue.d.mts +2 -2
  41. package/dist/vue.d.ts +2 -2
  42. package/dist/vue.js +53 -4
  43. package/dist/vue.js.map +1 -1
  44. package/dist/vue.mjs +53 -4
  45. package/dist/vue.mjs.map +1 -1
  46. package/package.json +1 -1
package/dist/vue.mjs CHANGED
@@ -257,6 +257,12 @@ function checkOrganization(value, required = false) {
257
257
  if (v.length > MAX_ORGANIZATION) return { field: "organization", code: "too_long", message: `Use at most ${MAX_ORGANIZATION} characters.` };
258
258
  return null;
259
259
  }
260
+ function checkOrganizationName(value) {
261
+ const v = typeof value === "string" ? value.trim() : "";
262
+ if (v === "") return { field: "name", code: "required", message: "Enter the organization's name." };
263
+ if (v.length > MAX_ORGANIZATION) return { field: "name", code: "too_long", message: `Use at most ${MAX_ORGANIZATION} characters.` };
264
+ return null;
265
+ }
260
266
  function checkCode(value) {
261
267
  const v = typeof value === "string" ? value.trim() : "";
262
268
  if (v === "") return { field: "code", code: "required", message: "Enter the code." };
@@ -341,11 +347,11 @@ function createAuthClient(options = {}) {
341
347
  if (res.ok) {
342
348
  const m = await res.json();
343
349
  if (m.password) policy = passwordPolicyOf(m.password);
344
- return { providers: [], signUp: true, magicLink: false, passkey: false, ...m, ...policy ? { password: policy } : {}, passkeyHere: m.passkeyHere === true, passkeyOnCauthy: m.passkeyOnCauthy ?? m.passkey === true, sso: m.sso === true };
350
+ return { providers: [], signUp: true, magicLink: false, passkey: false, ...m, ...policy ? { password: policy } : {}, passwordSignIn: m.passwordSignIn !== false, passkeyHere: m.passkeyHere === true, passkeyOnCauthy: m.passkeyOnCauthy ?? m.passkey === true, sso: m.sso === true };
345
351
  }
346
352
  } catch {
347
353
  }
348
- return { providers: [], signUp: true, magicLink: false, passkey: false, passkeyHere: false, passkeyOnCauthy: false, sso: false };
354
+ return { providers: [], signUp: true, passwordSignIn: true, magicLink: false, passkey: false, passkeyHere: false, passkeyOnCauthy: false, sso: false };
349
355
  }
350
356
  async function ceremony(begin, finish, form, run, more = {}) {
351
357
  const begun = await post(begin, form, null);
@@ -492,6 +498,20 @@ function createAuthClient(options = {}) {
492
498
  forgotPassword: (f) => post("/forgot-password", { email: f.email }, checkEmail(f.email), f.captchaToken),
493
499
  resetPassword: (f) => post("/reset-password", { token: f.token, password: f.password }, checkNewPassword(f.password, policy)),
494
500
  verifyEmail: (f) => post("/verify-email", { token: f.token }, null),
501
+ async invitation(token) {
502
+ const bad = { ok: false, error: { code: "invalid_token", message: "This invitation is not valid, or it has expired." } };
503
+ if (typeof token !== "string" || token.trim() === "") return bad;
504
+ try {
505
+ const res = await doFetch(`${base}/invitation?token=${encodeURIComponent(token.trim())}`, { credentials: "same-origin", headers: { accept: "application/json" } });
506
+ const body = await res.json().catch(() => null);
507
+ if (res.ok && body?.invitation) return { ok: true, status: "ok", invitation: body.invitation };
508
+ const e = body?.error;
509
+ return { ok: false, error: e && typeof e.code === "string" ? { code: e.code, message: String(e.message ?? "") } : UNAVAILABLE };
510
+ } catch {
511
+ return { ok: false, error: UNAVAILABLE };
512
+ }
513
+ },
514
+ acceptInvitation: (token, password) => post("/invitation/accept", { token, password }, checkNewPassword(password, policy)),
495
515
  signOut: () => post("/sign-out", {}, null),
496
516
  organizations: {
497
517
  async list(system) {
@@ -509,7 +529,23 @@ function createAuthClient(options = {}) {
509
529
  add: (id, f) => post("/me/organizations/members/add", { organizationId: id, email: f.email, role: f.role, name: f.name ?? "", locale: f.locale ?? "" }, checkEmail(f.email)),
510
530
  setRole: (id, userId, role) => post("/me/organizations/members/role", { organizationId: id, userId, role }, null),
511
531
  remove: (id, userId) => post("/me/organizations/members/remove", { organizationId: id, userId }, null),
512
- inviteAgain: (id, userId, f = {}) => post("/me/organizations/members/invite", { organizationId: id, userId, locale: f.locale ?? "" }, null)
532
+ inviteAgain: (id, userId, f = {}) => post("/me/organizations/members/invite", { organizationId: id, userId, locale: f.locale ?? "" }, null),
533
+ rename: (id, name) => post("/me/organizations/rename", { organizationId: id, name }, checkOrganizationName(name)),
534
+ archive: (id) => post("/me/organizations/archive", { organizationId: id }, null),
535
+ delete: (id) => post("/me/organizations/delete", { organizationId: id }, null),
536
+ async check(q) {
537
+ const organizationId = typeof q.organization === "string" ? q.organization : q.organization?.id ?? "";
538
+ const r = await post("/me/organizations/check", {
539
+ organizationId,
540
+ resource: q.resource,
541
+ action: q.action,
542
+ ...q.attributes !== void 0 ? { attributes: q.attributes } : {},
543
+ ...q.resourceId !== void 0 ? { resourceId: q.resourceId } : {},
544
+ ...q.ownerId !== void 0 ? { ownerId: q.ownerId } : {}
545
+ }, null);
546
+ if (!r.ok) return { allowed: false, reason: r.error.message, error: r.error };
547
+ return { allowed: r.allowed === true, reason: String(r.reason ?? ""), ...r.rule ? { rule: r.rule } : {} };
548
+ }
513
549
  },
514
550
  account: {
515
551
  async get() {
@@ -713,7 +749,9 @@ function can(organization, resource, action) {
713
749
  if ((organization.denied ?? []).some((d) => matches(d, resource, action))) return false;
714
750
  return organization.permissions.some((p) => matches(p, resource, action));
715
751
  }
716
- function definePolicy(roles) {
752
+ function definePolicy(given) {
753
+ const roles = Array.isArray(given) ? given : given.roles;
754
+ const resources = Array.isArray(given) ? [] : given.catalog ?? [];
717
755
  const entry = (p) => `${p.resource}:${p.action}`;
718
756
  const byName = new Map(roles.map((r) => [r.role, r]));
719
757
  const split = (permission) => {
@@ -728,6 +766,12 @@ function definePolicy(roles) {
728
766
  ...r.inherits ? { inherits: [...r.inherits] } : {},
729
767
  ...r.permissions ? { permissions: r.permissions.map((p) => ({ ...p })) } : {}
730
768
  })),
769
+ catalog: resources.map((c) => ({
770
+ resource: c.resource,
771
+ ...c.description !== void 0 ? { description: c.description } : {},
772
+ actions: [...c.actions],
773
+ ...c.attributes ? { attributes: { ...c.attributes } } : {}
774
+ })),
731
775
  roleNames: roles.map((r) => r.role),
732
776
  permissions: [...new Set(roles.flatMap((r) => (r.permissions ?? []).map(entry)))],
733
777
  can(organization, permission) {
@@ -749,6 +793,11 @@ function definePolicy(roles) {
749
793
  };
750
794
  walk(role);
751
795
  return [...out];
796
+ },
797
+ // The types did the work; this only hands the question on, as a method of
798
+ // the asker so it keeps its `this`.
799
+ check(asker, ...args) {
800
+ return asker.check(...args);
752
801
  }
753
802
  };
754
803
  }