UNPKG

uzen

Version:

General-purpose GraphQL subscription server library

60 lines (59 loc) 1.86 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JwtValidator = void 0; const timeLogger_1 = __importDefault(require("../utils/timeLogger")); function extractToken(headers) { let aT; let rT; const cookieHeader = headers['cookie']; const authorizationHeader = headers['authorization']; if (cookieHeader) { const cookies = cookieHeader.split('; '); const cookieMap = Object.fromEntries(cookies.map((c) => c.split('='))); if (cookieMap.access_token && cookieMap.refresh_token) { aT = cookieMap.access_token; rT = cookieMap.refresh_token; } } if (!aT && authorizationHeader) { const match = authorizationHeader.match(/Bearer\s+(\S+)/); if (match) { aT = match[1]; } } return { aT, rT }; } function isValidAuth(auth) { return Boolean(auth && auth.tenant && auth.id); } const JwtValidator = ({ headers, aTs, rTs, debug = false }) => { const { aT, rT } = extractToken(headers); if (!aT) return; let auth; try { debug && timeLogger_1.default.start('\rrust_jwt.verify: '); auth = aTs.verify(aT); debug && timeLogger_1.default.logTime('\rrust_jwt.verify: '); if (!isValidAuth(auth)) return; return auth; } catch { if (rT) { try { const payload = rTs.verify(rT); debug && console.log('\n\r refresh payload verify: ', payload); auth = payload; return auth; } catch (error) { return; } } } }; exports.JwtValidator = JwtValidator;