ecrs-auth-core 1.0.64 → 1.0.66

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.
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.EmployeeWorkProfileEntity = exports.WeekOffBasedOn = exports.WeekOffType = exports.EmployeeStatus = exports.EmployeeType = void 0;
13
+ const typeorm_1 = require("typeorm");
14
+ var EmployeeType;
15
+ (function (EmployeeType) {
16
+ EmployeeType["PERMANENT"] = "PERMANENT";
17
+ EmployeeType["CONTRACT"] = "CONTRACT";
18
+ EmployeeType["INTERN"] = "INTERN";
19
+ EmployeeType["CONSULTANT"] = "CONSULTANT";
20
+ })(EmployeeType || (exports.EmployeeType = EmployeeType = {}));
21
+ var EmployeeStatus;
22
+ (function (EmployeeStatus) {
23
+ EmployeeStatus["ACTIVE"] = "ACTIVE";
24
+ EmployeeStatus["INACTIVE"] = "INACTIVE";
25
+ EmployeeStatus["ON_LEAVE"] = "ON_LEAVE";
26
+ })(EmployeeStatus || (exports.EmployeeStatus = EmployeeStatus = {}));
27
+ // export enum WorkngShift {
28
+ // DAY = 'DAY',
29
+ // NIGHT = 'NIGHT',
30
+ // }
31
+ var WeekOffType;
32
+ (function (WeekOffType) {
33
+ WeekOffType["FIXED"] = "FIXED";
34
+ WeekOffType["ROTATIONAL"] = "ROTATIONAL";
35
+ WeekOffType["FLEXIBLE"] = "FLEXIBLE";
36
+ })(WeekOffType || (exports.WeekOffType = WeekOffType = {}));
37
+ var WeekOffBasedOn;
38
+ (function (WeekOffBasedOn) {
39
+ WeekOffBasedOn["WORK_LOCATION"] = "WORK_LOCATION";
40
+ WeekOffBasedOn["SHIFT"] = "SHIFT";
41
+ })(WeekOffBasedOn || (exports.WeekOffBasedOn = WeekOffBasedOn = {}));
42
+ let EmployeeWorkProfileEntity = class EmployeeWorkProfileEntity {
43
+ };
44
+ exports.EmployeeWorkProfileEntity = EmployeeWorkProfileEntity;
45
+ __decorate([
46
+ (0, typeorm_1.PrimaryGeneratedColumn)(),
47
+ __metadata("design:type", Number)
48
+ ], EmployeeWorkProfileEntity.prototype, "id", void 0);
49
+ __decorate([
50
+ (0, typeorm_1.Column)({ name: 'employee_id', type: 'int' }),
51
+ __metadata("design:type", Number)
52
+ ], EmployeeWorkProfileEntity.prototype, "employee_id", void 0);
53
+ __decorate([
54
+ (0, typeorm_1.Column)({ name: 'department_id', type: 'int' }),
55
+ __metadata("design:type", Number)
56
+ ], EmployeeWorkProfileEntity.prototype, "department_id", void 0);
57
+ __decorate([
58
+ (0, typeorm_1.Column)({ name: 'designation_id', type: 'int' }),
59
+ __metadata("design:type", Number)
60
+ ], EmployeeWorkProfileEntity.prototype, "designation_id", void 0);
61
+ __decorate([
62
+ (0, typeorm_1.Column)({ type: 'enum', enum: EmployeeType, nullable: false }),
63
+ __metadata("design:type", String)
64
+ ], EmployeeWorkProfileEntity.prototype, "employee_type", void 0);
65
+ __decorate([
66
+ (0, typeorm_1.Column)({
67
+ type: 'enum',
68
+ enum: EmployeeStatus,
69
+ default: EmployeeStatus.ACTIVE,
70
+ nullable: true,
71
+ }),
72
+ __metadata("design:type", String)
73
+ ], EmployeeWorkProfileEntity.prototype, "employee_status", void 0);
74
+ __decorate([
75
+ (0, typeorm_1.Column)({ name: 'shift_id', type: 'int', nullable: true }),
76
+ __metadata("design:type", Number)
77
+ ], EmployeeWorkProfileEntity.prototype, "shift_id", void 0);
78
+ __decorate([
79
+ (0, typeorm_1.Column)({ name: 'role_id', type: 'int' }),
80
+ __metadata("design:type", Number)
81
+ ], EmployeeWorkProfileEntity.prototype, "role_id", void 0);
82
+ __decorate([
83
+ (0, typeorm_1.Column)({ name: 'agency', type: 'varchar', nullable: true }),
84
+ __metadata("design:type", String)
85
+ ], EmployeeWorkProfileEntity.prototype, "agency", void 0);
86
+ __decorate([
87
+ (0, typeorm_1.Column)({ name: 'reporting_manager_id', type: 'int', nullable: true }),
88
+ __metadata("design:type", Object)
89
+ ], EmployeeWorkProfileEntity.prototype, "reporting_manager_id", void 0);
90
+ __decorate([
91
+ (0, typeorm_1.Column)({ name: 'l2_manager_id', type: 'int', nullable: true }),
92
+ __metadata("design:type", Object)
93
+ ], EmployeeWorkProfileEntity.prototype, "l2_manager_id", void 0);
94
+ __decorate([
95
+ (0, typeorm_1.Column)({ type: 'date', nullable: false }),
96
+ __metadata("design:type", Date)
97
+ ], EmployeeWorkProfileEntity.prototype, "joining_date", void 0);
98
+ __decorate([
99
+ (0, typeorm_1.Column)({ type: 'date', nullable: true }),
100
+ __metadata("design:type", Date)
101
+ ], EmployeeWorkProfileEntity.prototype, "resignation_date", void 0);
102
+ __decorate([
103
+ (0, typeorm_1.Column)({ name: 'branch_id', type: 'int', nullable: true }),
104
+ __metadata("design:type", Number)
105
+ ], EmployeeWorkProfileEntity.prototype, "branch_id", void 0);
106
+ __decorate([
107
+ (0, typeorm_1.Column)({ name: 'dispatch_center_id', type: 'int', nullable: true }),
108
+ __metadata("design:type", Number)
109
+ ], EmployeeWorkProfileEntity.prototype, "dispatch_center_id", void 0);
110
+ __decorate([
111
+ (0, typeorm_1.Column)({ name: 'cost_center_id', type: 'int' }),
112
+ __metadata("design:type", Number)
113
+ ], EmployeeWorkProfileEntity.prototype, "cost_center_id", void 0);
114
+ __decorate([
115
+ (0, typeorm_1.Column)({
116
+ type: 'enum',
117
+ enum: WeekOffType,
118
+ nullable: false,
119
+ }),
120
+ __metadata("design:type", String)
121
+ ], EmployeeWorkProfileEntity.prototype, "week_off_type", void 0);
122
+ __decorate([
123
+ (0, typeorm_1.Column)({
124
+ type: 'enum',
125
+ enum: WeekOffBasedOn,
126
+ nullable: false,
127
+ }),
128
+ __metadata("design:type", String)
129
+ ], EmployeeWorkProfileEntity.prototype, "week_off_based_on", void 0);
130
+ __decorate([
131
+ (0, typeorm_1.Column)({ length: 50, nullable: true }),
132
+ __metadata("design:type", String)
133
+ ], EmployeeWorkProfileEntity.prototype, "bioMetricCode", void 0);
134
+ __decorate([
135
+ (0, typeorm_1.Column)({ length: 255, nullable: true }),
136
+ __metadata("design:type", String)
137
+ ], EmployeeWorkProfileEntity.prototype, "linkedInProfile", void 0);
138
+ __decorate([
139
+ (0, typeorm_1.CreateDateColumn)({ type: 'timestamp' }),
140
+ __metadata("design:type", Date)
141
+ ], EmployeeWorkProfileEntity.prototype, "created_at", void 0);
142
+ __decorate([
143
+ (0, typeorm_1.Column)({ type: 'int', nullable: true }),
144
+ __metadata("design:type", Number)
145
+ ], EmployeeWorkProfileEntity.prototype, "created_by", void 0);
146
+ __decorate([
147
+ (0, typeorm_1.UpdateDateColumn)({ type: 'timestamp' }),
148
+ __metadata("design:type", Date)
149
+ ], EmployeeWorkProfileEntity.prototype, "updated_at", void 0);
150
+ __decorate([
151
+ (0, typeorm_1.Column)({ type: 'int', nullable: true }),
152
+ __metadata("design:type", Number)
153
+ ], EmployeeWorkProfileEntity.prototype, "updated_by", void 0);
154
+ __decorate([
155
+ (0, typeorm_1.Column)({ type: 'int', nullable: true }),
156
+ __metadata("design:type", Number)
157
+ ], EmployeeWorkProfileEntity.prototype, "deleted_by", void 0);
158
+ __decorate([
159
+ (0, typeorm_1.DeleteDateColumn)({ type: 'timestamp', nullable: true }),
160
+ __metadata("design:type", Date)
161
+ ], EmployeeWorkProfileEntity.prototype, "deleted_at", void 0);
162
+ exports.EmployeeWorkProfileEntity = EmployeeWorkProfileEntity = __decorate([
163
+ (0, typeorm_1.Entity)('tbl_hr_employee_work_profile'),
164
+ (0, typeorm_1.Unique)(['employee_id'])
165
+ ], EmployeeWorkProfileEntity);
@@ -1,15 +1,15 @@
1
- import { CanActivate, ExecutionContext } from '@nestjs/common';
2
- import { Reflector } from '@nestjs/core';
3
- import { Repository } from 'typeorm';
4
- import { ApiKeyEntity } from '../entities/api-key.entity';
5
- export declare class ApiKeyGuard implements CanActivate {
6
- private readonly reflector;
7
- private apiKeyRepo;
8
- private rateLimitMap;
9
- constructor(reflector: Reflector, apiKeyRepository?: Repository<ApiKeyEntity>);
10
- canActivate(context: ExecutionContext): Promise<boolean>;
11
- private extractApiKey;
12
- private validateApiKey;
13
- private getClientIp;
14
- private checkRateLimit;
15
- }
1
+ import { CanActivate, ExecutionContext } from '@nestjs/common';
2
+ import { Reflector } from '@nestjs/core';
3
+ import { Repository } from 'typeorm';
4
+ import { ApiKeyEntity } from '../entities/api-key.entity';
5
+ export declare class ApiKeyGuard implements CanActivate {
6
+ private readonly reflector;
7
+ private apiKeyRepo;
8
+ private rateLimitMap;
9
+ constructor(reflector: Reflector, apiKeyRepository?: Repository<ApiKeyEntity>);
10
+ canActivate(context: ExecutionContext): Promise<boolean>;
11
+ private extractApiKey;
12
+ private validateApiKey;
13
+ private getClientIp;
14
+ private checkRateLimit;
15
+ }
@@ -1,182 +1,182 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- 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;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- var __param = (this && this.__param) || function (paramIndex, decorator) {
12
- return function (target, key) { decorator(target, key, paramIndex); }
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.ApiKeyGuard = void 0;
16
- const common_1 = require("@nestjs/common");
17
- const core_1 = require("@nestjs/core");
18
- const typeorm_1 = require("typeorm");
19
- let ApiKeyGuard = class ApiKeyGuard {
20
- constructor(reflector, apiKeyRepository) {
21
- this.reflector = reflector;
22
- this.rateLimitMap = new Map();
23
- if (apiKeyRepository) {
24
- this.apiKeyRepo = apiKeyRepository;
25
- }
26
- }
27
- async canActivate(context) {
28
- const options = this.reflector.get('api_key_options', context.getHandler());
29
- // If no @ApiKey decorator is present, skip this guard
30
- if (!options) {
31
- return true;
32
- }
33
- const request = context.switchToHttp().getRequest();
34
- const apiKey = this.extractApiKey(request);
35
- if (!apiKey) {
36
- throw new common_1.UnauthorizedException('API key is missing');
37
- }
38
- // Validate API key
39
- const validation = await this.validateApiKey(apiKey, request, options);
40
- if (!validation.valid) {
41
- throw new common_1.UnauthorizedException(validation.message || 'Invalid API key');
42
- }
43
- // Attach API key info to request for downstream use
44
- request.apiKey = validation.apiKey;
45
- request.apiKeyClient = {
46
- id: validation.apiKey?.id,
47
- clientName: validation.apiKey?.clientName,
48
- scope: validation.apiKey?.scope,
49
- };
50
- return true;
51
- }
52
- extractApiKey(request) {
53
- // Check 'X-API-Key' header
54
- const headerKey = request.headers['x-api-key'];
55
- if (headerKey) {
56
- return headerKey;
57
- }
58
- // Check 'api_key' query parameter
59
- if (request.query?.api_key) {
60
- return request.query.api_key;
61
- }
62
- // Check Authorization header (format: 'ApiKey <key>')
63
- const authHeader = request.headers.authorization;
64
- if (authHeader && authHeader.startsWith('ApiKey ')) {
65
- return authHeader.substring(7);
66
- }
67
- return null;
68
- }
69
- async validateApiKey(apiKey, request, options) {
70
- if (!this.apiKeyRepo) {
71
- return { valid: false, message: 'API key validation not configured' };
72
- }
73
- const record = await this.apiKeyRepo.findOne({
74
- where: { key: apiKey, isActive: true },
75
- });
76
- if (!record) {
77
- return { valid: false, message: 'API key not found or inactive' };
78
- }
79
- // Check scope
80
- const requiredScope = options.scope || 'common';
81
- if (record.scope !== 'common' && record.scope !== requiredScope) {
82
- return {
83
- valid: false,
84
- message: `API key scope '${record.scope}' does not match required scope '${requiredScope}'`,
85
- };
86
- }
87
- // Check IP whitelist
88
- if (record.allowedIps) {
89
- const clientIp = this.getClientIp(request);
90
- const allowedIps = record.allowedIps.split(',').map((ip) => ip.trim());
91
- if (!allowedIps.includes(clientIp)) {
92
- return {
93
- valid: false,
94
- message: `Client IP '${clientIp}' is not whitelisted`,
95
- };
96
- }
97
- }
98
- // Check endpoint restriction
99
- if (record.allowedEndpoints) {
100
- const requestEndpoint = `${request.method} ${request.path}`;
101
- const allowedEndpoints = record.allowedEndpoints
102
- .split(',')
103
- .map((ep) => ep.trim());
104
- const isAllowed = allowedEndpoints.some((ep) => {
105
- // Support wildcard matching (e.g., 'GET /spot/*')
106
- if (ep.includes('*')) {
107
- const pattern = ep.replace(/\*/g, '.*');
108
- return new RegExp(`^${pattern}$`).test(requestEndpoint);
109
- }
110
- return ep === requestEndpoint;
111
- });
112
- if (!isAllowed) {
113
- return {
114
- valid: false,
115
- message: `Endpoint '${requestEndpoint}' is not allowed for this API key`,
116
- };
117
- }
118
- }
119
- // Check rate limit
120
- if (record.rateLimit) {
121
- const isRateLimited = this.checkRateLimit(apiKey, record.rateLimit);
122
- if (isRateLimited) {
123
- return {
124
- valid: false,
125
- message: `Rate limit exceeded (${record.rateLimit} requests per minute)`,
126
- };
127
- }
128
- }
129
- // Update last used timestamp
130
- if (this.apiKeyRepo) {
131
- await this.apiKeyRepo.update(record.id, {
132
- lastUsedAt: Date.now(),
133
- });
134
- }
135
- return { valid: true, apiKey: record };
136
- }
137
- getClientIp(request) {
138
- // Support common proxy headers
139
- const xForwardedFor = request.headers['x-forwarded-for'];
140
- if (xForwardedFor) {
141
- return xForwardedFor.split(',')[0].trim();
142
- }
143
- const xRealIp = request.headers['x-real-ip'];
144
- if (xRealIp) {
145
- return xRealIp;
146
- }
147
- return request.ip || request.connection.remoteAddress || 'unknown';
148
- }
149
- checkRateLimit(apiKey, rateLimit) {
150
- const now = Date.now();
151
- const limitData = this.rateLimitMap.get(apiKey);
152
- if (!limitData) {
153
- // First request, initialize
154
- this.rateLimitMap.set(apiKey, {
155
- count: 1,
156
- resetTime: now + 60000, // 1 minute
157
- });
158
- return false;
159
- }
160
- if (now > limitData.resetTime) {
161
- // Reset window expired
162
- this.rateLimitMap.set(apiKey, {
163
- count: 1,
164
- resetTime: now + 60000,
165
- });
166
- return false;
167
- }
168
- // Still within window
169
- if (limitData.count >= rateLimit) {
170
- return true;
171
- }
172
- limitData.count++;
173
- return false;
174
- }
175
- };
176
- exports.ApiKeyGuard = ApiKeyGuard;
177
- exports.ApiKeyGuard = ApiKeyGuard = __decorate([
178
- (0, common_1.Injectable)(),
179
- __param(1, (0, common_1.Inject)('API_KEY_REPOSITORY')),
180
- __metadata("design:paramtypes", [core_1.Reflector,
181
- typeorm_1.Repository])
182
- ], ApiKeyGuard);
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.ApiKeyGuard = void 0;
16
+ const common_1 = require("@nestjs/common");
17
+ const core_1 = require("@nestjs/core");
18
+ const typeorm_1 = require("typeorm");
19
+ let ApiKeyGuard = class ApiKeyGuard {
20
+ constructor(reflector, apiKeyRepository) {
21
+ this.reflector = reflector;
22
+ this.rateLimitMap = new Map();
23
+ if (apiKeyRepository) {
24
+ this.apiKeyRepo = apiKeyRepository;
25
+ }
26
+ }
27
+ async canActivate(context) {
28
+ const options = this.reflector.get('api_key_options', context.getHandler());
29
+ // If no @ApiKey decorator is present, skip this guard
30
+ if (!options) {
31
+ return true;
32
+ }
33
+ const request = context.switchToHttp().getRequest();
34
+ const apiKey = this.extractApiKey(request);
35
+ if (!apiKey) {
36
+ throw new common_1.UnauthorizedException('API key is missing');
37
+ }
38
+ // Validate API key
39
+ const validation = await this.validateApiKey(apiKey, request, options);
40
+ if (!validation.valid) {
41
+ throw new common_1.UnauthorizedException(validation.message || 'Invalid API key');
42
+ }
43
+ // Attach API key info to request for downstream use
44
+ request.apiKey = validation.apiKey;
45
+ request.apiKeyClient = {
46
+ id: validation.apiKey?.id,
47
+ clientName: validation.apiKey?.clientName,
48
+ scope: validation.apiKey?.scope,
49
+ };
50
+ return true;
51
+ }
52
+ extractApiKey(request) {
53
+ // Check 'X-API-Key' header
54
+ const headerKey = request.headers['x-api-key'];
55
+ if (headerKey) {
56
+ return headerKey;
57
+ }
58
+ // Check 'api_key' query parameter
59
+ if (request.query?.api_key) {
60
+ return request.query.api_key;
61
+ }
62
+ // Check Authorization header (format: 'ApiKey <key>')
63
+ const authHeader = request.headers.authorization;
64
+ if (authHeader && authHeader.startsWith('ApiKey ')) {
65
+ return authHeader.substring(7);
66
+ }
67
+ return null;
68
+ }
69
+ async validateApiKey(apiKey, request, options) {
70
+ if (!this.apiKeyRepo) {
71
+ return { valid: false, message: 'API key validation not configured' };
72
+ }
73
+ const record = await this.apiKeyRepo.findOne({
74
+ where: { key: apiKey, isActive: true },
75
+ });
76
+ if (!record) {
77
+ return { valid: false, message: 'API key not found or inactive' };
78
+ }
79
+ // Check scope
80
+ const requiredScope = options.scope || 'common';
81
+ if (record.scope !== 'common' && record.scope !== requiredScope) {
82
+ return {
83
+ valid: false,
84
+ message: `API key scope '${record.scope}' does not match required scope '${requiredScope}'`,
85
+ };
86
+ }
87
+ // Check IP whitelist
88
+ if (record.allowedIps) {
89
+ const clientIp = this.getClientIp(request);
90
+ const allowedIps = record.allowedIps.split(',').map((ip) => ip.trim());
91
+ if (!allowedIps.includes(clientIp)) {
92
+ return {
93
+ valid: false,
94
+ message: `Client IP '${clientIp}' is not whitelisted`,
95
+ };
96
+ }
97
+ }
98
+ // Check endpoint restriction
99
+ if (record.allowedEndpoints) {
100
+ const requestEndpoint = `${request.method} ${request.path}`;
101
+ const allowedEndpoints = record.allowedEndpoints
102
+ .split(',')
103
+ .map((ep) => ep.trim());
104
+ const isAllowed = allowedEndpoints.some((ep) => {
105
+ // Support wildcard matching (e.g., 'GET /spot/*')
106
+ if (ep.includes('*')) {
107
+ const pattern = ep.replace(/\*/g, '.*');
108
+ return new RegExp(`^${pattern}$`).test(requestEndpoint);
109
+ }
110
+ return ep === requestEndpoint;
111
+ });
112
+ if (!isAllowed) {
113
+ return {
114
+ valid: false,
115
+ message: `Endpoint '${requestEndpoint}' is not allowed for this API key`,
116
+ };
117
+ }
118
+ }
119
+ // Check rate limit
120
+ if (record.rateLimit) {
121
+ const isRateLimited = this.checkRateLimit(apiKey, record.rateLimit);
122
+ if (isRateLimited) {
123
+ return {
124
+ valid: false,
125
+ message: `Rate limit exceeded (${record.rateLimit} requests per minute)`,
126
+ };
127
+ }
128
+ }
129
+ // Update last used timestamp
130
+ if (this.apiKeyRepo) {
131
+ await this.apiKeyRepo.update(record.id, {
132
+ lastUsedAt: Date.now(),
133
+ });
134
+ }
135
+ return { valid: true, apiKey: record };
136
+ }
137
+ getClientIp(request) {
138
+ // Support common proxy headers
139
+ const xForwardedFor = request.headers['x-forwarded-for'];
140
+ if (xForwardedFor) {
141
+ return xForwardedFor.split(',')[0].trim();
142
+ }
143
+ const xRealIp = request.headers['x-real-ip'];
144
+ if (xRealIp) {
145
+ return xRealIp;
146
+ }
147
+ return request.ip || request.connection.remoteAddress || 'unknown';
148
+ }
149
+ checkRateLimit(apiKey, rateLimit) {
150
+ const now = Date.now();
151
+ const limitData = this.rateLimitMap.get(apiKey);
152
+ if (!limitData) {
153
+ // First request, initialize
154
+ this.rateLimitMap.set(apiKey, {
155
+ count: 1,
156
+ resetTime: now + 60000, // 1 minute
157
+ });
158
+ return false;
159
+ }
160
+ if (now > limitData.resetTime) {
161
+ // Reset window expired
162
+ this.rateLimitMap.set(apiKey, {
163
+ count: 1,
164
+ resetTime: now + 60000,
165
+ });
166
+ return false;
167
+ }
168
+ // Still within window
169
+ if (limitData.count >= rateLimit) {
170
+ return true;
171
+ }
172
+ limitData.count++;
173
+ return false;
174
+ }
175
+ };
176
+ exports.ApiKeyGuard = ApiKeyGuard;
177
+ exports.ApiKeyGuard = ApiKeyGuard = __decorate([
178
+ (0, common_1.Injectable)(),
179
+ __param(1, (0, common_1.Inject)('API_KEY_REPOSITORY')),
180
+ __metadata("design:paramtypes", [core_1.Reflector,
181
+ typeorm_1.Repository])
182
+ ], ApiKeyGuard);
package/dist/index.d.ts CHANGED
@@ -1,28 +1,29 @@
1
- export * from './auth.module';
2
- export * from './auth.service';
3
- export * from './dtos/login.dto';
4
- export * from './dtos/login-response.dto';
5
- export * from './decorators/current-user.decorator';
6
- export * from './decorators/feature.decorator';
7
- export * from './decorators/has-permission.decorator';
8
- export * from './decorators/roles.decorator';
9
- export * from './decorators/route-permission.decorator';
10
- export * from './decorators/api-key.decorator';
11
- export * from './guards/module.guard';
12
- export * from './guards/roles.guard';
13
- export * from './guards/feature.guard';
14
- export * from './guards/route.guard';
15
- export * from './guards/permission.guard';
16
- export * from './guards/api-key.guard';
17
- export * from './jwt/jwt.guard';
18
- export * from './jwt/jwt.strategy';
19
- export * from './interfaces/auth-core-options.interface';
20
- export * from './entities/user.entity';
21
- export * from './entities/role.entity';
22
- export * from './entities/module.entity';
23
- export * from './entities/feature.entity';
24
- export * from './entities/module-route.entity';
25
- export * from './entities/user-feature-access.entity';
26
- export * from './entities/user-module-access.entity';
27
- export * from './entities/module-screen-permission.entity';
28
- export * from './entities/api-key.entity';
1
+ export * from './auth.module';
2
+ export * from './auth.service';
3
+ export * from './dtos/login.dto';
4
+ export * from './dtos/login-response.dto';
5
+ export * from './decorators/current-user.decorator';
6
+ export * from './decorators/feature.decorator';
7
+ export * from './decorators/has-permission.decorator';
8
+ export * from './decorators/roles.decorator';
9
+ export * from './decorators/route-permission.decorator';
10
+ export * from './decorators/api-key.decorator';
11
+ export * from './guards/module.guard';
12
+ export * from './guards/roles.guard';
13
+ export * from './guards/feature.guard';
14
+ export * from './guards/route.guard';
15
+ export * from './guards/permission.guard';
16
+ export * from './guards/api-key.guard';
17
+ export * from './jwt/jwt.guard';
18
+ export * from './jwt/jwt.strategy';
19
+ export * from './interfaces/auth-core-options.interface';
20
+ export * from './entities/user.entity';
21
+ export * from './entities/role.entity';
22
+ export * from './entities/module.entity';
23
+ export * from './entities/feature.entity';
24
+ export * from './entities/module-route.entity';
25
+ export * from './entities/user-feature-access.entity';
26
+ export * from './entities/user-module-access.entity';
27
+ export * from './entities/module-screen-permission.entity';
28
+ export * from './entities/api-key.entity';
29
+ export * from './entities/user-last-login.entity';