@rsanjur-dev/id 1.0.0 → 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 CHANGED
@@ -152,6 +152,28 @@ export declare function checkAppAccess(options: {
152
152
  baseUrl?: string;
153
153
  fetcher?: typeof fetch;
154
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>;
155
177
  /**
156
178
  * Ensures `returnPath` stays within `appOrigin` (prevents open redirects) and builds `https://id.rsanjur.com/salir?volver=...`.
157
179
  */
package/dist/core.js CHANGED
@@ -452,6 +452,59 @@ export async function checkAppAccess(options) {
452
452
  rol: normalizeRole(data.rol),
453
453
  };
454
454
  }
455
+ const SERVER_KEY_RE = /^rsk_[A-Za-z0-9_-]{32,128}$/u;
456
+ const ACCOUNT_ID_RE = /^[A-Za-z0-9_-]{1,128}$/u;
457
+ /**
458
+ * Chequeo de acceso servidor a servidor (`POST /api/auth/acceso/servidor`), sin cookie:
459
+ * un backend registrado (p. ej. un servidor MCP de RS Connect) pregunta si `cuentaId`
460
+ * sigue aprobada para su app en Accesos. Se autentica con su clave de servidor `rsk_…`
461
+ * (secreto del Worker; nunca la pongas en el navegador ni en el repo).
462
+ * No lanza por acceso denegado: devuelve `permitido: false` con su `estado`.
463
+ */
464
+ export async function checkServerAccess(options) {
465
+ if (!CLIENT_ID_RE.test(options.clientId))
466
+ throw new TypeError("clientId inválido");
467
+ if (!ACCOUNT_ID_RE.test(options.cuentaId))
468
+ throw new TypeError("cuentaId inválido");
469
+ if (!SERVER_KEY_RE.test(options.serverKey))
470
+ throw new TypeError("serverKey inválida");
471
+ const base = normalizeRsidOrigin(options.baseUrl);
472
+ const fetcher = options.fetcher ?? globalThis.fetch;
473
+ let response;
474
+ try {
475
+ response = await fetcher(`${base}/api/auth/acceso/servidor`, {
476
+ method: "POST",
477
+ headers: {
478
+ "content-type": "application/json",
479
+ accept: "application/json",
480
+ authorization: `Bearer ${options.serverKey}`,
481
+ },
482
+ body: JSON.stringify({ client_id: options.clientId, cuenta_id: options.cuentaId }),
483
+ redirect: "manual",
484
+ cache: "no-store",
485
+ });
486
+ }
487
+ catch {
488
+ throw new RsidAccessError("unavailable", "No se pudo consultar /api/auth/acceso/servidor");
489
+ }
490
+ if (response.status === 401) {
491
+ throw new RsidAccessError("unauthenticated", "RS ID rechazó la clave de servidor", 401);
492
+ }
493
+ if (response.status >= 500) {
494
+ throw new RsidAccessError("unavailable", `RS ID no disponible (HTTP ${response.status})`, response.status);
495
+ }
496
+ const data = await readBoundedJson(response);
497
+ if (!response.ok || !data || data.ok !== true || data.cuenta_id !== options.cuentaId) {
498
+ throw new RsidAccessError("unavailable", `Respuesta inesperada de RS ID (HTTP ${response.status})`, response.status);
499
+ }
500
+ return {
501
+ clientId: options.clientId,
502
+ cuentaId: options.cuentaId,
503
+ estado: String(data.estado || "pendiente"),
504
+ permitido: data.permitido === true,
505
+ rol: normalizeRole(data.rol),
506
+ };
507
+ }
455
508
  /**
456
509
  * Ensures `returnPath` stays within `appOrigin` (prevents open redirects) and builds `https://id.rsanjur.com/salir?volver=...`.
457
510
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsanjur-dev/id",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "SDK oficial de RS ID (id.rsanjur.com): PKCE S256, sesión/identidad, cookies HMAC Web Crypto, SvelteKit, Svelte 5 y React.",
5
5
  "type": "module",
6
6
  "sideEffects": false,