UNPKG

@immobiliarelabs/backstage-plugin-ldap-auth-backend

Version:

Backstage LDAP Authentication plugin, this packages adds backend authentication and token generation/validation/management; sibling of @immobiliarelabs/backstage-plugin-ldap-auth

69 lines (65 loc) 2.17 kB
'use strict'; var errors = require('@backstage/errors'); var errors$1 = require('./errors.cjs.js'); const COOKIE_FIELD_KEY = "backstage-token"; const normalizeTime = (date) => Math.floor(date / 1e3); function parseJwtPayload(token) { try { const [, payload] = token.split("."); return JSON.parse(Buffer.from(payload, "base64").toString()); } catch (_e) { throw new errors.AuthenticationError(errors$1.JWT_INVALID_TOKEN); } } class JWTTokenValidator { store; increaseTokenExpireMs; constructor(store, increaseTokenExpireMs) { this.store = store; this.increaseTokenExpireMs = Number.isNaN(increaseTokenExpireMs || 0) ? 0 : increaseTokenExpireMs || 0; } async logout(jwt, ts) { await this.isValid(jwt); const { sub } = parseJwtPayload(jwt); await this.store.set(sub, ts); } // On logout and refresh set the new invalidBeforeDate for the user async invalidateToken(jwt) { await this.isValid(jwt); const { sub } = parseJwtPayload(jwt); await this.store.set(sub, normalizeTime(Date.now())); } // rejects tokens issued before logouts and refresh async isValid(jwt) { const { sub, iat, exp } = parseJwtPayload(jwt); if (normalizeTime(Date.now()) > exp + normalizeTime(this.increaseTokenExpireMs)) { throw new errors.AuthenticationError(errors$1.JWT_EXPIRED_TOKEN); } if (await this.store.has(sub)) { const invalidBeforeDate = await this.store.get(sub); if (invalidBeforeDate && iat < invalidBeforeDate) { throw new errors.AuthenticationError(errors$1.JWT_EXPIRED_TOKEN); } } return true; } } class TokenValidatorNoop { // On logout and refresh set the new invalidBeforeDate for the user async invalidateToken(_jwt) { return; } async logout(_jwt, _ts) { return; } // rejects tokens issued before logouts and refreshs async isValid(_jwt) { return true; } } exports.COOKIE_FIELD_KEY = COOKIE_FIELD_KEY; exports.JWTTokenValidator = JWTTokenValidator; exports.TokenValidatorNoop = TokenValidatorNoop; exports.normalizeTime = normalizeTime; exports.parseJwtPayload = parseJwtPayload; //# sourceMappingURL=jwt.cjs.js.map