cca-auth-module 0.1.85 → 0.1.87
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/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +148 -86
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +148 -86
- package/dist/index.mjs.map +1 -1
- package/dist/presentation/constants/constants.d.ts +24 -0
- package/dist/presentation/controller/AuthController.d.ts +2 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -232,8 +232,9 @@ declare class AuthController {
|
|
|
232
232
|
private readonly twoFactorVerifyUseCase;
|
|
233
233
|
private readonly twoFactorDisableUseCase;
|
|
234
234
|
constructor(loginUseCase: LoginUseCase, adminLoginUseCase: LoginAdminUseCase, logoutUseCase: LogoutUseCase, registerUseCase: RegisterUseCase, refreshTokenUseCase: RefreshTokenUseCase, twoFactorSetupUseCase: TwoFactorSetupUseCase, twoFactorEnableUseCase: TwoFactorEnableUseCase, twoFactorVerifyUseCase: TwoFactorVerifyUseCase, twoFactorDisableUseCase: TwoFactorDisableUseCase);
|
|
235
|
-
private
|
|
235
|
+
private createResponse;
|
|
236
236
|
private createAuthData;
|
|
237
|
+
private sendResponse;
|
|
237
238
|
login: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
238
239
|
adminLogin: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
239
240
|
logout: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
package/dist/index.d.ts
CHANGED
|
@@ -232,8 +232,9 @@ declare class AuthController {
|
|
|
232
232
|
private readonly twoFactorVerifyUseCase;
|
|
233
233
|
private readonly twoFactorDisableUseCase;
|
|
234
234
|
constructor(loginUseCase: LoginUseCase, adminLoginUseCase: LoginAdminUseCase, logoutUseCase: LogoutUseCase, registerUseCase: RegisterUseCase, refreshTokenUseCase: RefreshTokenUseCase, twoFactorSetupUseCase: TwoFactorSetupUseCase, twoFactorEnableUseCase: TwoFactorEnableUseCase, twoFactorVerifyUseCase: TwoFactorVerifyUseCase, twoFactorDisableUseCase: TwoFactorDisableUseCase);
|
|
235
|
-
private
|
|
235
|
+
private createResponse;
|
|
236
236
|
private createAuthData;
|
|
237
|
+
private sendResponse;
|
|
237
238
|
login: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
238
239
|
adminLogin: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
239
240
|
logout: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -732,6 +732,32 @@ var _TwoFactorDisableUseCase = class _TwoFactorDisableUseCase {
|
|
|
732
732
|
__name(_TwoFactorDisableUseCase, "TwoFactorDisableUseCase");
|
|
733
733
|
var TwoFactorDisableUseCase = _TwoFactorDisableUseCase;
|
|
734
734
|
|
|
735
|
+
// src/presentation/constants/constants.ts
|
|
736
|
+
var HTTP_STATUS = {
|
|
737
|
+
OK: 200,
|
|
738
|
+
CREATED: 201
|
|
739
|
+
};
|
|
740
|
+
var AUTH_STATUS = {
|
|
741
|
+
BASIC_AUTH: "basic_auth",
|
|
742
|
+
NEEDS_SETUP: "needs_setup",
|
|
743
|
+
PENDING_VERIFICATION: "pending_verification",
|
|
744
|
+
FULL_AUTH: "full_auth",
|
|
745
|
+
LOGGED_OUT: "logged_out",
|
|
746
|
+
REGISTERED: "registered",
|
|
747
|
+
TOKEN_REFRESHED: "token_refreshed"
|
|
748
|
+
};
|
|
749
|
+
var MESSAGES = {
|
|
750
|
+
LOGIN_SUCCESS: "Login successful",
|
|
751
|
+
ADMIN_LOGIN_SUCCESS: "Admin login successful",
|
|
752
|
+
LOGOUT_SUCCESS: "Logged out successfully",
|
|
753
|
+
REGISTER_SUCCESS: "User registered successfully",
|
|
754
|
+
TOKEN_REFRESH_SUCCESS: "Token refreshed successfully",
|
|
755
|
+
TWO_FA_SETUP_SUCCESS: "Two-factor authentication setup initiated",
|
|
756
|
+
TWO_FA_ENABLE_SUCCESS: "Two-factor authentication enabled",
|
|
757
|
+
TWO_FA_VERIFY_SUCCESS: "Two-factor authentication verified successfully",
|
|
758
|
+
TWO_FA_DISABLE_SUCCESS: "Two-factor authentication disabled"
|
|
759
|
+
};
|
|
760
|
+
|
|
735
761
|
// src/presentation/controller/AuthController.ts
|
|
736
762
|
var _AuthController = class _AuthController {
|
|
737
763
|
constructor(loginUseCase, adminLoginUseCase, logoutUseCase, registerUseCase, refreshTokenUseCase, twoFactorSetupUseCase, twoFactorEnableUseCase, twoFactorVerifyUseCase, twoFactorDisableUseCase) {
|
|
@@ -739,19 +765,23 @@ var _AuthController = class _AuthController {
|
|
|
739
765
|
try {
|
|
740
766
|
const loginDTO = req.body;
|
|
741
767
|
const result = await this.loginUseCase.execute(loginDTO);
|
|
742
|
-
const
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
768
|
+
const twoFactorEnabled = result.enabled ?? false;
|
|
769
|
+
const loginData = {
|
|
770
|
+
accessToken: result.accessToken,
|
|
771
|
+
userId: result.id,
|
|
772
|
+
expiresAt: result.expiresAt,
|
|
773
|
+
enabled: twoFactorEnabled,
|
|
774
|
+
auth: this.createAuthData(
|
|
775
|
+
true,
|
|
776
|
+
// hasAccessToken
|
|
777
|
+
twoFactorEnabled,
|
|
778
|
+
// enable (2FA enabled status)
|
|
779
|
+
twoFactorEnabled ? AUTH_STATUS.PENDING_VERIFICATION : AUTH_STATUS.BASIC_AUTH,
|
|
780
|
+
false
|
|
781
|
+
// verified - always false for basic login
|
|
782
|
+
)
|
|
783
|
+
};
|
|
784
|
+
this.sendResponse(res, HTTP_STATUS.OK, MESSAGES.LOGIN_SUCCESS, loginData);
|
|
755
785
|
} catch (error) {
|
|
756
786
|
next(error);
|
|
757
787
|
}
|
|
@@ -763,11 +793,20 @@ var _AuthController = class _AuthController {
|
|
|
763
793
|
throw new ForbiddenError("Admin password is required");
|
|
764
794
|
}
|
|
765
795
|
const result = await this.adminLoginUseCase.execute(loginDTO, adminPassword);
|
|
766
|
-
const
|
|
767
|
-
|
|
768
|
-
result
|
|
769
|
-
|
|
770
|
-
|
|
796
|
+
const adminLoginData = {
|
|
797
|
+
message: result,
|
|
798
|
+
// Assuming result is a string message
|
|
799
|
+
auth: this.createAuthData(
|
|
800
|
+
true,
|
|
801
|
+
// hasAccessToken
|
|
802
|
+
false,
|
|
803
|
+
// enable - assuming 2FA not enabled for admin login
|
|
804
|
+
AUTH_STATUS.BASIC_AUTH,
|
|
805
|
+
false
|
|
806
|
+
// verified
|
|
807
|
+
)
|
|
808
|
+
};
|
|
809
|
+
this.sendResponse(res, HTTP_STATUS.CREATED, MESSAGES.ADMIN_LOGIN_SUCCESS, adminLoginData);
|
|
771
810
|
} catch (error) {
|
|
772
811
|
next(error);
|
|
773
812
|
}
|
|
@@ -775,8 +814,18 @@ var _AuthController = class _AuthController {
|
|
|
775
814
|
this.logout = /* @__PURE__ */ __name(async (req, res, next) => {
|
|
776
815
|
try {
|
|
777
816
|
await this.logoutUseCase.execute(req.body.id);
|
|
778
|
-
const
|
|
779
|
-
|
|
817
|
+
const logoutData = {
|
|
818
|
+
auth: this.createAuthData(
|
|
819
|
+
false,
|
|
820
|
+
// hasAccessToken - no token after logout
|
|
821
|
+
false,
|
|
822
|
+
// enable - 2FA status irrelevant after logout
|
|
823
|
+
AUTH_STATUS.LOGGED_OUT,
|
|
824
|
+
false
|
|
825
|
+
// verified - false after logout
|
|
826
|
+
)
|
|
827
|
+
};
|
|
828
|
+
this.sendResponse(res, HTTP_STATUS.OK, MESSAGES.LOGOUT_SUCCESS, logoutData);
|
|
780
829
|
} catch (error) {
|
|
781
830
|
next(error);
|
|
782
831
|
}
|
|
@@ -785,12 +834,24 @@ var _AuthController = class _AuthController {
|
|
|
785
834
|
try {
|
|
786
835
|
const { email, name, password, role, adminPassword } = req.body;
|
|
787
836
|
await this.registerUseCase.execute(email, name, password, role, adminPassword);
|
|
788
|
-
const
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
837
|
+
const registerData = {
|
|
838
|
+
auth: this.createAuthData(
|
|
839
|
+
false,
|
|
840
|
+
// hasAccessToken - no token after registration
|
|
841
|
+
false,
|
|
842
|
+
// enable - 2FA not enabled for new users
|
|
843
|
+
AUTH_STATUS.REGISTERED,
|
|
844
|
+
false
|
|
845
|
+
// verified - false after registration
|
|
846
|
+
)
|
|
847
|
+
};
|
|
848
|
+
this.sendResponse(
|
|
849
|
+
res,
|
|
850
|
+
HTTP_STATUS.OK,
|
|
851
|
+
MESSAGES.REGISTER_SUCCESS,
|
|
852
|
+
registerData,
|
|
853
|
+
{ status: true }
|
|
792
854
|
);
|
|
793
|
-
res.status(200).json(response);
|
|
794
855
|
} catch (error) {
|
|
795
856
|
next(error);
|
|
796
857
|
}
|
|
@@ -799,11 +860,20 @@ var _AuthController = class _AuthController {
|
|
|
799
860
|
try {
|
|
800
861
|
const { refreshToken } = req.body;
|
|
801
862
|
const result = await this.refreshTokenUseCase.execute(refreshToken);
|
|
802
|
-
|
|
803
|
-
"
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
863
|
+
if (!result) {
|
|
864
|
+
throw new Error("Failed to refresh token");
|
|
865
|
+
}
|
|
866
|
+
const refreshData = {
|
|
867
|
+
accessToken: result.accessToken,
|
|
868
|
+
refreshToken: result.refreshToken,
|
|
869
|
+
auth: this.createAuthData(
|
|
870
|
+
true,
|
|
871
|
+
false,
|
|
872
|
+
AUTH_STATUS.BASIC_AUTH,
|
|
873
|
+
false
|
|
874
|
+
)
|
|
875
|
+
};
|
|
876
|
+
this.sendResponse(res, HTTP_STATUS.OK, MESSAGES.TOKEN_REFRESH_SUCCESS, refreshData);
|
|
807
877
|
} catch (error) {
|
|
808
878
|
next(error);
|
|
809
879
|
}
|
|
@@ -814,18 +884,15 @@ var _AuthController = class _AuthController {
|
|
|
814
884
|
throw new ForbiddenError("User authentication required");
|
|
815
885
|
}
|
|
816
886
|
const result = await this.twoFactorSetupUseCase.execute(req.auth.id);
|
|
817
|
-
const
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
}
|
|
827
|
-
);
|
|
828
|
-
res.status(200).json(response);
|
|
887
|
+
const setupData = {
|
|
888
|
+
qrCode: result.qrCodeUrl,
|
|
889
|
+
auth: this.createAuthData(true, false, AUTH_STATUS.NEEDS_SETUP)
|
|
890
|
+
};
|
|
891
|
+
const meta = {
|
|
892
|
+
nextStep: "Scan the QR code and enter your first code to verify",
|
|
893
|
+
redirectTo: "/2fa-enable"
|
|
894
|
+
};
|
|
895
|
+
this.sendResponse(res, HTTP_STATUS.OK, MESSAGES.TWO_FA_SETUP_SUCCESS, setupData, meta);
|
|
829
896
|
} catch (error) {
|
|
830
897
|
next(error);
|
|
831
898
|
}
|
|
@@ -834,19 +901,16 @@ var _AuthController = class _AuthController {
|
|
|
834
901
|
try {
|
|
835
902
|
const dto = { ...req.body, userId: req.auth?.id };
|
|
836
903
|
await this.twoFactorEnableUseCase.execute(dto);
|
|
837
|
-
const
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
}
|
|
848
|
-
);
|
|
849
|
-
res.status(200).json(response);
|
|
904
|
+
const enableData = {
|
|
905
|
+
isEnabled: true,
|
|
906
|
+
enabledAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
907
|
+
auth: this.createAuthData(true, true, AUTH_STATUS.PENDING_VERIFICATION)
|
|
908
|
+
};
|
|
909
|
+
const meta = {
|
|
910
|
+
nextStep: "Proceed to verify with a valid 2FA token",
|
|
911
|
+
redirectTo: "/verify-2fa"
|
|
912
|
+
};
|
|
913
|
+
this.sendResponse(res, HTTP_STATUS.OK, MESSAGES.TWO_FA_ENABLE_SUCCESS, enableData, meta);
|
|
850
914
|
} catch (error) {
|
|
851
915
|
next(error);
|
|
852
916
|
}
|
|
@@ -855,25 +919,22 @@ var _AuthController = class _AuthController {
|
|
|
855
919
|
try {
|
|
856
920
|
const dto = req.body;
|
|
857
921
|
const result = await this.twoFactorVerifyUseCase.execute(dto);
|
|
858
|
-
const
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
name: result?.data?.name,
|
|
867
|
-
role: result?.data?.role
|
|
868
|
-
},
|
|
869
|
-
auth: this.createAuthData(true, true, "full_auth", true)
|
|
922
|
+
const verifyData = {
|
|
923
|
+
token: result?.token,
|
|
924
|
+
refreshToken: result?.refreshToken,
|
|
925
|
+
user: {
|
|
926
|
+
id: result?.data?.id,
|
|
927
|
+
email: result?.data?.email,
|
|
928
|
+
name: result?.data?.name,
|
|
929
|
+
role: result?.data?.role
|
|
870
930
|
},
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
931
|
+
auth: this.createAuthData(true, true, AUTH_STATUS.FULL_AUTH, true)
|
|
932
|
+
};
|
|
933
|
+
const meta = {
|
|
934
|
+
recommendation: "You're fully authenticated",
|
|
935
|
+
redirectTo: "/"
|
|
936
|
+
};
|
|
937
|
+
this.sendResponse(res, HTTP_STATUS.OK, MESSAGES.TWO_FA_VERIFY_SUCCESS, verifyData, meta);
|
|
877
938
|
} catch (error) {
|
|
878
939
|
next(error);
|
|
879
940
|
}
|
|
@@ -883,18 +944,15 @@ var _AuthController = class _AuthController {
|
|
|
883
944
|
const userId = req.auth.id;
|
|
884
945
|
const dto = req.body;
|
|
885
946
|
await this.twoFactorDisableUseCase.execute(userId, dto);
|
|
886
|
-
const
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
}
|
|
896
|
-
);
|
|
897
|
-
res.status(200).json(response);
|
|
947
|
+
const disableData = {
|
|
948
|
+
disabledAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
949
|
+
auth: this.createAuthData(true, false, AUTH_STATUS.BASIC_AUTH, false)
|
|
950
|
+
};
|
|
951
|
+
const meta = {
|
|
952
|
+
securityNote: "Account now relies only on password. Re-enable 2FA for better security.",
|
|
953
|
+
redirectTo: "/login"
|
|
954
|
+
};
|
|
955
|
+
this.sendResponse(res, HTTP_STATUS.OK, MESSAGES.TWO_FA_DISABLE_SUCCESS, disableData, meta);
|
|
898
956
|
} catch (error) {
|
|
899
957
|
next(error);
|
|
900
958
|
}
|
|
@@ -909,7 +967,7 @@ var _AuthController = class _AuthController {
|
|
|
909
967
|
this.twoFactorVerifyUseCase = twoFactorVerifyUseCase;
|
|
910
968
|
this.twoFactorDisableUseCase = twoFactorDisableUseCase;
|
|
911
969
|
}
|
|
912
|
-
|
|
970
|
+
createResponse(message, data, meta) {
|
|
913
971
|
return {
|
|
914
972
|
success: true,
|
|
915
973
|
message,
|
|
@@ -928,6 +986,10 @@ var _AuthController = class _AuthController {
|
|
|
928
986
|
...verified !== void 0 && { verified }
|
|
929
987
|
};
|
|
930
988
|
}
|
|
989
|
+
sendResponse(res, statusCode, message, data, meta) {
|
|
990
|
+
const response = this.createResponse(message, data, meta);
|
|
991
|
+
res.status(statusCode).json(response);
|
|
992
|
+
}
|
|
931
993
|
};
|
|
932
994
|
__name(_AuthController, "AuthController");
|
|
933
995
|
var AuthController = _AuthController;
|