@flusys/nestjs-auth 1.0.0-rc → 1.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.
- package/README.md +530 -396
- package/cjs/controllers/authentication.controller.js +160 -28
- package/cjs/controllers/user-permission.controller.js +49 -136
- package/cjs/docs/auth-swagger.config.js +14 -7
- package/cjs/dtos/authentication.dto.js +0 -30
- package/cjs/dtos/index.js +0 -1
- package/cjs/entities/user-company-permission.entity.js +3 -3
- package/cjs/interceptors/clear-token.interceptor.js +8 -18
- package/cjs/interceptors/set-token.interceptor.js +8 -55
- package/cjs/interfaces/auth-email-provider.interface.js +1 -1
- package/cjs/modules/auth.module.js +63 -129
- package/cjs/services/auth-config.service.js +6 -29
- package/cjs/services/auth-datasource.provider.js +8 -20
- package/cjs/services/auth-email.service.js +72 -87
- package/cjs/services/authentication.service.js +211 -355
- package/cjs/services/branch.service.js +9 -5
- package/cjs/services/user-permission.service.js +43 -71
- package/cjs/services/user.service.js +18 -47
- package/cjs/strategies/jwt.strategy.js +44 -44
- package/controllers/authentication.controller.d.ts +5 -3
- package/controllers/user-permission.controller.d.ts +5 -4
- package/dtos/authentication.dto.d.ts +0 -4
- package/dtos/index.d.ts +0 -1
- package/entities/index.d.ts +3 -3
- package/entities/user-company-permission.entity.d.ts +1 -1
- package/fesm/controllers/authentication.controller.js +162 -30
- package/fesm/controllers/user-permission.controller.js +50 -137
- package/fesm/docs/auth-swagger.config.js +14 -7
- package/fesm/dtos/authentication.dto.js +0 -30
- package/fesm/dtos/index.js +0 -1
- package/fesm/entities/user-company-permission.entity.js +3 -3
- package/fesm/interceptors/clear-token.interceptor.js +8 -18
- package/fesm/interceptors/set-token.interceptor.js +8 -55
- package/fesm/interfaces/auth-email-provider.interface.js +1 -1
- package/fesm/modules/auth.module.js +63 -129
- package/fesm/services/auth-config.service.js +6 -29
- package/fesm/services/auth-datasource.provider.js +8 -20
- package/fesm/services/auth-email.service.js +72 -87
- package/fesm/services/authentication.service.js +211 -355
- package/fesm/services/branch.service.js +9 -5
- package/fesm/services/user-permission.service.js +44 -72
- package/fesm/services/user.service.js +18 -47
- package/fesm/strategies/jwt.strategy.js +44 -44
- package/interceptors/clear-token.interceptor.d.ts +0 -1
- package/interceptors/set-token.interceptor.d.ts +1 -2
- package/interfaces/authentication.interface.d.ts +0 -4
- package/modules/auth.module.d.ts +6 -2
- package/package.json +3 -3
- package/services/auth-config.service.d.ts +2 -7
- package/services/auth-datasource.provider.d.ts +2 -4
- package/services/auth-email.service.d.ts +7 -4
- package/services/authentication.service.d.ts +14 -8
- package/services/user-permission.service.d.ts +6 -8
- package/services/user.service.d.ts +1 -1
- package/strategies/jwt.strategy.d.ts +4 -1
- package/cjs/dtos/user-company-permission.dto.js +0 -110
- package/cjs/guards/auth-throttler.guard.js +0 -53
- package/dtos/user-company-permission.dto.d.ts +0 -10
- package/fesm/dtos/user-company-permission.dto.js +0 -98
- package/fesm/guards/auth-throttler.guard.js +0 -43
- package/guards/auth-throttler.guard.d.ts +0 -7
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { ILoggedUserInfo } from '@flusys/nestjs-shared/interfaces';
|
|
2
2
|
import { ModuleRef } from '@nestjs/core';
|
|
3
3
|
import { Request } from 'express';
|
|
4
|
-
import { AuthConfigService } from '../services/auth-config.service';
|
|
5
4
|
import { TokenPayload, TokenPayloadWithCompany } from '../interfaces/authentication.interface';
|
|
5
|
+
import { AuthConfigService } from '../services/auth-config.service';
|
|
6
6
|
declare const JwtStrategy_base: new (...args: any) => any;
|
|
7
7
|
export declare class JwtStrategy extends JwtStrategy_base {
|
|
8
8
|
private readonly authConfig;
|
|
9
9
|
private readonly moduleRef;
|
|
10
10
|
constructor(authConfig: AuthConfigService, moduleRef: ModuleRef);
|
|
11
11
|
validate(_req: Request, payload: TokenPayload | TokenPayloadWithCompany): Promise<ILoggedUserInfo>;
|
|
12
|
+
private resolveUserRepository;
|
|
13
|
+
private checkTokenRevocation;
|
|
14
|
+
private buildUserInfo;
|
|
12
15
|
}
|
|
13
16
|
export {};
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
get AssignBranchPermissionDto () {
|
|
13
|
-
return AssignBranchPermissionDto;
|
|
14
|
-
},
|
|
15
|
-
get AssignCompanyPermissionDto () {
|
|
16
|
-
return AssignCompanyPermissionDto;
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
const _swagger = require("@nestjs/swagger");
|
|
20
|
-
const _classvalidator = require("class-validator");
|
|
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
|
-
let AssignCompanyPermissionDto = class AssignCompanyPermissionDto {
|
|
44
|
-
constructor(){
|
|
45
|
-
_define_property(this, "userId", void 0);
|
|
46
|
-
_define_property(this, "companyId", void 0);
|
|
47
|
-
_define_property(this, "metadata", void 0);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
_ts_decorate([
|
|
51
|
-
(0, _swagger.ApiProperty)({
|
|
52
|
-
example: '550e8400-e29b-41d4-a716-446655440000',
|
|
53
|
-
description: 'User ID to assign company access to'
|
|
54
|
-
}),
|
|
55
|
-
(0, _classvalidator.IsUUID)(),
|
|
56
|
-
_ts_metadata("design:type", String)
|
|
57
|
-
], AssignCompanyPermissionDto.prototype, "userId", void 0);
|
|
58
|
-
_ts_decorate([
|
|
59
|
-
(0, _swagger.ApiProperty)({
|
|
60
|
-
example: '550e8400-e29b-41d4-a716-446655440001',
|
|
61
|
-
description: 'Company ID to grant access to'
|
|
62
|
-
}),
|
|
63
|
-
(0, _classvalidator.IsUUID)(),
|
|
64
|
-
_ts_metadata("design:type", String)
|
|
65
|
-
], AssignCompanyPermissionDto.prototype, "companyId", void 0);
|
|
66
|
-
_ts_decorate([
|
|
67
|
-
(0, _swagger.ApiPropertyOptional)({
|
|
68
|
-
example: {
|
|
69
|
-
role: 'member'
|
|
70
|
-
},
|
|
71
|
-
description: 'Additional permission metadata'
|
|
72
|
-
}),
|
|
73
|
-
(0, _classvalidator.IsOptional)(),
|
|
74
|
-
(0, _classvalidator.IsObject)(),
|
|
75
|
-
_ts_metadata("design:type", typeof Record === "undefined" ? Object : Record)
|
|
76
|
-
], AssignCompanyPermissionDto.prototype, "metadata", void 0);
|
|
77
|
-
let AssignBranchPermissionDto = class AssignBranchPermissionDto {
|
|
78
|
-
constructor(){
|
|
79
|
-
_define_property(this, "userId", void 0);
|
|
80
|
-
_define_property(this, "branchId", void 0);
|
|
81
|
-
_define_property(this, "metadata", void 0);
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
_ts_decorate([
|
|
85
|
-
(0, _swagger.ApiProperty)({
|
|
86
|
-
example: '550e8400-e29b-41d4-a716-446655440000',
|
|
87
|
-
description: 'User ID to assign branch access to'
|
|
88
|
-
}),
|
|
89
|
-
(0, _classvalidator.IsUUID)(),
|
|
90
|
-
_ts_metadata("design:type", String)
|
|
91
|
-
], AssignBranchPermissionDto.prototype, "userId", void 0);
|
|
92
|
-
_ts_decorate([
|
|
93
|
-
(0, _swagger.ApiProperty)({
|
|
94
|
-
example: '550e8400-e29b-41d4-a716-446655440002',
|
|
95
|
-
description: 'Branch ID to grant access to'
|
|
96
|
-
}),
|
|
97
|
-
(0, _classvalidator.IsUUID)(),
|
|
98
|
-
_ts_metadata("design:type", String)
|
|
99
|
-
], AssignBranchPermissionDto.prototype, "branchId", void 0);
|
|
100
|
-
_ts_decorate([
|
|
101
|
-
(0, _swagger.ApiPropertyOptional)({
|
|
102
|
-
example: {
|
|
103
|
-
role: 'staff'
|
|
104
|
-
},
|
|
105
|
-
description: 'Additional permission metadata'
|
|
106
|
-
}),
|
|
107
|
-
(0, _classvalidator.IsOptional)(),
|
|
108
|
-
(0, _classvalidator.IsObject)(),
|
|
109
|
-
_ts_metadata("design:type", typeof Record === "undefined" ? Object : Record)
|
|
110
|
-
], AssignBranchPermissionDto.prototype, "metadata", void 0);
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "AuthThrottlerGuard", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return AuthThrottlerGuard;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _common = require("@nestjs/common");
|
|
12
|
-
const _throttler = require("@nestjs/throttler");
|
|
13
|
-
function _ts_decorate(decorators, target, key, desc) {
|
|
14
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
15
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
16
|
-
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;
|
|
17
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
18
|
-
}
|
|
19
|
-
let AuthThrottlerGuard = class AuthThrottlerGuard extends _throttler.ThrottlerGuard {
|
|
20
|
-
/**
|
|
21
|
-
* Generate tracker key combining IP and email for login attempts.
|
|
22
|
-
* This prevents attackers from brute-forcing multiple accounts from the same IP.
|
|
23
|
-
*/ async getTracker(req) {
|
|
24
|
-
const ip = req.ip || req.headers?.['x-forwarded-for'] || 'unknown';
|
|
25
|
-
const body = req.body;
|
|
26
|
-
const email = body?.email || '';
|
|
27
|
-
return `auth:${ip}:${email}`;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Custom error message for rate limit exceeded.
|
|
31
|
-
*/ throwThrottlingException() {
|
|
32
|
-
throw new _throttler.ThrottlerException('Too many authentication attempts. Please try again later.');
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Skip rate limiting for authenticated users on certain endpoints.
|
|
36
|
-
*/ async shouldSkip(context) {
|
|
37
|
-
const request = context.switchToHttp().getRequest();
|
|
38
|
-
const path = request.path || request.url || '';
|
|
39
|
-
// Remove query string for comparison
|
|
40
|
-
const pathOnly = path.split('?')[0];
|
|
41
|
-
// Skip rate limiting for authenticated endpoints (they have their own protection)
|
|
42
|
-
// Use exact match or prefix match to prevent bypass via path manipulation
|
|
43
|
-
const authenticatedEndpoints = [
|
|
44
|
-
'/auth/me',
|
|
45
|
-
'/auth/logout',
|
|
46
|
-
'/auth/change-password'
|
|
47
|
-
];
|
|
48
|
-
return authenticatedEndpoints.some((endpoint)=>pathOnly === endpoint || pathOnly.startsWith(`${endpoint}/`));
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
AuthThrottlerGuard = _ts_decorate([
|
|
52
|
-
(0, _common.Injectable)()
|
|
53
|
-
], AuthThrottlerGuard);
|
|
@@ -1,98 +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 { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
|
24
|
-
import { IsObject, IsOptional, IsUUID } from 'class-validator';
|
|
25
|
-
/**
|
|
26
|
-
* Assign Company Permission DTO
|
|
27
|
-
* Used internally by UserPermissionService
|
|
28
|
-
*/ export class AssignCompanyPermissionDto {
|
|
29
|
-
constructor(){
|
|
30
|
-
_define_property(this, "userId", void 0);
|
|
31
|
-
_define_property(this, "companyId", void 0);
|
|
32
|
-
_define_property(this, "metadata", void 0);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
_ts_decorate([
|
|
36
|
-
ApiProperty({
|
|
37
|
-
example: '550e8400-e29b-41d4-a716-446655440000',
|
|
38
|
-
description: 'User ID to assign company access to'
|
|
39
|
-
}),
|
|
40
|
-
IsUUID(),
|
|
41
|
-
_ts_metadata("design:type", String)
|
|
42
|
-
], AssignCompanyPermissionDto.prototype, "userId", void 0);
|
|
43
|
-
_ts_decorate([
|
|
44
|
-
ApiProperty({
|
|
45
|
-
example: '550e8400-e29b-41d4-a716-446655440001',
|
|
46
|
-
description: 'Company ID to grant access to'
|
|
47
|
-
}),
|
|
48
|
-
IsUUID(),
|
|
49
|
-
_ts_metadata("design:type", String)
|
|
50
|
-
], AssignCompanyPermissionDto.prototype, "companyId", void 0);
|
|
51
|
-
_ts_decorate([
|
|
52
|
-
ApiPropertyOptional({
|
|
53
|
-
example: {
|
|
54
|
-
role: 'member'
|
|
55
|
-
},
|
|
56
|
-
description: 'Additional permission metadata'
|
|
57
|
-
}),
|
|
58
|
-
IsOptional(),
|
|
59
|
-
IsObject(),
|
|
60
|
-
_ts_metadata("design:type", typeof Record === "undefined" ? Object : Record)
|
|
61
|
-
], AssignCompanyPermissionDto.prototype, "metadata", void 0);
|
|
62
|
-
/**
|
|
63
|
-
* Assign Branch Permission DTO
|
|
64
|
-
* Used internally by UserPermissionService
|
|
65
|
-
*/ export class AssignBranchPermissionDto {
|
|
66
|
-
constructor(){
|
|
67
|
-
_define_property(this, "userId", void 0);
|
|
68
|
-
_define_property(this, "branchId", void 0);
|
|
69
|
-
_define_property(this, "metadata", void 0);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
_ts_decorate([
|
|
73
|
-
ApiProperty({
|
|
74
|
-
example: '550e8400-e29b-41d4-a716-446655440000',
|
|
75
|
-
description: 'User ID to assign branch access to'
|
|
76
|
-
}),
|
|
77
|
-
IsUUID(),
|
|
78
|
-
_ts_metadata("design:type", String)
|
|
79
|
-
], AssignBranchPermissionDto.prototype, "userId", void 0);
|
|
80
|
-
_ts_decorate([
|
|
81
|
-
ApiProperty({
|
|
82
|
-
example: '550e8400-e29b-41d4-a716-446655440002',
|
|
83
|
-
description: 'Branch ID to grant access to'
|
|
84
|
-
}),
|
|
85
|
-
IsUUID(),
|
|
86
|
-
_ts_metadata("design:type", String)
|
|
87
|
-
], AssignBranchPermissionDto.prototype, "branchId", void 0);
|
|
88
|
-
_ts_decorate([
|
|
89
|
-
ApiPropertyOptional({
|
|
90
|
-
example: {
|
|
91
|
-
role: 'staff'
|
|
92
|
-
},
|
|
93
|
-
description: 'Additional permission metadata'
|
|
94
|
-
}),
|
|
95
|
-
IsOptional(),
|
|
96
|
-
IsObject(),
|
|
97
|
-
_ts_metadata("design:type", typeof Record === "undefined" ? Object : Record)
|
|
98
|
-
], AssignBranchPermissionDto.prototype, "metadata", void 0);
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
function _ts_decorate(decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
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;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
}
|
|
7
|
-
import { Injectable } from '@nestjs/common';
|
|
8
|
-
import { ThrottlerException, ThrottlerGuard } from '@nestjs/throttler';
|
|
9
|
-
export class AuthThrottlerGuard extends ThrottlerGuard {
|
|
10
|
-
/**
|
|
11
|
-
* Generate tracker key combining IP and email for login attempts.
|
|
12
|
-
* This prevents attackers from brute-forcing multiple accounts from the same IP.
|
|
13
|
-
*/ async getTracker(req) {
|
|
14
|
-
const ip = req.ip || req.headers?.['x-forwarded-for'] || 'unknown';
|
|
15
|
-
const body = req.body;
|
|
16
|
-
const email = body?.email || '';
|
|
17
|
-
return `auth:${ip}:${email}`;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Custom error message for rate limit exceeded.
|
|
21
|
-
*/ throwThrottlingException() {
|
|
22
|
-
throw new ThrottlerException('Too many authentication attempts. Please try again later.');
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Skip rate limiting for authenticated users on certain endpoints.
|
|
26
|
-
*/ async shouldSkip(context) {
|
|
27
|
-
const request = context.switchToHttp().getRequest();
|
|
28
|
-
const path = request.path || request.url || '';
|
|
29
|
-
// Remove query string for comparison
|
|
30
|
-
const pathOnly = path.split('?')[0];
|
|
31
|
-
// Skip rate limiting for authenticated endpoints (they have their own protection)
|
|
32
|
-
// Use exact match or prefix match to prevent bypass via path manipulation
|
|
33
|
-
const authenticatedEndpoints = [
|
|
34
|
-
'/auth/me',
|
|
35
|
-
'/auth/logout',
|
|
36
|
-
'/auth/change-password'
|
|
37
|
-
];
|
|
38
|
-
return authenticatedEndpoints.some((endpoint)=>pathOnly === endpoint || pathOnly.startsWith(`${endpoint}/`));
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
AuthThrottlerGuard = _ts_decorate([
|
|
42
|
-
Injectable()
|
|
43
|
-
], AuthThrottlerGuard);
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { ExecutionContext } from '@nestjs/common';
|
|
2
|
-
import { ThrottlerGuard } from '@nestjs/throttler';
|
|
3
|
-
export declare class AuthThrottlerGuard extends ThrottlerGuard {
|
|
4
|
-
protected getTracker(req: Record<string, unknown>): Promise<string>;
|
|
5
|
-
protected throwThrottlingException(): Promise<void>;
|
|
6
|
-
protected shouldSkip(context: ExecutionContext): Promise<boolean>;
|
|
7
|
-
}
|