UNPKG

@zigatech/keycloak-auth

Version:

Keycloak authorization library for NestJS. Works out of the box.

56 lines (55 loc) 2.26 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); 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; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; import { Injectable } from "@nestjs/common"; import { Principal } from "../auth/principal.js"; import { KeycloakAdminClient } from "./adminClient.js"; let KeycloakAuthAdmin = class KeycloakAuthAdmin extends KeycloakAdminClient { userRealmName = "ziga"; constructor(config) { super({ realmName: config.adminRealm, baseUrl: config.baseUrl, }); if (config.userRealm) { this.userRealmName = config.userRealm; } } async findUsersByIds(userIds) { const principals = []; for (const id of userIds) { principals.push((await this.findUserById(id))); } return principals; } async findUserById(userId) { let user = await this.users.findOne({ id: userId, realm: this.userRealmName, }); if (!user) { user = (await this.users.find({ q: `zigaUserId:${userId}`, realm: this.userRealmName, }))[0]; } if (user) { const phone = user?.attributes?.phoneNumber ? user?.attributes?.phoneNumber[0] : undefined; return new Principal(userId, user?.email, user?.emailVerified, user?.lastName, user?.firstName, phone, user?.realmRoles); } return undefined; } }; KeycloakAuthAdmin = __decorate([ Injectable(), __metadata("design:paramtypes", [Object]) ], KeycloakAuthAdmin); export { KeycloakAuthAdmin };