UNPKG

embassy

Version:

Simple JSON Web Tokens (JWT) with embedded scopes for services

91 lines 3.88 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.splitCombined = exports.forEachScope = exports.combinedToDomainScopes = void 0; /** * Given an array of combined scope strings in the format `domain|scope` or just * `scope`, will provide a mapping of all domains in the array to the scopes * that appeared for them. Strings with no `domain|` prefix will use the default * "app" scope. * * @param combined - An array of strings in the format `domain|scope`. If the * domain portion is missing, the default scope of "app" will be used. * @returns a map of domains to arrays of scopes within that domain */ function combinedToDomainScopes(combined) { const domainScopes = combined.reduce((acc, str) => { const split = splitCombined(str); if (!acc[split.domain]) acc[split.domain] = []; acc[split.domain].push(split.key); return acc; }, {}); return domainScopes; } exports.combinedToDomainScopes = combinedToDomainScopes; /** * Runs a given function across each provided scope. * * @param domainScopesOrCombined - A DomainScopes map or scopes against which to * execute the function, or an array of combined-format domain|scope strings * @param fn - The function to execute for each domain/scope pair, iteratively. */ function forEachScope(domainScopesOrCombined, fn) { return __awaiter(this, void 0, void 0, function* () { // Convert whatever we have to a DomainScopes map const domainScopes = Array.isArray(domainScopesOrCombined) ? combinedToDomainScopes(domainScopesOrCombined) : domainScopesOrCombined; const domains = Object.keys(domainScopes); let broke = false; const breakFn = () => { broke = true; }; // Iterate through every domain in the map for (const domainIdx in domains) { const domain = domains[domainIdx]; const scopes = domainScopes[domain]; // Iterate through every scope string in that domain's scopes array for (const scopeIdx in scopes) { yield fn(domain, scopes[scopeIdx], breakFn); if (broke) break; } if (broke) break; } }); } exports.forEachScope = forEachScope; /** * Given a string that may have a domain included in the format `domain|key` * or `domain|complex|key`, returns an object that splits out the domain up * to the first `|` symbol. The examples above would result in: * * ```typescript * { domain: 'domain', key: 'key' } // First * { domain: 'domain', key: 'complex|key' } // Second * ``` * * In the event a `|` character is not included, the default domain "app" will * be used. * * @param combined - A string that may have a `domain:` prefix * @returns an object defining the components of the combined string */ function splitCombined(combined) { const match = combined.match(/^([^|]+)\|(.+)$/); if (!match) return { domain: 'app', key: combined }; return { domain: match[1], key: match[2] }; } exports.splitCombined = splitCombined; //# sourceMappingURL=util.js.map