UNPKG

@ragnaraven/zitadel-node-dual

Version:

Library for API access to ZITADEL with modern ES import syntax. Works everywhere - NestJS, Node.js, any TypeScript environment. No more require()!

130 lines 4.5 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var application_exports = {}; __export(application_exports, { Application: () => Application }); module.exports = __toCommonJS(application_exports); var import_jose = require("jose"); var import_node_rsa = __toESM(require("node-rsa")); class Application { /** * Create a new application * * @param appId The ID of the application. * @param clientId The client ID of the application. * @param keyId The ID of the RSA key. * @param key The private RSA key of the application. Used to sign the JWT. * * @throws {Error} If the clientId is not defined. * @throws {Error} If the keyId is not defined. * @throws {Error} If the key is not defined. */ constructor(appId, clientId, keyId, key) { this.appId = appId; this.clientId = clientId; this.keyId = keyId; this.key = key; if (!appId) { throw new Error("appId is required"); } if (!clientId) { throw new Error("clientId is required"); } if (!keyId) { throw new Error("keyId is required"); } if (!key) { throw new Error("key is required"); } } /** * The type of the object. */ static type = "application"; /** * Create an application from a JSON object. * * @param json The JSON object. * @returns An application. * * @throws {Error} If the constructor throws an error. * @throws {Error} If the passed JSON cannot be properly destructed. */ static fromJson({ appId, clientId, key, keyId }) { return new Application(appId, clientId, keyId, key); } /** * Create an application from a JSON string. * The string is parsed using `JSON.parse`. * * @param jsonString The JSON string. * @returns An application. * * @throws {Error} If the constructor throws an error. * @throws {Error} If the passed JSON cannot be properly parsed. * @throws {Error} If the passed JSON cannot be properly destructed. */ static fromJsonString(jsonString) { return Application.fromJson(JSON.parse(jsonString)); } /** * Create a JSON object from the application. * * @returns A JSON object. */ toJson() { return { type: Application.type, appId: this.appId, clientId: this.clientId, keyId: this.keyId, key: this.key }; } /** * Create and sign a JWT token for the given audience. * * The JWT is signed by the RSA key of the application. * The JWT is valid for one hour. * * @param audience The audience to use in the JWT. * @returns A signed JWT. */ async getSignedJwt(audience) { const rsa = new import_node_rsa.default(this.key); const key = await (0, import_jose.importPKCS8)(rsa.exportKey("pkcs8-private-pem"), "RSA256"); return await new import_jose.SignJWT({}).setProtectedHeader({ kid: this.keyId, alg: "RS256" }).setIssuedAt().setExpirationTime("1h").setAudience(audience).setIssuer(this.clientId).setSubject(this.clientId).sign(key); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Application }); //# sourceMappingURL=application.js.map