@rsanjur-dev/id 0.0.0-stage → 1.1.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.
package/dist/core.d.ts ADDED
@@ -0,0 +1,205 @@
1
+ /**
2
+ * @rsanjur-dev/id/core
3
+ * Zero-dependency Web Standard client for RS ID (https://id.rsanjur.com).
4
+ * Compatible with Cloudflare Workers, Hono, Astro, Next.js, SvelteKit, Bun, and Node.
5
+ */
6
+ export declare const RSID_DEFAULT_ORIGIN = "https://id.rsanjur.com";
7
+ export type RsidRole = "usuario" | "admin" | "owner";
8
+ export type RsidAccessState = "administrador" | "sin_restriccion" | "aprobado" | "pendiente" | "revocado";
9
+ export interface RsidProfile {
10
+ nombre?: string;
11
+ apellido?: string;
12
+ telefono?: string;
13
+ pais?: string;
14
+ cedula?: string;
15
+ [key: string]: unknown;
16
+ }
17
+ export interface RsidAuthorizeResult {
18
+ clientId: string;
19
+ state: string;
20
+ verifier: string;
21
+ challenge: string;
22
+ url: string;
23
+ cookieName: string;
24
+ cookieValue: string;
25
+ attemptCookie: string;
26
+ clearAttemptCookie: string;
27
+ }
28
+ export interface RsidAttempt {
29
+ clientId: string;
30
+ state: string;
31
+ verifier: string;
32
+ }
33
+ export interface RsidExchangedAccount {
34
+ email: string;
35
+ cuentaId: string;
36
+ rol: RsidRole;
37
+ foto: string;
38
+ perfil: RsidProfile;
39
+ perfilCompleto: boolean;
40
+ perfilFalta: string[];
41
+ compraFalta: string[];
42
+ }
43
+ export interface RsidSessionResponse {
44
+ autenticado: boolean;
45
+ cuentaId?: string;
46
+ email?: string;
47
+ foto?: string;
48
+ perfil?: RsidProfile;
49
+ perfilCompleto?: boolean;
50
+ perfilFalta?: string[];
51
+ esAdmin?: boolean;
52
+ raw?: Record<string, unknown>;
53
+ }
54
+ export interface RsidIdentityResponse {
55
+ autenticado: boolean;
56
+ cuentaId?: string;
57
+ }
58
+ export interface RsidAccessCheckResult {
59
+ ok: boolean;
60
+ clientId: string;
61
+ cuentaId: string;
62
+ estado: RsidAccessState;
63
+ permitido: boolean;
64
+ rol: RsidRole;
65
+ }
66
+ export interface CookieOptions {
67
+ path?: string;
68
+ domain?: string;
69
+ maxAge?: number;
70
+ httpOnly?: boolean;
71
+ secure?: boolean;
72
+ sameSite?: "Lax" | "Strict" | "None";
73
+ }
74
+ export declare class RsidExchangeError extends Error {
75
+ readonly kind: "rejected" | "unavailable" | "pending" | "revoked";
76
+ readonly status?: number;
77
+ constructor(kind: "rejected" | "unavailable" | "pending" | "revoked", message?: string, status?: number);
78
+ }
79
+ export declare class RsidAccessError extends Error {
80
+ readonly kind: "denied" | "unauthenticated" | "unavailable";
81
+ readonly status?: number;
82
+ constructor(kind: "denied" | "unauthenticated" | "unavailable", message?: string, status?: number);
83
+ }
84
+ export declare function toBase64Url(bytes: Uint8Array): string;
85
+ export declare function fromBase64Url(input: string): Uint8Array | null;
86
+ export declare function constantTimeEqual(a: string, b: string): boolean;
87
+ export declare function normalizeRsidOrigin(origin?: string): string;
88
+ export declare function attemptCookieName(clientId: string): string;
89
+ export declare function parseCookieHeader(cookieHeader: string | null | undefined): Map<string, string[]>;
90
+ export declare function formatSetCookie(name: string, value: string, options?: CookieOptions): string;
91
+ export declare function formatClearCookie(name: string, options?: Omit<CookieOptions, "maxAge">): string;
92
+ /**
93
+ * Generates a PKCE S256 authorization URL + host-only attempt cookie for RS ID.
94
+ */
95
+ export declare function createAuthorizeUrl(options: {
96
+ clientId: string;
97
+ baseUrl?: string;
98
+ attemptMaxAge?: number;
99
+ secure?: boolean;
100
+ }): Promise<RsidAuthorizeResult>;
101
+ /**
102
+ * Validates the PKCE attempt cookie against the returned callback `state`.
103
+ * Rejects duplicate cookies to prevent cookie-tossing attacks.
104
+ */
105
+ export declare function readAttemptCookie(cookieHeaderOrValue: string | Request | null | undefined, options: {
106
+ clientId: string;
107
+ state: string;
108
+ isRawValue?: boolean;
109
+ }): RsidAttempt | null;
110
+ export declare function resolvePhotoUrl(raw: unknown, baseUrl?: string): string;
111
+ /**
112
+ * Exchanges a one-time PKCE code with POST /api/auth/canjear.
113
+ */
114
+ export declare function exchangeCode(options: {
115
+ clientId: string;
116
+ code: string;
117
+ state: string;
118
+ verifier: string;
119
+ baseUrl?: string;
120
+ fetcher?: typeof fetch;
121
+ }): Promise<RsidExchangedAccount>;
122
+ /**
123
+ * Extracts only RS ID session cookies (`rs_id` and Better Auth `rsid.*`) from a Cookie header.
124
+ */
125
+ export declare function extractRsidCookieHeader(cookieHeader: string | null | undefined): string;
126
+ /**
127
+ * Fetches full RS ID session state (`GET /api/auth/yo`) using forwarded RS ID cookies.
128
+ * Works on *.rsanjur.com subdomains (or browser with credentials: "include").
129
+ */
130
+ export declare function fetchSession(options?: {
131
+ cookieHeader?: string | null;
132
+ request?: Request;
133
+ baseUrl?: string;
134
+ fetcher?: typeof fetch;
135
+ }): Promise<RsidSessionResponse>;
136
+ /**
137
+ * Lightweight session check (`GET /api/auth/identidad`).
138
+ */
139
+ export declare function fetchIdentity(options?: {
140
+ cookieHeader?: string | null;
141
+ request?: Request;
142
+ baseUrl?: string;
143
+ fetcher?: typeof fetch;
144
+ }): Promise<RsidIdentityResponse>;
145
+ /**
146
+ * Live access check (`POST /api/auth/acceso`) for accessManaged apps (e.g., ritmo, neurodesarrollo, rs-agents).
147
+ */
148
+ export declare function checkAppAccess(options: {
149
+ clientId: string;
150
+ appOrigin: string;
151
+ cookieHeader: string;
152
+ baseUrl?: string;
153
+ fetcher?: typeof fetch;
154
+ }): Promise<RsidAccessCheckResult>;
155
+ export interface RsidServerAccessResult {
156
+ clientId: string;
157
+ cuentaId: string;
158
+ /** Igual que `RsidAccessState`, más `sin_cuenta` cuando la cuenta ya no existe en RS ID. */
159
+ estado: RsidAccessState | "sin_cuenta";
160
+ permitido: boolean;
161
+ rol: RsidRole;
162
+ }
163
+ /**
164
+ * Chequeo de acceso servidor a servidor (`POST /api/auth/acceso/servidor`), sin cookie:
165
+ * un backend registrado (p. ej. un servidor MCP de RS Connect) pregunta si `cuentaId`
166
+ * sigue aprobada para su app en Accesos. Se autentica con su clave de servidor `rsk_…`
167
+ * (secreto del Worker; nunca la pongas en el navegador ni en el repo).
168
+ * No lanza por acceso denegado: devuelve `permitido: false` con su `estado`.
169
+ */
170
+ export declare function checkServerAccess(options: {
171
+ clientId: string;
172
+ cuentaId: string;
173
+ serverKey: string;
174
+ baseUrl?: string;
175
+ fetcher?: typeof fetch;
176
+ }): Promise<RsidServerAccessResult>;
177
+ /**
178
+ * Ensures `returnPath` stays within `appOrigin` (prevents open redirects) and builds `https://id.rsanjur.com/salir?volver=...`.
179
+ */
180
+ export declare function sanitizeReturnPath(path: string | null | undefined, fallback?: string): string;
181
+ export declare function buildLogoutUrl(options: {
182
+ appOrigin: string;
183
+ returnPath?: string;
184
+ baseUrl?: string;
185
+ }): string;
186
+ /**
187
+ * Signs a JSON payload as `<base64url(payload)>.<base64url(hmac-sha256)>` with expiration (`exp` in Unix ms).
188
+ */
189
+ export declare function signSessionCookie<T extends Record<string, unknown>>(payload: T, options: {
190
+ secret: string;
191
+ maxAgeSeconds?: number;
192
+ namespace?: string;
193
+ nowMs?: number;
194
+ }): Promise<string>;
195
+ /**
196
+ * Verifies an HMAC-SHA256 signed session cookie and returns the payload if valid and unexpired.
197
+ */
198
+ export declare function verifySessionCookie<T extends Record<string, unknown> = Record<string, unknown>>(token: string | null | undefined, options: {
199
+ secret: string;
200
+ namespace?: string;
201
+ nowMs?: number;
202
+ }): Promise<(T & {
203
+ iat: number;
204
+ exp: number;
205
+ }) | null>;