UNPKG

@zigatech/keycloak-auth

Version:

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

66 lines (65 loc) 2.75 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.KeycloakConfig = void 0; const common_1 = require("@nestjs/common"); const keycloak_auth_js_1 = require("../constant/keycloak.auth.js"); class KeycloakConfig { constructor(realm, authServerUrl) { Object.defineProperty(this, "realm", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "authServerUrl", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "pubKey", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.realm = realm; this.authServerUrl = authServerUrl; } getRealm() { return this.realm; } getAuthServerUrl() { return this.authServerUrl; } publicKey() { return __awaiter(this, void 0, void 0, function* () { if (this.pubKey) { return this.pubKey; } const CERT_HEADER = "-----BEGIN PUBLIC KEY-----\n"; const CERT_FOOTER = "\n-----END PUBLIC KEY-----"; try { const response = yield fetch(`${this.authServerUrl}/realms/${this.realm}`); const data = yield response.json(); common_1.Logger.debug("Obtained public key for jwt token verification.", keycloak_auth_js_1.KeycloakAuth.NAME); this.pubKey = `${CERT_HEADER}${data["public_key"]}${CERT_FOOTER}`; return this.pubKey; } catch (err) { common_1.Logger.error(`Could not obtain public key for jwt token verification: ${err.message}`, keycloak_auth_js_1.KeycloakAuth.NAME); throw new Error("Could not obtain public key for jwt token verification!"); } }); } } exports.KeycloakConfig = KeycloakConfig;