@jmlq/auth-plugin-jose 0.0.1-alpha.11 → 0.0.1-alpha.13

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.
@@ -0,0 +1,11 @@
1
+ import type { AuthErrorCode } from "@jmlq/auth";
2
+ /**
3
+ * Convierte un Error (posiblemente sin code) en uno con `code: AuthErrorCode`
4
+ * de forma segura y mínima, SIN cambiar el tipo del error original.
5
+ *
6
+ * - Si ya tiene code string, lo usa.
7
+ * - Si no, usa un fallback dado (por ej. "JWT_ERROR").
8
+ */
9
+ export declare function ensureAuthErrorCode(err: Error, fallback: AuthErrorCode): Error & {
10
+ code: AuthErrorCode;
11
+ };
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ensureAuthErrorCode = ensureAuthErrorCode;
4
+ /**
5
+ * Convierte un Error (posiblemente sin code) en uno con `code: AuthErrorCode`
6
+ * de forma segura y mínima, SIN cambiar el tipo del error original.
7
+ *
8
+ * - Si ya tiene code string, lo usa.
9
+ * - Si no, usa un fallback dado (por ej. "JWT_ERROR").
10
+ */
11
+ function ensureAuthErrorCode(err, fallback) {
12
+ const e = err;
13
+ const code = typeof e.code === "string" ? e.code : undefined;
14
+ // Importante: aquí NO validamos que code pertenezca al union.
15
+ // Solo garantizamos que al menos sea un AuthErrorCode conocido o fallback.
16
+ const safeCode = code ?? fallback;
17
+ // Mutación mínima: añadimos code si falta
18
+ e.code = safeCode;
19
+ return e;
20
+ }
@@ -0,0 +1 @@
1
+ export * from "./ensure-auth-error-code";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./ensure-auth-error-code"), exports);
@@ -1,3 +1,4 @@
1
+ import type { AuthErrorCode } from "@jmlq/auth";
1
2
  import type { JoseErrorContext, MappedAuthError } from "./types";
2
3
  export declare function mapJoseErrorToAuthError(err: unknown, ctx: JoseErrorContext): MappedAuthError;
3
4
  export declare function toAuthDomainError<TAuthError extends Error>(createAuthError: (args: {
@@ -5,4 +6,6 @@ export declare function toAuthDomainError<TAuthError extends Error>(createAuthEr
5
6
  message: string;
6
7
  cause?: unknown;
7
8
  meta?: Record<string, unknown>;
8
- }) => TAuthError, err: unknown, ctx: JoseErrorContext): TAuthError;
9
+ }) => TAuthError, err: unknown, ctx: JoseErrorContext): Error & {
10
+ code: AuthErrorCode;
11
+ };
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mapJoseErrorToAuthError = mapJoseErrorToAuthError;
4
4
  exports.toAuthDomainError = toAuthDomainError;
5
5
  const auth_1 = require("@jmlq/auth");
6
+ const errors_1 = require("./errors");
6
7
  /**
7
8
  * Mapper de errores de `jose` → error “entendible” por el core (@jmlq/auth).
8
9
  *
@@ -36,17 +37,19 @@ const JOSE_NAME_TO_AUTH_CODE = {
36
37
  JWTExpired: "TOKEN_EXPIRED",
37
38
  JWTNotBefore: "TOKEN_NOT_YET_VALID",
38
39
  JWTNotYetValid: "TOKEN_NOT_YET_VALID",
39
- // Tiempo
40
- JWSSignatureVerificationFailed: "SIGNATURE_INVALID",
41
- JWSInvalid: "SIGNATURE_INVALID",
42
- JWSError: "SIGNATURE_INVALID",
43
40
  // Firma
41
+ JWSSignatureVerificationFailed: "SIGNATURE_INVALID",
42
+ // Estructura/serialización JWS inválida
43
+ JWSInvalid: "TOKEN_MALFORMED",
44
+ JWSMalformed: "TOKEN_MALFORMED",
45
+ // Error genérico JWS (no asumir firma)
46
+ JWSError: "TOKEN_INVALID",
47
+ // Claims inválidos (issuer, exp, nbf, etc. dependiendo de jose)
44
48
  JWTClaimValidationFailed: "CLAIMS_VALIDATION_ERROR",
45
- // Formato / estructura inválida (canon: JWT_MALFORMED)
49
+ // Token inválido (pero no necesariamente “malformed”)
46
50
  JWTInvalid: "TOKEN_INVALID",
47
- // Token inválido (pero no malformado)
51
+ // Token malformado
48
52
  JWTMalformed: "TOKEN_MALFORMED",
49
- JWSMalformed: "TOKEN_MALFORMED",
50
53
  JOSEError: "TOKEN_MALFORMED",
51
54
  // Algoritmo / soporte
52
55
  JOSENotSupported: "ALGORITHM_UNSUPPORTED",
@@ -85,14 +88,14 @@ function mapJoseErrorToAuthError(err, ctx) {
85
88
  const message = AUTH_CODE_TO_MESSAGE[code] ??
86
89
  // fallback defensivo (no dependemos de message de jose)
87
90
  "JWT operation failed";
88
- console.log({
89
- package: "@jmlq/auth-plugin-jose",
90
- name,
91
- meta,
92
- code,
93
- message,
94
- err,
95
- });
91
+ // console.log({
92
+ // package: "@jmlq/auth-plugin-jose",
93
+ // name,
94
+ // meta,
95
+ // code,
96
+ // message,
97
+ // err,
98
+ // });
96
99
  return {
97
100
  code,
98
101
  message,
@@ -101,14 +104,17 @@ function mapJoseErrorToAuthError(err, ctx) {
101
104
  };
102
105
  }
103
106
  function toAuthDomainError(createAuthError, err, ctx) {
107
+ // Si ya es error del core, conservarlo (NO remapear)
104
108
  if (auth_1.AuthDomainError.isAuthError(err)) {
105
109
  return err;
106
110
  }
107
111
  const mapped = mapJoseErrorToAuthError(err, ctx);
108
- return createAuthError({
112
+ const created = createAuthError({
109
113
  code: mapped.code,
110
114
  message: mapped.message,
111
115
  cause: mapped.cause,
112
116
  meta: mapped.meta,
113
117
  });
118
+ // Garantizamos code usable por `isRetryableAuthCode`
119
+ return (0, errors_1.ensureAuthErrorCode)(created, mapped.code);
114
120
  }
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- // src/infrastructure/services/jose-token.service.ts
3
2
  Object.defineProperty(exports, "__esModule", { value: true });
4
3
  exports.JoseTokenService = void 0;
5
4
  const jose_1 = require("jose");
@@ -103,10 +102,19 @@ class JoseTokenService {
103
102
  return await fn();
104
103
  }
105
104
  catch (err) {
106
- // Preserve codes/details from core errors
107
- if (auth_1.AuthDomainError.isAuthError(err))
105
+ // 1) Si ya viene como error del core, NO tocarlo
106
+ if (auth_1.AuthDomainError.isAuthError(err)) {
108
107
  throw err;
109
- throw (0, mappers_1.toAuthDomainError)(this.createAuthError, err, this.ctx(operation, kind));
108
+ }
109
+ // 2) Si es error de jose u otro, mapear SIEMPRE
110
+ const authErr = (0, mappers_1.toAuthDomainError)(this.createAuthError, err, this.ctx(operation, kind));
111
+ // 3) (Opcional) Si tienes retry logic, que dependa solo de code del core
112
+ // y NO de códigos internos de jose.
113
+ // Ej: si no quieres reintentos, elimina esto.
114
+ if ((0, auth_1.isRetryableAuthCode)(authErr.code)) {
115
+ throw authErr;
116
+ }
117
+ throw authErr;
110
118
  }
111
119
  }
112
120
  // ---------------------------------------------------------------------------
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jmlq/auth-plugin-jose",
3
3
  "description": "Infrastructure plugin that integrates the jose library with @jmlq/auth, providing JWT token generation and verification following Clean Architecture principles.",
4
- "version": "0.0.1-alpha.11",
4
+ "version": "0.0.1-alpha.13",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
@@ -29,7 +29,7 @@
29
29
  "author": "MLahuasi",
30
30
  "license": "MIT",
31
31
  "dependencies": {
32
- "@jmlq/auth": "^0.0.1-alpha.32",
32
+ "@jmlq/auth": "^0.0.1-alpha.33",
33
33
  "jose": "^6.1.3"
34
34
  },
35
35
  "devDependencies": {