@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,140 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "JwtStrategy", {
6
- enumerable: true,
7
- get: function() {
8
- return JwtStrategy;
9
- }
10
- });
11
- const _constants = require("@flusys/nestjs-shared/constants");
12
- const _common = require("@nestjs/common");
13
- const _core = require("@nestjs/core");
14
- const _passport = require("@nestjs/passport");
15
- const _passportjwt = require("passport-jwt");
16
- const _config = require("../config");
17
- const _appuserentity = require("../entities/app-user.entity");
18
- const _authconfigservice = require("../services/auth-config.service");
19
- const _authdatasourceprovider = require("../services/auth-datasource.provider");
20
- const _authenticationservice = require("../services/authentication.service");
21
- function _define_property(obj, key, value) {
22
- if (key in obj) {
23
- Object.defineProperty(obj, key, {
24
- value: value,
25
- enumerable: true,
26
- configurable: true,
27
- writable: true
28
- });
29
- } else {
30
- obj[key] = value;
31
- }
32
- return obj;
33
- }
34
- function _ts_decorate(decorators, target, key, desc) {
35
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
36
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
37
- 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;
38
- return c > 3 && r && Object.defineProperty(target, key, r), r;
39
- }
40
- function _ts_metadata(k, v) {
41
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
42
- }
43
- function hasCompanyContext(payload) {
44
- return 'companyId' in payload && payload.companyId != null;
45
- }
46
- let JwtStrategy = class JwtStrategy extends (0, _passport.PassportStrategy)(_passportjwt.Strategy, 'jwt') {
47
- async validate(_req, payload) {
48
- const userRepo = await this.resolveUserRepository();
49
- await this.checkTokenRevocation(payload);
50
- const user = await userRepo.findOne({
51
- where: {
52
- id: payload.id,
53
- isActive: true
54
- }
55
- });
56
- if (!user) {
57
- throw new _common.UnauthorizedException({
58
- message: 'User not found or inactive',
59
- messageKey: _config.USER_MESSAGES.NOT_FOUND
60
- });
61
- }
62
- return this.buildUserInfo(payload);
63
- }
64
- async resolveUserRepository() {
65
- try {
66
- const provider = await this.moduleRef.resolve(_authdatasourceprovider.AuthDataSourceProvider, undefined, {
67
- strict: false
68
- });
69
- if (provider) {
70
- return provider.getRepository(_appuserentity.AppUser);
71
- }
72
- } catch {
73
- throw new _common.UnauthorizedException({
74
- message: 'Unable to access user data',
75
- messageKey: _constants.SYSTEM_MESSAGES.SERVICE_NOT_AVAILABLE,
76
- messageVariables: {
77
- provider: 'DataSourceProvider',
78
- available: ''
79
- }
80
- });
81
- }
82
- throw new _common.UnauthorizedException({
83
- message: 'User repository not available',
84
- messageKey: _constants.SYSTEM_MESSAGES.REPOSITORY_NOT_AVAILABLE,
85
- messageVariables: {
86
- entity: 'User'
87
- }
88
- });
89
- }
90
- async checkTokenRevocation(payload) {
91
- if (!payload.iat) return;
92
- try {
93
- const authService = await this.moduleRef.resolve(_authenticationservice.AuthenticationService, undefined, {
94
- strict: false
95
- });
96
- if (authService && await authService.isTokenRevoked(payload.id, payload.iat)) {
97
- throw new _common.UnauthorizedException({
98
- message: 'Token has been revoked',
99
- messageKey: _config.AUTH_MESSAGES.TOKEN_REVOKED
100
- });
101
- }
102
- } catch (err) {
103
- if (err instanceof _common.UnauthorizedException) throw err;
104
- }
105
- }
106
- buildUserInfo(payload) {
107
- const base = {
108
- id: payload.id,
109
- email: payload.email
110
- };
111
- if (!hasCompanyContext(payload)) {
112
- return base;
113
- }
114
- return {
115
- ...base,
116
- companyId: payload.companyId,
117
- ...payload.branchId && {
118
- branchId: payload.branchId
119
- }
120
- };
121
- }
122
- constructor(authConfig, moduleRef){
123
- super({
124
- jwtFromRequest: _passportjwt.ExtractJwt.fromAuthHeaderAsBearerToken(),
125
- ignoreExpiration: false,
126
- secretOrKeyProvider: (_req, _token, done)=>{
127
- done(null, this.authConfig.getJwtSecret());
128
- },
129
- passReqToCallback: true
130
- }), _define_property(this, "authConfig", void 0), _define_property(this, "moduleRef", void 0), this.authConfig = authConfig, this.moduleRef = moduleRef;
131
- }
132
- };
133
- JwtStrategy = _ts_decorate([
134
- (0, _common.Injectable)(),
135
- _ts_metadata("design:type", Function),
136
- _ts_metadata("design:paramtypes", [
137
- typeof _authconfigservice.AuthConfigService === "undefined" ? Object : _authconfigservice.AuthConfigService,
138
- typeof _core.ModuleRef === "undefined" ? Object : _core.ModuleRef
139
- ])
140
- ], JwtStrategy);
@@ -1,166 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- const _common = require("@nestjs/common");
6
- const _repositorymock = require("@test-utils/mocks/repository.mock");
7
- const _jwtstrategy = require("./jwt.strategy");
8
- const _authdatasourceprovider = require("../services/auth-datasource.provider");
9
- const _authenticationservice = require("../services/authentication.service");
10
- describe('JwtStrategy', ()=>{
11
- let strategy;
12
- let userRepo;
13
- let mockAuthConfig;
14
- let mockModuleRef;
15
- let mockDataSourceProvider;
16
- let mockAuthService;
17
- beforeEach(()=>{
18
- userRepo = (0, _repositorymock.createMockRepository)();
19
- mockAuthConfig = {
20
- getJwtSecret: jest.fn().mockReturnValue('jwt-secret')
21
- };
22
- mockDataSourceProvider = {
23
- getRepository: jest.fn().mockResolvedValue(userRepo)
24
- };
25
- mockAuthService = {
26
- isTokenRevoked: jest.fn().mockResolvedValue(false)
27
- };
28
- mockModuleRef = {
29
- resolve: jest.fn().mockImplementation(async (token)=>{
30
- if (token === _authdatasourceprovider.AuthDataSourceProvider) return mockDataSourceProvider;
31
- if (token === _authenticationservice.AuthenticationService) return mockAuthService;
32
- return null;
33
- })
34
- };
35
- strategy = new _jwtstrategy.JwtStrategy(mockAuthConfig, mockModuleRef);
36
- });
37
- describe('validate', ()=>{
38
- const req = {};
39
- it('returns basic user info for a payload without company context', async ()=>{
40
- userRepo.findOne.mockResolvedValue({
41
- id: 'user-1',
42
- isActive: true
43
- });
44
- const result = await strategy.validate(req, {
45
- id: 'user-1',
46
- email: 'user@example.com',
47
- type: 'access'
48
- });
49
- expect(result).toEqual({
50
- id: 'user-1',
51
- email: 'user@example.com'
52
- });
53
- });
54
- it('includes companyId and branchId when present in the payload', async ()=>{
55
- userRepo.findOne.mockResolvedValue({
56
- id: 'user-1',
57
- isActive: true
58
- });
59
- const result = await strategy.validate(req, {
60
- id: 'user-1',
61
- email: 'user@example.com',
62
- type: 'access',
63
- companyId: 'company-1',
64
- branchId: 'branch-1'
65
- });
66
- expect(result).toEqual({
67
- id: 'user-1',
68
- email: 'user@example.com',
69
- companyId: 'company-1',
70
- branchId: 'branch-1'
71
- });
72
- });
73
- it('omits branchId when only companyId is present', async ()=>{
74
- userRepo.findOne.mockResolvedValue({
75
- id: 'user-1',
76
- isActive: true
77
- });
78
- const result = await strategy.validate(req, {
79
- id: 'user-1',
80
- email: 'user@example.com',
81
- type: 'access',
82
- companyId: 'company-1'
83
- });
84
- expect(result).toEqual({
85
- id: 'user-1',
86
- email: 'user@example.com',
87
- companyId: 'company-1'
88
- });
89
- });
90
- it('throws UnauthorizedException when the user is not found or inactive', async ()=>{
91
- userRepo.findOne.mockResolvedValue(null);
92
- await expect(strategy.validate(req, {
93
- id: 'missing-user',
94
- email: 'x@example.com',
95
- type: 'access'
96
- })).rejects.toThrow(_common.UnauthorizedException);
97
- });
98
- it('throws UnauthorizedException when the token has been revoked', async ()=>{
99
- userRepo.findOne.mockResolvedValue({
100
- id: 'user-1',
101
- isActive: true
102
- });
103
- mockAuthService.isTokenRevoked.mockResolvedValue(true);
104
- await expect(strategy.validate(req, {
105
- id: 'user-1',
106
- email: 'user@example.com',
107
- type: 'access',
108
- iat: 1000
109
- })).rejects.toThrow(_common.UnauthorizedException);
110
- });
111
- it('skips revocation check when the payload has no iat', async ()=>{
112
- userRepo.findOne.mockResolvedValue({
113
- id: 'user-1',
114
- isActive: true
115
- });
116
- await strategy.validate(req, {
117
- id: 'user-1',
118
- email: 'user@example.com',
119
- type: 'access'
120
- });
121
- expect(mockAuthService.isTokenRevoked).not.toHaveBeenCalled();
122
- });
123
- it('throws UnauthorizedException when the data source provider cannot be resolved', async ()=>{
124
- mockModuleRef.resolve.mockImplementation(async (token)=>{
125
- if (token === _authdatasourceprovider.AuthDataSourceProvider) return null;
126
- return mockAuthService;
127
- });
128
- await expect(strategy.validate(req, {
129
- id: 'user-1',
130
- email: 'user@example.com',
131
- type: 'access'
132
- })).rejects.toThrow(_common.UnauthorizedException);
133
- });
134
- it('propagates an UnauthorizedException raised while resolving the data source provider', async ()=>{
135
- mockModuleRef.resolve.mockImplementation(async (token)=>{
136
- if (token === _authdatasourceprovider.AuthDataSourceProvider) throw new Error('resolution failed');
137
- return mockAuthService;
138
- });
139
- await expect(strategy.validate(req, {
140
- id: 'user-1',
141
- email: 'user@example.com',
142
- type: 'access'
143
- })).rejects.toThrow(_common.UnauthorizedException);
144
- });
145
- it('does not fail validation when the authentication service cannot be resolved for revocation checks', async ()=>{
146
- userRepo.findOne.mockResolvedValue({
147
- id: 'user-1',
148
- isActive: true
149
- });
150
- mockModuleRef.resolve.mockImplementation(async (token)=>{
151
- if (token === _authdatasourceprovider.AuthDataSourceProvider) return mockDataSourceProvider;
152
- return null;
153
- });
154
- const result = await strategy.validate(req, {
155
- id: 'user-1',
156
- email: 'user@example.com',
157
- type: 'access',
158
- iat: 1000
159
- });
160
- expect(result).toEqual({
161
- id: 'user-1',
162
- email: 'user@example.com'
163
- });
164
- });
165
- });
166
- });
@@ -1,45 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(exports, "IsStrongPassword", {
6
- enumerable: true,
7
- get: function() {
8
- return IsStrongPassword;
9
- }
10
- });
11
- const _classvalidator = require("class-validator");
12
- function IsStrongPassword(options = {}, validationOptions) {
13
- const { minLength = 8, maxLength = 128, requireUppercase = true, requireLowercase = true, requireNumber = true, requireSpecialChar = true } = options;
14
- return function(object, propertyName) {
15
- (0, _classvalidator.registerDecorator)({
16
- name: 'isStrongPassword',
17
- target: object.constructor,
18
- propertyName: propertyName,
19
- options: validationOptions,
20
- validator: {
21
- validate (value) {
22
- if (typeof value !== 'string') return false;
23
- const checks = [
24
- value.length >= minLength,
25
- value.length <= maxLength,
26
- !requireUppercase || /[A-Z]/.test(value),
27
- !requireLowercase || /[a-z]/.test(value),
28
- !requireNumber || /[0-9]/.test(value),
29
- !requireSpecialChar || /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~`]/.test(value)
30
- ];
31
- return checks.every(Boolean);
32
- },
33
- defaultMessage (args) {
34
- const requirements = [];
35
- if (minLength > 0) requirements.push(`at least ${minLength} characters`);
36
- if (requireUppercase) requirements.push('one uppercase letter');
37
- if (requireLowercase) requirements.push('one lowercase letter');
38
- if (requireNumber) requirements.push('one number');
39
- if (requireSpecialChar) requirements.push('one special character (!@#$%^&*...)');
40
- return `Password must contain ${requirements.join(', ')}`;
41
- }
42
- }
43
- });
44
- };
45
- }
@@ -1,161 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- const _classvalidator = require("class-validator");
6
- const _passwordvalidator = require("./password.validator");
7
- function _define_property(obj, key, value) {
8
- if (key in obj) {
9
- Object.defineProperty(obj, key, {
10
- value: value,
11
- enumerable: true,
12
- configurable: true,
13
- writable: true
14
- });
15
- } else {
16
- obj[key] = value;
17
- }
18
- return obj;
19
- }
20
- function _ts_decorate(decorators, target, key, desc) {
21
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
22
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
23
- 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;
24
- return c > 3 && r && Object.defineProperty(target, key, r), r;
25
- }
26
- function _ts_metadata(k, v) {
27
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
28
- }
29
- let DefaultPasswordDto = class DefaultPasswordDto {
30
- constructor(){
31
- _define_property(this, "password", void 0);
32
- }
33
- };
34
- _ts_decorate([
35
- (0, _passwordvalidator.IsStrongPassword)(),
36
- _ts_metadata("design:type", String)
37
- ], DefaultPasswordDto.prototype, "password", void 0);
38
- let CustomPasswordDto = class CustomPasswordDto {
39
- constructor(){
40
- _define_property(this, "password", void 0);
41
- }
42
- };
43
- _ts_decorate([
44
- (0, _passwordvalidator.IsStrongPassword)({
45
- minLength: 12,
46
- requireSpecialChar: false
47
- }),
48
- _ts_metadata("design:type", String)
49
- ], CustomPasswordDto.prototype, "password", void 0);
50
- let NoComplexityPasswordDto = class NoComplexityPasswordDto {
51
- constructor(){
52
- _define_property(this, "password", void 0);
53
- }
54
- };
55
- _ts_decorate([
56
- (0, _passwordvalidator.IsStrongPassword)({
57
- requireUppercase: false,
58
- requireLowercase: false,
59
- requireNumber: false,
60
- requireSpecialChar: false,
61
- minLength: 4
62
- }),
63
- _ts_metadata("design:type", String)
64
- ], NoComplexityPasswordDto.prototype, "password", void 0);
65
- async function validatePassword(dto) {
66
- const errors = await (0, _classvalidator.validate)(dto);
67
- return errors.length === 0;
68
- }
69
- describe('IsStrongPassword', ()=>{
70
- describe('default options', ()=>{
71
- it('accepts a password meeting all default complexity rules', async ()=>{
72
- const dto = new DefaultPasswordDto();
73
- dto.password = 'StrongP@ss1';
74
- expect(await validatePassword(dto)).toBe(true);
75
- });
76
- it('rejects a password shorter than the minimum length', async ()=>{
77
- const dto = new DefaultPasswordDto();
78
- dto.password = 'Sh0rt!';
79
- expect(await validatePassword(dto)).toBe(false);
80
- });
81
- it('accepts a password at exactly the minimum length boundary', async ()=>{
82
- const dto = new DefaultPasswordDto();
83
- dto.password = 'Ab1!Ab1!'; // 8 chars
84
- expect(await validatePassword(dto)).toBe(true);
85
- });
86
- it('rejects a password longer than the maximum length', async ()=>{
87
- const dto = new DefaultPasswordDto();
88
- dto.password = 'A1!' + 'a'.repeat(127); // 130 chars total
89
- expect(await validatePassword(dto)).toBe(false);
90
- });
91
- it('accepts a password at exactly the maximum length boundary', async ()=>{
92
- const dto = new DefaultPasswordDto();
93
- dto.password = 'A1!' + 'a'.repeat(125); // 128 chars total
94
- expect(await validatePassword(dto)).toBe(true);
95
- });
96
- it('rejects a password missing an uppercase letter', async ()=>{
97
- const dto = new DefaultPasswordDto();
98
- dto.password = 'lowercase1!';
99
- expect(await validatePassword(dto)).toBe(false);
100
- });
101
- it('rejects a password missing a lowercase letter', async ()=>{
102
- const dto = new DefaultPasswordDto();
103
- dto.password = 'UPPERCASE1!';
104
- expect(await validatePassword(dto)).toBe(false);
105
- });
106
- it('rejects a password missing a number', async ()=>{
107
- const dto = new DefaultPasswordDto();
108
- dto.password = 'NoNumbers!';
109
- expect(await validatePassword(dto)).toBe(false);
110
- });
111
- it('rejects a password missing a special character', async ()=>{
112
- const dto = new DefaultPasswordDto();
113
- dto.password = 'NoSpecial1';
114
- expect(await validatePassword(dto)).toBe(false);
115
- });
116
- it('rejects a non-string value', async ()=>{
117
- const dto = new DefaultPasswordDto();
118
- dto.password = 12345678;
119
- expect(await validatePassword(dto)).toBe(false);
120
- });
121
- it('rejects an empty string', async ()=>{
122
- const dto = new DefaultPasswordDto();
123
- dto.password = '';
124
- expect(await validatePassword(dto)).toBe(false);
125
- });
126
- it('produces a descriptive default error message', async ()=>{
127
- const dto = new DefaultPasswordDto();
128
- dto.password = 'bad';
129
- const errors = await (0, _classvalidator.validate)(dto);
130
- expect(errors).toHaveLength(1);
131
- const message = Object.values(errors[0].constraints ?? {})[0];
132
- expect(message).toContain('at least 8 characters');
133
- expect(message).toContain('one uppercase letter');
134
- expect(message).toContain('one lowercase letter');
135
- expect(message).toContain('one number');
136
- expect(message).toContain('one special character');
137
- });
138
- });
139
- describe('custom options', ()=>{
140
- it('enforces a custom minimum length', async ()=>{
141
- const dto = new CustomPasswordDto();
142
- dto.password = 'Short1Aa';
143
- expect(await validatePassword(dto)).toBe(false);
144
- });
145
- it('accepts a password without special chars when requireSpecialChar is false', async ()=>{
146
- const dto = new CustomPasswordDto();
147
- dto.password = 'LongEnoughPassword1';
148
- expect(await validatePassword(dto)).toBe(true);
149
- });
150
- it('allows fully relaxed complexity requirements', async ()=>{
151
- const dto = new NoComplexityPasswordDto();
152
- dto.password = 'abcd';
153
- expect(await validatePassword(dto)).toBe(true);
154
- });
155
- it('still enforces minLength when complexity checks are disabled', async ()=>{
156
- const dto = new NoComplexityPasswordDto();
157
- dto.password = 'abc';
158
- expect(await validatePassword(dto)).toBe(false);
159
- });
160
- });
161
- });
@@ -1,14 +0,0 @@
1
- // Injection Tokens
2
- export const AUTH_MODULE_OPTIONS = 'AUTH_MODULE_OPTIONS';
3
- export const USER_ENRICHER = 'USER_ENRICHER';
4
- // Security
5
- export const BCRYPT_SALT_ROUNDS = 12;
6
- // Cookie
7
- export const DEFAULT_COOKIE_NAME = 'fsn_refresh_token';
8
- export const MAX_COOKIE_BYTES = 3500;
9
- // Cache Key Prefixes
10
- export const SESSION_CACHE_PREFIX = 'auth:session';
11
- export const TOKEN_REVOKED_PREFIX = 'auth:revoked';
12
- export const SOCIAL_STATE_PREFIX = 'auth:social:state';
13
- // Social Login
14
- export const SOCIAL_STATE_TTL_MS = 10 * 60 * 1000; // 10 minutes
@@ -1,26 +0,0 @@
1
- /**
2
- * Domain event actions published by the auth module, alongside the CRUD
3
- * lifecycle actions every ApiService emits.
4
- */ export const AUTH_EVENT_ACTIONS = {
5
- LOGGED_IN: 'logged-in',
6
- LOGGED_OUT: 'logged-out',
7
- TOKEN_REFRESHED: 'token-refreshed',
8
- COMPANY_SWITCHED: 'company-switched',
9
- REGISTERED: 'registered',
10
- PASSWORD_CHANGED: 'password-changed',
11
- PASSWORD_RESET_REQUESTED: 'password-reset-requested',
12
- PASSWORD_RESET: 'password-reset',
13
- EMAIL_VERIFIED: 'email-verified',
14
- PERMISSION_GRANTED: 'permission-granted',
15
- PERMISSION_REVOKED: 'permission-revoked',
16
- SOCIAL_LOGGED_IN: 'social-logged-in'
17
- };
18
- /** Every entity the auth module publishes under, ApiService backed or not */ export const AUTH_EVENT_ENTITIES = {
19
- USER: 'appuser',
20
- COMPANY: 'company',
21
- COMPANY_BRANCH: 'company_branch',
22
- SESSION: 'session',
23
- USER_PERMISSION: 'user_company_permission',
24
- SOCIAL_AUTH_CONFIG: 'social_auth_config'
25
- };
26
- export const AUTH_EVENT_MODULE = 'auth';
@@ -1,99 +0,0 @@
1
- // ==================== AUTH MODULE MESSAGE KEYS ====================
2
- export const AUTH_MESSAGES = {
3
- LOGIN_SUCCESS: 'auth.login.success',
4
- LOGIN_REQUIRES_SELECTION: 'auth.login.requires.selection',
5
- LOGIN_INVALID_CREDENTIALS: 'auth.login.invalid.credentials',
6
- LOGIN_EMAIL_NOT_VERIFIED: 'auth.login.email.not.verified',
7
- LOGIN_ACCOUNT_DEACTIVATED: 'auth.login.account.deactivated',
8
- LOGOUT_SUCCESS: 'auth.logout.success',
9
- REGISTER_SUCCESS: 'auth.register.success',
10
- REGISTER_FAILED: 'auth.register.failed',
11
- REFRESH_SUCCESS: 'auth.refresh.success',
12
- ME_SUCCESS: 'auth.me.success',
13
- SELECT_SUCCESS: 'auth.select.success',
14
- SWITCH_COMPANY_SUCCESS: 'auth.switch.company.success',
15
- REGISTER_EMAIL_EXISTS: 'auth.register.email.exists',
16
- REGISTER_ACCOUNT_DELETED: 'auth.register.account.deleted',
17
- REGISTER_COMPANY_ALREADY_ASSIGNED: 'auth.register.company.already.assigned',
18
- REGISTER_SIGNUP_DISABLED: 'auth.register.signup.disabled',
19
- PASSWORD_CHANGE_SUCCESS: 'auth.password.change.success',
20
- PASSWORD_CHANGE_INVALID_CURRENT: 'auth.password.change.invalid.current',
21
- PASSWORD_CHANGE_CURRENT_REQUIRED: 'auth.password.change.current.required',
22
- PASSWORD_RESET_EMAIL_SENT: 'auth.password.reset.email.sent',
23
- PASSWORD_RESET_SUCCESS: 'auth.password.reset.success',
24
- TOKEN_REQUIRED: 'auth.token.required',
25
- TOKEN_INVALID: 'auth.token.invalid',
26
- TOKEN_EXPIRED: 'auth.token.expired',
27
- TOKEN_REVOKED: 'auth.token.revoked',
28
- TOKEN_TOO_LARGE: 'auth.token.too.large',
29
- COMPANY_LIST_SUCCESS: 'auth.company.list.success',
30
- COMPANY_NO_ACCESS: 'auth.company.no.access',
31
- COMPANY_NOT_FOUND: 'auth.company.not.found',
32
- COMPANY_REQUIRED: 'auth.company.required',
33
- COMPANY_FEATURE_NOT_ENABLED: 'auth.company.feature.not.enabled',
34
- BRANCH_LIST_SUCCESS: 'auth.branch.list.success',
35
- BRANCH_NO_ACCESS: 'auth.branch.no.access',
36
- BRANCH_NOT_FOUND: 'auth.branch.not.found',
37
- EMAIL_NOT_CONFIGURED: 'auth.email.not.configured',
38
- EMAIL_SEND_FAILED: 'auth.email.send.failed',
39
- SESSION_INVALID: 'auth.session.invalid',
40
- SESSION_NOT_AVAILABLE: 'auth.session.not.available',
41
- PROFILE_ACCESS_DENIED: 'auth.profile.access.denied',
42
- PROFILE_SECTIONS_NOT_SUPPORTED: 'auth.profile.sections.not.supported',
43
- LOGOUT_SERVICE_UNAVAILABLE: 'auth.logout.service.unavailable',
44
- PASSWORD_RESET_TOKEN_EXPIRED: 'auth.password.reset.token.expired',
45
- PASSWORD_RESET_TOKEN_USED: 'auth.password.reset.token.used'
46
- };
47
- export const EMAIL_VERIFICATION_MESSAGES = {
48
- VERIFICATION_SENT: 'email.verification.sent',
49
- VERIFICATION_SUCCESS: 'email.verification.success',
50
- VERIFICATION_TOKEN_INVALID: 'email.verification.token.invalid',
51
- VERIFICATION_TOKEN_EXPIRED: 'email.verification.token.expired',
52
- VERIFICATION_NOT_ENABLED: 'email.verification.not.enabled',
53
- RESEND_SUCCESS: 'email.verification.resend.success'
54
- };
55
- export const SOCIAL_AUTH_MESSAGES = {
56
- PROVIDERS_SUCCESS: 'auth.social.providers.success',
57
- AUTHORIZE_SUCCESS: 'auth.social.authorize.success',
58
- LOGIN_SUCCESS: 'auth.social.login.success',
59
- PROVIDER_DISABLED: 'auth.social.provider.disabled',
60
- PROVIDER_NOT_CONFIGURED: 'auth.social.provider.not.configured',
61
- STATE_INVALID: 'auth.social.state.invalid',
62
- REDIRECT_URI_MISMATCH: 'auth.social.redirect.uri.mismatch',
63
- EXCHANGE_FAILED: 'auth.social.exchange.failed',
64
- PROFILE_INCOMPLETE: 'auth.social.profile.incomplete',
65
- ACCOUNT_NOT_LINKED: 'auth.social.account.not.linked',
66
- COMPANY_REQUIRED: 'auth.social.company.required',
67
- EMAIL_REQUIRED: 'auth.social.email.required',
68
- EMAIL_NOT_VERIFIED: 'auth.social.email.not.verified',
69
- ACCOUNT_DELETED: 'auth.social.account.deleted'
70
- };
71
- export const SOCIAL_AUTH_CONFIG_MESSAGES = {
72
- PROVIDER_ALREADY_CONFIGURED: 'social.config.provider.already.configured'
73
- };
74
- export const USER_MESSAGES = {
75
- NOT_FOUND: 'user.not.found',
76
- PROFILE_UPDATE_SUCCESS: 'user.profile.update.success',
77
- PROFILE_EXTRAS_SUCCESS: 'user.profile.extras.success',
78
- PROFILE_SECTIONS_SUCCESS: 'user.profile.sections.success',
79
- PROFILE_SECTION_DATA_SUCCESS: 'user.profile.section.data.success',
80
- PROFILE_SECTION_UPDATE_SUCCESS: 'user.profile.section.update.success',
81
- PROFILE_COMPLETION_SUCCESS: 'user.profile.completion.success',
82
- EMAIL_VERIFY_SUCCESS: 'user.email.verify.success',
83
- PHONE_VERIFY_SUCCESS: 'user.phone.verify.success',
84
- STATUS_UPDATE_SUCCESS: 'user.status.update.success',
85
- LOOKUP_SUCCESS: 'user.lookup.success'
86
- };
87
- export const COMPANY_MESSAGES = {
88
- READONLY_DELETE_FORBIDDEN: 'company.readonly.delete.forbidden'
89
- };
90
- export const BRANCH_MESSAGES = {
91
- READONLY_DELETE_FORBIDDEN: 'branch.readonly.delete.forbidden'
92
- };
93
- export const USER_PERMISSION_MESSAGES = {
94
- COMPANY_LIST_SUCCESS: 'user.permission.company.list.success',
95
- BRANCH_LIST_SUCCESS: 'user.permission.branch.list.success',
96
- BATCH_SUCCESS: 'user.permission.batch.success',
97
- ALREADY_ASSIGNED: 'user.permission.already.assigned',
98
- PERMISSION_NOT_FOUND: 'user.permission.not.found'
99
- };