@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.
- package/README.md +68 -1
- package/config/auth.constants.d.ts +2 -0
- package/config/event-actions.d.ts +2 -0
- package/config/index.d.ts +3 -3
- package/config/message-keys.d.ts +23 -0
- package/controllers/authentication.controller.d.ts +5 -5
- package/controllers/branch.controller.d.ts +2 -2
- package/controllers/company-selection.controller.d.ts +4 -4
- package/controllers/company.controller.d.ts +2 -2
- package/controllers/email-verification.controller.d.ts +6 -6
- package/controllers/index.d.ts +9 -7
- package/controllers/social-auth-config.controller.d.ts +22 -0
- package/controllers/social-auth.controller.d.ts +11 -0
- package/controllers/user-permission.controller.d.ts +5 -4
- package/controllers/user.controller.d.ts +7 -7
- package/docs/auth-swagger.config.d.ts +2 -2
- package/docs/index.d.ts +1 -1
- package/dtos/index.d.ts +6 -5
- package/dtos/social-auth.dto.d.ts +42 -0
- package/entities/company-branch.entity.d.ts +1 -1
- package/entities/company.entity.d.ts +1 -1
- package/entities/index.d.ts +15 -13
- package/entities/pending-registration.entity.d.ts +1 -1
- package/entities/social-auth-config.entity.d.ts +9 -0
- package/entities/user-company-permission.entity.d.ts +4 -4
- package/enums/index.d.ts +2 -1
- package/enums/social-provider-type.enum.d.ts +6 -0
- package/fesm/118.js +1230 -0
- package/fesm/120.js +2197 -0
- package/fesm/24.js +4389 -0
- package/fesm/370.js +1163 -0
- package/fesm/446.js +877 -0
- package/fesm/chunks/117.js +69 -0
- package/fesm/config/index.js +244 -3
- package/fesm/controllers/index.js +27 -7
- package/fesm/docs/index.js +479 -1
- package/fesm/dtos/index.js +123 -5
- package/fesm/entities/index.js +68 -19
- package/fesm/enums/index.js +53 -1
- package/fesm/helpers/index.js +115 -2
- package/fesm/index.js +1437 -12
- package/fesm/interceptors/index.js +508 -2
- package/fesm/interfaces/index.js +75 -7
- package/fesm/modules/index.js +546 -1
- package/fesm/runtime.js +134 -0
- package/fesm/services/index.js +67 -9
- package/helpers/auth-response.mapper.d.ts +3 -3
- package/helpers/index.d.ts +2 -2
- package/helpers/token.helper.d.ts +2 -2
- package/index.d.ts +13 -12
- package/interceptors/clear-token.interceptor.d.ts +2 -2
- package/interceptors/index.d.ts +2 -2
- package/interceptors/set-token.interceptor.d.ts +2 -2
- package/interfaces/auth-module-options.interface.d.ts +3 -3
- package/interfaces/authentication.interface.d.ts +3 -3
- package/interfaces/company-branch.interface.d.ts +2 -2
- package/interfaces/company.interface.d.ts +1 -1
- package/interfaces/index.d.ts +8 -7
- package/interfaces/social-auth.interface.d.ts +43 -0
- package/interfaces/user-enricher.interface.d.ts +3 -3
- package/modules/auth.module.d.ts +4 -4
- package/modules/index.d.ts +1 -1
- package/package.json +32 -39
- package/providers/facebook.social-provider.d.ts +10 -0
- package/providers/google.social-provider.d.ts +10 -0
- package/providers/index.d.ts +1 -0
- package/providers/linkedin.social-provider.d.ts +10 -0
- package/providers/microsoft.social-provider.d.ts +10 -0
- package/providers/oauth-http.util.d.ts +10 -0
- package/providers/social-provider.registry.d.ts +3 -0
- package/services/auth-config.service.d.ts +4 -3
- package/services/auth-datasource.provider.d.ts +4 -3
- package/services/auth-email.service.d.ts +3 -3
- package/services/authentication.service.d.ts +15 -11
- package/services/branch.service.d.ts +7 -7
- package/services/company.service.d.ts +6 -6
- package/services/index.d.ts +11 -9
- package/services/social-auth-config.service.d.ts +21 -0
- package/services/social-auth.service.d.ts +24 -0
- package/services/user-permission.service.d.ts +8 -4
- package/services/user.service.d.ts +7 -7
- package/strategies/jwt.strategy.d.ts +4 -4
- package/validators/password.validator.d.ts +1 -1
- package/cjs/config/auth.constants.js +0 -41
- package/cjs/config/event-actions.js +0 -45
- package/cjs/config/index.js +0 -20
- package/cjs/config/message-keys.js +0 -108
- package/cjs/controllers/authentication.controller.js +0 -427
- package/cjs/controllers/authentication.controller.spec.js +0 -219
- package/cjs/controllers/branch.controller.js +0 -102
- package/cjs/controllers/branch.controller.spec.js +0 -134
- package/cjs/controllers/company-selection.controller.js +0 -198
- package/cjs/controllers/company-selection.controller.spec.js +0 -84
- package/cjs/controllers/company.controller.js +0 -102
- package/cjs/controllers/company.controller.spec.js +0 -131
- package/cjs/controllers/email-verification.controller.js +0 -224
- package/cjs/controllers/email-verification.controller.spec.js +0 -142
- package/cjs/controllers/index.js +0 -24
- package/cjs/controllers/user-permission.controller.js +0 -223
- package/cjs/controllers/user-permission.controller.spec.js +0 -138
- package/cjs/controllers/user.controller.js +0 -438
- package/cjs/controllers/user.controller.spec.js +0 -204
- package/cjs/docs/auth-swagger.config.js +0 -453
- package/cjs/docs/auth-swagger.config.spec.js +0 -130
- package/cjs/docs/index.js +0 -18
- package/cjs/dtos/authentication.dto.js +0 -755
- package/cjs/dtos/company-branch.dto.js +0 -294
- package/cjs/dtos/company.dto.js +0 -298
- package/cjs/dtos/index.js +0 -22
- package/cjs/dtos/user-permission.dto.js +0 -358
- package/cjs/dtos/user.dto.js +0 -413
- package/cjs/entities/app-user.entity.js +0 -25
- package/cjs/entities/company-branch.entity.js +0 -163
- package/cjs/entities/company.entity.js +0 -146
- package/cjs/entities/index.js +0 -65
- package/cjs/entities/index.spec.js +0 -73
- package/cjs/entities/pending-registration.entity.js +0 -135
- package/cjs/entities/user-company-permission.entity.js +0 -153
- package/cjs/entities/user-token.entity.js +0 -120
- package/cjs/enums/index.js +0 -18
- package/cjs/enums/user-org-permission-type.enum.js +0 -15
- package/cjs/helpers/auth-response.mapper.js +0 -46
- package/cjs/helpers/auth-response.mapper.spec.js +0 -139
- package/cjs/helpers/index.js +0 -19
- package/cjs/helpers/token.helper.js +0 -58
- package/cjs/helpers/token.helper.spec.js +0 -86
- package/cjs/index.js +0 -29
- package/cjs/interceptors/clear-token.interceptor.js +0 -73
- package/cjs/interceptors/clear-token.interceptor.spec.js +0 -107
- package/cjs/interceptors/index.js +0 -19
- package/cjs/interceptors/set-token.interceptor.js +0 -107
- package/cjs/interceptors/set-token.interceptor.spec.js +0 -165
- package/cjs/interfaces/auth-email-provider.interface.js +0 -45
- package/cjs/interfaces/auth-module-options.interface.js +0 -4
- package/cjs/interfaces/authentication.interface.js +0 -4
- package/cjs/interfaces/company-branch.interface.js +0 -4
- package/cjs/interfaces/company.interface.js +0 -4
- package/cjs/interfaces/index.js +0 -24
- package/cjs/interfaces/user-enricher.interface.js +0 -4
- package/cjs/interfaces/user.interface.js +0 -4
- package/cjs/modules/auth.module.js +0 -202
- package/cjs/modules/auth.module.spec.js +0 -243
- package/cjs/modules/index.js +0 -18
- package/cjs/services/auth-config.service.js +0 -110
- package/cjs/services/auth-config.service.spec.js +0 -97
- package/cjs/services/auth-datasource.provider.js +0 -134
- package/cjs/services/auth-datasource.provider.spec.js +0 -161
- package/cjs/services/auth-email.service.js +0 -539
- package/cjs/services/auth-email.service.spec.js +0 -456
- package/cjs/services/authentication.service.js +0 -1173
- package/cjs/services/authentication.service.spec.js +0 -962
- package/cjs/services/branch.service.js +0 -153
- package/cjs/services/branch.service.spec.js +0 -169
- package/cjs/services/company-selection-session.service.js +0 -121
- package/cjs/services/company-selection-session.service.spec.js +0 -90
- package/cjs/services/company.service.js +0 -140
- package/cjs/services/company.service.spec.js +0 -127
- package/cjs/services/index.js +0 -26
- package/cjs/services/user-permission.service.js +0 -236
- package/cjs/services/user-permission.service.spec.js +0 -254
- package/cjs/services/user.service.js +0 -671
- package/cjs/services/user.service.spec.js +0 -718
- package/cjs/strategies/jwt.strategy.js +0 -140
- package/cjs/strategies/jwt.strategy.spec.js +0 -166
- package/cjs/validators/password.validator.js +0 -45
- package/cjs/validators/password.validator.spec.js +0 -161
- package/fesm/config/auth.constants.js +0 -11
- package/fesm/config/event-actions.js +0 -24
- package/fesm/config/message-keys.js +0 -78
- package/fesm/controllers/authentication.controller.js +0 -417
- package/fesm/controllers/authentication.controller.spec.js +0 -215
- package/fesm/controllers/branch.controller.js +0 -92
- package/fesm/controllers/branch.controller.spec.js +0 -130
- package/fesm/controllers/company-selection.controller.js +0 -188
- package/fesm/controllers/company-selection.controller.spec.js +0 -80
- package/fesm/controllers/company.controller.js +0 -92
- package/fesm/controllers/company.controller.spec.js +0 -127
- package/fesm/controllers/email-verification.controller.js +0 -214
- package/fesm/controllers/email-verification.controller.spec.js +0 -138
- package/fesm/controllers/user-permission.controller.js +0 -213
- package/fesm/controllers/user-permission.controller.spec.js +0 -134
- package/fesm/controllers/user.controller.js +0 -428
- package/fesm/controllers/user.controller.spec.js +0 -200
- package/fesm/docs/auth-swagger.config.js +0 -446
- package/fesm/docs/auth-swagger.config.spec.js +0 -126
- package/fesm/dtos/authentication.dto.js +0 -683
- package/fesm/dtos/company-branch.dto.js +0 -279
- package/fesm/dtos/company.dto.js +0 -283
- package/fesm/dtos/user-permission.dto.js +0 -339
- package/fesm/dtos/user.dto.js +0 -391
- package/fesm/entities/app-user.entity.js +0 -15
- package/fesm/entities/company-branch.entity.js +0 -153
- package/fesm/entities/company.entity.js +0 -136
- package/fesm/entities/index.spec.js +0 -69
- package/fesm/entities/pending-registration.entity.js +0 -125
- package/fesm/entities/user-company-permission.entity.js +0 -136
- package/fesm/entities/user-token.entity.js +0 -104
- package/fesm/enums/user-org-permission-type.enum.js +0 -5
- package/fesm/helpers/auth-response.mapper.js +0 -25
- package/fesm/helpers/auth-response.mapper.spec.js +0 -135
- package/fesm/helpers/token.helper.js +0 -40
- package/fesm/helpers/token.helper.spec.js +0 -82
- package/fesm/interceptors/clear-token.interceptor.js +0 -63
- package/fesm/interceptors/clear-token.interceptor.spec.js +0 -103
- package/fesm/interceptors/set-token.interceptor.js +0 -98
- package/fesm/interceptors/set-token.interceptor.spec.js +0 -161
- package/fesm/interfaces/auth-email-provider.interface.js +0 -38
- package/fesm/interfaces/auth-module-options.interface.js +0 -3
- package/fesm/interfaces/authentication.interface.js +0 -1
- package/fesm/interfaces/company-branch.interface.js +0 -1
- package/fesm/interfaces/company.interface.js +0 -1
- package/fesm/interfaces/user-enricher.interface.js +0 -4
- package/fesm/interfaces/user.interface.js +0 -1
- package/fesm/modules/auth.module.js +0 -192
- package/fesm/modules/auth.module.spec.js +0 -239
- package/fesm/services/auth-config.service.js +0 -100
- package/fesm/services/auth-config.service.spec.js +0 -93
- package/fesm/services/auth-datasource.provider.js +0 -83
- package/fesm/services/auth-datasource.provider.spec.js +0 -157
- package/fesm/services/auth-email.service.js +0 -488
- package/fesm/services/auth-email.service.spec.js +0 -411
- package/fesm/services/authentication.service.js +0 -1122
- package/fesm/services/authentication.service.spec.js +0 -917
- package/fesm/services/branch.service.js +0 -143
- package/fesm/services/branch.service.spec.js +0 -165
- package/fesm/services/company-selection-session.service.js +0 -111
- package/fesm/services/company-selection-session.service.spec.js +0 -86
- package/fesm/services/company.service.js +0 -130
- package/fesm/services/company.service.spec.js +0 -123
- package/fesm/services/user-permission.service.js +0 -226
- package/fesm/services/user-permission.service.spec.js +0 -250
- package/fesm/services/user.service.js +0 -620
- package/fesm/services/user.service.spec.js +0 -673
- package/fesm/strategies/jwt.strategy.js +0 -130
- package/fesm/strategies/jwt.strategy.spec.js +0 -162
- package/fesm/validators/password.validator.js +0 -48
- 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
|
-
});
|