UNPKG

@antelopejs/auth-jwt

Version:

Authentication and authorization module that implements the Auth interface of antelopejs with JWT

62 lines 3.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Authentication = exports.defaultAuthenticator = exports.AttachAuthDecorator = exports.Sign = exports.Verify = void 0; exports.ValidateRaw = ValidateRaw; exports.SignRaw = SignRaw; exports.defaultSource = defaultSource; exports.CheckAuthentication = CheckAuthentication; exports.SignServerResponse = SignServerResponse; exports.CreateAuthDecorator = CreateAuthDecorator; const _1_1 = require("@ajs/core/1"); const decorators_1 = require("@ajs/core/1/decorators"); const dev_1 = require("@ajs/api/dev"); exports.Verify = (0, _1_1.InterfaceFunction)(); exports.Sign = (0, _1_1.InterfaceFunction)(); exports.AttachAuthDecorator = (0, _1_1.InterfaceFunction)(); function ValidateRaw(token, options) { return (0, exports.Verify)(token, options); } function SignRaw(data, options) { return (0, exports.Sign)(data, options); } function defaultSource(req) { if (req.headers['x-antelopejs-auth']) { return req.headers['x-antelopejs-auth']; } if (req.headers['cookie']) { const match = (' ' + req.headers['cookie']).match(/ ANTELOPEJS_AUTH=([^;]*)/); if (match) { return match[1]; } } return undefined; } exports.defaultAuthenticator = ValidateRaw; async function CheckAuthentication(req, res, source = undefined, authenticator = undefined, authenticatorOptions = undefined, validator = undefined) { const data = await (authenticator || exports.defaultAuthenticator)((source || defaultSource)(req, res), authenticatorOptions); return validator ? await validator(data) : data; } function SignServerResponse(res, data, signOptions, cookieOptions) { const opts = cookieOptions ? Object.keys(cookieOptions) .map((key) => `${key}=${cookieOptions[key]}`) .join('; ') : ''; return SignRaw(data, signOptions).then((data) => res.setHeader('Set-Cookie', `ANTELOPEJS_AUTH=${data}; ${opts}`)); } function CreateAuthDecorator(callbacks) { const source = callbacks.source || defaultSource; const authenticator = callbacks.authenticator || exports.defaultAuthenticator; const authenticatorOptions = callbacks.authenticatorOptions || {}; const defaultValidator = callbacks.validator; return (0, decorators_1.MakeParameterAndMethodAndPropertyAndClassDecorator)((target, key, index, validator) => { if (typeof index === 'object' || key === undefined) { (0, dev_1.SetParameterProvider)(target, Symbol(''), undefined, (context) => CheckAuthentication(context.rawRequest, context.rawResponse, source, authenticator, authenticatorOptions, validator || defaultValidator)); } else { (0, dev_1.SetParameterProvider)(target, key, index, (context) => CheckAuthentication(context.rawRequest, context.rawResponse, source, authenticator, authenticatorOptions, validator || defaultValidator)); } }); } exports.Authentication = CreateAuthDecorator({}); //# sourceMappingURL=index.js.map