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
package/dist/index.mjs
CHANGED
|
@@ -149,10 +149,10 @@ var schemas = {
|
|
|
149
149
|
),
|
|
150
150
|
role: yup.string().oneOf(Object.values(UserRole), "Invalid role specified")
|
|
151
151
|
};
|
|
152
|
-
var validateEmail = /* @__PURE__ */ __name(async (email, repository) => {
|
|
152
|
+
var validateEmail = /* @__PURE__ */ __name(async (email, repository, isAdmin) => {
|
|
153
153
|
try {
|
|
154
154
|
await schemas.email.validate(email?.trim().toLowerCase());
|
|
155
|
-
const user = await repository.findByEmail(email);
|
|
155
|
+
const user = await repository.findByEmail(email, isAdmin);
|
|
156
156
|
if (!user) {
|
|
157
157
|
throw new NotFoundError(
|
|
158
158
|
"The email address or password is incorrect. Please retry"
|
|
@@ -195,13 +195,13 @@ var validateRegisterDTO = /* @__PURE__ */ __name(async (auth, repository) => {
|
|
|
195
195
|
validatePassword(password)
|
|
196
196
|
]);
|
|
197
197
|
}, "validateRegisterDTO");
|
|
198
|
-
var validateLoginDTO = /* @__PURE__ */ __name(async (data, repository) => {
|
|
198
|
+
var validateLoginDTO = /* @__PURE__ */ __name(async (data, repository, isAdmin) => {
|
|
199
199
|
const { email, role, password } = data;
|
|
200
200
|
if (role) {
|
|
201
201
|
await schemas.role.validate(role);
|
|
202
202
|
}
|
|
203
203
|
await schemas.password.validate(password);
|
|
204
|
-
const auth = await validateEmail(email, repository);
|
|
204
|
+
const auth = await validateEmail(email, repository, isAdmin);
|
|
205
205
|
if (!auth || !auth.password) {
|
|
206
206
|
throw new NotFoundError("Invalid credentials");
|
|
207
207
|
}
|
|
@@ -240,8 +240,12 @@ var _LoginUseCase = class _LoginUseCase {
|
|
|
240
240
|
async initialize() {
|
|
241
241
|
await validateRepository(this.repository, (repo) => repo.getAll());
|
|
242
242
|
}
|
|
243
|
-
async execute(loginDTO) {
|
|
244
|
-
|
|
243
|
+
async execute(loginDTO, providedAdminPassword) {
|
|
244
|
+
if (providedAdminPassword) {
|
|
245
|
+
await validateAdminSecret(providedAdminPassword);
|
|
246
|
+
}
|
|
247
|
+
const isAdmin = !!providedAdminPassword;
|
|
248
|
+
const auth = await validateLoginDTO(loginDTO, this.repository, isAdmin);
|
|
245
249
|
const accessToken = this.jwtService.generateAccessToken(auth.user, auth.role);
|
|
246
250
|
const expiresAt = jwtDecode(accessToken).exp;
|
|
247
251
|
return { id: auth.user.id, accessToken, expiresAt, enabled: auth.twoFactorEnabled };
|
|
@@ -250,32 +254,14 @@ var _LoginUseCase = class _LoginUseCase {
|
|
|
250
254
|
__name(_LoginUseCase, "LoginUseCase");
|
|
251
255
|
var LoginUseCase = _LoginUseCase;
|
|
252
256
|
|
|
253
|
-
// src/application/useCase/LoginAdminUseCase.ts
|
|
254
|
-
import { validateRepository as validateRepository2 } from "cca-core";
|
|
255
|
-
var _LoginAdminUseCase = class _LoginAdminUseCase {
|
|
256
|
-
constructor(repository) {
|
|
257
|
-
this.repository = repository;
|
|
258
|
-
}
|
|
259
|
-
async initialize() {
|
|
260
|
-
await validateRepository2(this.repository, (repo) => repo.getAll());
|
|
261
|
-
}
|
|
262
|
-
async execute(loginDTO, adminPassword) {
|
|
263
|
-
const auth = await validateLoginDTO(loginDTO, this.repository);
|
|
264
|
-
await validateAdminSecret(adminPassword);
|
|
265
|
-
return auth.user.id;
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
__name(_LoginAdminUseCase, "LoginAdminUseCase");
|
|
269
|
-
var LoginAdminUseCase = _LoginAdminUseCase;
|
|
270
|
-
|
|
271
257
|
// src/application/useCase/LogoutUseCase.ts
|
|
272
|
-
import { validateRepository as
|
|
258
|
+
import { validateRepository as validateRepository2 } from "cca-core";
|
|
273
259
|
var _LogoutUseCase = class _LogoutUseCase {
|
|
274
260
|
constructor(repository) {
|
|
275
261
|
this.repository = repository;
|
|
276
262
|
}
|
|
277
263
|
async initialize() {
|
|
278
|
-
await
|
|
264
|
+
await validateRepository2(this.repository, (repo) => repo.getAll());
|
|
279
265
|
}
|
|
280
266
|
async execute(authId) {
|
|
281
267
|
try {
|
|
@@ -289,7 +275,7 @@ __name(_LogoutUseCase, "LogoutUseCase");
|
|
|
289
275
|
var LogoutUseCase = _LogoutUseCase;
|
|
290
276
|
|
|
291
277
|
// src/application/useCase/RegisterUseCase.ts
|
|
292
|
-
import { validateRepository as
|
|
278
|
+
import { validateRepository as validateRepository3 } from "cca-core";
|
|
293
279
|
import * as bcrypt2 from "bcrypt";
|
|
294
280
|
import { AdminEntity as AdminEntity2, AuthEntity as AuthEntity3, UserEntity as UserEntity2, UserRole as UserRole2 } from "cca-entities";
|
|
295
281
|
|
|
@@ -410,7 +396,7 @@ var _RegisterUseCase = class _RegisterUseCase {
|
|
|
410
396
|
this.SALT_ROUNDS = 10;
|
|
411
397
|
}
|
|
412
398
|
async initialize() {
|
|
413
|
-
await
|
|
399
|
+
await validateRepository3(this.repository, (repo) => repo.getAll());
|
|
414
400
|
}
|
|
415
401
|
async execute(email, name, password, role = UserRole2.GUEST, adminPassword) {
|
|
416
402
|
try {
|
|
@@ -453,11 +439,7 @@ var _RegisterUseCase = class _RegisterUseCase {
|
|
|
453
439
|
const authEntity = mapper.map(dto, RegisterDTO, AuthEntity3);
|
|
454
440
|
const userOrAdminEntity = isAdmin ? mapper.map(dto, RegisterDTO, AdminEntity2) : mapper.map(dto, RegisterDTO, UserEntity2);
|
|
455
441
|
userOrAdminEntity.updatedAt = void 0;
|
|
456
|
-
|
|
457
|
-
authEntity.admin = userOrAdminEntity;
|
|
458
|
-
} else {
|
|
459
|
-
authEntity.user = userOrAdminEntity;
|
|
460
|
-
}
|
|
442
|
+
authEntity.user = userOrAdminEntity;
|
|
461
443
|
authEntity.password = hashedPassword;
|
|
462
444
|
authEntity.refreshToken = "";
|
|
463
445
|
return authEntity;
|
|
@@ -467,14 +449,14 @@ __name(_RegisterUseCase, "RegisterUseCase");
|
|
|
467
449
|
var RegisterUseCase = _RegisterUseCase;
|
|
468
450
|
|
|
469
451
|
// src/application/useCase/RefreshTokenUseCase.ts
|
|
470
|
-
import { validateRepository as
|
|
452
|
+
import { validateRepository as validateRepository4 } from "cca-core";
|
|
471
453
|
var _RefreshTokenUseCase = class _RefreshTokenUseCase {
|
|
472
454
|
constructor(repository, service) {
|
|
473
455
|
this.repository = repository;
|
|
474
456
|
this.service = service;
|
|
475
457
|
}
|
|
476
458
|
async initialize() {
|
|
477
|
-
await
|
|
459
|
+
await validateRepository4(this.repository, (repo) => repo.getAll());
|
|
478
460
|
}
|
|
479
461
|
async execute(refreshToken) {
|
|
480
462
|
try {
|
|
@@ -489,7 +471,7 @@ var _RefreshTokenUseCase = class _RefreshTokenUseCase {
|
|
|
489
471
|
if (!authEntity) {
|
|
490
472
|
return null;
|
|
491
473
|
}
|
|
492
|
-
const user = authEntity.
|
|
474
|
+
const user = authEntity.user;
|
|
493
475
|
const accessToken = this.service.generateAccessToken(user, authEntity.role);
|
|
494
476
|
const newRefreshToken = this.service.generateRefreshToken(user);
|
|
495
477
|
await this.repository.update(authEntity.id, {
|
|
@@ -542,7 +524,7 @@ __name(_TwoFactorSetupUseCase, "TwoFactorSetupUseCase");
|
|
|
542
524
|
var TwoFactorSetupUseCase = _TwoFactorSetupUseCase;
|
|
543
525
|
|
|
544
526
|
// src/application/useCase/TwoFactorEnableUseCase.ts
|
|
545
|
-
import { validateRepository as
|
|
527
|
+
import { validateRepository as validateRepository5 } from "cca-core";
|
|
546
528
|
var _TwoFactorEnableUseCase = class _TwoFactorEnableUseCase {
|
|
547
529
|
constructor(twoFactorService, authRepository) {
|
|
548
530
|
this.isInitialized = false;
|
|
@@ -553,7 +535,7 @@ var _TwoFactorEnableUseCase = class _TwoFactorEnableUseCase {
|
|
|
553
535
|
if (this.isInitialized) return;
|
|
554
536
|
await Promise.all([
|
|
555
537
|
this.twoFactorService.initialize(),
|
|
556
|
-
|
|
538
|
+
validateRepository5(this.authRepository, (repo) => repo.getAll())
|
|
557
539
|
]);
|
|
558
540
|
this.isInitialized = true;
|
|
559
541
|
}
|
|
@@ -583,7 +565,7 @@ __name(_TwoFactorEnableUseCase, "TwoFactorEnableUseCase");
|
|
|
583
565
|
var TwoFactorEnableUseCase = _TwoFactorEnableUseCase;
|
|
584
566
|
|
|
585
567
|
// src/application/useCase/TwoFactorVerifyUseCase.ts
|
|
586
|
-
import { validateRepository as
|
|
568
|
+
import { validateRepository as validateRepository6 } from "cca-core";
|
|
587
569
|
import { AdminEntity as AdminEntity3, UserEntity as UserEntity3 } from "cca-entities";
|
|
588
570
|
var _TwoFactorVerifyUseCase = class _TwoFactorVerifyUseCase {
|
|
589
571
|
constructor(twoFactorService, authRepository, jwtService) {
|
|
@@ -597,7 +579,7 @@ var _TwoFactorVerifyUseCase = class _TwoFactorVerifyUseCase {
|
|
|
597
579
|
await Promise.all([
|
|
598
580
|
this.twoFactorService.initialize(),
|
|
599
581
|
this.jwtService.initialize(),
|
|
600
|
-
|
|
582
|
+
validateRepository6(this.authRepository, (repo) => repo.getAll())
|
|
601
583
|
]);
|
|
602
584
|
this.isInitialized = true;
|
|
603
585
|
}
|
|
@@ -620,18 +602,11 @@ var _TwoFactorVerifyUseCase = class _TwoFactorVerifyUseCase {
|
|
|
620
602
|
const tokenPair = this.generateTokens(auth);
|
|
621
603
|
await this.updateUserStatus(auth);
|
|
622
604
|
await this.updateUserRefreshToken(auth, tokenPair.refreshToken);
|
|
623
|
-
if (auth.admin) {
|
|
624
|
-
return {
|
|
625
|
-
token: tokenPair.accessToken,
|
|
626
|
-
refreshToken: tokenPair.refreshToken,
|
|
627
|
-
data: this.mapAdminToDTO(auth.admin)
|
|
628
|
-
};
|
|
629
|
-
}
|
|
630
605
|
if (auth.user) {
|
|
631
606
|
return {
|
|
632
607
|
token: tokenPair.accessToken,
|
|
633
608
|
refreshToken: tokenPair.refreshToken,
|
|
634
|
-
data: this.
|
|
609
|
+
data: this.mapAdminToDTO(auth.user)
|
|
635
610
|
};
|
|
636
611
|
}
|
|
637
612
|
return null;
|
|
@@ -662,7 +637,7 @@ __name(_TwoFactorVerifyUseCase, "TwoFactorVerifyUseCase");
|
|
|
662
637
|
var TwoFactorVerifyUseCase = _TwoFactorVerifyUseCase;
|
|
663
638
|
|
|
664
639
|
// src/application/useCase/TwoFactorDisableUseCase.ts
|
|
665
|
-
import { validateRepository as
|
|
640
|
+
import { validateRepository as validateRepository7 } from "cca-core";
|
|
666
641
|
var _TwoFactorDisableUseCase = class _TwoFactorDisableUseCase {
|
|
667
642
|
constructor(twoFactorService, authRepository) {
|
|
668
643
|
this.isInitialized = false;
|
|
@@ -673,7 +648,7 @@ var _TwoFactorDisableUseCase = class _TwoFactorDisableUseCase {
|
|
|
673
648
|
if (this.isInitialized) return;
|
|
674
649
|
await Promise.all([
|
|
675
650
|
this.twoFactorService.initialize(),
|
|
676
|
-
|
|
651
|
+
validateRepository7(this.authRepository, (repo) => repo.getAll())
|
|
677
652
|
]);
|
|
678
653
|
4;
|
|
679
654
|
this.isInitialized = true;
|
|
@@ -725,7 +700,7 @@ var MESSAGES = {
|
|
|
725
700
|
|
|
726
701
|
// src/presentation/controller/AuthController.ts
|
|
727
702
|
var _AuthController = class _AuthController {
|
|
728
|
-
constructor(loginUseCase,
|
|
703
|
+
constructor(loginUseCase, logoutUseCase, registerUseCase, refreshTokenUseCase, twoFactorSetupUseCase, twoFactorEnableUseCase, twoFactorVerifyUseCase, twoFactorDisableUseCase) {
|
|
729
704
|
this.login = /* @__PURE__ */ __name(async (req, res, next) => {
|
|
730
705
|
try {
|
|
731
706
|
const loginDTO = req.body;
|
|
@@ -756,7 +731,7 @@ var _AuthController = class _AuthController {
|
|
|
756
731
|
if (!adminPassword) {
|
|
757
732
|
throw new ForbiddenError("Admin password is required");
|
|
758
733
|
}
|
|
759
|
-
const result = await this.
|
|
734
|
+
const result = await this.loginUseCase.execute(loginDTO, adminPassword);
|
|
760
735
|
const adminLoginData = {
|
|
761
736
|
message: result,
|
|
762
737
|
auth: this.createAuthData(
|
|
@@ -911,7 +886,6 @@ var _AuthController = class _AuthController {
|
|
|
911
886
|
}
|
|
912
887
|
}, "disable2FA");
|
|
913
888
|
this.loginUseCase = loginUseCase;
|
|
914
|
-
this.adminLoginUseCase = adminLoginUseCase;
|
|
915
889
|
this.logoutUseCase = logoutUseCase;
|
|
916
890
|
this.registerUseCase = registerUseCase;
|
|
917
891
|
this.refreshTokenUseCase = refreshTokenUseCase;
|
|
@@ -982,9 +956,9 @@ var _AuthRepository = class _AuthRepository extends BaseRepository {
|
|
|
982
956
|
constructor(repository) {
|
|
983
957
|
super(repository);
|
|
984
958
|
}
|
|
985
|
-
async findByEmail(email) {
|
|
986
|
-
const
|
|
987
|
-
return await
|
|
959
|
+
async findByEmail(email, isAdmin) {
|
|
960
|
+
const alias = isAdmin ? "admin" : "user";
|
|
961
|
+
return await this.repository.createQueryBuilder("auth").leftJoinAndSelect("auth.user", alias).addSelect("auth.password").where("auth.email = :email", { email }).getOne();
|
|
988
962
|
}
|
|
989
963
|
async create(entity) {
|
|
990
964
|
return super.create(entity);
|
|
@@ -1049,8 +1023,7 @@ var AuthRepository = _AuthRepository;
|
|
|
1049
1023
|
|
|
1050
1024
|
// src/infrastructure/services/JwtAuthService.ts
|
|
1051
1025
|
import * as jwt from "jsonwebtoken";
|
|
1052
|
-
import
|
|
1053
|
-
import { validateRepository as validateRepository9 } from "cca-core";
|
|
1026
|
+
import { validateRepository as validateRepository8 } from "cca-core";
|
|
1054
1027
|
var _JwtAuthService = class _JwtAuthService {
|
|
1055
1028
|
constructor(repository, config) {
|
|
1056
1029
|
this.repository = repository;
|
|
@@ -1068,24 +1041,13 @@ var _JwtAuthService = class _JwtAuthService {
|
|
|
1068
1041
|
this.validateConfiguration();
|
|
1069
1042
|
}
|
|
1070
1043
|
async initialize() {
|
|
1071
|
-
await
|
|
1044
|
+
await validateRepository8(this.repository, (repo) => repo.getAll());
|
|
1072
1045
|
}
|
|
1073
1046
|
validateConfiguration() {
|
|
1074
1047
|
if (!this.jwtConfig?.accessTokenSecret || !this.jwtConfig?.refreshTokenSecret) {
|
|
1075
1048
|
throw new JwtError("JWT secrets required in config");
|
|
1076
1049
|
}
|
|
1077
1050
|
}
|
|
1078
|
-
async validateUser(email, password) {
|
|
1079
|
-
const user = await this.repository.findByEmail(email);
|
|
1080
|
-
if (!user) {
|
|
1081
|
-
throw new NotFoundError("Invalid credentials");
|
|
1082
|
-
}
|
|
1083
|
-
const validPassword = await bcrypt3.compare(password, user.password);
|
|
1084
|
-
if (!validPassword) {
|
|
1085
|
-
throw new ForbiddenError("Invalid credentials");
|
|
1086
|
-
}
|
|
1087
|
-
return user;
|
|
1088
|
-
}
|
|
1089
1051
|
verifyJwtConfig() {
|
|
1090
1052
|
if (!this.jwtConfig) throw new JwtError("JWT config not loaded");
|
|
1091
1053
|
}
|
|
@@ -1232,9 +1194,6 @@ async function createAuthContainer(database) {
|
|
|
1232
1194
|
container.registerService("TwoFactorService", twoFactorService);
|
|
1233
1195
|
const requireComplete2FA = new RequireComplete2FA(jwtAuthService);
|
|
1234
1196
|
const loginUseCase = new LoginUseCase(authRepository, jwtAuthService);
|
|
1235
|
-
const loginAdminUseCase = new LoginAdminUseCase(
|
|
1236
|
-
authRepository
|
|
1237
|
-
);
|
|
1238
1197
|
const logoutUseCase = new LogoutUseCase(authRepository);
|
|
1239
1198
|
const registerUseCase = new RegisterUseCase(authRepository);
|
|
1240
1199
|
const refreshTokenUseCase = new RefreshTokenUseCase(
|
|
@@ -1250,7 +1209,6 @@ async function createAuthContainer(database) {
|
|
|
1250
1209
|
);
|
|
1251
1210
|
const twoFactorDisableUseCase = new TwoFactorDisableUseCase(twoFactorService, authRepository);
|
|
1252
1211
|
container.registerService("LoginUseCase", loginUseCase);
|
|
1253
|
-
container.registerService("LoginAdminUseCase", loginAdminUseCase);
|
|
1254
1212
|
container.registerService("LogoutUseCase", logoutUseCase);
|
|
1255
1213
|
container.registerService("RegisterUseCase", registerUseCase);
|
|
1256
1214
|
container.registerService("RefreshTokenUseCase", refreshTokenUseCase);
|
|
@@ -1260,7 +1218,6 @@ async function createAuthContainer(database) {
|
|
|
1260
1218
|
container.registerService("TwoFactorDisableUseCase", twoFactorDisableUseCase);
|
|
1261
1219
|
const authController = new AuthController(
|
|
1262
1220
|
loginUseCase,
|
|
1263
|
-
loginAdminUseCase,
|
|
1264
1221
|
logoutUseCase,
|
|
1265
1222
|
registerUseCase,
|
|
1266
1223
|
refreshTokenUseCase,
|