@flusys/nestjs-auth 7.1.0 → 8.0.0

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.
Files changed (271) hide show
  1. package/README.md +1 -1
  2. package/config/index.d.ts +3 -3
  3. package/config/message-keys.d.ts +4 -0
  4. package/controllers/authentication.controller.d.ts +5 -5
  5. package/controllers/branch.controller.d.ts +2 -2
  6. package/controllers/company-selection.controller.d.ts +4 -4
  7. package/controllers/company.controller.d.ts +2 -2
  8. package/controllers/email-verification.controller.d.ts +6 -6
  9. package/controllers/index.d.ts +9 -9
  10. package/controllers/social-auth-config.controller.d.ts +2 -2
  11. package/controllers/social-auth.controller.d.ts +3 -3
  12. package/controllers/user-permission.controller.d.ts +7 -4
  13. package/controllers/user.controller.d.ts +7 -7
  14. package/docs/auth-swagger.config.d.ts +2 -2
  15. package/docs/index.d.ts +1 -1
  16. package/dtos/index.d.ts +6 -6
  17. package/dtos/social-auth.dto.d.ts +1 -1
  18. package/dtos/user-permission.dto.d.ts +30 -0
  19. package/entities/company-branch.entity.d.ts +1 -1
  20. package/entities/company.entity.d.ts +1 -1
  21. package/entities/index.d.ts +14 -14
  22. package/entities/pending-registration.entity.d.ts +1 -1
  23. package/entities/user-company-permission.entity.d.ts +4 -4
  24. package/enums/index.d.ts +2 -2
  25. package/fesm/118.js +1404 -0
  26. package/fesm/120.js +2265 -0
  27. package/fesm/24.js +4422 -0
  28. package/fesm/370.js +1163 -0
  29. package/fesm/446.js +877 -0
  30. package/fesm/chunks/117.js +69 -0
  31. package/fesm/config/index.js +246 -3
  32. package/fesm/controllers/index.js +27 -9
  33. package/fesm/docs/index.js +479 -1
  34. package/fesm/dtos/index.js +135 -6
  35. package/fesm/entities/index.js +68 -22
  36. package/fesm/enums/index.js +53 -2
  37. package/fesm/helpers/index.js +115 -2
  38. package/fesm/index.js +1455 -13
  39. package/fesm/interceptors/index.js +510 -2
  40. package/fesm/interfaces/index.js +75 -8
  41. package/fesm/modules/index.js +546 -1
  42. package/fesm/runtime.js +134 -0
  43. package/fesm/services/index.js +67 -11
  44. package/helpers/auth-response.mapper.d.ts +3 -3
  45. package/helpers/index.d.ts +2 -2
  46. package/helpers/token.helper.d.ts +2 -2
  47. package/index.d.ts +13 -13
  48. package/interceptors/clear-token.interceptor.d.ts +2 -2
  49. package/interceptors/index.d.ts +2 -2
  50. package/interceptors/set-token.interceptor.d.ts +2 -2
  51. package/interfaces/auth-module-options.interface.d.ts +3 -3
  52. package/interfaces/authentication.interface.d.ts +3 -3
  53. package/interfaces/company-branch.interface.d.ts +2 -2
  54. package/interfaces/company.interface.d.ts +1 -1
  55. package/interfaces/index.d.ts +8 -8
  56. package/interfaces/social-auth.interface.d.ts +2 -2
  57. package/interfaces/user-enricher.interface.d.ts +3 -3
  58. package/modules/auth.module.d.ts +4 -4
  59. package/modules/index.d.ts +1 -1
  60. package/package.json +32 -39
  61. package/providers/facebook.social-provider.d.ts +2 -2
  62. package/providers/google.social-provider.d.ts +2 -2
  63. package/providers/index.d.ts +1 -1
  64. package/providers/linkedin.social-provider.d.ts +2 -2
  65. package/providers/microsoft.social-provider.d.ts +2 -2
  66. package/providers/social-provider.registry.d.ts +2 -2
  67. package/services/auth-config.service.d.ts +3 -3
  68. package/services/auth-datasource.provider.d.ts +3 -3
  69. package/services/auth-email.service.d.ts +3 -3
  70. package/services/authentication.service.d.ts +12 -12
  71. package/services/branch.service.d.ts +7 -7
  72. package/services/company.service.d.ts +6 -6
  73. package/services/index.d.ts +11 -11
  74. package/services/social-auth-config.service.d.ts +6 -6
  75. package/services/social-auth.service.d.ts +5 -5
  76. package/services/user-permission.service.d.ts +10 -4
  77. package/services/user.service.d.ts +7 -7
  78. package/strategies/jwt.strategy.d.ts +4 -4
  79. package/validators/password.validator.d.ts +1 -1
  80. package/cjs/config/auth.constants.js +0 -49
  81. package/cjs/config/event-actions.js +0 -47
  82. package/cjs/config/index.js +0 -20
  83. package/cjs/config/message-keys.js +0 -135
  84. package/cjs/controllers/authentication.controller.js +0 -427
  85. package/cjs/controllers/authentication.controller.spec.js +0 -219
  86. package/cjs/controllers/branch.controller.js +0 -102
  87. package/cjs/controllers/branch.controller.spec.js +0 -134
  88. package/cjs/controllers/company-selection.controller.js +0 -198
  89. package/cjs/controllers/company-selection.controller.spec.js +0 -84
  90. package/cjs/controllers/company.controller.js +0 -102
  91. package/cjs/controllers/company.controller.spec.js +0 -131
  92. package/cjs/controllers/email-verification.controller.js +0 -224
  93. package/cjs/controllers/email-verification.controller.spec.js +0 -142
  94. package/cjs/controllers/index.js +0 -26
  95. package/cjs/controllers/social-auth-config.controller.js +0 -107
  96. package/cjs/controllers/social-auth.controller.js +0 -175
  97. package/cjs/controllers/social-auth.controller.spec.js +0 -79
  98. package/cjs/controllers/user-permission.controller.js +0 -223
  99. package/cjs/controllers/user-permission.controller.spec.js +0 -138
  100. package/cjs/controllers/user.controller.js +0 -438
  101. package/cjs/controllers/user.controller.spec.js +0 -204
  102. package/cjs/docs/auth-swagger.config.js +0 -461
  103. package/cjs/docs/auth-swagger.config.spec.js +0 -130
  104. package/cjs/docs/index.js +0 -18
  105. package/cjs/dtos/authentication.dto.js +0 -755
  106. package/cjs/dtos/company-branch.dto.js +0 -294
  107. package/cjs/dtos/company.dto.js +0 -298
  108. package/cjs/dtos/index.js +0 -23
  109. package/cjs/dtos/social-auth.dto.js +0 -318
  110. package/cjs/dtos/user-permission.dto.js +0 -358
  111. package/cjs/dtos/user.dto.js +0 -413
  112. package/cjs/entities/app-user.entity.js +0 -25
  113. package/cjs/entities/company-branch.entity.js +0 -163
  114. package/cjs/entities/company.entity.js +0 -146
  115. package/cjs/entities/index.js +0 -67
  116. package/cjs/entities/index.spec.js +0 -84
  117. package/cjs/entities/pending-registration.entity.js +0 -135
  118. package/cjs/entities/social-auth-config.entity.js +0 -102
  119. package/cjs/entities/user-company-permission.entity.js +0 -153
  120. package/cjs/entities/user-token.entity.js +0 -120
  121. package/cjs/enums/index.js +0 -19
  122. package/cjs/enums/social-provider-type.enum.js +0 -17
  123. package/cjs/enums/user-org-permission-type.enum.js +0 -15
  124. package/cjs/helpers/auth-response.mapper.js +0 -46
  125. package/cjs/helpers/auth-response.mapper.spec.js +0 -139
  126. package/cjs/helpers/index.js +0 -19
  127. package/cjs/helpers/token.helper.js +0 -58
  128. package/cjs/helpers/token.helper.spec.js +0 -86
  129. package/cjs/index.js +0 -30
  130. package/cjs/interceptors/clear-token.interceptor.js +0 -73
  131. package/cjs/interceptors/clear-token.interceptor.spec.js +0 -107
  132. package/cjs/interceptors/index.js +0 -19
  133. package/cjs/interceptors/set-token.interceptor.js +0 -107
  134. package/cjs/interceptors/set-token.interceptor.spec.js +0 -165
  135. package/cjs/interfaces/auth-email-provider.interface.js +0 -45
  136. package/cjs/interfaces/auth-module-options.interface.js +0 -4
  137. package/cjs/interfaces/authentication.interface.js +0 -4
  138. package/cjs/interfaces/company-branch.interface.js +0 -4
  139. package/cjs/interfaces/company.interface.js +0 -4
  140. package/cjs/interfaces/index.js +0 -25
  141. package/cjs/interfaces/social-auth.interface.js +0 -4
  142. package/cjs/interfaces/user-enricher.interface.js +0 -4
  143. package/cjs/interfaces/user.interface.js +0 -4
  144. package/cjs/modules/auth.module.js +0 -208
  145. package/cjs/modules/auth.module.spec.js +0 -258
  146. package/cjs/modules/index.js +0 -18
  147. package/cjs/providers/facebook.social-provider.js +0 -73
  148. package/cjs/providers/google.social-provider.js +0 -68
  149. package/cjs/providers/index.js +0 -18
  150. package/cjs/providers/linkedin.social-provider.js +0 -66
  151. package/cjs/providers/microsoft.social-provider.js +0 -78
  152. package/cjs/providers/oauth-http.util.js +0 -107
  153. package/cjs/providers/social-provider.registry.js +0 -24
  154. package/cjs/providers/social-provider.registry.spec.js +0 -135
  155. package/cjs/services/auth-config.service.js +0 -113
  156. package/cjs/services/auth-config.service.spec.js +0 -100
  157. package/cjs/services/auth-datasource.provider.js +0 -138
  158. package/cjs/services/auth-datasource.provider.spec.js +0 -191
  159. package/cjs/services/auth-email.service.js +0 -539
  160. package/cjs/services/auth-email.service.spec.js +0 -456
  161. package/cjs/services/authentication.service.js +0 -1203
  162. package/cjs/services/authentication.service.spec.js +0 -1020
  163. package/cjs/services/branch.service.js +0 -153
  164. package/cjs/services/branch.service.spec.js +0 -169
  165. package/cjs/services/company-selection-session.service.js +0 -121
  166. package/cjs/services/company-selection-session.service.spec.js +0 -90
  167. package/cjs/services/company.service.js +0 -140
  168. package/cjs/services/company.service.spec.js +0 -127
  169. package/cjs/services/index.js +0 -28
  170. package/cjs/services/social-auth-config.service.js +0 -103
  171. package/cjs/services/social-auth-config.service.spec.js +0 -71
  172. package/cjs/services/social-auth.service.js +0 -310
  173. package/cjs/services/social-auth.service.spec.js +0 -310
  174. package/cjs/services/user-permission.service.js +0 -236
  175. package/cjs/services/user-permission.service.spec.js +0 -254
  176. package/cjs/services/user.service.js +0 -671
  177. package/cjs/services/user.service.spec.js +0 -718
  178. package/cjs/strategies/jwt.strategy.js +0 -140
  179. package/cjs/strategies/jwt.strategy.spec.js +0 -166
  180. package/cjs/validators/password.validator.js +0 -45
  181. package/cjs/validators/password.validator.spec.js +0 -161
  182. package/fesm/config/auth.constants.js +0 -14
  183. package/fesm/config/event-actions.js +0 -26
  184. package/fesm/config/message-keys.js +0 -99
  185. package/fesm/controllers/authentication.controller.js +0 -417
  186. package/fesm/controllers/authentication.controller.spec.js +0 -215
  187. package/fesm/controllers/branch.controller.js +0 -92
  188. package/fesm/controllers/branch.controller.spec.js +0 -130
  189. package/fesm/controllers/company-selection.controller.js +0 -188
  190. package/fesm/controllers/company-selection.controller.spec.js +0 -80
  191. package/fesm/controllers/company.controller.js +0 -92
  192. package/fesm/controllers/company.controller.spec.js +0 -127
  193. package/fesm/controllers/email-verification.controller.js +0 -214
  194. package/fesm/controllers/email-verification.controller.spec.js +0 -138
  195. package/fesm/controllers/social-auth-config.controller.js +0 -97
  196. package/fesm/controllers/social-auth.controller.js +0 -165
  197. package/fesm/controllers/social-auth.controller.spec.js +0 -75
  198. package/fesm/controllers/user-permission.controller.js +0 -213
  199. package/fesm/controllers/user-permission.controller.spec.js +0 -134
  200. package/fesm/controllers/user.controller.js +0 -428
  201. package/fesm/controllers/user.controller.spec.js +0 -200
  202. package/fesm/docs/auth-swagger.config.js +0 -454
  203. package/fesm/docs/auth-swagger.config.spec.js +0 -126
  204. package/fesm/dtos/authentication.dto.js +0 -683
  205. package/fesm/dtos/company-branch.dto.js +0 -279
  206. package/fesm/dtos/company.dto.js +0 -283
  207. package/fesm/dtos/social-auth.dto.js +0 -282
  208. package/fesm/dtos/user-permission.dto.js +0 -339
  209. package/fesm/dtos/user.dto.js +0 -391
  210. package/fesm/entities/app-user.entity.js +0 -15
  211. package/fesm/entities/company-branch.entity.js +0 -153
  212. package/fesm/entities/company.entity.js +0 -136
  213. package/fesm/entities/index.spec.js +0 -80
  214. package/fesm/entities/pending-registration.entity.js +0 -125
  215. package/fesm/entities/social-auth-config.entity.js +0 -92
  216. package/fesm/entities/user-company-permission.entity.js +0 -136
  217. package/fesm/entities/user-token.entity.js +0 -104
  218. package/fesm/enums/social-provider-type.enum.js +0 -7
  219. package/fesm/enums/user-org-permission-type.enum.js +0 -5
  220. package/fesm/helpers/auth-response.mapper.js +0 -25
  221. package/fesm/helpers/auth-response.mapper.spec.js +0 -135
  222. package/fesm/helpers/token.helper.js +0 -40
  223. package/fesm/helpers/token.helper.spec.js +0 -82
  224. package/fesm/interceptors/clear-token.interceptor.js +0 -63
  225. package/fesm/interceptors/clear-token.interceptor.spec.js +0 -103
  226. package/fesm/interceptors/set-token.interceptor.js +0 -98
  227. package/fesm/interceptors/set-token.interceptor.spec.js +0 -161
  228. package/fesm/interfaces/auth-email-provider.interface.js +0 -38
  229. package/fesm/interfaces/auth-module-options.interface.js +0 -3
  230. package/fesm/interfaces/authentication.interface.js +0 -1
  231. package/fesm/interfaces/company-branch.interface.js +0 -1
  232. package/fesm/interfaces/company.interface.js +0 -1
  233. package/fesm/interfaces/social-auth.interface.js +0 -1
  234. package/fesm/interfaces/user-enricher.interface.js +0 -4
  235. package/fesm/interfaces/user.interface.js +0 -1
  236. package/fesm/modules/auth.module.js +0 -198
  237. package/fesm/modules/auth.module.spec.js +0 -254
  238. package/fesm/providers/facebook.social-provider.js +0 -63
  239. package/fesm/providers/google.social-provider.js +0 -58
  240. package/fesm/providers/index.js +0 -1
  241. package/fesm/providers/linkedin.social-provider.js +0 -56
  242. package/fesm/providers/microsoft.social-provider.js +0 -68
  243. package/fesm/providers/oauth-http.util.js +0 -77
  244. package/fesm/providers/social-provider.registry.js +0 -14
  245. package/fesm/providers/social-provider.registry.spec.js +0 -131
  246. package/fesm/services/auth-config.service.js +0 -103
  247. package/fesm/services/auth-config.service.spec.js +0 -96
  248. package/fesm/services/auth-datasource.provider.js +0 -87
  249. package/fesm/services/auth-datasource.provider.spec.js +0 -187
  250. package/fesm/services/auth-email.service.js +0 -488
  251. package/fesm/services/auth-email.service.spec.js +0 -411
  252. package/fesm/services/authentication.service.js +0 -1152
  253. package/fesm/services/authentication.service.spec.js +0 -975
  254. package/fesm/services/branch.service.js +0 -143
  255. package/fesm/services/branch.service.spec.js +0 -165
  256. package/fesm/services/company-selection-session.service.js +0 -111
  257. package/fesm/services/company-selection-session.service.spec.js +0 -86
  258. package/fesm/services/company.service.js +0 -130
  259. package/fesm/services/company.service.spec.js +0 -123
  260. package/fesm/services/social-auth-config.service.js +0 -93
  261. package/fesm/services/social-auth-config.service.spec.js +0 -67
  262. package/fesm/services/social-auth.service.js +0 -300
  263. package/fesm/services/social-auth.service.spec.js +0 -265
  264. package/fesm/services/user-permission.service.js +0 -226
  265. package/fesm/services/user-permission.service.spec.js +0 -250
  266. package/fesm/services/user.service.js +0 -620
  267. package/fesm/services/user.service.spec.js +0 -673
  268. package/fesm/strategies/jwt.strategy.js +0 -130
  269. package/fesm/strategies/jwt.strategy.spec.js +0 -162
  270. package/fesm/validators/password.validator.js +0 -48
  271. package/fesm/validators/password.validator.spec.js +0 -157
@@ -1,130 +0,0 @@
1
- function _define_property(obj, key, value) {
2
- if (key in obj) {
3
- Object.defineProperty(obj, key, {
4
- value: value,
5
- enumerable: true,
6
- configurable: true,
7
- writable: true
8
- });
9
- } else {
10
- obj[key] = value;
11
- }
12
- return obj;
13
- }
14
- function _ts_decorate(decorators, target, key, desc) {
15
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
16
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
17
- else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
18
- return c > 3 && r && Object.defineProperty(target, key, r), r;
19
- }
20
- function _ts_metadata(k, v) {
21
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
22
- }
23
- import { SYSTEM_MESSAGES } from '@flusys/nestjs-shared/constants';
24
- import { Injectable, UnauthorizedException } from '@nestjs/common';
25
- import { ModuleRef } from '@nestjs/core';
26
- import { PassportStrategy } from '@nestjs/passport';
27
- import { ExtractJwt, Strategy } from 'passport-jwt';
28
- import { AUTH_MESSAGES, USER_MESSAGES } from '../config';
29
- import { AppUser } from '../entities/app-user.entity';
30
- import { AuthConfigService } from '../services/auth-config.service';
31
- import { AuthDataSourceProvider } from '../services/auth-datasource.provider';
32
- import { AuthenticationService } from '../services/authentication.service';
33
- function hasCompanyContext(payload) {
34
- return 'companyId' in payload && payload.companyId != null;
35
- }
36
- export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
37
- async validate(_req, payload) {
38
- const userRepo = await this.resolveUserRepository();
39
- await this.checkTokenRevocation(payload);
40
- const user = await userRepo.findOne({
41
- where: {
42
- id: payload.id,
43
- isActive: true
44
- }
45
- });
46
- if (!user) {
47
- throw new UnauthorizedException({
48
- message: 'User not found or inactive',
49
- messageKey: USER_MESSAGES.NOT_FOUND
50
- });
51
- }
52
- return this.buildUserInfo(payload);
53
- }
54
- async resolveUserRepository() {
55
- try {
56
- const provider = await this.moduleRef.resolve(AuthDataSourceProvider, undefined, {
57
- strict: false
58
- });
59
- if (provider) {
60
- return provider.getRepository(AppUser);
61
- }
62
- } catch {
63
- throw new UnauthorizedException({
64
- message: 'Unable to access user data',
65
- messageKey: SYSTEM_MESSAGES.SERVICE_NOT_AVAILABLE,
66
- messageVariables: {
67
- provider: 'DataSourceProvider',
68
- available: ''
69
- }
70
- });
71
- }
72
- throw new UnauthorizedException({
73
- message: 'User repository not available',
74
- messageKey: SYSTEM_MESSAGES.REPOSITORY_NOT_AVAILABLE,
75
- messageVariables: {
76
- entity: 'User'
77
- }
78
- });
79
- }
80
- async checkTokenRevocation(payload) {
81
- if (!payload.iat) return;
82
- try {
83
- const authService = await this.moduleRef.resolve(AuthenticationService, undefined, {
84
- strict: false
85
- });
86
- if (authService && await authService.isTokenRevoked(payload.id, payload.iat)) {
87
- throw new UnauthorizedException({
88
- message: 'Token has been revoked',
89
- messageKey: AUTH_MESSAGES.TOKEN_REVOKED
90
- });
91
- }
92
- } catch (err) {
93
- if (err instanceof UnauthorizedException) throw err;
94
- }
95
- }
96
- buildUserInfo(payload) {
97
- const base = {
98
- id: payload.id,
99
- email: payload.email
100
- };
101
- if (!hasCompanyContext(payload)) {
102
- return base;
103
- }
104
- return {
105
- ...base,
106
- companyId: payload.companyId,
107
- ...payload.branchId && {
108
- branchId: payload.branchId
109
- }
110
- };
111
- }
112
- constructor(authConfig, moduleRef){
113
- super({
114
- jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
115
- ignoreExpiration: false,
116
- secretOrKeyProvider: (_req, _token, done)=>{
117
- done(null, this.authConfig.getJwtSecret());
118
- },
119
- passReqToCallback: true
120
- }), _define_property(this, "authConfig", void 0), _define_property(this, "moduleRef", void 0), this.authConfig = authConfig, this.moduleRef = moduleRef;
121
- }
122
- }
123
- JwtStrategy = _ts_decorate([
124
- Injectable(),
125
- _ts_metadata("design:type", Function),
126
- _ts_metadata("design:paramtypes", [
127
- typeof AuthConfigService === "undefined" ? Object : AuthConfigService,
128
- typeof ModuleRef === "undefined" ? Object : ModuleRef
129
- ])
130
- ], JwtStrategy);
@@ -1,162 +0,0 @@
1
- import { UnauthorizedException } from '@nestjs/common';
2
- import { createMockRepository } from '@test-utils/mocks/repository.mock';
3
- import { JwtStrategy } from './jwt.strategy';
4
- import { AuthDataSourceProvider } from '../services/auth-datasource.provider';
5
- import { AuthenticationService } from '../services/authentication.service';
6
- describe('JwtStrategy', ()=>{
7
- let strategy;
8
- let userRepo;
9
- let mockAuthConfig;
10
- let mockModuleRef;
11
- let mockDataSourceProvider;
12
- let mockAuthService;
13
- beforeEach(()=>{
14
- userRepo = createMockRepository();
15
- mockAuthConfig = {
16
- getJwtSecret: jest.fn().mockReturnValue('jwt-secret')
17
- };
18
- mockDataSourceProvider = {
19
- getRepository: jest.fn().mockResolvedValue(userRepo)
20
- };
21
- mockAuthService = {
22
- isTokenRevoked: jest.fn().mockResolvedValue(false)
23
- };
24
- mockModuleRef = {
25
- resolve: jest.fn().mockImplementation(async (token)=>{
26
- if (token === AuthDataSourceProvider) return mockDataSourceProvider;
27
- if (token === AuthenticationService) return mockAuthService;
28
- return null;
29
- })
30
- };
31
- strategy = new JwtStrategy(mockAuthConfig, mockModuleRef);
32
- });
33
- describe('validate', ()=>{
34
- const req = {};
35
- it('returns basic user info for a payload without company context', async ()=>{
36
- userRepo.findOne.mockResolvedValue({
37
- id: 'user-1',
38
- isActive: true
39
- });
40
- const result = await strategy.validate(req, {
41
- id: 'user-1',
42
- email: 'user@example.com',
43
- type: 'access'
44
- });
45
- expect(result).toEqual({
46
- id: 'user-1',
47
- email: 'user@example.com'
48
- });
49
- });
50
- it('includes companyId and branchId when present in the payload', async ()=>{
51
- userRepo.findOne.mockResolvedValue({
52
- id: 'user-1',
53
- isActive: true
54
- });
55
- const result = await strategy.validate(req, {
56
- id: 'user-1',
57
- email: 'user@example.com',
58
- type: 'access',
59
- companyId: 'company-1',
60
- branchId: 'branch-1'
61
- });
62
- expect(result).toEqual({
63
- id: 'user-1',
64
- email: 'user@example.com',
65
- companyId: 'company-1',
66
- branchId: 'branch-1'
67
- });
68
- });
69
- it('omits branchId when only companyId is present', async ()=>{
70
- userRepo.findOne.mockResolvedValue({
71
- id: 'user-1',
72
- isActive: true
73
- });
74
- const result = await strategy.validate(req, {
75
- id: 'user-1',
76
- email: 'user@example.com',
77
- type: 'access',
78
- companyId: 'company-1'
79
- });
80
- expect(result).toEqual({
81
- id: 'user-1',
82
- email: 'user@example.com',
83
- companyId: 'company-1'
84
- });
85
- });
86
- it('throws UnauthorizedException when the user is not found or inactive', async ()=>{
87
- userRepo.findOne.mockResolvedValue(null);
88
- await expect(strategy.validate(req, {
89
- id: 'missing-user',
90
- email: 'x@example.com',
91
- type: 'access'
92
- })).rejects.toThrow(UnauthorizedException);
93
- });
94
- it('throws UnauthorizedException when the token has been revoked', async ()=>{
95
- userRepo.findOne.mockResolvedValue({
96
- id: 'user-1',
97
- isActive: true
98
- });
99
- mockAuthService.isTokenRevoked.mockResolvedValue(true);
100
- await expect(strategy.validate(req, {
101
- id: 'user-1',
102
- email: 'user@example.com',
103
- type: 'access',
104
- iat: 1000
105
- })).rejects.toThrow(UnauthorizedException);
106
- });
107
- it('skips revocation check when the payload has no iat', async ()=>{
108
- userRepo.findOne.mockResolvedValue({
109
- id: 'user-1',
110
- isActive: true
111
- });
112
- await strategy.validate(req, {
113
- id: 'user-1',
114
- email: 'user@example.com',
115
- type: 'access'
116
- });
117
- expect(mockAuthService.isTokenRevoked).not.toHaveBeenCalled();
118
- });
119
- it('throws UnauthorizedException when the data source provider cannot be resolved', async ()=>{
120
- mockModuleRef.resolve.mockImplementation(async (token)=>{
121
- if (token === AuthDataSourceProvider) return null;
122
- return mockAuthService;
123
- });
124
- await expect(strategy.validate(req, {
125
- id: 'user-1',
126
- email: 'user@example.com',
127
- type: 'access'
128
- })).rejects.toThrow(UnauthorizedException);
129
- });
130
- it('propagates an UnauthorizedException raised while resolving the data source provider', async ()=>{
131
- mockModuleRef.resolve.mockImplementation(async (token)=>{
132
- if (token === AuthDataSourceProvider) throw new Error('resolution failed');
133
- return mockAuthService;
134
- });
135
- await expect(strategy.validate(req, {
136
- id: 'user-1',
137
- email: 'user@example.com',
138
- type: 'access'
139
- })).rejects.toThrow(UnauthorizedException);
140
- });
141
- it('does not fail validation when the authentication service cannot be resolved for revocation checks', async ()=>{
142
- userRepo.findOne.mockResolvedValue({
143
- id: 'user-1',
144
- isActive: true
145
- });
146
- mockModuleRef.resolve.mockImplementation(async (token)=>{
147
- if (token === AuthDataSourceProvider) return mockDataSourceProvider;
148
- return null;
149
- });
150
- const result = await strategy.validate(req, {
151
- id: 'user-1',
152
- email: 'user@example.com',
153
- type: 'access',
154
- iat: 1000
155
- });
156
- expect(result).toEqual({
157
- id: 'user-1',
158
- email: 'user@example.com'
159
- });
160
- });
161
- });
162
- });
@@ -1,48 +0,0 @@
1
- import { registerDecorator } from 'class-validator';
2
- /**
3
- * Strong password validation decorator.
4
- * Validates that passwords meet complexity requirements to prevent weak passwords.
5
- *
6
- * @example
7
- * ```typescript
8
- * @IsStrongPassword()
9
- * password!: string;
10
- *
11
- * // With custom options
12
- * @IsStrongPassword({ minLength: 12, requireSpecialChar: false })
13
- * password!: string;
14
- * ```
15
- */ export function IsStrongPassword(options = {}, validationOptions) {
16
- const { minLength = 8, maxLength = 128, requireUppercase = true, requireLowercase = true, requireNumber = true, requireSpecialChar = true } = options;
17
- return function(object, propertyName) {
18
- registerDecorator({
19
- name: 'isStrongPassword',
20
- target: object.constructor,
21
- propertyName: propertyName,
22
- options: validationOptions,
23
- validator: {
24
- validate (value) {
25
- if (typeof value !== 'string') return false;
26
- const checks = [
27
- value.length >= minLength,
28
- value.length <= maxLength,
29
- !requireUppercase || /[A-Z]/.test(value),
30
- !requireLowercase || /[a-z]/.test(value),
31
- !requireNumber || /[0-9]/.test(value),
32
- !requireSpecialChar || /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~`]/.test(value)
33
- ];
34
- return checks.every(Boolean);
35
- },
36
- defaultMessage (args) {
37
- const requirements = [];
38
- if (minLength > 0) requirements.push(`at least ${minLength} characters`);
39
- if (requireUppercase) requirements.push('one uppercase letter');
40
- if (requireLowercase) requirements.push('one lowercase letter');
41
- if (requireNumber) requirements.push('one number');
42
- if (requireSpecialChar) requirements.push('one special character (!@#$%^&*...)');
43
- return `Password must contain ${requirements.join(', ')}`;
44
- }
45
- }
46
- });
47
- };
48
- }
@@ -1,157 +0,0 @@
1
- function _define_property(obj, key, value) {
2
- if (key in obj) {
3
- Object.defineProperty(obj, key, {
4
- value: value,
5
- enumerable: true,
6
- configurable: true,
7
- writable: true
8
- });
9
- } else {
10
- obj[key] = value;
11
- }
12
- return obj;
13
- }
14
- function _ts_decorate(decorators, target, key, desc) {
15
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
16
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
17
- else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
18
- return c > 3 && r && Object.defineProperty(target, key, r), r;
19
- }
20
- function _ts_metadata(k, v) {
21
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
22
- }
23
- import { validate } from 'class-validator';
24
- import { IsStrongPassword } from './password.validator';
25
- let DefaultPasswordDto = class DefaultPasswordDto {
26
- constructor(){
27
- _define_property(this, "password", void 0);
28
- }
29
- };
30
- _ts_decorate([
31
- IsStrongPassword(),
32
- _ts_metadata("design:type", String)
33
- ], DefaultPasswordDto.prototype, "password", void 0);
34
- let CustomPasswordDto = class CustomPasswordDto {
35
- constructor(){
36
- _define_property(this, "password", void 0);
37
- }
38
- };
39
- _ts_decorate([
40
- IsStrongPassword({
41
- minLength: 12,
42
- requireSpecialChar: false
43
- }),
44
- _ts_metadata("design:type", String)
45
- ], CustomPasswordDto.prototype, "password", void 0);
46
- let NoComplexityPasswordDto = class NoComplexityPasswordDto {
47
- constructor(){
48
- _define_property(this, "password", void 0);
49
- }
50
- };
51
- _ts_decorate([
52
- IsStrongPassword({
53
- requireUppercase: false,
54
- requireLowercase: false,
55
- requireNumber: false,
56
- requireSpecialChar: false,
57
- minLength: 4
58
- }),
59
- _ts_metadata("design:type", String)
60
- ], NoComplexityPasswordDto.prototype, "password", void 0);
61
- async function validatePassword(dto) {
62
- const errors = await validate(dto);
63
- return errors.length === 0;
64
- }
65
- describe('IsStrongPassword', ()=>{
66
- describe('default options', ()=>{
67
- it('accepts a password meeting all default complexity rules', async ()=>{
68
- const dto = new DefaultPasswordDto();
69
- dto.password = 'StrongP@ss1';
70
- expect(await validatePassword(dto)).toBe(true);
71
- });
72
- it('rejects a password shorter than the minimum length', async ()=>{
73
- const dto = new DefaultPasswordDto();
74
- dto.password = 'Sh0rt!';
75
- expect(await validatePassword(dto)).toBe(false);
76
- });
77
- it('accepts a password at exactly the minimum length boundary', async ()=>{
78
- const dto = new DefaultPasswordDto();
79
- dto.password = 'Ab1!Ab1!'; // 8 chars
80
- expect(await validatePassword(dto)).toBe(true);
81
- });
82
- it('rejects a password longer than the maximum length', async ()=>{
83
- const dto = new DefaultPasswordDto();
84
- dto.password = 'A1!' + 'a'.repeat(127); // 130 chars total
85
- expect(await validatePassword(dto)).toBe(false);
86
- });
87
- it('accepts a password at exactly the maximum length boundary', async ()=>{
88
- const dto = new DefaultPasswordDto();
89
- dto.password = 'A1!' + 'a'.repeat(125); // 128 chars total
90
- expect(await validatePassword(dto)).toBe(true);
91
- });
92
- it('rejects a password missing an uppercase letter', async ()=>{
93
- const dto = new DefaultPasswordDto();
94
- dto.password = 'lowercase1!';
95
- expect(await validatePassword(dto)).toBe(false);
96
- });
97
- it('rejects a password missing a lowercase letter', async ()=>{
98
- const dto = new DefaultPasswordDto();
99
- dto.password = 'UPPERCASE1!';
100
- expect(await validatePassword(dto)).toBe(false);
101
- });
102
- it('rejects a password missing a number', async ()=>{
103
- const dto = new DefaultPasswordDto();
104
- dto.password = 'NoNumbers!';
105
- expect(await validatePassword(dto)).toBe(false);
106
- });
107
- it('rejects a password missing a special character', async ()=>{
108
- const dto = new DefaultPasswordDto();
109
- dto.password = 'NoSpecial1';
110
- expect(await validatePassword(dto)).toBe(false);
111
- });
112
- it('rejects a non-string value', async ()=>{
113
- const dto = new DefaultPasswordDto();
114
- dto.password = 12345678;
115
- expect(await validatePassword(dto)).toBe(false);
116
- });
117
- it('rejects an empty string', async ()=>{
118
- const dto = new DefaultPasswordDto();
119
- dto.password = '';
120
- expect(await validatePassword(dto)).toBe(false);
121
- });
122
- it('produces a descriptive default error message', async ()=>{
123
- const dto = new DefaultPasswordDto();
124
- dto.password = 'bad';
125
- const errors = await validate(dto);
126
- expect(errors).toHaveLength(1);
127
- const message = Object.values(errors[0].constraints ?? {})[0];
128
- expect(message).toContain('at least 8 characters');
129
- expect(message).toContain('one uppercase letter');
130
- expect(message).toContain('one lowercase letter');
131
- expect(message).toContain('one number');
132
- expect(message).toContain('one special character');
133
- });
134
- });
135
- describe('custom options', ()=>{
136
- it('enforces a custom minimum length', async ()=>{
137
- const dto = new CustomPasswordDto();
138
- dto.password = 'Short1Aa';
139
- expect(await validatePassword(dto)).toBe(false);
140
- });
141
- it('accepts a password without special chars when requireSpecialChar is false', async ()=>{
142
- const dto = new CustomPasswordDto();
143
- dto.password = 'LongEnoughPassword1';
144
- expect(await validatePassword(dto)).toBe(true);
145
- });
146
- it('allows fully relaxed complexity requirements', async ()=>{
147
- const dto = new NoComplexityPasswordDto();
148
- dto.password = 'abcd';
149
- expect(await validatePassword(dto)).toBe(true);
150
- });
151
- it('still enforces minLength when complexity checks are disabled', async ()=>{
152
- const dto = new NoComplexityPasswordDto();
153
- dto.password = 'abc';
154
- expect(await validatePassword(dto)).toBe(false);
155
- });
156
- });
157
- });