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/svelte.mjs CHANGED
@@ -254,6 +254,12 @@ function checkOrganization(value, required = false) {
254
254
  if (v.length > MAX_ORGANIZATION) return { field: "organization", code: "too_long", message: `Use at most ${MAX_ORGANIZATION} characters.` };
255
255
  return null;
256
256
  }
257
+ function checkOrganizationName(value) {
258
+ const v = typeof value === "string" ? value.trim() : "";
259
+ if (v === "") return { field: "name", code: "required", message: "Enter the organization's name." };
260
+ if (v.length > MAX_ORGANIZATION) return { field: "name", code: "too_long", message: `Use at most ${MAX_ORGANIZATION} characters.` };
261
+ return null;
262
+ }
257
263
  function checkCode(value) {
258
264
  const v = typeof value === "string" ? value.trim() : "";
259
265
  if (v === "") return { field: "code", code: "required", message: "Enter the code." };
@@ -338,11 +344,11 @@ function createAuthClient(options = {}) {
338
344
  if (res.ok) {
339
345
  const m = await res.json();
340
346
  if (m.password) policy = passwordPolicyOf(m.password);
341
- 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 };
347
+ 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 };
342
348
  }
343
349
  } catch {
344
350
  }
345
- return { providers: [], signUp: true, magicLink: false, passkey: false, passkeyHere: false, passkeyOnCauthy: false, sso: false };
351
+ return { providers: [], signUp: true, passwordSignIn: true, magicLink: false, passkey: false, passkeyHere: false, passkeyOnCauthy: false, sso: false };
346
352
  }
347
353
  async function ceremony(begin, finish, form, run, more = {}) {
348
354
  const begun = await post(begin, form, null);
@@ -489,6 +495,20 @@ function createAuthClient(options = {}) {
489
495
  forgotPassword: (f) => post("/forgot-password", { email: f.email }, checkEmail(f.email), f.captchaToken),
490
496
  resetPassword: (f) => post("/reset-password", { token: f.token, password: f.password }, checkNewPassword(f.password, policy)),
491
497
  verifyEmail: (f) => post("/verify-email", { token: f.token }, null),
498
+ async invitation(token) {
499
+ const bad = { ok: false, error: { code: "invalid_token", message: "This invitation is not valid, or it has expired." } };
500
+ if (typeof token !== "string" || token.trim() === "") return bad;
501
+ try {
502
+ const res = await doFetch(`${base}/invitation?token=${encodeURIComponent(token.trim())}`, { credentials: "same-origin", headers: { accept: "application/json" } });
503
+ const body = await res.json().catch(() => null);
504
+ if (res.ok && body?.invitation) return { ok: true, status: "ok", invitation: body.invitation };
505
+ const e = body?.error;
506
+ return { ok: false, error: e && typeof e.code === "string" ? { code: e.code, message: String(e.message ?? "") } : UNAVAILABLE };
507
+ } catch {
508
+ return { ok: false, error: UNAVAILABLE };
509
+ }
510
+ },
511
+ acceptInvitation: (token, password) => post("/invitation/accept", { token, password }, checkNewPassword(password, policy)),
492
512
  signOut: () => post("/sign-out", {}, null),
493
513
  organizations: {
494
514
  async list(system) {
@@ -506,7 +526,23 @@ function createAuthClient(options = {}) {
506
526
  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)),
507
527
  setRole: (id, userId, role) => post("/me/organizations/members/role", { organizationId: id, userId, role }, null),
508
528
  remove: (id, userId) => post("/me/organizations/members/remove", { organizationId: id, userId }, null),
509
- inviteAgain: (id, userId, f = {}) => post("/me/organizations/members/invite", { organizationId: id, userId, locale: f.locale ?? "" }, null)
529
+ inviteAgain: (id, userId, f = {}) => post("/me/organizations/members/invite", { organizationId: id, userId, locale: f.locale ?? "" }, null),
530
+ rename: (id, name) => post("/me/organizations/rename", { organizationId: id, name }, checkOrganizationName(name)),
531
+ archive: (id) => post("/me/organizations/archive", { organizationId: id }, null),
532
+ delete: (id) => post("/me/organizations/delete", { organizationId: id }, null),
533
+ async check(q) {
534
+ const organizationId = typeof q.organization === "string" ? q.organization : q.organization?.id ?? "";
535
+ const r = await post("/me/organizations/check", {
536
+ organizationId,
537
+ resource: q.resource,
538
+ action: q.action,
539
+ ...q.attributes !== void 0 ? { attributes: q.attributes } : {},
540
+ ...q.resourceId !== void 0 ? { resourceId: q.resourceId } : {},
541
+ ...q.ownerId !== void 0 ? { ownerId: q.ownerId } : {}
542
+ }, null);
543
+ if (!r.ok) return { allowed: false, reason: r.error.message, error: r.error };
544
+ return { allowed: r.allowed === true, reason: String(r.reason ?? ""), ...r.rule ? { rule: r.rule } : {} };
545
+ }
510
546
  },
511
547
  account: {
512
548
  async get() {
@@ -710,7 +746,9 @@ function can(organization, resource, action) {
710
746
  if ((organization.denied ?? []).some((d) => matches(d, resource, action))) return false;
711
747
  return organization.permissions.some((p) => matches(p, resource, action));
712
748
  }
713
- function definePolicy(roles) {
749
+ function definePolicy(given) {
750
+ const roles = Array.isArray(given) ? given : given.roles;
751
+ const resources = Array.isArray(given) ? [] : given.catalog ?? [];
714
752
  const entry = (p) => `${p.resource}:${p.action}`;
715
753
  const byName = new Map(roles.map((r) => [r.role, r]));
716
754
  const split = (permission) => {
@@ -725,6 +763,12 @@ function definePolicy(roles) {
725
763
  ...r.inherits ? { inherits: [...r.inherits] } : {},
726
764
  ...r.permissions ? { permissions: r.permissions.map((p) => ({ ...p })) } : {}
727
765
  })),
766
+ catalog: resources.map((c) => ({
767
+ resource: c.resource,
768
+ ...c.description !== void 0 ? { description: c.description } : {},
769
+ actions: [...c.actions],
770
+ ...c.attributes ? { attributes: { ...c.attributes } } : {}
771
+ })),
728
772
  roleNames: roles.map((r) => r.role),
729
773
  permissions: [...new Set(roles.flatMap((r) => (r.permissions ?? []).map(entry)))],
730
774
  can(organization, permission) {
@@ -746,6 +790,11 @@ function definePolicy(roles) {
746
790
  };
747
791
  walk(role);
748
792
  return [...out];
793
+ },
794
+ // The types did the work; this only hands the question on, as a method of
795
+ // the asker so it keeps its `this`.
796
+ check(asker, ...args) {
797
+ return asker.check(...args);
749
798
  }
750
799
  };
751
800
  }