Files
RSSHub/middleware/auth.js
T
Henry Wang 54d07cb85c Add http basic authentication (#584)
* Add http basic authentication

closes #560

* Add docs

* Add http basic authentication middleware

* Minor update

* Move settings to config.js
2018-08-31 10:46:17 +08:00

32 lines
911 B
JavaScript

const logger = require('../utils/logger');
const Raven = require('raven');
const config = require('../config');
if (config.sentry) {
Raven.config(config.sentry).install();
}
module.exports = async (ctx, next) => {
try {
await next();
} catch (err) {
if (err.status === 401) {
logger.error('Authentication error: ' + (err instanceof Error ? err.stack : err));
ctx.set('WWW-Authenticate', 'Basic');
ctx.body = 'RSSHub: Unauthenticated access.';
ctx.status = 401;
if (config.sentry) {
Raven.captureException(
err,
{
req: ctx.req,
},
function(err, eventId) {
console.log('Authentication error ' + eventId);
}
);
}
}
}
};