@mondart/nestjs-common-module 2.6.8 → 2.7.5

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.
@@ -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, OnModuleInit } from '@nestjs/common';
2
- import { KeycloakService } from './keycloak.service';
1
+ import { DynamicModule } from '@nestjs/common';
3
2
  import { KeycloakModuleAsyncOptions, KeycloakModuleOptions } from './interfaces';
4
- export declare class KeycloakModule implements OnModuleInit {
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, KeycloakModule_1],
25
+ exports: [keycloak_service_1.KeycloakService],
36
26
  };
37
27
  }
38
28
  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);
29
+ const asyncProviders = this.createAsyncProviders(options);
51
30
  return {
52
31
  module: KeycloakModule_1,
53
- providers,
54
- exports: [interfaces_1.KEYCLOAK_MODULE_OPTIONS],
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 (optionsFactory) => await optionsFactory.createModuleOptions(),
73
- inject: [options.useExisting || options.useClass],
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,29 @@
1
+ import { OnModuleInit } from '@nestjs/common';
2
+ import { KeycloakModuleOptions } from '../interfaces';
3
+ export declare class KeycloakService implements OnModuleInit {
4
+ private readonly options;
5
+ private readonly logger;
6
+ private kc;
7
+ constructor(options: KeycloakModuleOptions);
8
+ onModuleInit(): Promise<void>;
9
+ get users(): import("@keycloak/keycloak-admin-client/lib/resources/users").Users;
10
+ get realms(): import("@keycloak/keycloak-admin-client/lib/resources/realms").Realms;
11
+ get clients(): import("@keycloak/keycloak-admin-client/lib/resources/clients").Clients;
12
+ get roles(): import("@keycloak/keycloak-admin-client/lib/resources/roles").Roles;
13
+ get groups(): import("@keycloak/keycloak-admin-client/lib/resources/groups").Groups;
14
+ get organizations(): import("@keycloak/keycloak-admin-client/lib/resources/organizations").Organizations;
15
+ get userStorageProvider(): import("@keycloak/keycloak-admin-client/lib/resources/userStorageProvider").UserStorageProvider;
16
+ get clientScopes(): import("@keycloak/keycloak-admin-client/lib/resources/clientScopes").ClientScopes;
17
+ get clientPolicies(): import("@keycloak/keycloak-admin-client/lib/resources/clientPolicies").ClientPolicies;
18
+ get identityProviders(): import("@keycloak/keycloak-admin-client/lib/resources/identityProviders").IdentityProviders;
19
+ get components(): import("@keycloak/keycloak-admin-client/lib/resources/components").Components;
20
+ get serverInfo(): import("@keycloak/keycloak-admin-client/lib/resources/serverInfo").ServerInfo;
21
+ get whoAmI(): import("@keycloak/keycloak-admin-client/lib/resources/whoAmI").WhoAmI;
22
+ get attackDetection(): import("@keycloak/keycloak-admin-client/lib/resources/attackDetection").AttackDetection;
23
+ get authenticationManagement(): import("@keycloak/keycloak-admin-client/lib/resources/authenticationManagement").AuthenticationManagement;
24
+ get cache(): import("@keycloak/keycloak-admin-client/lib/resources/cache").Cache;
25
+ private handleToken;
26
+ private getAccessTokenWithPassword;
27
+ private getAccessTokenWithRefreshToken;
28
+ private importKC;
29
+ }
@@ -0,0 +1,137 @@
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 onModuleInit() {
26
+ const KcAdminClient = await this.importKC();
27
+ if (!this.kc) {
28
+ this.logger.debug(`start initiating`);
29
+ this.kc = new KcAdminClient({
30
+ baseUrl: this.options.baseUrl,
31
+ realmName: this.options.realmName,
32
+ });
33
+ }
34
+ this.kc.setConfig({
35
+ baseUrl: this.options.baseUrl,
36
+ realmName: this.options.realmName,
37
+ });
38
+ await this.getAccessTokenWithPassword();
39
+ this.kc.registerTokenProvider({
40
+ getAccessToken: async () => {
41
+ await this.handleToken();
42
+ return this.kc.accessToken;
43
+ },
44
+ });
45
+ }
46
+ get users() {
47
+ return this.kc.users;
48
+ }
49
+ get realms() {
50
+ return this.kc.realms;
51
+ }
52
+ get clients() {
53
+ return this.kc.clients;
54
+ }
55
+ get roles() {
56
+ return this.kc.roles;
57
+ }
58
+ get groups() {
59
+ return this.kc.groups;
60
+ }
61
+ get organizations() {
62
+ return this.kc.organizations;
63
+ }
64
+ get userStorageProvider() {
65
+ return this.kc.userStorageProvider;
66
+ }
67
+ get clientScopes() {
68
+ return this.kc.clientScopes;
69
+ }
70
+ get clientPolicies() {
71
+ return this.kc.clientPolicies;
72
+ }
73
+ get identityProviders() {
74
+ return this.kc.identityProviders;
75
+ }
76
+ get components() {
77
+ return this.kc.components;
78
+ }
79
+ get serverInfo() {
80
+ return this.kc.serverInfo;
81
+ }
82
+ get whoAmI() {
83
+ return this.kc.whoAmI;
84
+ }
85
+ get attackDetection() {
86
+ return this.kc.attackDetection;
87
+ }
88
+ get authenticationManagement() {
89
+ return this.kc.authenticationManagement;
90
+ }
91
+ get cache() {
92
+ return this.kc.cache;
93
+ }
94
+ async handleToken() {
95
+ const refreshTokenExpireIn = jwt_util_1.JwtUtils.getTokenExpiry(this.kc.refreshToken);
96
+ const expireIn = refreshTokenExpireIn > 15 ? refreshTokenExpireIn - 15 : 0;
97
+ if (expireIn <= 0) {
98
+ await this.getAccessTokenWithPassword();
99
+ }
100
+ else {
101
+ try {
102
+ await this.getAccessTokenWithRefreshToken();
103
+ }
104
+ catch (err) {
105
+ this.logger.error(err);
106
+ await this.getAccessTokenWithPassword();
107
+ }
108
+ }
109
+ }
110
+ async getAccessTokenWithPassword() {
111
+ await this.kc.auth({
112
+ clientId: this.options.clientId,
113
+ clientSecret: this.options.clientSecret,
114
+ grantType: 'password',
115
+ username: this.options.username,
116
+ password: this.options.password,
117
+ });
118
+ }
119
+ async getAccessTokenWithRefreshToken() {
120
+ await this.kc.auth({
121
+ clientId: this.options.clientId,
122
+ clientSecret: this.options.clientSecret,
123
+ grantType: 'refresh_token',
124
+ refreshToken: this.kc.refreshToken,
125
+ });
126
+ }
127
+ async importKC() {
128
+ const { default: KeycloakAdminClient } = await eval(`import('@keycloak/keycloak-admin-client')`);
129
+ return KeycloakAdminClient;
130
+ }
131
+ };
132
+ exports.KeycloakService = KeycloakService;
133
+ exports.KeycloakService = KeycloakService = KeycloakService_1 = __decorate([
134
+ (0, common_1.Injectable)(),
135
+ __param(0, (0, common_1.Inject)(interfaces_1.KEYCLOAK_MODULE_OPTIONS)),
136
+ __metadata("design:paramtypes", [Object])
137
+ ], KeycloakService);
@@ -4,6 +4,8 @@ exports.JwtUtils = void 0;
4
4
  class JwtUtils {
5
5
  static getTokenExpiry(token) {
6
6
  try {
7
+ if (!token)
8
+ return null;
7
9
  const payload = token.split('.')[1];
8
10
  if (!payload)
9
11
  throw new Error('Invalid JWT structure');