2022-06-19 00:25:21 +00:00
|
|
|
import colors from "chalk";
|
|
|
|
import log from "../../log";
|
|
|
|
import Helper from "../../helper";
|
|
|
|
import type {AuthHandler} from "../auth";
|
2017-08-29 16:05:06 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const localAuth: AuthHandler = (manager, client, user, password, callback) => {
|
2017-08-29 16:05:06 +00:00
|
|
|
// If no user is found, or if the client has not provided a password,
|
|
|
|
// fail the authentication straight away
|
|
|
|
if (!client || !password) {
|
|
|
|
return callback(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this user has no password set, fail the authentication
|
|
|
|
if (!client.config.password) {
|
2019-07-17 09:33:59 +00:00
|
|
|
log.error(
|
|
|
|
`User ${colors.bold(
|
|
|
|
user
|
|
|
|
)} with no local password set tried to sign in. (Probably a LDAP user)`
|
|
|
|
);
|
2017-08-29 16:05:06 +00:00
|
|
|
return callback(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Helper.password
|
|
|
|
.compare(password, client.config.password)
|
|
|
|
.then((matching) => {
|
|
|
|
if (matching && Helper.password.requiresUpdate(client.config.password)) {
|
|
|
|
const hash = Helper.password.hash(password);
|
|
|
|
|
|
|
|
client.setPassword(hash, (success) => {
|
|
|
|
if (success) {
|
2019-07-17 09:33:59 +00:00
|
|
|
log.info(
|
|
|
|
`User ${colors.bold(
|
|
|
|
client.name
|
|
|
|
)} logged in and their hashed password has been updated to match new security requirements`
|
|
|
|
);
|
2017-08-29 16:05:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(matching);
|
2019-07-17 09:33:59 +00:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2022-06-19 00:25:21 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
2017-08-29 16:05:06 +00:00
|
|
|
log.error(`Error while checking users password. Error: ${error}`);
|
|
|
|
});
|
2022-06-19 00:25:21 +00:00
|
|
|
};
|
2017-08-29 16:05:06 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
export default {
|
2020-04-22 13:33:35 +00:00
|
|
|
moduleName: "local",
|
2017-08-29 17:11:06 +00:00
|
|
|
auth: localAuth,
|
2017-11-15 06:35:15 +00:00
|
|
|
isEnabled: () => true,
|
2017-08-29 17:11:06 +00:00
|
|
|
};
|