UNPKG

@saleor/app-sdk

Version:

SDK for building great Saleor Apps

125 lines (119 loc) 3.86 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } var _chunk3OYW6U6Kjs = require('../../chunk-3OYW6U6K.js'); require('../../chunk-DE4A7PET.js'); // src/APL/env/jwks-cache.ts var debug = _chunk3OYW6U6Kjs.createAPLDebug.call(void 0, "EnvAPL:jwks-cache"); var JwksCache = class { set(jwks, ttlMs) { this.entry = { jwks, expiresAt: Date.now() + ttlMs }; debug("Stored jwks in cache, expires at %d", this.entry.expiresAt); } get() { if (!this.entry) { debug("Cache miss - no entry"); return void 0; } if (Date.now() >= this.entry.expiresAt) { debug("Cache miss - entry expired"); this.entry = void 0; return void 0; } debug("Cache hit"); return this.entry.jwks; } clear() { debug("Cache cleared"); this.entry = void 0; } }; var jwksCache = new JwksCache(); // src/APL/env/env-apl.ts var debug2 = _chunk3OYW6U6Kjs.createAPLDebug.call(void 0, "EnvAPL"); var DEFAULT_CACHE_TTL_MS = 5 * 60 * 1e3; var EnvAPL = class { constructor(options) { this.defaultOptions = { printAuthDataOnRegister: false, cacheTtlMs: DEFAULT_CACHE_TTL_MS }; if (!this.isAuthDataValid(options.env)) { console.warn( 'EnvAPL constructor not filled with valid AuthData config. Try to install the app with "printAuthDataOnRegister" enabled and check console logs' ); } this.options = { ...this.defaultOptions, ...options }; } isAuthDataValid(authData) { const keysToValidateAgainst = ["appId", "saleorApiUrl", "token"]; return keysToValidateAgainst.every( (key) => authData[key] && typeof authData[key] === "string" && authData[key].length > 0 ); } buildAuthData() { const cachedJwks = jwksCache.get(); if (cachedJwks !== void 0) { return { ...this.options.env, jwks: cachedJwks }; } return { ...this.options.env }; } async isReady() { return this.isAuthDataValid(this.options.env) ? { ready: true } : { ready: false, error: new Error("Auth data not valid, check constructor and pass env variables") }; } /** * Always return its configured, because otherwise .set() will never be called * so env can't be printed */ async isConfigured() { return { configured: true }; } async set(authData) { if (this.options.printAuthDataOnRegister) { console.log("Displaying registration values for the app. Use them to configure EnvAPL"); console.log(JSON.stringify(authData, null, 2)); console.warn( "\u{1F6D1}'printAuthDataOnRegister' option should be turned off once APL is configured, to avoid possible leaks" ); } if (typeof authData.jwks === "string" && authData.jwks.length > 0) { const ttlMs = _nullishCoalesce(this.options.cacheTtlMs, () => ( DEFAULT_CACHE_TTL_MS)); jwksCache.set(authData.jwks, ttlMs); } debug2("Called set method"); } async get(saleorApiUrl) { if (!this.isAuthDataValid(this.options.env)) { debug2("Trying to get AuthData but APL constructor was not filled with proper AuthData"); return void 0; } if (saleorApiUrl !== this.options.env.saleorApiUrl) { throw new Error( `Requested AuthData for domain "${saleorApiUrl}", however APL is configured for ${this.options.env.saleorApiUrl}. You may trying to install app in invalid Saleor URL ` ); } return this.buildAuthData(); } async getAll() { if (!this.isAuthDataValid(this.options.env)) { return []; } return [this.buildAuthData()]; } async delete(saleorApiUrl) { jwksCache.clear(); debug2("Called delete method for %s", saleorApiUrl); } }; exports.EnvAPL = EnvAPL;