@jmlq/auth 0.0.1-alpha.11 → 0.0.1-alpha.12

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.
@@ -1,3 +1,3 @@
1
1
  export * from "./request";
2
2
  export * from "./response";
3
- export * from "./type";
3
+ export * from "./types";
@@ -17,4 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  //src/application/dtos/index.ts
18
18
  __exportStar(require("./request"), exports);
19
19
  __exportStar(require("./response"), exports);
20
- __exportStar(require("./type"), exports);
20
+ __exportStar(require("./types"), exports);
@@ -1,4 +1,4 @@
1
- import { UserRole } from "../type";
1
+ import { UserRole } from "../types";
2
2
  export interface RegisterUserRequest {
3
3
  email: string;
4
4
  password: string;
@@ -14,5 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- //src/application/dtos/type/index.ts
17
+ //src/application/dtos/types/index.ts
18
18
  __exportStar(require("./user-role.type"), exports);
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ChangePasswordUseCase = void 0;
4
4
  const object_values_1 = require("../../domain/object-values");
5
5
  const errors_1 = require("../../domain/errors");
6
+ const internal_1 = require("./internal");
6
7
  /**
7
8
  * Cambia contraseña con validación de password actual.
8
9
  *
@@ -22,13 +23,8 @@ class ChangePasswordUseCase {
22
23
  this.passwordPolicy = passwordPolicy;
23
24
  }
24
25
  async execute(request) {
25
- if (request.newPassword !== request.confirmNewPassword) {
26
- throw new errors_1.PasswordMismatchError("Passwords do not match");
27
- }
28
- const strength = this.passwordPolicy.validateStrength(request.newPassword);
29
- if (!strength.isValid) {
30
- throw new errors_1.PasswordPolicyViolationError(strength.errors);
31
- }
26
+ (0, internal_1.assertPasswordsMatch)(request.newPassword, request.confirmNewPassword);
27
+ (0, internal_1.assertPasswordPolicy)(this.passwordPolicy, request.newPassword);
32
28
  const user = await this.userRepository.findById(new object_values_1.Id(request.userId));
33
29
  if (!user)
34
30
  throw new errors_1.UserNotFoundError("User not found");
@@ -0,0 +1 @@
1
+ export * from "./password-assertions";
@@ -0,0 +1,18 @@
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
+ //src/application/use-cases/internal/index.ts
18
+ __exportStar(require("./password-assertions"), exports);
@@ -0,0 +1,13 @@
1
+ import type { IPasswordPolicyPort } from "../../../domain/ports";
2
+ /**
3
+ * Asserts that `newPassword` equals `confirmPassword`.
4
+ *
5
+ * Application-level validation to keep use cases small and consistent.
6
+ */
7
+ export declare function assertPasswordsMatch(newPassword: string, confirmPassword: string): void;
8
+ /**
9
+ * Asserts that `password` satisfies the configured password policy.
10
+ *
11
+ * This runs BEFORE hashing to fail fast and reduce CPU cost.
12
+ */
13
+ export declare function assertPasswordPolicy(passwordPolicy: IPasswordPolicyPort, password: string): void;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertPasswordsMatch = assertPasswordsMatch;
4
+ exports.assertPasswordPolicy = assertPasswordPolicy;
5
+ const errors_1 = require("../../../domain/errors");
6
+ /**
7
+ * Asserts that `newPassword` equals `confirmPassword`.
8
+ *
9
+ * Application-level validation to keep use cases small and consistent.
10
+ */
11
+ function assertPasswordsMatch(newPassword, confirmPassword) {
12
+ if (newPassword !== confirmPassword) {
13
+ throw new errors_1.PasswordMismatchError("Passwords do not match");
14
+ }
15
+ }
16
+ /**
17
+ * Asserts that `password` satisfies the configured password policy.
18
+ *
19
+ * This runs BEFORE hashing to fail fast and reduce CPU cost.
20
+ */
21
+ function assertPasswordPolicy(passwordPolicy, password) {
22
+ const strength = passwordPolicy.validateStrength(password);
23
+ if (!strength.isValid) {
24
+ throw new errors_1.PasswordPolicyViolationError(strength.errors);
25
+ }
26
+ }
@@ -17,7 +17,7 @@ class RefreshTokenUseCase {
17
17
  };
18
18
  }
19
19
  catch (error) {
20
- throw new errors_1.InvalidOrExpiredRefreshTokenError();
20
+ throw new errors_1.TokenExpiredError();
21
21
  }
22
22
  }
23
23
  }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RegisterUserUseCase = void 0;
4
4
  //src/application/use-cases/register-user.use-case.ts
5
5
  const domain_1 = require("../../domain");
6
+ const internal_1 = require("./internal");
6
7
  class RegisterUserUseCase {
7
8
  constructor(userRepository, passwordHasher, passwordPolicy) {
8
9
  this.userRepository = userRepository;
@@ -33,11 +34,8 @@ class RegisterUserUseCase {
33
34
  // Use case
34
35
  // ---------------------------------------------------------------------------
35
36
  async execute(request) {
36
- // Validar política de contraseñas
37
- const passwordValidation = this.passwordPolicy.validateStrength(request.password);
38
- if (!passwordValidation.isValid) {
39
- throw new domain_1.PasswordPolicyViolationError(passwordValidation.errors);
40
- }
37
+ // Validar policy antes de hacer trabajo costoso (hash)
38
+ (0, internal_1.assertPasswordPolicy)(this.passwordPolicy, request.password);
41
39
  // Verificar que el email no esté en uso
42
40
  const email = new domain_1.Email(request.email);
43
41
  const exists = await this.userRepository.findByEmail(email);
@@ -4,6 +4,7 @@ exports.ResetPasswordUseCase = void 0;
4
4
  const object_values_1 = require("../../domain/object-values");
5
5
  const errors_1 = require("../../domain/errors");
6
6
  const password_reset_errors_1 = require("../../domain/errors/password-reset.errors");
7
+ const internal_1 = require("./internal");
7
8
  /**
8
9
  * Confirma el reseteo de contraseña usando token (single-use).
9
10
  *
@@ -21,15 +22,9 @@ class ResetPasswordUseCase {
21
22
  this.passwordResetToken = passwordResetToken;
22
23
  }
23
24
  async execute(request) {
24
- // Validación de request (application): confirmación
25
- if (request.newPassword !== request.confirmNewPassword) {
26
- throw new errors_1.PasswordMismatchError("Passwords do not match");
27
- }
28
- // Validar policy antes de hacer trabajo costoso (hash)
29
- const strength = this.passwordPolicy.validateStrength(request.newPassword);
30
- if (!strength.isValid) {
31
- throw new errors_1.PasswordPolicyViolationError(strength.errors);
32
- }
25
+ // Validación de request (application)
26
+ (0, internal_1.assertPasswordsMatch)(request.newPassword, request.confirmNewPassword);
27
+ (0, internal_1.assertPasswordPolicy)(this.passwordPolicy, request.newPassword);
33
28
  // Consumir token (single-use). La implementación debe asegurar atomicidad.
34
29
  const consumed = await this.passwordResetToken
35
30
  .consume(request.resetToken)
@@ -62,11 +62,6 @@ export declare class Credential {
62
62
  * @returns Verdadero si las credenciales han expirado, falso en caso contrario
63
63
  */
64
64
  isExpired(): boolean;
65
- /**
66
- * Evalúa si las credenciales son válidas (no expiradas)
67
- * @returns Verdadero si las credenciales son válidas (no expiradas), falso en caso contrario
68
- */
69
- isValid(): boolean;
70
65
  /**
71
66
  * Crea una nueva instancia de Credential
72
67
  * @param userId Identificador del usuario
@@ -63,13 +63,6 @@ class Credential {
63
63
  isExpired() {
64
64
  return new Date() > this._expiresAt;
65
65
  }
66
- /**
67
- * Evalúa si las credenciales son válidas (no expiradas)
68
- * @returns Verdadero si las credenciales son válidas (no expiradas), falso en caso contrario
69
- */
70
- isValid() {
71
- return !this.isExpired();
72
- }
73
66
  /**
74
67
  * Crea una nueva instancia de Credential
75
68
  * @param userId Identificador del usuario
@@ -34,13 +34,6 @@ export declare class InvalidSignatureError extends AuthDomainError {
34
34
  export declare class AuthenticationError extends AuthDomainError {
35
35
  constructor(message?: string, details?: unknown);
36
36
  }
37
- /** Alias histórico si lo usabas antes */
38
- export declare class InvalidOrExpiredRefreshTokenError extends AuthDomainError {
39
- constructor(details?: {
40
- exp?: number;
41
- now?: number;
42
- });
43
- }
44
37
  export declare class SessionAuthError extends AuthDomainError {
45
38
  constructor(message?: string, details?: unknown);
46
39
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SessionAuthError = exports.InvalidOrExpiredRefreshTokenError = exports.AuthenticationError = exports.InvalidSignatureError = exports.InvalidTokenFormatError = exports.TokenExpiredError = exports.AuthDomainError = void 0;
3
+ exports.SessionAuthError = exports.AuthenticationError = exports.InvalidSignatureError = exports.InvalidTokenFormatError = exports.TokenExpiredError = exports.AuthDomainError = void 0;
4
4
  class AuthDomainError extends Error {
5
5
  constructor(message, code, details) {
6
6
  super(message);
@@ -53,13 +53,6 @@ class AuthenticationError extends AuthDomainError {
53
53
  }
54
54
  }
55
55
  exports.AuthenticationError = AuthenticationError;
56
- /** Alias histórico si lo usabas antes */
57
- class InvalidOrExpiredRefreshTokenError extends AuthDomainError {
58
- constructor(details) {
59
- super("Invalid or expired refresh token", "TOKEN_EXPIRED", details);
60
- }
61
- }
62
- exports.InvalidOrExpiredRefreshTokenError = InvalidOrExpiredRefreshTokenError;
63
56
  class SessionAuthError extends AuthDomainError {
64
57
  constructor(message = "Session Authentication failed", details) {
65
58
  super(message, "AUTHENTICATION_FAILED", details);
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ // src/domain/props/index.ts
17
18
  __exportStar(require("./entities"), exports);
18
19
  __exportStar(require("./jwt"), exports);
@@ -95,11 +95,11 @@ class TokenSessionService {
95
95
  async refreshSession(refreshToken) {
96
96
  const existing = await this.credentialRepository.findByRefreshToken(refreshToken);
97
97
  if (!existing)
98
- throw new domain_1.InvalidOrExpiredRefreshTokenError();
98
+ throw new domain_1.TokenExpiredError();
99
99
  const payload = await this.tokenService
100
100
  .verifyRefreshToken(refreshToken)
101
101
  .catch(() => {
102
- throw new domain_1.InvalidOrExpiredRefreshTokenError();
102
+ throw new domain_1.TokenExpiredError();
103
103
  });
104
104
  const user = await this.userRepository.findById(new domain_1.Id(payload.sub));
105
105
  if (!user)
@@ -14,5 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- //src/infrastructure/type/index.ts
17
+ //src/infrastructure/types/index.ts
18
18
  __exportStar(require("./auth-service-container"), exports);
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@jmlq/auth",
3
3
  "description": "JWT authentication package with clean architecture",
4
- "version": "0.0.1-alpha.11",
4
+ "version": "0.0.1-alpha.12",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "dev": "rimraf dist && mkdir dist && tsc -p tsconfig.json",
9
9
  "build": "rimraf dist && mkdir dist && tsc -p tsconfig.build.json",
10
+ "package:script": "node scripts/package-script.mjs",
10
11
  "prepublishOnly": "npm run build",
11
12
  "test": "jest --passWithNoTests",
12
13
  "test:watch": "jest --watch",