@mondart/nestjs-common-module 1.1.69 → 1.1.71
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/get-agent.decorator.d.ts +1 -0
- package/dist/decorators/get-agent.decorator.js +24 -0
- package/dist/decorators/get-store-id.decorator.d.ts +1 -0
- package/dist/decorators/get-store-id.decorator.js +12 -0
- package/dist/decorators/get-user.decorator.d.ts +1 -0
- package/dist/decorators/get-user.decorator.js +23 -0
- package/dist/decorators/iam-context.decorator.d.ts +1 -0
- package/dist/decorators/iam-context.decorator.js +44 -0
- package/dist/dto/response/iam-context.dto.d.ts +13 -0
- package/dist/dto/response/iam-context.dto.js +71 -0
- package/dist/interfaces/get-agent.interface.d.ts +7 -0
- package/dist/interfaces/get-agent.interface.js +2 -0
- package/dist/interfaces/get-user.interface.d.ts +6 -0
- package/dist/interfaces/get-user.interface.js +2 -0
- 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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const GetAgent: (...dataOrPipes: unknown[]) => ParameterDecorator;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GetAgent = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
exports.GetAgent = (0, common_1.createParamDecorator)((data, ctx) => {
|
|
6
|
+
const request = ctx.switchToHttp().getRequest();
|
|
7
|
+
const injectedPayload = request?.headers?.['injectedpayload'];
|
|
8
|
+
if (injectedPayload) {
|
|
9
|
+
const data = JSON.parse(injectedPayload);
|
|
10
|
+
const token = request?.headers?.['authorization'];
|
|
11
|
+
if (data) {
|
|
12
|
+
if (data?.is_agent)
|
|
13
|
+
request.agent = {
|
|
14
|
+
agentId: data?.sub,
|
|
15
|
+
isAgent: data?.is_agent,
|
|
16
|
+
storeIds: data?.store_ids,
|
|
17
|
+
groupsMembership: data?.groups_membership,
|
|
18
|
+
token,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
delete request?.headers?.['authorization'];
|
|
22
|
+
}
|
|
23
|
+
return request?.agent ? request?.agent : null;
|
|
24
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const GetStoreId: (...dataOrPipes: unknown[]) => ParameterDecorator;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GetStoreId = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
exports.GetStoreId = (0, common_1.createParamDecorator)((data, ctx) => {
|
|
6
|
+
const request = ctx.switchToHttp().getRequest();
|
|
7
|
+
const stringData = request?.headers?.['store_id'];
|
|
8
|
+
if (stringData) {
|
|
9
|
+
return { storeId: Number(stringData) ?? null };
|
|
10
|
+
}
|
|
11
|
+
return { storeId: null };
|
|
12
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const GetUser: (...dataOrPipes: unknown[]) => ParameterDecorator;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GetUser = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
exports.GetUser = (0, common_1.createParamDecorator)((data, ctx) => {
|
|
6
|
+
const request = ctx.switchToHttp().getRequest();
|
|
7
|
+
const injectedPayload = request?.headers?.['injectedpayload'];
|
|
8
|
+
if (injectedPayload) {
|
|
9
|
+
const data = JSON.parse(injectedPayload);
|
|
10
|
+
const token = request?.headers?.['authorization'];
|
|
11
|
+
if (data) {
|
|
12
|
+
if (!data?.is_agent && (data?.contact_id || data?.store_id))
|
|
13
|
+
request.user = {
|
|
14
|
+
contactId: data?.contact_id,
|
|
15
|
+
storeId: data?.store_id,
|
|
16
|
+
isAgent: data?.is_agent ?? false,
|
|
17
|
+
token,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
delete request?.headers?.['authorization'];
|
|
21
|
+
}
|
|
22
|
+
return request?.user ? request?.user : null;
|
|
23
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const IamContext: (...dataOrPipes: unknown[]) => ParameterDecorator;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IamContext = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
const crypto_1 = require("crypto");
|
|
6
|
+
const iam_context_dto_1 = require("../dto/response/iam-context.dto");
|
|
7
|
+
exports.IamContext = (0, common_1.createParamDecorator)((data, ctx) => {
|
|
8
|
+
const request = ctx.switchToHttp().getRequest();
|
|
9
|
+
const iam = new iam_context_dto_1.IamContextDto();
|
|
10
|
+
const injectedPayload = request?.headers?.['injectedpayload'];
|
|
11
|
+
iam.ip =
|
|
12
|
+
request?.headers['x-forwarded-for'] ||
|
|
13
|
+
request?.socket?.remoteAddress ||
|
|
14
|
+
null;
|
|
15
|
+
iam.requestId = request?.headers['request-id'] ?? (0, crypto_1.randomUUID)();
|
|
16
|
+
iam.timestamp = new Date(request?.headers?.['timestamp']);
|
|
17
|
+
if (injectedPayload) {
|
|
18
|
+
const data = JSON.parse(injectedPayload);
|
|
19
|
+
const token = request?.headers?.['authorization'];
|
|
20
|
+
if (data) {
|
|
21
|
+
if (!data?.is_agent && (data?.contact_id || data?.store_id)) {
|
|
22
|
+
iam.user = {
|
|
23
|
+
userId: data?.sub,
|
|
24
|
+
contactId: data?.contact_id,
|
|
25
|
+
storeId: data?.store_id,
|
|
26
|
+
token,
|
|
27
|
+
};
|
|
28
|
+
iam.userId = data?.sub;
|
|
29
|
+
}
|
|
30
|
+
else if (data?.is_agent) {
|
|
31
|
+
iam.agent = {
|
|
32
|
+
agentId: data?.sub,
|
|
33
|
+
isAgent: data?.is_agent,
|
|
34
|
+
storeIds: data?.store_ids,
|
|
35
|
+
groupsMembership: data?.groups_membership,
|
|
36
|
+
token,
|
|
37
|
+
};
|
|
38
|
+
iam.agentId = data?.sub;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
delete request?.headers?.['authorization'];
|
|
42
|
+
}
|
|
43
|
+
return iam;
|
|
44
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { GetUserInterface } from '../../interfaces/get-user.interface';
|
|
2
|
+
import { GetAgentInterface } from '../../interfaces/get-agent.interface';
|
|
3
|
+
export declare class IamContextDto {
|
|
4
|
+
timestamp: Date;
|
|
5
|
+
requestId: string;
|
|
6
|
+
ip?: string;
|
|
7
|
+
user?: GetUserInterface;
|
|
8
|
+
userId: string;
|
|
9
|
+
contactId: number;
|
|
10
|
+
agent?: GetAgentInterface;
|
|
11
|
+
agentId: string;
|
|
12
|
+
constructor(init?: Partial<any>);
|
|
13
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
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.IamContextDto = void 0;
|
|
13
|
+
const swagger_1 = require("@nestjs/swagger");
|
|
14
|
+
const class_validator_1 = require("class-validator");
|
|
15
|
+
const crypto_1 = require("crypto");
|
|
16
|
+
class IamContextDto {
|
|
17
|
+
constructor(init) {
|
|
18
|
+
this.ip = init?.ip;
|
|
19
|
+
this.agent = init?.agent;
|
|
20
|
+
if (init?.agent?.agentId)
|
|
21
|
+
this.agentId = init?.agent?.agentId;
|
|
22
|
+
this.user = init?.user;
|
|
23
|
+
if (init?.user?.contactId)
|
|
24
|
+
this.contactId = init?.user?.contactId;
|
|
25
|
+
if (init?.user?.userId)
|
|
26
|
+
this.userId = init?.user?.userId;
|
|
27
|
+
this.requestId = init?.requestId ?? (0, crypto_1.randomUUID)();
|
|
28
|
+
this.timestamp = init?.timestamp ?? new Date();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
exports.IamContextDto = IamContextDto;
|
|
32
|
+
__decorate([
|
|
33
|
+
(0, swagger_1.ApiResponseProperty)({ type: Date }),
|
|
34
|
+
(0, class_validator_1.IsDate)(),
|
|
35
|
+
__metadata("design:type", Date)
|
|
36
|
+
], IamContextDto.prototype, "timestamp", void 0);
|
|
37
|
+
__decorate([
|
|
38
|
+
(0, swagger_1.ApiResponseProperty)({ type: String }),
|
|
39
|
+
(0, class_validator_1.IsString)(),
|
|
40
|
+
__metadata("design:type", String)
|
|
41
|
+
], IamContextDto.prototype, "requestId", void 0);
|
|
42
|
+
__decorate([
|
|
43
|
+
(0, swagger_1.ApiResponseProperty)({ type: String }),
|
|
44
|
+
(0, class_validator_1.IsString)(),
|
|
45
|
+
__metadata("design:type", String)
|
|
46
|
+
], IamContextDto.prototype, "ip", void 0);
|
|
47
|
+
__decorate([
|
|
48
|
+
(0, swagger_1.ApiResponseProperty)({ type: Object }),
|
|
49
|
+
(0, class_validator_1.IsObject)(),
|
|
50
|
+
__metadata("design:type", Object)
|
|
51
|
+
], IamContextDto.prototype, "user", void 0);
|
|
52
|
+
__decorate([
|
|
53
|
+
(0, swagger_1.ApiResponseProperty)({ type: String }),
|
|
54
|
+
(0, class_validator_1.IsString)(),
|
|
55
|
+
__metadata("design:type", String)
|
|
56
|
+
], IamContextDto.prototype, "userId", void 0);
|
|
57
|
+
__decorate([
|
|
58
|
+
(0, swagger_1.ApiResponseProperty)({ type: Number }),
|
|
59
|
+
(0, class_validator_1.IsNumber)(),
|
|
60
|
+
__metadata("design:type", Number)
|
|
61
|
+
], IamContextDto.prototype, "contactId", void 0);
|
|
62
|
+
__decorate([
|
|
63
|
+
(0, swagger_1.ApiResponseProperty)({ type: Object }),
|
|
64
|
+
(0, class_validator_1.IsObject)(),
|
|
65
|
+
__metadata("design:type", Object)
|
|
66
|
+
], IamContextDto.prototype, "agent", void 0);
|
|
67
|
+
__decorate([
|
|
68
|
+
(0, swagger_1.ApiResponseProperty)({ type: String }),
|
|
69
|
+
(0, class_validator_1.IsString)(),
|
|
70
|
+
__metadata("design:type", String)
|
|
71
|
+
], IamContextDto.prototype, "agentId", void 0);
|
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);
|