UNPKG

@better-auth-ui/core

Version:

Authentication components and data utilities for [Better Auth](https://better-auth.com), available for React and Solid.

217 lines (216 loc) 6.68 kB
import { t as e } from "../../auth-query-keys-B_bcsA3s.js"; import { t } from "../../create-auth-plugin-BXWyolC8.js"; import { skipToken as n } from "@tanstack/query-core"; //#region src/plugins/agent-auth/agent-auth-adapter.ts var r = (e) => e && typeof e == "object" ? e : {}, i = (e) => { let t = r(e); return "data" in t ? t.data : e; }, a = (e) => { if (!e) return; let t = e instanceof Date ? e : new Date(String(e)); return Number.isNaN(t.getTime()) ? void 0 : t; }, o = (e) => e === "webauthn" || e === "session" ? e : "none", s = (e) => e === "active" || e === "denied" || e === "revoked" || e === "pending" ? e : "pending", c = (e) => ({ throw: !0, signal: e }); function l(e) { let t = e; return r(t.error ?? t.body); } function u(e, t) { let n = r(e), i = String(n.capability ?? ""), c = t.get(i) ?? {}; return { capability: i, status: s(n.status), description: typeof n.description == "string" ? n.description : typeof c.description == "string" ? c.description : void 0, approvalStrength: o(c.approval_strength), reason: typeof n.reason == "string" ? n.reason : void 0, constraints: n.constraints && typeof n.constraints == "object" ? r(n.constraints) : void 0, expiresAt: a(n.expires_at) }; } function d(e, t, n) { return { id: String(e.agent_id ?? ""), name: String(e.name ?? "Agent"), status: String(e.status ?? "pending"), mode: e.mode === "autonomous" ? "autonomous" : "delegated", hostId: String(e.host_id ?? ""), hostName: n ?? (typeof e.host_name == "string" ? e.host_name : void 0), grants: Array.isArray(e.agent_capability_grants) ? e.agent_capability_grants.map((e) => u(e, t)).filter((e) => e.capability) : [], createdAt: a(e.created_at) ?? /* @__PURE__ */ new Date(0), lastUsedAt: a(e.last_used_at), expiresAt: a(e.expires_at) }; } function f(e, t = {}) { let n = async (t) => { let n = r(i(await e.capability.list({ query: { limit: 100 }, fetchOptions: c(t) }))).capabilities; return new Map((Array.isArray(n) ? n : []).map((e) => { let t = r(e); return [String(t.name ?? ""), t]; })); }, a = async (t) => { let n = []; for (let a = 0;; a += 100) { let o = r(i(await e.agent.list({ query: { limit: 100, offset: a }, fetchOptions: c(t) }))).agents, s = Array.isArray(o) ? o : []; if (n.push(...s), s.length < 100) return n; } }, o = async (e) => { let [t, i] = await Promise.all([a(e), n(e)]); return t.map((e) => d(r(e), i)); }, s = async (n, i) => { let a = { agent_id: i.agentId, approval_id: i.approvalId, user_code: i.userCode, action: n, capabilities: i.capabilities, ttl: i.ttl, reason: i.reason }; try { await e.agent.approveCapability({ ...a, fetchOptions: c() }); } catch (n) { let i = l(n); if (i.error !== "webauthn_required") throw n; if (!t.authenticateWithPasskey) throw Error("This approval requires a passkey. Configure authenticateWithPasskey on the Agent Auth adapter."); let o = await t.authenticateWithPasskey(r(i.webauthn_options)); await e.agent.approveCapability({ ...a, webauthn_response: o, fetchOptions: c() }); } }; return { async getApproval(t, o) { let [s, l, u] = await Promise.all([ e.agent.get({ query: { agent_id: t.agentId }, fetchOptions: c(o) }), a(o), n(o) ]), f = l.map((e) => r(e)).find((e) => String(e.agent_id ?? "") === t.agentId), p = d(r(i(s)), u, typeof f?.host_name == "string" ? f.host_name : void 0); return { ...p, requestedCapabilities: p.grants.filter((e) => e.status === "pending") }; }, approve: (e) => s("approve", e), deny: (e) => s("deny", e), listAgents: o, async revoke(t, n) { await e.agent.revokeCapability({ agent_id: t, capabilities: n, fetchOptions: c() }); } }; } //#endregion //#region src/plugins/agent-auth/agent-auth-localization.ts var p = { approvalTitle: "Approve agent access", approvalDescription: "Review what this agent wants to do on your behalf.", requestedCapabilities: "Requested capabilities", requestReason: "Reason", constraints: "Limits", delegatedAgent: "Delegated agent", autonomousAgent: "Autonomous agent", approvalNone: "No extra check", approvalSession: "Recent sign-in", approvalWebauthn: "Passkey required", allow: "Allow selected", deny: "Deny", approvedTitle: "Access approved", approvedDescription: "The agent can now use the capabilities you selected.", deniedTitle: "Access denied", deniedDescription: "The agent did not receive access.", noCapabilities: "This request has no pending capabilities.", invalidRequest: "This approval link is missing the agent identifier.", approvalError: "We could not update this request. Try again.", agents: "Agent access", agentsDescription: "Review agents and revoke capabilities you no longer trust.", noAgents: "No agents have access to your account.", active: "Active", pending: "Pending", denied: "Denied", revoked: "Revoked", expires: "Expires {date}", lastUsed: "Last used {date}", neverUsed: "Never used", revoke: "Revoke", revokeTitle: "Revoke capability?", revokeDescription: "The agent will immediately lose this capability.", confirmRevoke: "Revoke capability" }, m = { all: (t) => [...e.user(t), "agent-auth"], approval: (e, t) => [ ...m.all(t), "approval", e?.agentId, e?.approvalId, e?.userCode ], agents: (e) => [...m.all(e), "agents"] }, h = (e, t, r) => ({ queryKey: m.approval(t, r), meta: { errorPresentation: "inline" }, queryFn: t && r ? ({ signal: n }) => e.getApproval(t, n) : n }), g = (e, t) => ({ queryKey: m.agents(t), queryFn: t ? ({ signal: t }) => e.listAgents(t) : n }), _ = { approve: [ "auth", "agent-auth", "approve" ], deny: [ "auth", "agent-auth", "deny" ], revoke: [ "auth", "agent-auth", "revoke" ] }, v = (e) => ({ awaits: [m.all(e)] }), y = (e, t) => ({ mutationKey: _.approve, mutationFn: (t) => e.approve(t), meta: v(t) }), b = (e, t) => ({ mutationKey: _.deny, mutationFn: (t) => e.deny(t), meta: v(t) }), x = (e, t) => ({ mutationKey: _.revoke, mutationFn: ({ agentId: t, capability: n }) => e.revoke(t, [n]), meta: v(t) }), S = t("agentAuth", (e) => ({ adapter: e.adapter, localization: { ...p, ...e.localization }, grants: e.grants ?? !0, viewPaths: { auth: { agentApproval: e.path ?? "agent-approval" } } })); //#endregion export { h as agentApprovalOptions, p as agentAuthLocalization, _ as agentAuthMutationKeys, S as agentAuthPlugin, m as agentAuthQueryKeys, g as agentAuthorizationsOptions, y as approveAgentOptions, f as createAgentAuthClientAdapter, b as denyAgentOptions, x as revokeAgentCapabilityOptions };