UNPKG

@piggly/fastify-chassis

Version:

An ESM/CommonJS toolkit to help you to do common operations in your back-end applications with Fastify and NodeJS.

38 lines 1.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BasicAuthMiddleware = void 0; const index_js_1 = require("../events/index.js"); const index_js_2 = require("../errors/index.js"); const index_js_3 = require("../utils/index.js"); /** * BasicAuthMiddleware is a middleware that checks if the request * is authenticated using Basic Authentication. * * If request is not authenticated, it will: * - Publish UnauthorizedAccessEvent. * - Return UnauthorizedError. * * @param {Object} expected - The expected username and password. * @param {string} expected.username - The expected username. * @param {string} expected.password - The expected password. * @returns The middleware function. * @since 1.0.0 * @author Caique Araujo <caique@piggly.com.br> */ const BasicAuthMiddleware = (expected) => (request, reply, done) => { const auth = (0, index_js_3.getBasicToken)(request.headers); if (auth === undefined) { index_js_1.UnauthorizedAccessEvent.publish(request); done(new index_js_2.UnauthorizedError()); return; } const { password, username } = auth; if (username !== expected.username || password !== expected.password) { index_js_1.UnauthorizedAccessEvent.publish(request); done(new index_js_2.UnauthorizedError()); return; } done(); }; exports.BasicAuthMiddleware = BasicAuthMiddleware; //# sourceMappingURL=BasicAuthMiddleware.js.map