@mondart/nestjs-common-module 2.6.7 → 2.7.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/dist/decorators/index.d.ts +1 -0
- package/dist/decorators/index.js +1 -0
- package/dist/decorators/kafka-timestamp.decorator.d.ts +1 -0
- package/dist/decorators/kafka-timestamp.decorator.js +28 -0
- package/dist/filters/global-exception.filter.js +2 -1
- package/dist/lib/keycloak/index.d.ts +5 -1
- package/dist/lib/keycloak/index.js +2 -1
- package/dist/lib/keycloak/keycloak.module.d.ts +2 -7
- package/dist/lib/keycloak/keycloak.module.js +10 -40
- package/dist/lib/keycloak/services/keycloak.service.d.ts +12 -0
- package/dist/lib/keycloak/services/keycloak.service.js +79 -0
- package/dist/lib/keycloak/utils/jwt.util.js +2 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/eslint.config.js +49 -0
- package/package.json +3 -3
- package/dist/lib/keycloak/keycloak.service.d.ts +0 -14
- package/dist/lib/keycloak/keycloak.service.js +0 -85
package/dist/decorators/index.js
CHANGED
|
@@ -21,3 +21,4 @@ __exportStar(require("./get-agent.decorator"), exports);
|
|
|
21
21
|
__exportStar(require("./get-store-id.decorator"), exports);
|
|
22
22
|
__exportStar(require("./get-user.decorator"), exports);
|
|
23
23
|
__exportStar(require("./iam-context.decorator"), exports);
|
|
24
|
+
__exportStar(require("./kafka-timestamp.decorator"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function CheckKafkaTimestamp(timeoutInSeconds?: number): <TFunction extends Function, Y>(target: TFunction | object, propertyKey?: string | symbol, descriptor?: TypedPropertyDescriptor<Y>) => void;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CheckKafkaTimestamp = CheckKafkaTimestamp;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
function CheckKafkaTimestamp(timeoutInSeconds = 30) {
|
|
6
|
+
return (0, common_1.applyDecorators)((0, common_1.UseInterceptors)({
|
|
7
|
+
intercept: async (context, next) => {
|
|
8
|
+
const logger = new common_1.Logger('CheckKafkaTimestamp');
|
|
9
|
+
const kafkaCtx = context.getArgByIndex(1);
|
|
10
|
+
if (kafkaCtx && typeof kafkaCtx.getMessage === 'function') {
|
|
11
|
+
const message = kafkaCtx.getMessage();
|
|
12
|
+
const nowTimeInServer = new Date().getTime();
|
|
13
|
+
const messageTimestamp = Number(message.timestamp);
|
|
14
|
+
const diffTime = nowTimeInServer - messageTimestamp;
|
|
15
|
+
if (diffTime > timeoutInSeconds * 1000) {
|
|
16
|
+
logger.debug({
|
|
17
|
+
nowTimeInServer,
|
|
18
|
+
messageTimestamp,
|
|
19
|
+
diffTime,
|
|
20
|
+
message,
|
|
21
|
+
});
|
|
22
|
+
throw new common_1.BadRequestException(`Message is too old to process. Current time: ${nowTimeInServer}, Message timestamp: ${messageTimestamp}, Difference: ${diffTime}ms`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return next.handle();
|
|
26
|
+
},
|
|
27
|
+
}), (0, common_1.SetMetadata)('kafka-timestamp-check', true));
|
|
28
|
+
}
|
|
@@ -54,7 +54,8 @@ let GlobalExceptionFilter = class GlobalExceptionFilter {
|
|
|
54
54
|
if (type === 'http') {
|
|
55
55
|
const response = context.getResponse();
|
|
56
56
|
try {
|
|
57
|
-
|
|
57
|
+
const exceptionsName = Object.values(exceptions).map((exception) => exception.name);
|
|
58
|
+
if (exceptionsName.includes(exception.name)) {
|
|
58
59
|
const status = exception?.getStatus();
|
|
59
60
|
const result = exception.getResponse();
|
|
60
61
|
const message = typeof result?.message === 'string'
|
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
export * from './keycloak.module';
|
|
2
|
-
export * from './keycloak.service';
|
|
2
|
+
export * from './services/keycloak.service';
|
|
3
3
|
export * from './interfaces';
|
|
4
|
+
export * from '@keycloak/keycloak-admin-client/lib';
|
|
5
|
+
import type { Users, UserQuery } from '@keycloak/keycloak-admin-client/lib/resources/users';
|
|
6
|
+
import type RealmRepresentation from '@keycloak/keycloak-admin-client/lib/defs/realmRepresentation';
|
|
7
|
+
export { RealmRepresentation, UserQuery, Users };
|
|
@@ -15,5 +15,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./keycloak.module"), exports);
|
|
18
|
-
__exportStar(require("./keycloak.service"), exports);
|
|
18
|
+
__exportStar(require("./services/keycloak.service"), exports);
|
|
19
19
|
__exportStar(require("./interfaces"), exports);
|
|
20
|
+
__exportStar(require("@keycloak/keycloak-admin-client/lib"), exports);
|
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
import { DynamicModule
|
|
2
|
-
import { KeycloakService } from './keycloak.service';
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
3
2
|
import { KeycloakModuleAsyncOptions, KeycloakModuleOptions } from './interfaces';
|
|
4
|
-
export declare class KeycloakModule
|
|
5
|
-
private readonly service;
|
|
6
|
-
constructor(service: KeycloakService);
|
|
7
|
-
onModuleInit(): Promise<void>;
|
|
3
|
+
export declare class KeycloakModule {
|
|
8
4
|
static register(options: KeycloakModuleOptions): DynamicModule;
|
|
9
5
|
static registerAsync(options: KeycloakModuleAsyncOptions): DynamicModule;
|
|
10
|
-
private static createAsyncOptionsProvider;
|
|
11
6
|
private static createAsyncProviders;
|
|
12
7
|
}
|
|
@@ -5,26 +5,16 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
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
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
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
8
|
var KeycloakModule_1;
|
|
12
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
10
|
exports.KeycloakModule = void 0;
|
|
14
11
|
const common_1 = require("@nestjs/common");
|
|
15
|
-
const keycloak_service_1 = require("./keycloak.service");
|
|
12
|
+
const keycloak_service_1 = require("./services/keycloak.service");
|
|
16
13
|
const interfaces_1 = require("./interfaces");
|
|
17
14
|
let KeycloakModule = KeycloakModule_1 = class KeycloakModule {
|
|
18
|
-
constructor(service) {
|
|
19
|
-
this.service = service;
|
|
20
|
-
}
|
|
21
|
-
async onModuleInit() {
|
|
22
|
-
await this.service.init();
|
|
23
|
-
}
|
|
24
15
|
static register(options) {
|
|
25
16
|
return {
|
|
26
17
|
module: KeycloakModule_1,
|
|
27
|
-
imports: [],
|
|
28
18
|
providers: [
|
|
29
19
|
{
|
|
30
20
|
provide: interfaces_1.KEYCLOAK_MODULE_OPTIONS,
|
|
@@ -32,26 +22,16 @@ let KeycloakModule = KeycloakModule_1 = class KeycloakModule {
|
|
|
32
22
|
},
|
|
33
23
|
keycloak_service_1.KeycloakService,
|
|
34
24
|
],
|
|
35
|
-
exports: [keycloak_service_1.KeycloakService
|
|
25
|
+
exports: [keycloak_service_1.KeycloakService],
|
|
36
26
|
};
|
|
37
27
|
}
|
|
38
28
|
static registerAsync(options) {
|
|
39
|
-
const
|
|
40
|
-
return {
|
|
41
|
-
module: KeycloakModule_1,
|
|
42
|
-
imports: [...(options.imports || []), asyncOptionsProvider],
|
|
43
|
-
providers: [
|
|
44
|
-
keycloak_service_1.KeycloakService,
|
|
45
|
-
],
|
|
46
|
-
exports: [keycloak_service_1.KeycloakService, KeycloakModule_1],
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
static createAsyncOptionsProvider(options) {
|
|
50
|
-
const providers = this.createAsyncProviders(options);
|
|
29
|
+
const asyncProviders = this.createAsyncProviders(options);
|
|
51
30
|
return {
|
|
52
31
|
module: KeycloakModule_1,
|
|
53
|
-
|
|
54
|
-
|
|
32
|
+
imports: options.imports || [],
|
|
33
|
+
providers: [...asyncProviders, keycloak_service_1.KeycloakService],
|
|
34
|
+
exports: [keycloak_service_1.KeycloakService],
|
|
55
35
|
};
|
|
56
36
|
}
|
|
57
37
|
static createAsyncProviders(options) {
|
|
@@ -69,17 +49,10 @@ let KeycloakModule = KeycloakModule_1 = class KeycloakModule {
|
|
|
69
49
|
return [
|
|
70
50
|
{
|
|
71
51
|
provide: interfaces_1.KEYCLOAK_MODULE_OPTIONS,
|
|
72
|
-
useFactory: async (
|
|
73
|
-
inject: [
|
|
52
|
+
useFactory: async (factory) => factory.createModuleOptions(),
|
|
53
|
+
inject: [useClass],
|
|
74
54
|
},
|
|
75
|
-
...(options.useClass
|
|
76
|
-
? [
|
|
77
|
-
{
|
|
78
|
-
provide: options.useClass,
|
|
79
|
-
useClass: options.useClass,
|
|
80
|
-
},
|
|
81
|
-
]
|
|
82
|
-
: []),
|
|
55
|
+
...(options.useClass ? [{ provide: useClass, useClass }] : []),
|
|
83
56
|
];
|
|
84
57
|
}
|
|
85
58
|
throw new Error('Invalid KeycloakModuleAsyncOptions');
|
|
@@ -88,8 +61,5 @@ let KeycloakModule = KeycloakModule_1 = class KeycloakModule {
|
|
|
88
61
|
exports.KeycloakModule = KeycloakModule;
|
|
89
62
|
exports.KeycloakModule = KeycloakModule = KeycloakModule_1 = __decorate([
|
|
90
63
|
(0, common_1.Global)(),
|
|
91
|
-
(0, common_1.Module)({
|
|
92
|
-
providers: [],
|
|
93
|
-
}),
|
|
94
|
-
__metadata("design:paramtypes", [keycloak_service_1.KeycloakService])
|
|
64
|
+
(0, common_1.Module)({})
|
|
95
65
|
], KeycloakModule);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { KeycloakAdminClient } from '@keycloak/keycloak-admin-client/lib/client';
|
|
3
|
+
import { KeycloakModuleOptions } from '../interfaces';
|
|
4
|
+
export declare class KeycloakService extends KeycloakAdminClient implements OnModuleInit {
|
|
5
|
+
private readonly options;
|
|
6
|
+
private readonly logger;
|
|
7
|
+
constructor(options: KeycloakModuleOptions);
|
|
8
|
+
onModuleInit(): Promise<void>;
|
|
9
|
+
private handleToken;
|
|
10
|
+
private getAccessTokenWithPassword;
|
|
11
|
+
private getAccessTokenWithRefreshToken;
|
|
12
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
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
|
+
var KeycloakService_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.KeycloakService = void 0;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const client_1 = require("@keycloak/keycloak-admin-client/lib/client");
|
|
19
|
+
const interfaces_1 = require("../interfaces");
|
|
20
|
+
const jwt_util_1 = require("../utils/jwt.util");
|
|
21
|
+
let KeycloakService = KeycloakService_1 = class KeycloakService extends client_1.KeycloakAdminClient {
|
|
22
|
+
constructor(options) {
|
|
23
|
+
super({
|
|
24
|
+
baseUrl: options.baseUrl,
|
|
25
|
+
realmName: options.realmName,
|
|
26
|
+
});
|
|
27
|
+
this.options = options;
|
|
28
|
+
this.logger = new common_1.Logger(KeycloakService_1.name);
|
|
29
|
+
}
|
|
30
|
+
async onModuleInit() {
|
|
31
|
+
this.logger.debug('Initializing Keycloak client');
|
|
32
|
+
await this.getAccessTokenWithPassword();
|
|
33
|
+
super.registerTokenProvider({
|
|
34
|
+
getAccessToken: async () => {
|
|
35
|
+
await this.handleToken();
|
|
36
|
+
return this.accessToken;
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
async handleToken() {
|
|
41
|
+
const refreshTokenExpireIn = jwt_util_1.JwtUtils.getTokenExpiry(this.refreshToken);
|
|
42
|
+
const expireIn = refreshTokenExpireIn > 15 ? refreshTokenExpireIn - 15 : 0;
|
|
43
|
+
if (expireIn <= 0) {
|
|
44
|
+
await this.getAccessTokenWithPassword();
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
try {
|
|
48
|
+
await this.getAccessTokenWithRefreshToken();
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
this.logger.error(err);
|
|
52
|
+
await this.getAccessTokenWithPassword();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async getAccessTokenWithPassword() {
|
|
57
|
+
await this.auth({
|
|
58
|
+
clientId: this.options.clientId,
|
|
59
|
+
clientSecret: this.options.clientSecret,
|
|
60
|
+
grantType: 'password',
|
|
61
|
+
username: this.options.username,
|
|
62
|
+
password: this.options.password,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
async getAccessTokenWithRefreshToken() {
|
|
66
|
+
await this.auth({
|
|
67
|
+
clientId: this.options.clientId,
|
|
68
|
+
clientSecret: this.options.clientSecret,
|
|
69
|
+
grantType: 'refresh_token',
|
|
70
|
+
refreshToken: this.refreshToken,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
exports.KeycloakService = KeycloakService;
|
|
75
|
+
exports.KeycloakService = KeycloakService = KeycloakService_1 = __decorate([
|
|
76
|
+
(0, common_1.Injectable)(),
|
|
77
|
+
__param(0, (0, common_1.Inject)(interfaces_1.KEYCLOAK_MODULE_OPTIONS)),
|
|
78
|
+
__metadata("design:paramtypes", [Object])
|
|
79
|
+
], KeycloakService);
|