ecrs-auth-core 1.0.61 → 1.0.63
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/dist/auth.module.js +12 -0
- package/dist/auth.service.js +0 -1
- package/dist/decorators/api-key.decorator.d.ts +5 -0
- package/dist/decorators/api-key.decorator.js +8 -0
- package/dist/entities/api-key.entity.d.ts +16 -0
- package/dist/entities/api-key.entity.js +75 -0
- package/dist/guards/api-key.guard.d.ts +17 -0
- package/dist/guards/api-key.guard.js +195 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/interfaces/auth-core-options.interface.d.ts +2 -0
- package/dist/services/encryption.service.d.ts +16 -0
- package/dist/services/encryption.service.js +70 -0
- package/package.json +1 -1
package/dist/auth.module.js
CHANGED
|
@@ -19,6 +19,8 @@ const roles_guard_1 = require("./guards/roles.guard");
|
|
|
19
19
|
const feature_guard_1 = require("./guards/feature.guard");
|
|
20
20
|
const route_guard_1 = require("./guards/route.guard");
|
|
21
21
|
const permission_guard_1 = require("./guards/permission.guard");
|
|
22
|
+
const api_key_guard_1 = require("./guards/api-key.guard");
|
|
23
|
+
const encryption_service_1 = require("./services/encryption.service");
|
|
22
24
|
exports.AUTH_CORE_OPTIONS = 'AUTH_CORE_OPTIONS';
|
|
23
25
|
// @Global()
|
|
24
26
|
// @Module({})
|
|
@@ -109,6 +111,12 @@ let AuthCoreModule = AuthCoreModule_1 = class AuthCoreModule {
|
|
|
109
111
|
useFactory: (opts) => opts.moduleConfig || {},
|
|
110
112
|
inject: [exports.AUTH_CORE_OPTIONS],
|
|
111
113
|
},
|
|
114
|
+
{
|
|
115
|
+
provide: 'API_KEY_REPOSITORY',
|
|
116
|
+
useFactory: (opts) => opts.repositories?.apiKeyRepo || null,
|
|
117
|
+
inject: [exports.AUTH_CORE_OPTIONS],
|
|
118
|
+
},
|
|
119
|
+
encryption_service_1.EncryptionService,
|
|
112
120
|
auth_service_1.AuthService,
|
|
113
121
|
jwt_strategy_1.JwtStrategy,
|
|
114
122
|
jwt_guard_1.JwtAuthGuard,
|
|
@@ -117,14 +125,17 @@ let AuthCoreModule = AuthCoreModule_1 = class AuthCoreModule {
|
|
|
117
125
|
feature_guard_1.FeatureGuard,
|
|
118
126
|
route_guard_1.RouteGuard,
|
|
119
127
|
permission_guard_1.PermissionGuard,
|
|
128
|
+
api_key_guard_1.ApiKeyGuard,
|
|
120
129
|
],
|
|
121
130
|
controllers: [auth_controller_1.AuthController],
|
|
122
131
|
exports: [
|
|
123
132
|
// ⬇️ export these so Superadmin can resolve guard deps
|
|
124
133
|
exports.AUTH_CORE_OPTIONS,
|
|
125
134
|
'MODULE_CONFIG',
|
|
135
|
+
'API_KEY_REPOSITORY',
|
|
126
136
|
jwt_1.JwtModule,
|
|
127
137
|
auth_service_1.AuthService,
|
|
138
|
+
encryption_service_1.EncryptionService,
|
|
128
139
|
jwt_strategy_1.JwtStrategy,
|
|
129
140
|
jwt_guard_1.JwtAuthGuard,
|
|
130
141
|
module_guard_1.ModuleGuard,
|
|
@@ -132,6 +143,7 @@ let AuthCoreModule = AuthCoreModule_1 = class AuthCoreModule {
|
|
|
132
143
|
feature_guard_1.FeatureGuard,
|
|
133
144
|
route_guard_1.RouteGuard,
|
|
134
145
|
permission_guard_1.PermissionGuard,
|
|
146
|
+
api_key_guard_1.ApiKeyGuard,
|
|
135
147
|
],
|
|
136
148
|
};
|
|
137
149
|
}
|
package/dist/auth.service.js
CHANGED
|
@@ -74,7 +74,6 @@ let AuthService = class AuthService {
|
|
|
74
74
|
async hasModuleAccess(userId, moduleId) {
|
|
75
75
|
if (!Number.isFinite(moduleId))
|
|
76
76
|
return false;
|
|
77
|
-
// Check both isDeleted and status flags for active access
|
|
78
77
|
const access = await this.moduleAccessRepo.findOne({
|
|
79
78
|
where: { userId, moduleId, isDeleted: 0, status: 1 },
|
|
80
79
|
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApiKey = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const ApiKey = (options) => {
|
|
6
|
+
return (0, common_1.SetMetadata)('api_key_options', options || {});
|
|
7
|
+
};
|
|
8
|
+
exports.ApiKey = ApiKey;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare class ApiKeyEntity {
|
|
2
|
+
id: number;
|
|
3
|
+
key: string;
|
|
4
|
+
clientName: string;
|
|
5
|
+
clientDescription: string | null;
|
|
6
|
+
scope: string;
|
|
7
|
+
allowedIps: string | null;
|
|
8
|
+
allowedEndpoints: string | null;
|
|
9
|
+
isActive: boolean;
|
|
10
|
+
rateLimit: number | null;
|
|
11
|
+
lastUsedAt: number | null;
|
|
12
|
+
createdAt: Date;
|
|
13
|
+
updatedAt: Date;
|
|
14
|
+
createdBy: string | null;
|
|
15
|
+
metadata: string | null;
|
|
16
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
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.ApiKeyEntity = void 0;
|
|
13
|
+
const typeorm_1 = require("typeorm");
|
|
14
|
+
let ApiKeyEntity = class ApiKeyEntity {
|
|
15
|
+
};
|
|
16
|
+
exports.ApiKeyEntity = ApiKeyEntity;
|
|
17
|
+
__decorate([
|
|
18
|
+
(0, typeorm_1.PrimaryGeneratedColumn)(),
|
|
19
|
+
__metadata("design:type", Number)
|
|
20
|
+
], ApiKeyEntity.prototype, "id", void 0);
|
|
21
|
+
__decorate([
|
|
22
|
+
(0, typeorm_1.Column)({ type: 'varchar', length: 255, unique: true }),
|
|
23
|
+
__metadata("design:type", String)
|
|
24
|
+
], ApiKeyEntity.prototype, "key", void 0);
|
|
25
|
+
__decorate([
|
|
26
|
+
(0, typeorm_1.Column)({ type: 'varchar', length: 255 }),
|
|
27
|
+
__metadata("design:type", String)
|
|
28
|
+
], ApiKeyEntity.prototype, "clientName", void 0);
|
|
29
|
+
__decorate([
|
|
30
|
+
(0, typeorm_1.Column)({ type: 'varchar', length: 255, nullable: true }),
|
|
31
|
+
__metadata("design:type", Object)
|
|
32
|
+
], ApiKeyEntity.prototype, "clientDescription", void 0);
|
|
33
|
+
__decorate([
|
|
34
|
+
(0, typeorm_1.Column)({ type: 'varchar', length: 50, default: 'common' }),
|
|
35
|
+
__metadata("design:type", String)
|
|
36
|
+
], ApiKeyEntity.prototype, "scope", void 0);
|
|
37
|
+
__decorate([
|
|
38
|
+
(0, typeorm_1.Column)({ type: 'text', nullable: true }),
|
|
39
|
+
__metadata("design:type", Object)
|
|
40
|
+
], ApiKeyEntity.prototype, "allowedIps", void 0);
|
|
41
|
+
__decorate([
|
|
42
|
+
(0, typeorm_1.Column)({ type: 'text', nullable: true }),
|
|
43
|
+
__metadata("design:type", Object)
|
|
44
|
+
], ApiKeyEntity.prototype, "allowedEndpoints", void 0);
|
|
45
|
+
__decorate([
|
|
46
|
+
(0, typeorm_1.Column)({ type: 'boolean', default: true }),
|
|
47
|
+
__metadata("design:type", Boolean)
|
|
48
|
+
], ApiKeyEntity.prototype, "isActive", void 0);
|
|
49
|
+
__decorate([
|
|
50
|
+
(0, typeorm_1.Column)({ type: 'bigint', nullable: true }),
|
|
51
|
+
__metadata("design:type", Object)
|
|
52
|
+
], ApiKeyEntity.prototype, "rateLimit", void 0);
|
|
53
|
+
__decorate([
|
|
54
|
+
(0, typeorm_1.Column)({ type: 'bigint', nullable: true }),
|
|
55
|
+
__metadata("design:type", Object)
|
|
56
|
+
], ApiKeyEntity.prototype, "lastUsedAt", void 0);
|
|
57
|
+
__decorate([
|
|
58
|
+
(0, typeorm_1.CreateDateColumn)(),
|
|
59
|
+
__metadata("design:type", Date)
|
|
60
|
+
], ApiKeyEntity.prototype, "createdAt", void 0);
|
|
61
|
+
__decorate([
|
|
62
|
+
(0, typeorm_1.UpdateDateColumn)(),
|
|
63
|
+
__metadata("design:type", Date)
|
|
64
|
+
], ApiKeyEntity.prototype, "updatedAt", void 0);
|
|
65
|
+
__decorate([
|
|
66
|
+
(0, typeorm_1.Column)({ type: 'varchar', length: 255, nullable: true }),
|
|
67
|
+
__metadata("design:type", Object)
|
|
68
|
+
], ApiKeyEntity.prototype, "createdBy", void 0);
|
|
69
|
+
__decorate([
|
|
70
|
+
(0, typeorm_1.Column)({ type: 'text', nullable: true }),
|
|
71
|
+
__metadata("design:type", Object)
|
|
72
|
+
], ApiKeyEntity.prototype, "metadata", void 0);
|
|
73
|
+
exports.ApiKeyEntity = ApiKeyEntity = __decorate([
|
|
74
|
+
(0, typeorm_1.Entity)({ name: 'tbl_c_api_keys' })
|
|
75
|
+
], ApiKeyEntity);
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
import { EncryptionService } from '../services/encryption.service';
|
|
6
|
+
export declare class ApiKeyGuard implements CanActivate {
|
|
7
|
+
private readonly reflector;
|
|
8
|
+
private readonly encryptionService;
|
|
9
|
+
private apiKeyRepo;
|
|
10
|
+
private rateLimitMap;
|
|
11
|
+
constructor(reflector: Reflector, encryptionService: EncryptionService, apiKeyRepository?: Repository<ApiKeyEntity>);
|
|
12
|
+
canActivate(context: ExecutionContext): Promise<boolean>;
|
|
13
|
+
private extractApiKey;
|
|
14
|
+
private validateApiKey;
|
|
15
|
+
private getClientIp;
|
|
16
|
+
private checkRateLimit;
|
|
17
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
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
|
+
const encryption_service_1 = require("../services/encryption.service");
|
|
20
|
+
let ApiKeyGuard = class ApiKeyGuard {
|
|
21
|
+
constructor(reflector, encryptionService, apiKeyRepository) {
|
|
22
|
+
this.reflector = reflector;
|
|
23
|
+
this.encryptionService = encryptionService;
|
|
24
|
+
this.rateLimitMap = new Map();
|
|
25
|
+
if (apiKeyRepository) {
|
|
26
|
+
this.apiKeyRepo = apiKeyRepository;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async canActivate(context) {
|
|
30
|
+
const options = this.reflector.get('api_key_options', context.getHandler());
|
|
31
|
+
// If no @ApiKey decorator is present, skip this guard
|
|
32
|
+
if (!options) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
const request = context.switchToHttp().getRequest();
|
|
36
|
+
const apiKey = this.extractApiKey(request);
|
|
37
|
+
if (!apiKey) {
|
|
38
|
+
throw new common_1.UnauthorizedException('API key is missing');
|
|
39
|
+
}
|
|
40
|
+
// Validate API key
|
|
41
|
+
const validation = await this.validateApiKey(apiKey, request, options);
|
|
42
|
+
if (!validation.valid) {
|
|
43
|
+
throw new common_1.UnauthorizedException(validation.message || 'Invalid API key');
|
|
44
|
+
}
|
|
45
|
+
// Attach API key info to request for downstream use
|
|
46
|
+
request.apiKey = validation.apiKey;
|
|
47
|
+
request.apiKeyClient = {
|
|
48
|
+
id: validation.apiKey?.id,
|
|
49
|
+
clientName: validation.apiKey?.clientName,
|
|
50
|
+
scope: validation.apiKey?.scope,
|
|
51
|
+
};
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
extractApiKey(request) {
|
|
55
|
+
// Check 'X-API-Key' header
|
|
56
|
+
const headerKey = request.headers['x-api-key'];
|
|
57
|
+
if (headerKey) {
|
|
58
|
+
return headerKey;
|
|
59
|
+
}
|
|
60
|
+
// Check 'api_key' query parameter
|
|
61
|
+
if (request.query?.api_key) {
|
|
62
|
+
return request.query.api_key;
|
|
63
|
+
}
|
|
64
|
+
// Check Authorization header (format: 'ApiKey <key>')
|
|
65
|
+
const authHeader = request.headers.authorization;
|
|
66
|
+
if (authHeader && authHeader.startsWith('ApiKey ')) {
|
|
67
|
+
return authHeader.substring(7);
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
async validateApiKey(apiKey, request, options) {
|
|
72
|
+
if (!this.apiKeyRepo) {
|
|
73
|
+
return { valid: false, message: 'API key validation not configured' };
|
|
74
|
+
}
|
|
75
|
+
// Find all active API keys and compare hashes
|
|
76
|
+
const records = await this.apiKeyRepo.find({
|
|
77
|
+
where: { isActive: true },
|
|
78
|
+
});
|
|
79
|
+
let record = null;
|
|
80
|
+
// Compare incoming key with hashed keys in database
|
|
81
|
+
for (const dbRecord of records) {
|
|
82
|
+
const isMatch = await this.encryptionService.compareKey(apiKey, dbRecord.key);
|
|
83
|
+
if (isMatch) {
|
|
84
|
+
record = dbRecord;
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (!record) {
|
|
89
|
+
return { valid: false, message: 'API key not found or inactive' };
|
|
90
|
+
}
|
|
91
|
+
// Check scope
|
|
92
|
+
const requiredScope = options.scope || 'common';
|
|
93
|
+
if (record.scope !== 'common' && record.scope !== requiredScope) {
|
|
94
|
+
return {
|
|
95
|
+
valid: false,
|
|
96
|
+
message: `API key scope '${record.scope}' does not match required scope '${requiredScope}'`,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
// Check IP whitelist
|
|
100
|
+
if (record.allowedIps) {
|
|
101
|
+
const clientIp = this.getClientIp(request);
|
|
102
|
+
const allowedIps = record.allowedIps.split(',').map((ip) => ip.trim());
|
|
103
|
+
if (!allowedIps.includes(clientIp)) {
|
|
104
|
+
return {
|
|
105
|
+
valid: false,
|
|
106
|
+
message: `Client IP '${clientIp}' is not whitelisted`,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// Check endpoint restriction
|
|
111
|
+
if (record.allowedEndpoints) {
|
|
112
|
+
const requestEndpoint = `${request.method} ${request.path}`;
|
|
113
|
+
const allowedEndpoints = record.allowedEndpoints
|
|
114
|
+
.split(',')
|
|
115
|
+
.map((ep) => ep.trim());
|
|
116
|
+
const isAllowed = allowedEndpoints.some((ep) => {
|
|
117
|
+
// Support wildcard matching (e.g., 'GET /spot/*')
|
|
118
|
+
if (ep.includes('*')) {
|
|
119
|
+
const pattern = ep.replace(/\*/g, '.*');
|
|
120
|
+
return new RegExp(`^${pattern}$`).test(requestEndpoint);
|
|
121
|
+
}
|
|
122
|
+
return ep === requestEndpoint;
|
|
123
|
+
});
|
|
124
|
+
if (!isAllowed) {
|
|
125
|
+
return {
|
|
126
|
+
valid: false,
|
|
127
|
+
message: `Endpoint '${requestEndpoint}' is not allowed for this API key`,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
// Check rate limit
|
|
132
|
+
if (record.rateLimit) {
|
|
133
|
+
const isRateLimited = this.checkRateLimit(apiKey, record.rateLimit);
|
|
134
|
+
if (isRateLimited) {
|
|
135
|
+
return {
|
|
136
|
+
valid: false,
|
|
137
|
+
message: `Rate limit exceeded (${record.rateLimit} requests per minute)`,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// Update last used timestamp
|
|
142
|
+
if (this.apiKeyRepo) {
|
|
143
|
+
await this.apiKeyRepo.update(record.id, {
|
|
144
|
+
lastUsedAt: Date.now(),
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
return { valid: true, apiKey: record };
|
|
148
|
+
}
|
|
149
|
+
getClientIp(request) {
|
|
150
|
+
// Support common proxy headers
|
|
151
|
+
const xForwardedFor = request.headers['x-forwarded-for'];
|
|
152
|
+
if (xForwardedFor) {
|
|
153
|
+
return xForwardedFor.split(',')[0].trim();
|
|
154
|
+
}
|
|
155
|
+
const xRealIp = request.headers['x-real-ip'];
|
|
156
|
+
if (xRealIp) {
|
|
157
|
+
return xRealIp;
|
|
158
|
+
}
|
|
159
|
+
return request.ip || request.connection.remoteAddress || 'unknown';
|
|
160
|
+
}
|
|
161
|
+
checkRateLimit(apiKey, rateLimit) {
|
|
162
|
+
const now = Date.now();
|
|
163
|
+
const limitData = this.rateLimitMap.get(apiKey);
|
|
164
|
+
if (!limitData) {
|
|
165
|
+
// First request, initialize
|
|
166
|
+
this.rateLimitMap.set(apiKey, {
|
|
167
|
+
count: 1,
|
|
168
|
+
resetTime: now + 60000, // 1 minute
|
|
169
|
+
});
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
if (now > limitData.resetTime) {
|
|
173
|
+
// Reset window expired
|
|
174
|
+
this.rateLimitMap.set(apiKey, {
|
|
175
|
+
count: 1,
|
|
176
|
+
resetTime: now + 60000,
|
|
177
|
+
});
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
// Still within window
|
|
181
|
+
if (limitData.count >= rateLimit) {
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
limitData.count++;
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
exports.ApiKeyGuard = ApiKeyGuard;
|
|
189
|
+
exports.ApiKeyGuard = ApiKeyGuard = __decorate([
|
|
190
|
+
(0, common_1.Injectable)(),
|
|
191
|
+
__param(2, (0, common_1.Inject)('API_KEY_REPOSITORY')),
|
|
192
|
+
__metadata("design:paramtypes", [core_1.Reflector,
|
|
193
|
+
encryption_service_1.EncryptionService,
|
|
194
|
+
typeorm_1.Repository])
|
|
195
|
+
], ApiKeyGuard);
|
package/dist/index.d.ts
CHANGED
|
@@ -7,13 +7,16 @@ export * from './decorators/feature.decorator';
|
|
|
7
7
|
export * from './decorators/has-permission.decorator';
|
|
8
8
|
export * from './decorators/roles.decorator';
|
|
9
9
|
export * from './decorators/route-permission.decorator';
|
|
10
|
+
export * from './decorators/api-key.decorator';
|
|
10
11
|
export * from './guards/module.guard';
|
|
11
12
|
export * from './guards/roles.guard';
|
|
12
13
|
export * from './guards/feature.guard';
|
|
13
14
|
export * from './guards/route.guard';
|
|
14
15
|
export * from './guards/permission.guard';
|
|
16
|
+
export * from './guards/api-key.guard';
|
|
15
17
|
export * from './jwt/jwt.guard';
|
|
16
18
|
export * from './jwt/jwt.strategy';
|
|
19
|
+
export * from './services/encryption.service';
|
|
17
20
|
export * from './interfaces/auth-core-options.interface';
|
|
18
21
|
export * from './entities/user.entity';
|
|
19
22
|
export * from './entities/role.entity';
|
|
@@ -23,3 +26,4 @@ export * from './entities/module-route.entity';
|
|
|
23
26
|
export * from './entities/user-feature-access.entity';
|
|
24
27
|
export * from './entities/user-module-access.entity';
|
|
25
28
|
export * from './entities/module-screen-permission.entity';
|
|
29
|
+
export * from './entities/api-key.entity';
|
package/dist/index.js
CHANGED
|
@@ -26,15 +26,19 @@ __exportStar(require("./decorators/feature.decorator"), exports);
|
|
|
26
26
|
__exportStar(require("./decorators/has-permission.decorator"), exports);
|
|
27
27
|
__exportStar(require("./decorators/roles.decorator"), exports);
|
|
28
28
|
__exportStar(require("./decorators/route-permission.decorator"), exports);
|
|
29
|
+
__exportStar(require("./decorators/api-key.decorator"), exports);
|
|
29
30
|
// Guards
|
|
30
31
|
__exportStar(require("./guards/module.guard"), exports);
|
|
31
32
|
__exportStar(require("./guards/roles.guard"), exports);
|
|
32
33
|
__exportStar(require("./guards/feature.guard"), exports);
|
|
33
34
|
__exportStar(require("./guards/route.guard"), exports);
|
|
34
35
|
__exportStar(require("./guards/permission.guard"), exports);
|
|
36
|
+
__exportStar(require("./guards/api-key.guard"), exports);
|
|
35
37
|
// JWT
|
|
36
38
|
__exportStar(require("./jwt/jwt.guard"), exports);
|
|
37
39
|
__exportStar(require("./jwt/jwt.strategy"), exports);
|
|
40
|
+
// Services
|
|
41
|
+
__exportStar(require("./services/encryption.service"), exports);
|
|
38
42
|
// Interfaces
|
|
39
43
|
__exportStar(require("./interfaces/auth-core-options.interface"), exports);
|
|
40
44
|
// ✅ Entities
|
|
@@ -46,3 +50,4 @@ __exportStar(require("./entities/module-route.entity"), exports);
|
|
|
46
50
|
__exportStar(require("./entities/user-feature-access.entity"), exports);
|
|
47
51
|
__exportStar(require("./entities/user-module-access.entity"), exports);
|
|
48
52
|
__exportStar(require("./entities/module-screen-permission.entity"), exports);
|
|
53
|
+
__exportStar(require("./entities/api-key.entity"), exports);
|
|
@@ -7,6 +7,7 @@ import { ModuleRoute } from '../entities/module-route.entity';
|
|
|
7
7
|
import { UserFeatureAccess } from '../entities/user-feature-access.entity';
|
|
8
8
|
import { UserModuleAccess } from '../entities/user-module-access.entity';
|
|
9
9
|
import { ModuleScreenPermission } from '../entities/module-screen-permission.entity';
|
|
10
|
+
import { ApiKeyEntity } from '../entities/api-key.entity';
|
|
10
11
|
export interface Repositories {
|
|
11
12
|
userRepo: Repository<User>;
|
|
12
13
|
roleRepo: Repository<Role>;
|
|
@@ -16,6 +17,7 @@ export interface Repositories {
|
|
|
16
17
|
featureRepo: Repository<Feature>;
|
|
17
18
|
routeRepo: Repository<ModuleRoute>;
|
|
18
19
|
moduleAccessRepo: Repository<UserModuleAccess>;
|
|
20
|
+
apiKeyRepo?: Repository<ApiKeyEntity>;
|
|
19
21
|
}
|
|
20
22
|
export interface AuthModuleConfig {
|
|
21
23
|
enable2FA?: boolean;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare class EncryptionService {
|
|
2
|
+
private readonly saltRounds;
|
|
3
|
+
/**
|
|
4
|
+
* Hash an API key using bcrypt
|
|
5
|
+
* @param plainKey - The plain text API key
|
|
6
|
+
* @returns Hashed key
|
|
7
|
+
*/
|
|
8
|
+
hashKey(plainKey: string): Promise<string>;
|
|
9
|
+
/**
|
|
10
|
+
* Compare plain key with hashed key
|
|
11
|
+
* @param plainKey - The plain text API key
|
|
12
|
+
* @param hashedKey - The hashed key from database
|
|
13
|
+
* @returns True if keys match, false otherwise
|
|
14
|
+
*/
|
|
15
|
+
compareKey(plainKey: string, hashedKey: string): Promise<boolean>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
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;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.EncryptionService = void 0;
|
|
43
|
+
const common_1 = require("@nestjs/common");
|
|
44
|
+
const bcrypt = __importStar(require("bcrypt"));
|
|
45
|
+
let EncryptionService = class EncryptionService {
|
|
46
|
+
constructor() {
|
|
47
|
+
this.saltRounds = 10;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Hash an API key using bcrypt
|
|
51
|
+
* @param plainKey - The plain text API key
|
|
52
|
+
* @returns Hashed key
|
|
53
|
+
*/
|
|
54
|
+
async hashKey(plainKey) {
|
|
55
|
+
return bcrypt.hash(plainKey, this.saltRounds);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Compare plain key with hashed key
|
|
59
|
+
* @param plainKey - The plain text API key
|
|
60
|
+
* @param hashedKey - The hashed key from database
|
|
61
|
+
* @returns True if keys match, false otherwise
|
|
62
|
+
*/
|
|
63
|
+
async compareKey(plainKey, hashedKey) {
|
|
64
|
+
return bcrypt.compare(plainKey, hashedKey);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
exports.EncryptionService = EncryptionService;
|
|
68
|
+
exports.EncryptionService = EncryptionService = __decorate([
|
|
69
|
+
(0, common_1.Injectable)()
|
|
70
|
+
], EncryptionService);
|