UNPKG

@boostercloud/application-tester

Version:

Contains Booster types related to the information extracted from the user project

40 lines (39 loc) 1.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TokenHelper = void 0; const fs = require("fs"); const jwt = require("jsonwebtoken"); const path = require("path"); /** * This helper will create a valid token using a real private key for testing * The keyset file is expecgted to be located in "<package root>/keys/private.key file" */ class TokenHelper { constructor() { this.privateKey = fs.readFileSync(path.join(__dirname, '..', 'keys', 'private.key')); } forUser(email, role, tokenOptions) { const keyid = 'booster'; const issuer = 'booster'; const options = { algorithm: 'RS256', subject: email, issuer, keyid, }; if ((tokenOptions === null || tokenOptions === void 0 ? void 0 : tokenOptions.expiresIn) !== undefined) { options['expiresIn'] = tokenOptions === null || tokenOptions === void 0 ? void 0 : tokenOptions.expiresIn; } if (tokenOptions === null || tokenOptions === void 0 ? void 0 : tokenOptions.notBefore) { options['notBefore'] = tokenOptions === null || tokenOptions === void 0 ? void 0 : tokenOptions.notBefore; } const payload = { id: email, email, ...tokenOptions === null || tokenOptions === void 0 ? void 0 : tokenOptions.customClaims, }; const rolesClaim = role ? { 'booster:role': role } : {}; return jwt.sign({ ...payload, ...rolesClaim }, this.privateKey, options); } } exports.TokenHelper = TokenHelper;