UNPKG

adonis5-swagger

Version:
30 lines (29 loc) 1.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthMiddleware = void 0; class AuthMiddleware { constructor(config, tokenDecoder) { this.config = config; this.tokenDecoder = tokenDecoder; } async handle({ request, response }, next) { const authHeader = request.headers().authorization || ''; const decodeResult = this.tokenDecoder.decode(authHeader); if (!decodeResult.success || !(await this.verifyCredentials(decodeResult.payload))) { return response.status(401).header('WWW-Authenticate', `Basic realm="Swagger docs"`).send({}); } await next(); } async verifyCredentials(userCredentials) { if (!userCredentials) { return false; } const { login, password } = userCredentials; if (typeof this.config.authCheck === 'function') { return this.config.authCheck(login, password); } return (this.config?.authCredentials?.login === login && this.config?.authCredentials?.password === password); } } exports.AuthMiddleware = AuthMiddleware;