cca-auth-module 0.1.90 → 0.1.92
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/application/useCase/LoginUseCase.d.ts +1 -1
- package/dist/application/validators/authValidation.d.ts +2 -2
- package/dist/domain/interfaces/IAuthService.d.ts +3 -4
- package/dist/index.d.mts +8 -18
- package/dist/index.d.ts +8 -18
- package/dist/index.js +36 -79
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +32 -75
- package/dist/index.mjs.map +1 -1
- package/dist/infrastructure/repository/AuthRepository.d.ts +1 -1
- package/dist/infrastructure/services/JwtAuthService.d.ts +3 -4
- package/dist/presentation/controller/AuthController.d.ts +1 -3
- package/package.json +2 -2
- package/dist/application/useCase/LoginAdminUseCase.d.ts +0 -9
|
@@ -7,7 +7,7 @@ export declare class LoginUseCase implements IBaseService {
|
|
|
7
7
|
private readonly jwtService;
|
|
8
8
|
constructor(repository: AuthRepository, jwtService: JwtAuthService);
|
|
9
9
|
initialize(): Promise<void>;
|
|
10
|
-
execute(loginDTO: LoginDTO): Promise<{
|
|
10
|
+
execute(loginDTO: LoginDTO, providedAdminPassword?: string): Promise<{
|
|
11
11
|
id: string;
|
|
12
12
|
accessToken: string;
|
|
13
13
|
expiresAt: number;
|
|
@@ -2,9 +2,9 @@ import { AuthEntity } from "cca-entities";
|
|
|
2
2
|
import { AuthRepository } from "../../infrastructure/repository/AuthRepository";
|
|
3
3
|
import { RegisterDTO } from "../dtos/RegisterDTO";
|
|
4
4
|
import { LoginDTO } from "../dtos/LoginDTO";
|
|
5
|
-
export declare const validateEmail: (email: string, repository: AuthRepository) => Promise<AuthEntity>;
|
|
5
|
+
export declare const validateEmail: (email: string, repository: AuthRepository, isAdmin: boolean) => Promise<AuthEntity>;
|
|
6
6
|
export declare const validatePassword: (password?: string) => Promise<void>;
|
|
7
7
|
export declare const validateEmailUniqueness: (repository: AuthRepository, email: string, excludeUserId?: string) => Promise<void>;
|
|
8
8
|
export declare const validateRegisterDTO: (auth: RegisterDTO, repository: AuthRepository) => Promise<void>;
|
|
9
|
-
export declare const validateLoginDTO: (data: LoginDTO, repository: AuthRepository) => Promise<AuthEntity>;
|
|
9
|
+
export declare const validateLoginDTO: (data: LoginDTO, repository: AuthRepository, isAdmin: boolean) => Promise<AuthEntity>;
|
|
10
10
|
export declare const validateAdminSecret: (secretPassword?: string) => Promise<void>;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AdminEntity, UserEntity, UserRole } from "cca-entities";
|
|
2
2
|
import { IDecodedToken } from "./IDecodedToken";
|
|
3
3
|
export interface IAuthService {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
generateRefreshToken(user: UserEntity): string;
|
|
4
|
+
generateAccessToken(user: UserEntity | AdminEntity, role: UserRole): string;
|
|
5
|
+
generateRefreshToken(user: UserEntity | AdminEntity): string;
|
|
7
6
|
verifyAccessToken(token: string): Promise<IDecodedToken>;
|
|
8
7
|
verifyRefreshToken(token: string): IDecodedToken;
|
|
9
8
|
}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseRepository, IExtendedBaseRepository, IBaseService, BaseDatabase, BaseContainer } from 'cca-core';
|
|
2
2
|
import { Request, Response, NextFunction } from 'express';
|
|
3
|
-
import { AuthEntity, UserRole, UserEntity } from 'cca-entities';
|
|
3
|
+
import { AuthEntity, UserRole, UserEntity, AdminEntity } from 'cca-entities';
|
|
4
4
|
import { Repository } from 'typeorm';
|
|
5
5
|
import * as jwt from 'jsonwebtoken';
|
|
6
6
|
import { JwtPayload } from 'jsonwebtoken';
|
|
@@ -22,7 +22,7 @@ declare const authConfig: (configSource: ConfigSource) => void;
|
|
|
22
22
|
|
|
23
23
|
declare class AuthRepository extends BaseRepository<AuthEntity> implements IExtendedBaseRepository<AuthEntity> {
|
|
24
24
|
constructor(repository: Repository<AuthEntity>);
|
|
25
|
-
findByEmail(email: string): Promise<AuthEntity | null>;
|
|
25
|
+
findByEmail(email: string, isAdmin?: boolean): Promise<AuthEntity | null>;
|
|
26
26
|
create(entity: Omit<AuthEntity, "createdAt">): Promise<AuthEntity>;
|
|
27
27
|
findByUserId(userId: string): Promise<AuthEntity | null>;
|
|
28
28
|
findByUseAdminId(userId: string, isAdmin?: boolean): Promise<AuthEntity | null>;
|
|
@@ -67,9 +67,8 @@ interface IDecodedToken extends JwtPayload {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
interface IAuthService {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
generateRefreshToken(user: UserEntity): string;
|
|
70
|
+
generateAccessToken(user: UserEntity | AdminEntity, role: UserRole): string;
|
|
71
|
+
generateRefreshToken(user: UserEntity | AdminEntity): string;
|
|
73
72
|
verifyAccessToken(token: string): Promise<IDecodedToken>;
|
|
74
73
|
verifyRefreshToken(token: string): IDecodedToken;
|
|
75
74
|
}
|
|
@@ -81,10 +80,9 @@ declare class JwtAuthService implements IBaseService, IAuthService {
|
|
|
81
80
|
private loadConfig;
|
|
82
81
|
initialize(): Promise<void>;
|
|
83
82
|
private validateConfiguration;
|
|
84
|
-
validateUser(email: string, password: string): Promise<AuthEntity | null>;
|
|
85
83
|
private verifyJwtConfig;
|
|
86
|
-
generateAccessToken(user: UserEntity, role: UserRole): string;
|
|
87
|
-
generateRefreshToken(user: UserEntity): string;
|
|
84
|
+
generateAccessToken(user: UserEntity | AdminEntity, role: UserRole): string;
|
|
85
|
+
generateRefreshToken(user: UserEntity | AdminEntity): string;
|
|
88
86
|
verifyToken(token: string, secret: string): Promise<IDecodedToken>;
|
|
89
87
|
verifyAccessToken(token: string): Promise<IDecodedToken>;
|
|
90
88
|
verifyRefreshToken(token: string): Promise<IDecodedToken>;
|
|
@@ -95,7 +93,7 @@ declare class LoginUseCase implements IBaseService {
|
|
|
95
93
|
private readonly jwtService;
|
|
96
94
|
constructor(repository: AuthRepository, jwtService: JwtAuthService);
|
|
97
95
|
initialize(): Promise<void>;
|
|
98
|
-
execute(loginDTO: LoginDTO): Promise<{
|
|
96
|
+
execute(loginDTO: LoginDTO, providedAdminPassword?: string): Promise<{
|
|
99
97
|
id: string;
|
|
100
98
|
accessToken: string;
|
|
101
99
|
expiresAt: number;
|
|
@@ -103,13 +101,6 @@ declare class LoginUseCase implements IBaseService {
|
|
|
103
101
|
}>;
|
|
104
102
|
}
|
|
105
103
|
|
|
106
|
-
declare class LoginAdminUseCase implements IBaseService {
|
|
107
|
-
private readonly repository;
|
|
108
|
-
constructor(repository: AuthRepository);
|
|
109
|
-
initialize(): Promise<void>;
|
|
110
|
-
execute(loginDTO: LoginDTO, adminPassword: string): Promise<string>;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
104
|
declare class LogoutUseCase implements IBaseService {
|
|
114
105
|
private readonly repository;
|
|
115
106
|
constructor(repository: AuthRepository);
|
|
@@ -224,7 +215,6 @@ declare class TwoFactorDisableUseCase implements IBaseService {
|
|
|
224
215
|
|
|
225
216
|
declare class AuthController {
|
|
226
217
|
private readonly loginUseCase;
|
|
227
|
-
private readonly adminLoginUseCase;
|
|
228
218
|
private readonly logoutUseCase;
|
|
229
219
|
private readonly registerUseCase;
|
|
230
220
|
private readonly refreshTokenUseCase;
|
|
@@ -232,7 +222,7 @@ declare class AuthController {
|
|
|
232
222
|
private readonly twoFactorEnableUseCase;
|
|
233
223
|
private readonly twoFactorVerifyUseCase;
|
|
234
224
|
private readonly twoFactorDisableUseCase;
|
|
235
|
-
constructor(loginUseCase: LoginUseCase,
|
|
225
|
+
constructor(loginUseCase: LoginUseCase, logoutUseCase: LogoutUseCase, registerUseCase: RegisterUseCase, refreshTokenUseCase: RefreshTokenUseCase, twoFactorSetupUseCase: TwoFactorSetupUseCase, twoFactorEnableUseCase: TwoFactorEnableUseCase, twoFactorVerifyUseCase: TwoFactorVerifyUseCase, twoFactorDisableUseCase: TwoFactorDisableUseCase);
|
|
236
226
|
private createResponse;
|
|
237
227
|
private createAuthData;
|
|
238
228
|
private sendResponse;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseRepository, IExtendedBaseRepository, IBaseService, BaseDatabase, BaseContainer } from 'cca-core';
|
|
2
2
|
import { Request, Response, NextFunction } from 'express';
|
|
3
|
-
import { AuthEntity, UserRole, UserEntity } from 'cca-entities';
|
|
3
|
+
import { AuthEntity, UserRole, UserEntity, AdminEntity } from 'cca-entities';
|
|
4
4
|
import { Repository } from 'typeorm';
|
|
5
5
|
import * as jwt from 'jsonwebtoken';
|
|
6
6
|
import { JwtPayload } from 'jsonwebtoken';
|
|
@@ -22,7 +22,7 @@ declare const authConfig: (configSource: ConfigSource) => void;
|
|
|
22
22
|
|
|
23
23
|
declare class AuthRepository extends BaseRepository<AuthEntity> implements IExtendedBaseRepository<AuthEntity> {
|
|
24
24
|
constructor(repository: Repository<AuthEntity>);
|
|
25
|
-
findByEmail(email: string): Promise<AuthEntity | null>;
|
|
25
|
+
findByEmail(email: string, isAdmin?: boolean): Promise<AuthEntity | null>;
|
|
26
26
|
create(entity: Omit<AuthEntity, "createdAt">): Promise<AuthEntity>;
|
|
27
27
|
findByUserId(userId: string): Promise<AuthEntity | null>;
|
|
28
28
|
findByUseAdminId(userId: string, isAdmin?: boolean): Promise<AuthEntity | null>;
|
|
@@ -67,9 +67,8 @@ interface IDecodedToken extends JwtPayload {
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
interface IAuthService {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
generateRefreshToken(user: UserEntity): string;
|
|
70
|
+
generateAccessToken(user: UserEntity | AdminEntity, role: UserRole): string;
|
|
71
|
+
generateRefreshToken(user: UserEntity | AdminEntity): string;
|
|
73
72
|
verifyAccessToken(token: string): Promise<IDecodedToken>;
|
|
74
73
|
verifyRefreshToken(token: string): IDecodedToken;
|
|
75
74
|
}
|
|
@@ -81,10 +80,9 @@ declare class JwtAuthService implements IBaseService, IAuthService {
|
|
|
81
80
|
private loadConfig;
|
|
82
81
|
initialize(): Promise<void>;
|
|
83
82
|
private validateConfiguration;
|
|
84
|
-
validateUser(email: string, password: string): Promise<AuthEntity | null>;
|
|
85
83
|
private verifyJwtConfig;
|
|
86
|
-
generateAccessToken(user: UserEntity, role: UserRole): string;
|
|
87
|
-
generateRefreshToken(user: UserEntity): string;
|
|
84
|
+
generateAccessToken(user: UserEntity | AdminEntity, role: UserRole): string;
|
|
85
|
+
generateRefreshToken(user: UserEntity | AdminEntity): string;
|
|
88
86
|
verifyToken(token: string, secret: string): Promise<IDecodedToken>;
|
|
89
87
|
verifyAccessToken(token: string): Promise<IDecodedToken>;
|
|
90
88
|
verifyRefreshToken(token: string): Promise<IDecodedToken>;
|
|
@@ -95,7 +93,7 @@ declare class LoginUseCase implements IBaseService {
|
|
|
95
93
|
private readonly jwtService;
|
|
96
94
|
constructor(repository: AuthRepository, jwtService: JwtAuthService);
|
|
97
95
|
initialize(): Promise<void>;
|
|
98
|
-
execute(loginDTO: LoginDTO): Promise<{
|
|
96
|
+
execute(loginDTO: LoginDTO, providedAdminPassword?: string): Promise<{
|
|
99
97
|
id: string;
|
|
100
98
|
accessToken: string;
|
|
101
99
|
expiresAt: number;
|
|
@@ -103,13 +101,6 @@ declare class LoginUseCase implements IBaseService {
|
|
|
103
101
|
}>;
|
|
104
102
|
}
|
|
105
103
|
|
|
106
|
-
declare class LoginAdminUseCase implements IBaseService {
|
|
107
|
-
private readonly repository;
|
|
108
|
-
constructor(repository: AuthRepository);
|
|
109
|
-
initialize(): Promise<void>;
|
|
110
|
-
execute(loginDTO: LoginDTO, adminPassword: string): Promise<string>;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
104
|
declare class LogoutUseCase implements IBaseService {
|
|
114
105
|
private readonly repository;
|
|
115
106
|
constructor(repository: AuthRepository);
|
|
@@ -224,7 +215,6 @@ declare class TwoFactorDisableUseCase implements IBaseService {
|
|
|
224
215
|
|
|
225
216
|
declare class AuthController {
|
|
226
217
|
private readonly loginUseCase;
|
|
227
|
-
private readonly adminLoginUseCase;
|
|
228
218
|
private readonly logoutUseCase;
|
|
229
219
|
private readonly registerUseCase;
|
|
230
220
|
private readonly refreshTokenUseCase;
|
|
@@ -232,7 +222,7 @@ declare class AuthController {
|
|
|
232
222
|
private readonly twoFactorEnableUseCase;
|
|
233
223
|
private readonly twoFactorVerifyUseCase;
|
|
234
224
|
private readonly twoFactorDisableUseCase;
|
|
235
|
-
constructor(loginUseCase: LoginUseCase,
|
|
225
|
+
constructor(loginUseCase: LoginUseCase, logoutUseCase: LogoutUseCase, registerUseCase: RegisterUseCase, refreshTokenUseCase: RefreshTokenUseCase, twoFactorSetupUseCase: TwoFactorSetupUseCase, twoFactorEnableUseCase: TwoFactorEnableUseCase, twoFactorVerifyUseCase: TwoFactorVerifyUseCase, twoFactorDisableUseCase: TwoFactorDisableUseCase);
|
|
236
226
|
private createResponse;
|
|
237
227
|
private createAuthData;
|
|
238
228
|
private sendResponse;
|
package/dist/index.js
CHANGED
|
@@ -163,7 +163,7 @@ var createConfigInstance = /* @__PURE__ */ __name(async () => {
|
|
|
163
163
|
}, "createConfigInstance");
|
|
164
164
|
|
|
165
165
|
// src/infrastructure/container/createAuthContainer.ts
|
|
166
|
-
var
|
|
166
|
+
var import_cca_core10 = require("cca-core");
|
|
167
167
|
var import_cca_entities5 = require("cca-entities");
|
|
168
168
|
|
|
169
169
|
// src/application/useCase/LoginUseCase.ts
|
|
@@ -184,10 +184,10 @@ var schemas = {
|
|
|
184
184
|
),
|
|
185
185
|
role: yup.string().oneOf(Object.values(import_cca_entities.UserRole), "Invalid role specified")
|
|
186
186
|
};
|
|
187
|
-
var validateEmail = /* @__PURE__ */ __name(async (email, repository) => {
|
|
187
|
+
var validateEmail = /* @__PURE__ */ __name(async (email, repository, isAdmin) => {
|
|
188
188
|
try {
|
|
189
189
|
await schemas.email.validate(email?.trim().toLowerCase());
|
|
190
|
-
const user = await repository.findByEmail(email);
|
|
190
|
+
const user = await repository.findByEmail(email, isAdmin);
|
|
191
191
|
if (!user) {
|
|
192
192
|
throw new NotFoundError(
|
|
193
193
|
"The email address or password is incorrect. Please retry"
|
|
@@ -230,13 +230,13 @@ var validateRegisterDTO = /* @__PURE__ */ __name(async (auth, repository) => {
|
|
|
230
230
|
validatePassword(password)
|
|
231
231
|
]);
|
|
232
232
|
}, "validateRegisterDTO");
|
|
233
|
-
var validateLoginDTO = /* @__PURE__ */ __name(async (data, repository) => {
|
|
233
|
+
var validateLoginDTO = /* @__PURE__ */ __name(async (data, repository, isAdmin) => {
|
|
234
234
|
const { email, role, password } = data;
|
|
235
235
|
if (role) {
|
|
236
236
|
await schemas.role.validate(role);
|
|
237
237
|
}
|
|
238
238
|
await schemas.password.validate(password);
|
|
239
|
-
const auth = await validateEmail(email, repository);
|
|
239
|
+
const auth = await validateEmail(email, repository, isAdmin);
|
|
240
240
|
if (!auth || !auth.password) {
|
|
241
241
|
throw new NotFoundError("Invalid credentials");
|
|
242
242
|
}
|
|
@@ -275,8 +275,12 @@ var _LoginUseCase = class _LoginUseCase {
|
|
|
275
275
|
async initialize() {
|
|
276
276
|
await (0, import_cca_core.validateRepository)(this.repository, (repo) => repo.getAll());
|
|
277
277
|
}
|
|
278
|
-
async execute(loginDTO) {
|
|
279
|
-
|
|
278
|
+
async execute(loginDTO, providedAdminPassword) {
|
|
279
|
+
if (providedAdminPassword) {
|
|
280
|
+
await validateAdminSecret(providedAdminPassword);
|
|
281
|
+
}
|
|
282
|
+
const isAdmin = !!providedAdminPassword;
|
|
283
|
+
const auth = await validateLoginDTO(loginDTO, this.repository, isAdmin);
|
|
280
284
|
const accessToken = this.jwtService.generateAccessToken(auth.user, auth.role);
|
|
281
285
|
const expiresAt = (0, import_jwt_decode.jwtDecode)(accessToken).exp;
|
|
282
286
|
return { id: auth.user.id, accessToken, expiresAt, enabled: auth.twoFactorEnabled };
|
|
@@ -285,32 +289,14 @@ var _LoginUseCase = class _LoginUseCase {
|
|
|
285
289
|
__name(_LoginUseCase, "LoginUseCase");
|
|
286
290
|
var LoginUseCase = _LoginUseCase;
|
|
287
291
|
|
|
288
|
-
// src/application/useCase/LoginAdminUseCase.ts
|
|
289
|
-
var import_cca_core2 = require("cca-core");
|
|
290
|
-
var _LoginAdminUseCase = class _LoginAdminUseCase {
|
|
291
|
-
constructor(repository) {
|
|
292
|
-
this.repository = repository;
|
|
293
|
-
}
|
|
294
|
-
async initialize() {
|
|
295
|
-
await (0, import_cca_core2.validateRepository)(this.repository, (repo) => repo.getAll());
|
|
296
|
-
}
|
|
297
|
-
async execute(loginDTO, adminPassword) {
|
|
298
|
-
const auth = await validateLoginDTO(loginDTO, this.repository);
|
|
299
|
-
await validateAdminSecret(adminPassword);
|
|
300
|
-
return auth.user.id;
|
|
301
|
-
}
|
|
302
|
-
};
|
|
303
|
-
__name(_LoginAdminUseCase, "LoginAdminUseCase");
|
|
304
|
-
var LoginAdminUseCase = _LoginAdminUseCase;
|
|
305
|
-
|
|
306
292
|
// src/application/useCase/LogoutUseCase.ts
|
|
307
|
-
var
|
|
293
|
+
var import_cca_core2 = require("cca-core");
|
|
308
294
|
var _LogoutUseCase = class _LogoutUseCase {
|
|
309
295
|
constructor(repository) {
|
|
310
296
|
this.repository = repository;
|
|
311
297
|
}
|
|
312
298
|
async initialize() {
|
|
313
|
-
await (0,
|
|
299
|
+
await (0, import_cca_core2.validateRepository)(this.repository, (repo) => repo.getAll());
|
|
314
300
|
}
|
|
315
301
|
async execute(authId) {
|
|
316
302
|
try {
|
|
@@ -324,7 +310,7 @@ __name(_LogoutUseCase, "LogoutUseCase");
|
|
|
324
310
|
var LogoutUseCase = _LogoutUseCase;
|
|
325
311
|
|
|
326
312
|
// src/application/useCase/RegisterUseCase.ts
|
|
327
|
-
var
|
|
313
|
+
var import_cca_core3 = require("cca-core");
|
|
328
314
|
var bcrypt2 = __toESM(require("bcrypt"));
|
|
329
315
|
var import_cca_entities3 = require("cca-entities");
|
|
330
316
|
|
|
@@ -445,7 +431,7 @@ var _RegisterUseCase = class _RegisterUseCase {
|
|
|
445
431
|
this.SALT_ROUNDS = 10;
|
|
446
432
|
}
|
|
447
433
|
async initialize() {
|
|
448
|
-
await (0,
|
|
434
|
+
await (0, import_cca_core3.validateRepository)(this.repository, (repo) => repo.getAll());
|
|
449
435
|
}
|
|
450
436
|
async execute(email, name, password, role = import_cca_entities3.UserRole.GUEST, adminPassword) {
|
|
451
437
|
try {
|
|
@@ -488,11 +474,7 @@ var _RegisterUseCase = class _RegisterUseCase {
|
|
|
488
474
|
const authEntity = mapper.map(dto, RegisterDTO, import_cca_entities3.AuthEntity);
|
|
489
475
|
const userOrAdminEntity = isAdmin ? mapper.map(dto, RegisterDTO, import_cca_entities3.AdminEntity) : mapper.map(dto, RegisterDTO, import_cca_entities3.UserEntity);
|
|
490
476
|
userOrAdminEntity.updatedAt = void 0;
|
|
491
|
-
|
|
492
|
-
authEntity.admin = userOrAdminEntity;
|
|
493
|
-
} else {
|
|
494
|
-
authEntity.user = userOrAdminEntity;
|
|
495
|
-
}
|
|
477
|
+
authEntity.user = userOrAdminEntity;
|
|
496
478
|
authEntity.password = hashedPassword;
|
|
497
479
|
authEntity.refreshToken = "";
|
|
498
480
|
return authEntity;
|
|
@@ -502,14 +484,14 @@ __name(_RegisterUseCase, "RegisterUseCase");
|
|
|
502
484
|
var RegisterUseCase = _RegisterUseCase;
|
|
503
485
|
|
|
504
486
|
// src/application/useCase/RefreshTokenUseCase.ts
|
|
505
|
-
var
|
|
487
|
+
var import_cca_core4 = require("cca-core");
|
|
506
488
|
var _RefreshTokenUseCase = class _RefreshTokenUseCase {
|
|
507
489
|
constructor(repository, service) {
|
|
508
490
|
this.repository = repository;
|
|
509
491
|
this.service = service;
|
|
510
492
|
}
|
|
511
493
|
async initialize() {
|
|
512
|
-
await (0,
|
|
494
|
+
await (0, import_cca_core4.validateRepository)(this.repository, (repo) => repo.getAll());
|
|
513
495
|
}
|
|
514
496
|
async execute(refreshToken) {
|
|
515
497
|
try {
|
|
@@ -524,7 +506,7 @@ var _RefreshTokenUseCase = class _RefreshTokenUseCase {
|
|
|
524
506
|
if (!authEntity) {
|
|
525
507
|
return null;
|
|
526
508
|
}
|
|
527
|
-
const user = authEntity.
|
|
509
|
+
const user = authEntity.user;
|
|
528
510
|
const accessToken = this.service.generateAccessToken(user, authEntity.role);
|
|
529
511
|
const newRefreshToken = this.service.generateRefreshToken(user);
|
|
530
512
|
await this.repository.update(authEntity.id, {
|
|
@@ -577,7 +559,7 @@ __name(_TwoFactorSetupUseCase, "TwoFactorSetupUseCase");
|
|
|
577
559
|
var TwoFactorSetupUseCase = _TwoFactorSetupUseCase;
|
|
578
560
|
|
|
579
561
|
// src/application/useCase/TwoFactorEnableUseCase.ts
|
|
580
|
-
var
|
|
562
|
+
var import_cca_core5 = require("cca-core");
|
|
581
563
|
var _TwoFactorEnableUseCase = class _TwoFactorEnableUseCase {
|
|
582
564
|
constructor(twoFactorService, authRepository) {
|
|
583
565
|
this.isInitialized = false;
|
|
@@ -588,7 +570,7 @@ var _TwoFactorEnableUseCase = class _TwoFactorEnableUseCase {
|
|
|
588
570
|
if (this.isInitialized) return;
|
|
589
571
|
await Promise.all([
|
|
590
572
|
this.twoFactorService.initialize(),
|
|
591
|
-
(0,
|
|
573
|
+
(0, import_cca_core5.validateRepository)(this.authRepository, (repo) => repo.getAll())
|
|
592
574
|
]);
|
|
593
575
|
this.isInitialized = true;
|
|
594
576
|
}
|
|
@@ -618,7 +600,7 @@ __name(_TwoFactorEnableUseCase, "TwoFactorEnableUseCase");
|
|
|
618
600
|
var TwoFactorEnableUseCase = _TwoFactorEnableUseCase;
|
|
619
601
|
|
|
620
602
|
// src/application/useCase/TwoFactorVerifyUseCase.ts
|
|
621
|
-
var
|
|
603
|
+
var import_cca_core6 = require("cca-core");
|
|
622
604
|
var import_cca_entities4 = require("cca-entities");
|
|
623
605
|
var _TwoFactorVerifyUseCase = class _TwoFactorVerifyUseCase {
|
|
624
606
|
constructor(twoFactorService, authRepository, jwtService) {
|
|
@@ -632,7 +614,7 @@ var _TwoFactorVerifyUseCase = class _TwoFactorVerifyUseCase {
|
|
|
632
614
|
await Promise.all([
|
|
633
615
|
this.twoFactorService.initialize(),
|
|
634
616
|
this.jwtService.initialize(),
|
|
635
|
-
(0,
|
|
617
|
+
(0, import_cca_core6.validateRepository)(this.authRepository, (repo) => repo.getAll())
|
|
636
618
|
]);
|
|
637
619
|
this.isInitialized = true;
|
|
638
620
|
}
|
|
@@ -655,18 +637,11 @@ var _TwoFactorVerifyUseCase = class _TwoFactorVerifyUseCase {
|
|
|
655
637
|
const tokenPair = this.generateTokens(auth);
|
|
656
638
|
await this.updateUserStatus(auth);
|
|
657
639
|
await this.updateUserRefreshToken(auth, tokenPair.refreshToken);
|
|
658
|
-
if (auth.admin) {
|
|
659
|
-
return {
|
|
660
|
-
token: tokenPair.accessToken,
|
|
661
|
-
refreshToken: tokenPair.refreshToken,
|
|
662
|
-
data: this.mapAdminToDTO(auth.admin)
|
|
663
|
-
};
|
|
664
|
-
}
|
|
665
640
|
if (auth.user) {
|
|
666
641
|
return {
|
|
667
642
|
token: tokenPair.accessToken,
|
|
668
643
|
refreshToken: tokenPair.refreshToken,
|
|
669
|
-
data: this.
|
|
644
|
+
data: this.mapAdminToDTO(auth.user)
|
|
670
645
|
};
|
|
671
646
|
}
|
|
672
647
|
return null;
|
|
@@ -697,7 +672,7 @@ __name(_TwoFactorVerifyUseCase, "TwoFactorVerifyUseCase");
|
|
|
697
672
|
var TwoFactorVerifyUseCase = _TwoFactorVerifyUseCase;
|
|
698
673
|
|
|
699
674
|
// src/application/useCase/TwoFactorDisableUseCase.ts
|
|
700
|
-
var
|
|
675
|
+
var import_cca_core7 = require("cca-core");
|
|
701
676
|
var _TwoFactorDisableUseCase = class _TwoFactorDisableUseCase {
|
|
702
677
|
constructor(twoFactorService, authRepository) {
|
|
703
678
|
this.isInitialized = false;
|
|
@@ -708,7 +683,7 @@ var _TwoFactorDisableUseCase = class _TwoFactorDisableUseCase {
|
|
|
708
683
|
if (this.isInitialized) return;
|
|
709
684
|
await Promise.all([
|
|
710
685
|
this.twoFactorService.initialize(),
|
|
711
|
-
(0,
|
|
686
|
+
(0, import_cca_core7.validateRepository)(this.authRepository, (repo) => repo.getAll())
|
|
712
687
|
]);
|
|
713
688
|
4;
|
|
714
689
|
this.isInitialized = true;
|
|
@@ -760,7 +735,7 @@ var MESSAGES = {
|
|
|
760
735
|
|
|
761
736
|
// src/presentation/controller/AuthController.ts
|
|
762
737
|
var _AuthController = class _AuthController {
|
|
763
|
-
constructor(loginUseCase,
|
|
738
|
+
constructor(loginUseCase, logoutUseCase, registerUseCase, refreshTokenUseCase, twoFactorSetupUseCase, twoFactorEnableUseCase, twoFactorVerifyUseCase, twoFactorDisableUseCase) {
|
|
764
739
|
this.login = /* @__PURE__ */ __name(async (req, res, next) => {
|
|
765
740
|
try {
|
|
766
741
|
const loginDTO = req.body;
|
|
@@ -791,7 +766,7 @@ var _AuthController = class _AuthController {
|
|
|
791
766
|
if (!adminPassword) {
|
|
792
767
|
throw new ForbiddenError("Admin password is required");
|
|
793
768
|
}
|
|
794
|
-
const result = await this.
|
|
769
|
+
const result = await this.loginUseCase.execute(loginDTO, adminPassword);
|
|
795
770
|
const adminLoginData = {
|
|
796
771
|
message: result,
|
|
797
772
|
auth: this.createAuthData(
|
|
@@ -946,7 +921,6 @@ var _AuthController = class _AuthController {
|
|
|
946
921
|
}
|
|
947
922
|
}, "disable2FA");
|
|
948
923
|
this.loginUseCase = loginUseCase;
|
|
949
|
-
this.adminLoginUseCase = adminLoginUseCase;
|
|
950
924
|
this.logoutUseCase = logoutUseCase;
|
|
951
925
|
this.registerUseCase = registerUseCase;
|
|
952
926
|
this.refreshTokenUseCase = refreshTokenUseCase;
|
|
@@ -1012,14 +986,14 @@ __name(_RequireComplete2FA, "RequireComplete2FA");
|
|
|
1012
986
|
var RequireComplete2FA = _RequireComplete2FA;
|
|
1013
987
|
|
|
1014
988
|
// src/infrastructure/repository/AuthRepository.ts
|
|
1015
|
-
var
|
|
1016
|
-
var _AuthRepository = class _AuthRepository extends
|
|
989
|
+
var import_cca_core8 = require("cca-core");
|
|
990
|
+
var _AuthRepository = class _AuthRepository extends import_cca_core8.BaseRepository {
|
|
1017
991
|
constructor(repository) {
|
|
1018
992
|
super(repository);
|
|
1019
993
|
}
|
|
1020
|
-
async findByEmail(email) {
|
|
1021
|
-
const
|
|
1022
|
-
return await
|
|
994
|
+
async findByEmail(email, isAdmin) {
|
|
995
|
+
const alias = isAdmin ? "admin" : "user";
|
|
996
|
+
return await this.repository.createQueryBuilder("auth").leftJoinAndSelect("auth.user", alias).addSelect("auth.password").where("auth.email = :email", { email }).getOne();
|
|
1023
997
|
}
|
|
1024
998
|
async create(entity) {
|
|
1025
999
|
return super.create(entity);
|
|
@@ -1084,8 +1058,7 @@ var AuthRepository = _AuthRepository;
|
|
|
1084
1058
|
|
|
1085
1059
|
// src/infrastructure/services/JwtAuthService.ts
|
|
1086
1060
|
var jwt = __toESM(require("jsonwebtoken"));
|
|
1087
|
-
var
|
|
1088
|
-
var import_cca_core10 = require("cca-core");
|
|
1061
|
+
var import_cca_core9 = require("cca-core");
|
|
1089
1062
|
var _JwtAuthService = class _JwtAuthService {
|
|
1090
1063
|
constructor(repository, config) {
|
|
1091
1064
|
this.repository = repository;
|
|
@@ -1103,24 +1076,13 @@ var _JwtAuthService = class _JwtAuthService {
|
|
|
1103
1076
|
this.validateConfiguration();
|
|
1104
1077
|
}
|
|
1105
1078
|
async initialize() {
|
|
1106
|
-
await (0,
|
|
1079
|
+
await (0, import_cca_core9.validateRepository)(this.repository, (repo) => repo.getAll());
|
|
1107
1080
|
}
|
|
1108
1081
|
validateConfiguration() {
|
|
1109
1082
|
if (!this.jwtConfig?.accessTokenSecret || !this.jwtConfig?.refreshTokenSecret) {
|
|
1110
1083
|
throw new JwtError("JWT secrets required in config");
|
|
1111
1084
|
}
|
|
1112
1085
|
}
|
|
1113
|
-
async validateUser(email, password) {
|
|
1114
|
-
const user = await this.repository.findByEmail(email);
|
|
1115
|
-
if (!user) {
|
|
1116
|
-
throw new NotFoundError("Invalid credentials");
|
|
1117
|
-
}
|
|
1118
|
-
const validPassword = await bcrypt3.compare(password, user.password);
|
|
1119
|
-
if (!validPassword) {
|
|
1120
|
-
throw new ForbiddenError("Invalid credentials");
|
|
1121
|
-
}
|
|
1122
|
-
return user;
|
|
1123
|
-
}
|
|
1124
1086
|
verifyJwtConfig() {
|
|
1125
1087
|
if (!this.jwtConfig) throw new JwtError("JWT config not loaded");
|
|
1126
1088
|
}
|
|
@@ -1255,7 +1217,7 @@ var TwoFactorService = _TwoFactorService;
|
|
|
1255
1217
|
|
|
1256
1218
|
// src/infrastructure/container/createAuthContainer.ts
|
|
1257
1219
|
async function createAuthContainer(database) {
|
|
1258
|
-
const container = new
|
|
1220
|
+
const container = new import_cca_core10.BaseContainer({ database });
|
|
1259
1221
|
const authRepository = new AuthRepository(
|
|
1260
1222
|
database.getRepository(import_cca_entities5.AuthEntity)
|
|
1261
1223
|
);
|
|
@@ -1267,9 +1229,6 @@ async function createAuthContainer(database) {
|
|
|
1267
1229
|
container.registerService("TwoFactorService", twoFactorService);
|
|
1268
1230
|
const requireComplete2FA = new RequireComplete2FA(jwtAuthService);
|
|
1269
1231
|
const loginUseCase = new LoginUseCase(authRepository, jwtAuthService);
|
|
1270
|
-
const loginAdminUseCase = new LoginAdminUseCase(
|
|
1271
|
-
authRepository
|
|
1272
|
-
);
|
|
1273
1232
|
const logoutUseCase = new LogoutUseCase(authRepository);
|
|
1274
1233
|
const registerUseCase = new RegisterUseCase(authRepository);
|
|
1275
1234
|
const refreshTokenUseCase = new RefreshTokenUseCase(
|
|
@@ -1285,7 +1244,6 @@ async function createAuthContainer(database) {
|
|
|
1285
1244
|
);
|
|
1286
1245
|
const twoFactorDisableUseCase = new TwoFactorDisableUseCase(twoFactorService, authRepository);
|
|
1287
1246
|
container.registerService("LoginUseCase", loginUseCase);
|
|
1288
|
-
container.registerService("LoginAdminUseCase", loginAdminUseCase);
|
|
1289
1247
|
container.registerService("LogoutUseCase", logoutUseCase);
|
|
1290
1248
|
container.registerService("RegisterUseCase", registerUseCase);
|
|
1291
1249
|
container.registerService("RefreshTokenUseCase", refreshTokenUseCase);
|
|
@@ -1295,7 +1253,6 @@ async function createAuthContainer(database) {
|
|
|
1295
1253
|
container.registerService("TwoFactorDisableUseCase", twoFactorDisableUseCase);
|
|
1296
1254
|
const authController = new AuthController(
|
|
1297
1255
|
loginUseCase,
|
|
1298
|
-
loginAdminUseCase,
|
|
1299
1256
|
logoutUseCase,
|
|
1300
1257
|
registerUseCase,
|
|
1301
1258
|
refreshTokenUseCase,
|