cca-auth-module 0.1.32 → 0.1.34

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,6 +1,6 @@
1
1
  import { IBaseService } from "cca-core";
2
2
  import { AuthRepository } from "../../infrastructure/repository/AuthRepository";
3
- import { JwtAuthService } from "../../infrastructure/auth/JwtAuthService";
3
+ import { JwtAuthService } from "../../infrastructure/services/JwtAuthService";
4
4
  import { UserDTO } from "../dtos/UserDTO";
5
5
  import { LoginDTO } from "../dtos/LoginDTO";
6
6
  interface TokenPair {
@@ -1,6 +1,6 @@
1
1
  import { IBaseService } from "cca-core";
2
2
  import { IDecodedToken } from "../../domain/interfaces/IDecodedToken";
3
- import { JwtAuthService } from "../../infrastructure/auth/JwtAuthService";
3
+ import { JwtAuthService } from "../../infrastructure/services/JwtAuthService";
4
4
  import { AuthRepository } from "../../infrastructure/repository/AuthRepository";
5
5
  export declare class RefreshTokenUseCase implements IBaseService {
6
6
  private readonly repository;
@@ -0,0 +1,12 @@
1
+ import { IBaseService } from 'cca-core';
2
+ import { TwoFactorService } from '../../infrastructure/services/TwoFactorService';
3
+ import { AuthRepository } from '../../infrastructure/repository/AuthRepository';
4
+ import { ITwoFactorEnable } from '../../domain/interfaces/ITwoFactorEnable';
5
+ export declare class TwoFactorDisableUseCase implements IBaseService {
6
+ private twoFactorService;
7
+ private authRepository;
8
+ private isInitialized;
9
+ constructor(twoFactorService: TwoFactorService, authRepository: AuthRepository);
10
+ initialize(): Promise<void>;
11
+ execute(userId: string, dto: ITwoFactorEnable): Promise<void>;
12
+ }
@@ -0,0 +1,12 @@
1
+ import { IBaseService } from 'cca-core';
2
+ import { TwoFactorService } from '../../infrastructure/services/TwoFactorService';
3
+ import { AuthRepository } from '../../infrastructure/repository/AuthRepository';
4
+ import { ITwoFactorEnable } from '../../domain/interfaces/ITwoFactorEnable';
5
+ export declare class TwoFactorEnableUseCase implements IBaseService {
6
+ private twoFactorService;
7
+ private authRepository;
8
+ private isInitialized;
9
+ constructor(twoFactorService: TwoFactorService, authRepository: AuthRepository);
10
+ initialize(): Promise<void>;
11
+ execute(userId: string, dto: ITwoFactorEnable): Promise<void>;
12
+ }
@@ -0,0 +1,12 @@
1
+ import { IBaseService } from 'cca-core';
2
+ import { TwoFactorService } from '../../infrastructure/services/TwoFactorService';
3
+ import { AuthRepository } from '../../infrastructure/repository/AuthRepository';
4
+ import { ITwoFactorSetupResponse } from '../../domain/interfaces/ITwoFactorSetupResponse';
5
+ export declare class TwoFactorSetupUseCase implements IBaseService {
6
+ private twoFactorService;
7
+ private authRepository;
8
+ private isInitialized;
9
+ constructor(twoFactorService: TwoFactorService, authRepository: AuthRepository);
10
+ initialize(): Promise<void>;
11
+ execute(userId: string): Promise<ITwoFactorSetupResponse>;
12
+ }
@@ -0,0 +1,17 @@
1
+ import { IBaseService } from 'cca-core';
2
+ import { TwoFactorService } from '../../infrastructure/services/TwoFactorService';
3
+ import { AuthRepository } from '../../infrastructure/repository/AuthRepository';
4
+ import { JwtAuthService } from '../../infrastructure/services/JwtAuthService';
5
+ import { ITwoFactorVerify } from '../../domain/interfaces/ITwoFactorVerify';
6
+ export declare class TwoFactorVerifyUseCase implements IBaseService {
7
+ private twoFactorService;
8
+ private authRepository;
9
+ private jwtService;
10
+ private isInitialized;
11
+ constructor(twoFactorService: TwoFactorService, authRepository: AuthRepository, jwtService: JwtAuthService);
12
+ initialize(): Promise<void>;
13
+ execute(dto: ITwoFactorVerify): Promise<{
14
+ token: string;
15
+ refreshToken: string;
16
+ }>;
17
+ }
@@ -0,0 +1,3 @@
1
+ export interface ITwoFactorEnable {
2
+ token: string;
3
+ }
@@ -0,0 +1,5 @@
1
+ export interface ITwoFactorSetupResponse {
2
+ secret: string;
3
+ otpAuthUrl: string;
4
+ qrCodeUrl: string;
5
+ }
@@ -0,0 +1,4 @@
1
+ export interface ITwoFactorVerify {
2
+ userId: string;
3
+ token: string;
4
+ }
@@ -4,5 +4,8 @@ export interface IConfig {
4
4
  accessTokenExpiry: string;
5
5
  refreshTokenExpiry: string;
6
6
  adminSecretPassword: string;
7
+ app_name: string;
8
+ secretLength: string;
9
+ tokenWindow: string;
7
10
  }
8
11
  export type ConfigSource = () => Promise<IConfig>;
package/dist/index.d.mts CHANGED
@@ -11,6 +11,9 @@ interface IConfig {
11
11
  accessTokenExpiry: string;
12
12
  refreshTokenExpiry: string;
13
13
  adminSecretPassword: string;
14
+ app_name: string;
15
+ secretLength: string;
16
+ tokenWindow: string;
14
17
  }
15
18
  type ConfigSource = () => Promise<IConfig>;
16
19
 
@@ -22,6 +25,11 @@ declare class AuthRepository extends BaseRepository<AuthEntity> implements IExte
22
25
  create(entity: Omit<AuthEntity, "createdAt">): Promise<AuthEntity>;
23
26
  findByUserId(userId: string): Promise<AuthEntity | null>;
24
27
  logout(userId: string): Promise<void>;
28
+ updateTwoFactorSecret(userId: string, secret: string): Promise<void>;
29
+ enableTwoFactor(userId: string): Promise<void>;
30
+ disableTwoFactor(userId: string): Promise<void>;
31
+ isTwoFactorEnabled(userId: string): Promise<boolean>;
32
+ getTwoFactorSecret(userId: string): Promise<string | null>;
25
33
  }
26
34
 
27
35
  declare class RegisterUseCase implements IBaseService {
@@ -128,22 +136,108 @@ declare class RefreshTokenUseCase implements IBaseService {
128
136
  verityToken(token: string): Promise<IDecodedToken>;
129
137
  }
130
138
 
139
+ declare class TwoFactorService implements IBaseService {
140
+ private readonly config;
141
+ private initialized;
142
+ private readonly twoFactorConfig;
143
+ constructor(config: IConfig);
144
+ initialize(): Promise<void>;
145
+ private validateConfiguration;
146
+ private ensureInitialized;
147
+ generateSecret(email: string): {
148
+ secret: string;
149
+ otpAuthUrl: string;
150
+ };
151
+ generateQRCode(otpAuthUrl: string): Promise<string>;
152
+ verifyToken(token: string, secret: string): boolean;
153
+ }
154
+
155
+ interface ITwoFactorSetupResponse {
156
+ secret: string;
157
+ otpAuthUrl: string;
158
+ qrCodeUrl: string;
159
+ }
160
+
161
+ declare class TwoFactorSetupUseCase implements IBaseService {
162
+ private twoFactorService;
163
+ private authRepository;
164
+ private isInitialized;
165
+ constructor(twoFactorService: TwoFactorService, authRepository: AuthRepository);
166
+ initialize(): Promise<void>;
167
+ execute(userId: string): Promise<ITwoFactorSetupResponse>;
168
+ }
169
+
170
+ interface ITwoFactorEnable {
171
+ token: string;
172
+ }
173
+
174
+ declare class TwoFactorEnableUseCase implements IBaseService {
175
+ private twoFactorService;
176
+ private authRepository;
177
+ private isInitialized;
178
+ constructor(twoFactorService: TwoFactorService, authRepository: AuthRepository);
179
+ initialize(): Promise<void>;
180
+ execute(userId: string, dto: ITwoFactorEnable): Promise<void>;
181
+ }
182
+
183
+ interface ITwoFactorVerify {
184
+ userId: string;
185
+ token: string;
186
+ }
187
+
188
+ declare class TwoFactorVerifyUseCase implements IBaseService {
189
+ private twoFactorService;
190
+ private authRepository;
191
+ private jwtService;
192
+ private isInitialized;
193
+ constructor(twoFactorService: TwoFactorService, authRepository: AuthRepository, jwtService: JwtAuthService);
194
+ initialize(): Promise<void>;
195
+ execute(dto: ITwoFactorVerify): Promise<{
196
+ token: string;
197
+ refreshToken: string;
198
+ }>;
199
+ }
200
+
201
+ declare class TwoFactorDisableUseCase implements IBaseService {
202
+ private twoFactorService;
203
+ private authRepository;
204
+ private isInitialized;
205
+ constructor(twoFactorService: TwoFactorService, authRepository: AuthRepository);
206
+ initialize(): Promise<void>;
207
+ execute(userId: string, dto: ITwoFactorEnable): Promise<void>;
208
+ }
209
+
131
210
  declare class AuthController {
132
- private loginUseCase;
211
+ private readonly loginUseCase;
133
212
  private readonly logoutUseCase;
134
- private registerUseCase;
135
- private refreshTokenUseCase;
136
- constructor(loginUseCase: LoginUseCase, logoutUseCase: LogoutUseCase, registerUseCase: RegisterUseCase, refreshTokenUseCase: RefreshTokenUseCase);
213
+ private readonly registerUseCase;
214
+ private readonly refreshTokenUseCase;
215
+ private twoFactorSetupUseCase;
216
+ private twoFactorEnableUseCase;
217
+ private twoFactorVerifyUseCase;
218
+ private twoFactorDisableUseCase;
219
+ constructor(loginUseCase: LoginUseCase, logoutUseCase: LogoutUseCase, registerUseCase: RegisterUseCase, refreshTokenUseCase: RefreshTokenUseCase, twoFactorSetupUseCase: TwoFactorSetupUseCase, twoFactorEnableUseCase: TwoFactorEnableUseCase, twoFactorVerifyUseCase: TwoFactorVerifyUseCase, twoFactorDisableUseCase: TwoFactorDisableUseCase);
137
220
  login: (req: Request, res: Response, next: NextFunction) => Promise<void>;
138
221
  logout: (req: Request, res: Response, next: NextFunction) => Promise<void>;
139
222
  register: (req: Request, res: Response, next: NextFunction) => Promise<void>;
140
223
  refreshToken: (req: Request, res: Response) => Promise<void>;
141
224
  verifyToken: (token: string) => Promise<IDecodedToken>;
225
+ setup2FA: (req: Request, res: Response, next: NextFunction) => Promise<void>;
226
+ enable2FA: (req: Request, res: Response, next: NextFunction) => Promise<void>;
227
+ verify2FA: (req: Request, res: Response, next: NextFunction) => Promise<void>;
228
+ disable2FA: (req: Request, res: Response, next: NextFunction) => Promise<void>;
229
+ }
230
+
231
+ declare class RequireComplete2FA {
232
+ private readonly jwtService;
233
+ constructor(jwtService: JwtAuthService);
234
+ execute(req: Request, res: Response, next: NextFunction): Promise<Response<any, Record<string, any>> | undefined>;
142
235
  }
143
236
 
144
- declare function createAuthContainer(database: BaseDatabase): {
237
+ declare function createAuthContainer(database: BaseDatabase): Promise<{
145
238
  container: BaseContainer;
146
239
  authController: AuthController;
147
- };
240
+ requireComplete2FA: RequireComplete2FA;
241
+ }>;
148
242
 
149
243
  export { AuthController, type ConfigSource, type IConfig, authConfig, createAuthContainer };
package/dist/index.d.ts CHANGED
@@ -11,6 +11,9 @@ interface IConfig {
11
11
  accessTokenExpiry: string;
12
12
  refreshTokenExpiry: string;
13
13
  adminSecretPassword: string;
14
+ app_name: string;
15
+ secretLength: string;
16
+ tokenWindow: string;
14
17
  }
15
18
  type ConfigSource = () => Promise<IConfig>;
16
19
 
@@ -22,6 +25,11 @@ declare class AuthRepository extends BaseRepository<AuthEntity> implements IExte
22
25
  create(entity: Omit<AuthEntity, "createdAt">): Promise<AuthEntity>;
23
26
  findByUserId(userId: string): Promise<AuthEntity | null>;
24
27
  logout(userId: string): Promise<void>;
28
+ updateTwoFactorSecret(userId: string, secret: string): Promise<void>;
29
+ enableTwoFactor(userId: string): Promise<void>;
30
+ disableTwoFactor(userId: string): Promise<void>;
31
+ isTwoFactorEnabled(userId: string): Promise<boolean>;
32
+ getTwoFactorSecret(userId: string): Promise<string | null>;
25
33
  }
26
34
 
27
35
  declare class RegisterUseCase implements IBaseService {
@@ -128,22 +136,108 @@ declare class RefreshTokenUseCase implements IBaseService {
128
136
  verityToken(token: string): Promise<IDecodedToken>;
129
137
  }
130
138
 
139
+ declare class TwoFactorService implements IBaseService {
140
+ private readonly config;
141
+ private initialized;
142
+ private readonly twoFactorConfig;
143
+ constructor(config: IConfig);
144
+ initialize(): Promise<void>;
145
+ private validateConfiguration;
146
+ private ensureInitialized;
147
+ generateSecret(email: string): {
148
+ secret: string;
149
+ otpAuthUrl: string;
150
+ };
151
+ generateQRCode(otpAuthUrl: string): Promise<string>;
152
+ verifyToken(token: string, secret: string): boolean;
153
+ }
154
+
155
+ interface ITwoFactorSetupResponse {
156
+ secret: string;
157
+ otpAuthUrl: string;
158
+ qrCodeUrl: string;
159
+ }
160
+
161
+ declare class TwoFactorSetupUseCase implements IBaseService {
162
+ private twoFactorService;
163
+ private authRepository;
164
+ private isInitialized;
165
+ constructor(twoFactorService: TwoFactorService, authRepository: AuthRepository);
166
+ initialize(): Promise<void>;
167
+ execute(userId: string): Promise<ITwoFactorSetupResponse>;
168
+ }
169
+
170
+ interface ITwoFactorEnable {
171
+ token: string;
172
+ }
173
+
174
+ declare class TwoFactorEnableUseCase implements IBaseService {
175
+ private twoFactorService;
176
+ private authRepository;
177
+ private isInitialized;
178
+ constructor(twoFactorService: TwoFactorService, authRepository: AuthRepository);
179
+ initialize(): Promise<void>;
180
+ execute(userId: string, dto: ITwoFactorEnable): Promise<void>;
181
+ }
182
+
183
+ interface ITwoFactorVerify {
184
+ userId: string;
185
+ token: string;
186
+ }
187
+
188
+ declare class TwoFactorVerifyUseCase implements IBaseService {
189
+ private twoFactorService;
190
+ private authRepository;
191
+ private jwtService;
192
+ private isInitialized;
193
+ constructor(twoFactorService: TwoFactorService, authRepository: AuthRepository, jwtService: JwtAuthService);
194
+ initialize(): Promise<void>;
195
+ execute(dto: ITwoFactorVerify): Promise<{
196
+ token: string;
197
+ refreshToken: string;
198
+ }>;
199
+ }
200
+
201
+ declare class TwoFactorDisableUseCase implements IBaseService {
202
+ private twoFactorService;
203
+ private authRepository;
204
+ private isInitialized;
205
+ constructor(twoFactorService: TwoFactorService, authRepository: AuthRepository);
206
+ initialize(): Promise<void>;
207
+ execute(userId: string, dto: ITwoFactorEnable): Promise<void>;
208
+ }
209
+
131
210
  declare class AuthController {
132
- private loginUseCase;
211
+ private readonly loginUseCase;
133
212
  private readonly logoutUseCase;
134
- private registerUseCase;
135
- private refreshTokenUseCase;
136
- constructor(loginUseCase: LoginUseCase, logoutUseCase: LogoutUseCase, registerUseCase: RegisterUseCase, refreshTokenUseCase: RefreshTokenUseCase);
213
+ private readonly registerUseCase;
214
+ private readonly refreshTokenUseCase;
215
+ private twoFactorSetupUseCase;
216
+ private twoFactorEnableUseCase;
217
+ private twoFactorVerifyUseCase;
218
+ private twoFactorDisableUseCase;
219
+ constructor(loginUseCase: LoginUseCase, logoutUseCase: LogoutUseCase, registerUseCase: RegisterUseCase, refreshTokenUseCase: RefreshTokenUseCase, twoFactorSetupUseCase: TwoFactorSetupUseCase, twoFactorEnableUseCase: TwoFactorEnableUseCase, twoFactorVerifyUseCase: TwoFactorVerifyUseCase, twoFactorDisableUseCase: TwoFactorDisableUseCase);
137
220
  login: (req: Request, res: Response, next: NextFunction) => Promise<void>;
138
221
  logout: (req: Request, res: Response, next: NextFunction) => Promise<void>;
139
222
  register: (req: Request, res: Response, next: NextFunction) => Promise<void>;
140
223
  refreshToken: (req: Request, res: Response) => Promise<void>;
141
224
  verifyToken: (token: string) => Promise<IDecodedToken>;
225
+ setup2FA: (req: Request, res: Response, next: NextFunction) => Promise<void>;
226
+ enable2FA: (req: Request, res: Response, next: NextFunction) => Promise<void>;
227
+ verify2FA: (req: Request, res: Response, next: NextFunction) => Promise<void>;
228
+ disable2FA: (req: Request, res: Response, next: NextFunction) => Promise<void>;
229
+ }
230
+
231
+ declare class RequireComplete2FA {
232
+ private readonly jwtService;
233
+ constructor(jwtService: JwtAuthService);
234
+ execute(req: Request, res: Response, next: NextFunction): Promise<Response<any, Record<string, any>> | undefined>;
142
235
  }
143
236
 
144
- declare function createAuthContainer(database: BaseDatabase): {
237
+ declare function createAuthContainer(database: BaseDatabase): Promise<{
145
238
  container: BaseContainer;
146
239
  authController: AuthController;
147
- };
240
+ requireComplete2FA: RequireComplete2FA;
241
+ }>;
148
242
 
149
243
  export { AuthController, type ConfigSource, type IConfig, authConfig, createAuthContainer };