mirror of
https://github.com/thelounge/thelounge.git
synced 2026-03-14 14:35:50 +01:00
feat(auth): add LocalAuthProvider class to local.ts
This commit is contained in:
parent
f6ef500d79
commit
3846d9c821
1 changed files with 76 additions and 0 deletions
|
|
@ -2,6 +2,17 @@ import colors from "chalk";
|
|||
import log from "../../log";
|
||||
import Helper from "../../helper";
|
||||
import type {AuthHandler} from "../auth";
|
||||
import type {
|
||||
AuthProvider,
|
||||
AuthenticateParams,
|
||||
AuthResult,
|
||||
AuthStartParams,
|
||||
AuthStartInfo,
|
||||
DisconnectParams,
|
||||
GetValidUsersParams,
|
||||
LogoutParams,
|
||||
LogoutInfo,
|
||||
} from "./types";
|
||||
|
||||
const localAuth: AuthHandler = (_manager, client, user, password, callback) => {
|
||||
// If no user is found, or if the client has not provided a password,
|
||||
|
|
@ -44,6 +55,71 @@ const localAuth: AuthHandler = (_manager, client, user, password, callback) => {
|
|||
});
|
||||
};
|
||||
|
||||
export class LocalAuthProvider implements AuthProvider {
|
||||
name = "local";
|
||||
canChangePassword = true;
|
||||
|
||||
async init(): Promise<void> {}
|
||||
|
||||
async getValidUsers({users}: GetValidUsersParams): Promise<string[]> {
|
||||
return users;
|
||||
}
|
||||
|
||||
authStart(_params: AuthStartParams): AuthStartInfo {
|
||||
return {};
|
||||
}
|
||||
|
||||
async authenticate({client, username, password}: AuthenticateParams): Promise<AuthResult> {
|
||||
if (!client || !password) {
|
||||
return {success: false};
|
||||
}
|
||||
|
||||
if (!client.config.password) {
|
||||
log.error(
|
||||
`User ${colors.bold(
|
||||
username
|
||||
)} with no local password set tried to sign in. (Probably a LDAP user)`
|
||||
);
|
||||
return {success: false};
|
||||
}
|
||||
|
||||
let matching: boolean;
|
||||
|
||||
try {
|
||||
matching = await Helper.password.compare(password, client.config.password);
|
||||
} catch (error) {
|
||||
log.error(`Error while checking users password. Error: ${error}`);
|
||||
return {success: false};
|
||||
}
|
||||
|
||||
if (!matching) {
|
||||
return {success: false};
|
||||
}
|
||||
|
||||
if (Helper.password.requiresUpdate(client.config.password)) {
|
||||
const hash = Helper.password.hash(password);
|
||||
|
||||
client.setPassword(hash, (success) => {
|
||||
if (success) {
|
||||
log.info(
|
||||
`User ${colors.bold(
|
||||
client.name
|
||||
)} logged in and their hashed password has been updated to match new security requirements`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return {success: true, username};
|
||||
}
|
||||
|
||||
logout(_params: LogoutParams): LogoutInfo {
|
||||
return {};
|
||||
}
|
||||
|
||||
disconnect(_params: DisconnectParams): void {}
|
||||
}
|
||||
|
||||
export default {
|
||||
moduleName: "local",
|
||||
auth: localAuth,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue