mirror of
https://github.com/louislam/uptime-kuma.git
synced 2025-07-23 23:11:13 -04:00
Add Basic Auth for /metrics
This commit is contained in:
parent
cc6f1d7487
commit
209fa83cff
6 changed files with 141 additions and 47 deletions
40
server/auth.js
Normal file
40
server/auth.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
const basicAuth = require('express-basic-auth')
|
||||
const passwordHash = require('./password-hash');
|
||||
const {R} = require("redbean-node");
|
||||
|
||||
/**
|
||||
*
|
||||
* @param username : string
|
||||
* @param password : string
|
||||
* @returns {Promise<Bean|null>}
|
||||
*/
|
||||
exports.login = async function (username, password) {
|
||||
let user = await R.findOne("user", " username = ? AND active = 1 ", [
|
||||
username
|
||||
])
|
||||
|
||||
if (user && passwordHash.verify(password, user.password)) {
|
||||
// Upgrade the hash to bcrypt
|
||||
if (passwordHash.needRehash(user.password)) {
|
||||
await R.exec("UPDATE `user` SET password = ? WHERE id = ? ", [
|
||||
passwordHash.generate(password),
|
||||
user.id
|
||||
]);
|
||||
}
|
||||
return user;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function myAuthorizer(username, password, callback) {
|
||||
exports.login(username, password).then((user) => {
|
||||
callback(null, user != null)
|
||||
})
|
||||
}
|
||||
|
||||
exports.basicAuth = basicAuth({
|
||||
authorizer: myAuthorizer,
|
||||
authorizeAsync: true,
|
||||
challenge: true
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue