@mondart/nestjs-common-module 1.1.68 → 1.1.70
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/validations/is-one-of-property.decorator.js +2 -1
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.js +1 -0
- package/dist/lib/keycloak/index.d.ts +3 -0
- package/dist/lib/keycloak/index.js +19 -0
- package/dist/lib/keycloak/interfaces/index.d.ts +1 -0
- package/dist/lib/keycloak/interfaces/index.js +17 -0
- package/dist/lib/keycloak/interfaces/keycloak.interface.d.ts +23 -0
- package/dist/lib/keycloak/interfaces/keycloak.interface.js +10 -0
- package/dist/lib/keycloak/keycloak.module.d.ts +12 -0
- package/dist/lib/keycloak/keycloak.module.js +95 -0
- package/dist/lib/keycloak/keycloak.service.d.ts +14 -0
- package/dist/lib/keycloak/keycloak.service.js +85 -0
- package/dist/lib/keycloak/utils/jwt.util.d.ts +3 -0
- package/dist/lib/keycloak/utils/jwt.util.js +24 -0
- package/dist/lib/saga/types/index.d.ts +1 -0
- package/dist/lib/saga/types/index.js +1 -0
- package/dist/lib/saga/types/saga-step-class.type.d.ts +4 -0
- package/dist/lib/saga/types/saga-step-class.type.js +8 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -1
|
@@ -13,7 +13,8 @@ function IsOnlyOneOf(relatedProperty, validationOptions) {
|
|
|
13
13
|
validator: {
|
|
14
14
|
validate(value, args) {
|
|
15
15
|
const relatedValue = args.object[args.constraints[0]];
|
|
16
|
-
return (value
|
|
16
|
+
return ((!(value === undefined || value === null) && !relatedValue) ||
|
|
17
|
+
((value === undefined || value === null) && relatedValue));
|
|
17
18
|
},
|
|
18
19
|
defaultMessage(args) {
|
|
19
20
|
return `You can only provide either ${args.property} or ${args.constraints[0]}, but not both.`;
|
package/dist/lib/index.d.ts
CHANGED
package/dist/lib/index.js
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./keycloak.module"), exports);
|
|
18
|
+
__exportStar(require("./keycloak.service"), exports);
|
|
19
|
+
__exportStar(require("./interfaces"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './keycloak.interface';
|
|
@@ -0,0 +1,17 @@
|
|
|
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./keycloak.interface"), exports);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ModuleMetadata, Type } from '@nestjs/common';
|
|
2
|
+
import { Credentials } from '@keycloak/keycloak-admin-client/lib/utils/auth';
|
|
3
|
+
import UserRepresentation from '@keycloak/keycloak-admin-client/lib/defs/userRepresentation';
|
|
4
|
+
export declare const KEYCLOAK_MODULE_OPTIONS = "KEYCLOAK_MODULE_OPTIONS";
|
|
5
|
+
export interface KeycloakModuleOptions extends Credentials {
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
realmName?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface KeycloakModuleFactory {
|
|
10
|
+
createModuleOptions: () => Promise<KeycloakModuleOptions> | KeycloakModuleOptions;
|
|
11
|
+
}
|
|
12
|
+
export interface KeycloakModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
13
|
+
inject?: any[];
|
|
14
|
+
useClass?: Type<KeycloakModuleFactory>;
|
|
15
|
+
useExisting?: Type<KeycloakModuleFactory>;
|
|
16
|
+
useFactory?: (...args: any[]) => Promise<KeycloakModuleOptions> | KeycloakModuleOptions;
|
|
17
|
+
}
|
|
18
|
+
export declare enum KeycloakGrantType {
|
|
19
|
+
CLIENT_CREDENTIALS = "client_credentials",
|
|
20
|
+
PASSWORD = "password",
|
|
21
|
+
REFRESH_TOKEN = "refresh_token"
|
|
22
|
+
}
|
|
23
|
+
export type KeycloakUserRepresentation = UserRepresentation;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KeycloakGrantType = exports.KEYCLOAK_MODULE_OPTIONS = void 0;
|
|
4
|
+
exports.KEYCLOAK_MODULE_OPTIONS = 'KEYCLOAK_MODULE_OPTIONS';
|
|
5
|
+
var KeycloakGrantType;
|
|
6
|
+
(function (KeycloakGrantType) {
|
|
7
|
+
KeycloakGrantType["CLIENT_CREDENTIALS"] = "client_credentials";
|
|
8
|
+
KeycloakGrantType["PASSWORD"] = "password";
|
|
9
|
+
KeycloakGrantType["REFRESH_TOKEN"] = "refresh_token";
|
|
10
|
+
})(KeycloakGrantType || (exports.KeycloakGrantType = KeycloakGrantType = {}));
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { DynamicModule, OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { KeycloakService } from './keycloak.service';
|
|
3
|
+
import { KeycloakModuleAsyncOptions, KeycloakModuleOptions } from './interfaces';
|
|
4
|
+
export declare class KeycloakModule implements OnModuleInit {
|
|
5
|
+
private readonly service;
|
|
6
|
+
constructor(service: KeycloakService);
|
|
7
|
+
onModuleInit(): Promise<void>;
|
|
8
|
+
static register(options: KeycloakModuleOptions): DynamicModule;
|
|
9
|
+
static registerAsync(options: KeycloakModuleAsyncOptions): DynamicModule;
|
|
10
|
+
private static createAsyncOptionsProvider;
|
|
11
|
+
private static createAsyncProviders;
|
|
12
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
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 KeycloakModule_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.KeycloakModule = void 0;
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const keycloak_service_1 = require("./keycloak.service");
|
|
16
|
+
const interfaces_1 = require("./interfaces");
|
|
17
|
+
let KeycloakModule = KeycloakModule_1 = class KeycloakModule {
|
|
18
|
+
constructor(service) {
|
|
19
|
+
this.service = service;
|
|
20
|
+
}
|
|
21
|
+
async onModuleInit() {
|
|
22
|
+
await this.service.init();
|
|
23
|
+
}
|
|
24
|
+
static register(options) {
|
|
25
|
+
return {
|
|
26
|
+
module: KeycloakModule_1,
|
|
27
|
+
imports: [],
|
|
28
|
+
providers: [
|
|
29
|
+
{
|
|
30
|
+
provide: interfaces_1.KEYCLOAK_MODULE_OPTIONS,
|
|
31
|
+
useValue: options,
|
|
32
|
+
},
|
|
33
|
+
keycloak_service_1.KeycloakService,
|
|
34
|
+
],
|
|
35
|
+
exports: [keycloak_service_1.KeycloakService, KeycloakModule_1],
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
static registerAsync(options) {
|
|
39
|
+
const asyncOptionsProvider = this.createAsyncOptionsProvider(options);
|
|
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);
|
|
51
|
+
return {
|
|
52
|
+
module: KeycloakModule_1,
|
|
53
|
+
providers,
|
|
54
|
+
exports: [interfaces_1.KEYCLOAK_MODULE_OPTIONS],
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
static createAsyncProviders(options) {
|
|
58
|
+
if (options.useFactory) {
|
|
59
|
+
return [
|
|
60
|
+
{
|
|
61
|
+
provide: interfaces_1.KEYCLOAK_MODULE_OPTIONS,
|
|
62
|
+
useFactory: options.useFactory,
|
|
63
|
+
inject: options.inject || [],
|
|
64
|
+
},
|
|
65
|
+
];
|
|
66
|
+
}
|
|
67
|
+
const useClass = options.useClass || options.useExisting;
|
|
68
|
+
if (useClass) {
|
|
69
|
+
return [
|
|
70
|
+
{
|
|
71
|
+
provide: interfaces_1.KEYCLOAK_MODULE_OPTIONS,
|
|
72
|
+
useFactory: async (optionsFactory) => await optionsFactory.createModuleOptions(),
|
|
73
|
+
inject: [options.useExisting || options.useClass],
|
|
74
|
+
},
|
|
75
|
+
...(options.useClass
|
|
76
|
+
? [
|
|
77
|
+
{
|
|
78
|
+
provide: options.useClass,
|
|
79
|
+
useClass: options.useClass,
|
|
80
|
+
},
|
|
81
|
+
]
|
|
82
|
+
: []),
|
|
83
|
+
];
|
|
84
|
+
}
|
|
85
|
+
throw new Error('Invalid KeycloakModuleAsyncOptions');
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
exports.KeycloakModule = KeycloakModule;
|
|
89
|
+
exports.KeycloakModule = KeycloakModule = KeycloakModule_1 = __decorate([
|
|
90
|
+
(0, common_1.Global)(),
|
|
91
|
+
(0, common_1.Module)({
|
|
92
|
+
providers: [],
|
|
93
|
+
}),
|
|
94
|
+
__metadata("design:paramtypes", [keycloak_service_1.KeycloakService])
|
|
95
|
+
], KeycloakModule);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { KeycloakAdminClient } from '@keycloak/keycloak-admin-client/lib/client';
|
|
2
|
+
import { KeycloakModuleOptions } from './interfaces';
|
|
3
|
+
export declare class KeycloakService {
|
|
4
|
+
private readonly options;
|
|
5
|
+
kc: KeycloakAdminClient;
|
|
6
|
+
private readonly logger;
|
|
7
|
+
private accessTokenExpireIn;
|
|
8
|
+
constructor(options: KeycloakModuleOptions);
|
|
9
|
+
init(): Promise<void>;
|
|
10
|
+
private authenticate;
|
|
11
|
+
private getAccessTokenWithRefreshToken;
|
|
12
|
+
private getAccessToken;
|
|
13
|
+
private importKC;
|
|
14
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
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 interfaces_1 = require("./interfaces");
|
|
19
|
+
const jwt_util_1 = require("./utils/jwt.util");
|
|
20
|
+
let KeycloakService = KeycloakService_1 = class KeycloakService {
|
|
21
|
+
constructor(options) {
|
|
22
|
+
this.options = options;
|
|
23
|
+
this.logger = new common_1.Logger(KeycloakService_1.name);
|
|
24
|
+
}
|
|
25
|
+
async init() {
|
|
26
|
+
const KcAdminClient = await this.importKC();
|
|
27
|
+
if (!this.kc) {
|
|
28
|
+
this.logger.debug(`start initiating`);
|
|
29
|
+
this.kc = new KcAdminClient();
|
|
30
|
+
}
|
|
31
|
+
this.kc.setConfig({
|
|
32
|
+
baseUrl: this.options.baseUrl,
|
|
33
|
+
});
|
|
34
|
+
await this.authenticate();
|
|
35
|
+
}
|
|
36
|
+
async authenticate() {
|
|
37
|
+
await this.getAccessToken();
|
|
38
|
+
const accessTokenInterval = setInterval(async () => {
|
|
39
|
+
const refreshTokenExpireIn = jwt_util_1.JwtUtils.getTokenExpiry(this.kc.refreshToken);
|
|
40
|
+
const expireIn = refreshTokenExpireIn > 15 ? refreshTokenExpireIn - 15 : 0;
|
|
41
|
+
if (expireIn <= 0) {
|
|
42
|
+
await this.getAccessToken();
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
await this.getAccessTokenWithRefreshToken().catch(async (err) => {
|
|
46
|
+
this.logger.error(err);
|
|
47
|
+
await this.getAccessToken().catch((err) => {
|
|
48
|
+
this.logger.error(err);
|
|
49
|
+
clearInterval(accessTokenInterval);
|
|
50
|
+
throw err;
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}, (this.accessTokenExpireIn - 15) * 1000);
|
|
55
|
+
}
|
|
56
|
+
async getAccessTokenWithRefreshToken() {
|
|
57
|
+
await this.kc.auth({
|
|
58
|
+
clientId: this.options?.clientId,
|
|
59
|
+
grantType: 'refresh_token',
|
|
60
|
+
clientSecret: this.options?.clientSecret,
|
|
61
|
+
refreshToken: this.kc.refreshToken,
|
|
62
|
+
});
|
|
63
|
+
this.accessTokenExpireIn = jwt_util_1.JwtUtils.getTokenExpiry(this.kc.accessToken);
|
|
64
|
+
}
|
|
65
|
+
async getAccessToken() {
|
|
66
|
+
await this.kc.auth({
|
|
67
|
+
clientId: this.options?.clientId,
|
|
68
|
+
clientSecret: this.options?.clientSecret,
|
|
69
|
+
grantType: 'password',
|
|
70
|
+
username: this.options?.username,
|
|
71
|
+
password: this.options?.password,
|
|
72
|
+
});
|
|
73
|
+
this.accessTokenExpireIn = jwt_util_1.JwtUtils.getTokenExpiry(this.kc.accessToken);
|
|
74
|
+
}
|
|
75
|
+
async importKC() {
|
|
76
|
+
const { default: KeycloakAdminClient } = await eval(`import('@keycloak/keycloak-admin-client')`);
|
|
77
|
+
return KeycloakAdminClient;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
exports.KeycloakService = KeycloakService;
|
|
81
|
+
exports.KeycloakService = KeycloakService = KeycloakService_1 = __decorate([
|
|
82
|
+
(0, common_1.Injectable)(),
|
|
83
|
+
__param(0, (0, common_1.Inject)(interfaces_1.KEYCLOAK_MODULE_OPTIONS)),
|
|
84
|
+
__metadata("design:paramtypes", [Object])
|
|
85
|
+
], KeycloakService);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JwtUtils = void 0;
|
|
4
|
+
class JwtUtils {
|
|
5
|
+
static getTokenExpiry(token) {
|
|
6
|
+
try {
|
|
7
|
+
const payload = token.split('.')[1];
|
|
8
|
+
if (!payload)
|
|
9
|
+
throw new Error('Invalid JWT structure');
|
|
10
|
+
const decoded = JSON.parse(Buffer.from(payload, 'base64').toString('utf-8'));
|
|
11
|
+
if (!decoded.exp) {
|
|
12
|
+
throw new Error('Missing "exp" claim in JWT');
|
|
13
|
+
}
|
|
14
|
+
const currentTime = Math.floor(Date.now() / 1000);
|
|
15
|
+
const expiresIn = decoded.exp - currentTime;
|
|
16
|
+
return expiresIn > 0 ? expiresIn : 0;
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
console.error('Error decoding JWT:', error.message);
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.JwtUtils = JwtUtils;
|
|
@@ -20,3 +20,4 @@ __exportStar(require("./next-options.type"), exports);
|
|
|
20
20
|
__exportStar(require("./condition.type"), exports);
|
|
21
21
|
__exportStar(require("./builder-method-return-result.type"), exports);
|
|
22
22
|
__exportStar(require("./builder-method-step-result.type"), exports);
|
|
23
|
+
__exportStar(require("./saga-step-class.type"), exports);
|