@flusys/nestjs-auth 7.0.2 → 8.0.0-beta.1

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 (237) hide show
  1. package/README.md +68 -1
  2. package/config/auth.constants.d.ts +2 -0
  3. package/config/event-actions.d.ts +2 -0
  4. package/config/index.d.ts +3 -3
  5. package/config/message-keys.d.ts +23 -0
  6. package/controllers/authentication.controller.d.ts +5 -5
  7. package/controllers/branch.controller.d.ts +2 -2
  8. package/controllers/company-selection.controller.d.ts +4 -4
  9. package/controllers/company.controller.d.ts +2 -2
  10. package/controllers/email-verification.controller.d.ts +6 -6
  11. package/controllers/index.d.ts +9 -7
  12. package/controllers/social-auth-config.controller.d.ts +22 -0
  13. package/controllers/social-auth.controller.d.ts +11 -0
  14. package/controllers/user-permission.controller.d.ts +5 -4
  15. package/controllers/user.controller.d.ts +7 -7
  16. package/docs/auth-swagger.config.d.ts +2 -2
  17. package/docs/index.d.ts +1 -1
  18. package/dtos/index.d.ts +6 -5
  19. package/dtos/social-auth.dto.d.ts +42 -0
  20. package/entities/company-branch.entity.d.ts +1 -1
  21. package/entities/company.entity.d.ts +1 -1
  22. package/entities/index.d.ts +15 -13
  23. package/entities/pending-registration.entity.d.ts +1 -1
  24. package/entities/social-auth-config.entity.d.ts +9 -0
  25. package/entities/user-company-permission.entity.d.ts +4 -4
  26. package/enums/index.d.ts +2 -1
  27. package/enums/social-provider-type.enum.d.ts +6 -0
  28. package/fesm/118.js +1230 -0
  29. package/fesm/120.js +2197 -0
  30. package/fesm/24.js +4389 -0
  31. package/fesm/370.js +1163 -0
  32. package/fesm/446.js +877 -0
  33. package/fesm/chunks/117.js +69 -0
  34. package/fesm/config/index.js +244 -3
  35. package/fesm/controllers/index.js +27 -7
  36. package/fesm/docs/index.js +479 -1
  37. package/fesm/dtos/index.js +123 -5
  38. package/fesm/entities/index.js +68 -19
  39. package/fesm/enums/index.js +53 -1
  40. package/fesm/helpers/index.js +115 -2
  41. package/fesm/index.js +1437 -12
  42. package/fesm/interceptors/index.js +508 -2
  43. package/fesm/interfaces/index.js +75 -7
  44. package/fesm/modules/index.js +546 -1
  45. package/fesm/runtime.js +134 -0
  46. package/fesm/services/index.js +67 -9
  47. package/helpers/auth-response.mapper.d.ts +3 -3
  48. package/helpers/index.d.ts +2 -2
  49. package/helpers/token.helper.d.ts +2 -2
  50. package/index.d.ts +13 -12
  51. package/interceptors/clear-token.interceptor.d.ts +2 -2
  52. package/interceptors/index.d.ts +2 -2
  53. package/interceptors/set-token.interceptor.d.ts +2 -2
  54. package/interfaces/auth-module-options.interface.d.ts +3 -3
  55. package/interfaces/authentication.interface.d.ts +3 -3
  56. package/interfaces/company-branch.interface.d.ts +2 -2
  57. package/interfaces/company.interface.d.ts +1 -1
  58. package/interfaces/index.d.ts +8 -7
  59. package/interfaces/social-auth.interface.d.ts +43 -0
  60. package/interfaces/user-enricher.interface.d.ts +3 -3
  61. package/modules/auth.module.d.ts +4 -4
  62. package/modules/index.d.ts +1 -1
  63. package/package.json +32 -39
  64. package/providers/facebook.social-provider.d.ts +10 -0
  65. package/providers/google.social-provider.d.ts +10 -0
  66. package/providers/index.d.ts +1 -0
  67. package/providers/linkedin.social-provider.d.ts +10 -0
  68. package/providers/microsoft.social-provider.d.ts +10 -0
  69. package/providers/oauth-http.util.d.ts +10 -0
  70. package/providers/social-provider.registry.d.ts +3 -0
  71. package/services/auth-config.service.d.ts +4 -3
  72. package/services/auth-datasource.provider.d.ts +4 -3
  73. package/services/auth-email.service.d.ts +3 -3
  74. package/services/authentication.service.d.ts +15 -11
  75. package/services/branch.service.d.ts +7 -7
  76. package/services/company.service.d.ts +6 -6
  77. package/services/index.d.ts +11 -9
  78. package/services/social-auth-config.service.d.ts +21 -0
  79. package/services/social-auth.service.d.ts +24 -0
  80. package/services/user-permission.service.d.ts +8 -4
  81. package/services/user.service.d.ts +7 -7
  82. package/strategies/jwt.strategy.d.ts +4 -4
  83. package/validators/password.validator.d.ts +1 -1
  84. package/cjs/config/auth.constants.js +0 -41
  85. package/cjs/config/event-actions.js +0 -45
  86. package/cjs/config/index.js +0 -20
  87. package/cjs/config/message-keys.js +0 -108
  88. package/cjs/controllers/authentication.controller.js +0 -427
  89. package/cjs/controllers/authentication.controller.spec.js +0 -219
  90. package/cjs/controllers/branch.controller.js +0 -102
  91. package/cjs/controllers/branch.controller.spec.js +0 -134
  92. package/cjs/controllers/company-selection.controller.js +0 -198
  93. package/cjs/controllers/company-selection.controller.spec.js +0 -84
  94. package/cjs/controllers/company.controller.js +0 -102
  95. package/cjs/controllers/company.controller.spec.js +0 -131
  96. package/cjs/controllers/email-verification.controller.js +0 -224
  97. package/cjs/controllers/email-verification.controller.spec.js +0 -142
  98. package/cjs/controllers/index.js +0 -24
  99. package/cjs/controllers/user-permission.controller.js +0 -223
  100. package/cjs/controllers/user-permission.controller.spec.js +0 -138
  101. package/cjs/controllers/user.controller.js +0 -438
  102. package/cjs/controllers/user.controller.spec.js +0 -204
  103. package/cjs/docs/auth-swagger.config.js +0 -453
  104. package/cjs/docs/auth-swagger.config.spec.js +0 -130
  105. package/cjs/docs/index.js +0 -18
  106. package/cjs/dtos/authentication.dto.js +0 -755
  107. package/cjs/dtos/company-branch.dto.js +0 -294
  108. package/cjs/dtos/company.dto.js +0 -298
  109. package/cjs/dtos/index.js +0 -22
  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 -65
  116. package/cjs/entities/index.spec.js +0 -73
  117. package/cjs/entities/pending-registration.entity.js +0 -135
  118. package/cjs/entities/user-company-permission.entity.js +0 -153
  119. package/cjs/entities/user-token.entity.js +0 -120
  120. package/cjs/enums/index.js +0 -18
  121. package/cjs/enums/user-org-permission-type.enum.js +0 -15
  122. package/cjs/helpers/auth-response.mapper.js +0 -46
  123. package/cjs/helpers/auth-response.mapper.spec.js +0 -139
  124. package/cjs/helpers/index.js +0 -19
  125. package/cjs/helpers/token.helper.js +0 -58
  126. package/cjs/helpers/token.helper.spec.js +0 -86
  127. package/cjs/index.js +0 -29
  128. package/cjs/interceptors/clear-token.interceptor.js +0 -73
  129. package/cjs/interceptors/clear-token.interceptor.spec.js +0 -107
  130. package/cjs/interceptors/index.js +0 -19
  131. package/cjs/interceptors/set-token.interceptor.js +0 -107
  132. package/cjs/interceptors/set-token.interceptor.spec.js +0 -165
  133. package/cjs/interfaces/auth-email-provider.interface.js +0 -45
  134. package/cjs/interfaces/auth-module-options.interface.js +0 -4
  135. package/cjs/interfaces/authentication.interface.js +0 -4
  136. package/cjs/interfaces/company-branch.interface.js +0 -4
  137. package/cjs/interfaces/company.interface.js +0 -4
  138. package/cjs/interfaces/index.js +0 -24
  139. package/cjs/interfaces/user-enricher.interface.js +0 -4
  140. package/cjs/interfaces/user.interface.js +0 -4
  141. package/cjs/modules/auth.module.js +0 -202
  142. package/cjs/modules/auth.module.spec.js +0 -243
  143. package/cjs/modules/index.js +0 -18
  144. package/cjs/services/auth-config.service.js +0 -110
  145. package/cjs/services/auth-config.service.spec.js +0 -97
  146. package/cjs/services/auth-datasource.provider.js +0 -134
  147. package/cjs/services/auth-datasource.provider.spec.js +0 -161
  148. package/cjs/services/auth-email.service.js +0 -539
  149. package/cjs/services/auth-email.service.spec.js +0 -456
  150. package/cjs/services/authentication.service.js +0 -1173
  151. package/cjs/services/authentication.service.spec.js +0 -962
  152. package/cjs/services/branch.service.js +0 -153
  153. package/cjs/services/branch.service.spec.js +0 -169
  154. package/cjs/services/company-selection-session.service.js +0 -121
  155. package/cjs/services/company-selection-session.service.spec.js +0 -90
  156. package/cjs/services/company.service.js +0 -140
  157. package/cjs/services/company.service.spec.js +0 -127
  158. package/cjs/services/index.js +0 -26
  159. package/cjs/services/user-permission.service.js +0 -236
  160. package/cjs/services/user-permission.service.spec.js +0 -254
  161. package/cjs/services/user.service.js +0 -671
  162. package/cjs/services/user.service.spec.js +0 -718
  163. package/cjs/strategies/jwt.strategy.js +0 -140
  164. package/cjs/strategies/jwt.strategy.spec.js +0 -166
  165. package/cjs/validators/password.validator.js +0 -45
  166. package/cjs/validators/password.validator.spec.js +0 -161
  167. package/fesm/config/auth.constants.js +0 -11
  168. package/fesm/config/event-actions.js +0 -24
  169. package/fesm/config/message-keys.js +0 -78
  170. package/fesm/controllers/authentication.controller.js +0 -417
  171. package/fesm/controllers/authentication.controller.spec.js +0 -215
  172. package/fesm/controllers/branch.controller.js +0 -92
  173. package/fesm/controllers/branch.controller.spec.js +0 -130
  174. package/fesm/controllers/company-selection.controller.js +0 -188
  175. package/fesm/controllers/company-selection.controller.spec.js +0 -80
  176. package/fesm/controllers/company.controller.js +0 -92
  177. package/fesm/controllers/company.controller.spec.js +0 -127
  178. package/fesm/controllers/email-verification.controller.js +0 -214
  179. package/fesm/controllers/email-verification.controller.spec.js +0 -138
  180. package/fesm/controllers/user-permission.controller.js +0 -213
  181. package/fesm/controllers/user-permission.controller.spec.js +0 -134
  182. package/fesm/controllers/user.controller.js +0 -428
  183. package/fesm/controllers/user.controller.spec.js +0 -200
  184. package/fesm/docs/auth-swagger.config.js +0 -446
  185. package/fesm/docs/auth-swagger.config.spec.js +0 -126
  186. package/fesm/dtos/authentication.dto.js +0 -683
  187. package/fesm/dtos/company-branch.dto.js +0 -279
  188. package/fesm/dtos/company.dto.js +0 -283
  189. package/fesm/dtos/user-permission.dto.js +0 -339
  190. package/fesm/dtos/user.dto.js +0 -391
  191. package/fesm/entities/app-user.entity.js +0 -15
  192. package/fesm/entities/company-branch.entity.js +0 -153
  193. package/fesm/entities/company.entity.js +0 -136
  194. package/fesm/entities/index.spec.js +0 -69
  195. package/fesm/entities/pending-registration.entity.js +0 -125
  196. package/fesm/entities/user-company-permission.entity.js +0 -136
  197. package/fesm/entities/user-token.entity.js +0 -104
  198. package/fesm/enums/user-org-permission-type.enum.js +0 -5
  199. package/fesm/helpers/auth-response.mapper.js +0 -25
  200. package/fesm/helpers/auth-response.mapper.spec.js +0 -135
  201. package/fesm/helpers/token.helper.js +0 -40
  202. package/fesm/helpers/token.helper.spec.js +0 -82
  203. package/fesm/interceptors/clear-token.interceptor.js +0 -63
  204. package/fesm/interceptors/clear-token.interceptor.spec.js +0 -103
  205. package/fesm/interceptors/set-token.interceptor.js +0 -98
  206. package/fesm/interceptors/set-token.interceptor.spec.js +0 -161
  207. package/fesm/interfaces/auth-email-provider.interface.js +0 -38
  208. package/fesm/interfaces/auth-module-options.interface.js +0 -3
  209. package/fesm/interfaces/authentication.interface.js +0 -1
  210. package/fesm/interfaces/company-branch.interface.js +0 -1
  211. package/fesm/interfaces/company.interface.js +0 -1
  212. package/fesm/interfaces/user-enricher.interface.js +0 -4
  213. package/fesm/interfaces/user.interface.js +0 -1
  214. package/fesm/modules/auth.module.js +0 -192
  215. package/fesm/modules/auth.module.spec.js +0 -239
  216. package/fesm/services/auth-config.service.js +0 -100
  217. package/fesm/services/auth-config.service.spec.js +0 -93
  218. package/fesm/services/auth-datasource.provider.js +0 -83
  219. package/fesm/services/auth-datasource.provider.spec.js +0 -157
  220. package/fesm/services/auth-email.service.js +0 -488
  221. package/fesm/services/auth-email.service.spec.js +0 -411
  222. package/fesm/services/authentication.service.js +0 -1122
  223. package/fesm/services/authentication.service.spec.js +0 -917
  224. package/fesm/services/branch.service.js +0 -143
  225. package/fesm/services/branch.service.spec.js +0 -165
  226. package/fesm/services/company-selection-session.service.js +0 -111
  227. package/fesm/services/company-selection-session.service.spec.js +0 -86
  228. package/fesm/services/company.service.js +0 -130
  229. package/fesm/services/company.service.spec.js +0 -123
  230. package/fesm/services/user-permission.service.js +0 -226
  231. package/fesm/services/user-permission.service.spec.js +0 -250
  232. package/fesm/services/user.service.js +0 -620
  233. package/fesm/services/user.service.spec.js +0 -673
  234. package/fesm/strategies/jwt.strategy.js +0 -130
  235. package/fesm/strategies/jwt.strategy.spec.js +0 -162
  236. package/fesm/validators/password.validator.js +0 -48
  237. 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
- });