thelounge/src/plugins/auth.ts

68 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-05-02 07:56:38 +02:00
import colors from "chalk";
2022-05-05 00:41:57 +02:00
import Client from "../client";
import ClientManager from "../clientManager";
2022-05-02 07:56:38 +02:00
import log from "../log";
2022-05-05 00:41:57 +02:00
export type AuthHandler = (
manager: ClientManager,
client: Client,
user: string,
password: string,
callback: (success: boolean) => void
) => void;
// The order defines priority: the first available plugin is used.
// Always keep 'local' auth plugin at the end of the list; it should always be enabled.
2022-05-03 03:32:20 +02:00
const plugins = [import("./auth/ldap"), import("./auth/local")];
2022-05-02 07:56:38 +02:00
const toExport = {
moduleName: "<module with no name>",
// Must override: implements authentication mechanism
auth: () => unimplemented("auth"),
// Optional to override: implements filter for loading users at start up
// This allows an auth plugin to check if a user is still acceptable, if the plugin
// can do so without access to the user's unhashed password.
// Returning 'false' triggers fallback to default behaviour of loading all users
loadUsers: () => false,
2022-05-30 22:10:28 +02:00
// local auth should always be enabled, but check here to verify
initialized: false,
2022-05-02 07:56:38 +02:00
// TODO: fix typing
2022-05-30 22:10:28 +02:00
async initialize() {
if (toExport.initialized) {
return;
}
2022-05-02 07:56:38 +02:00
2022-05-30 22:10:28 +02:00
// Override default API stubs with exports from first enabled plugin found
const resolvedPlugins = await Promise.all(plugins);
2022-05-30 22:10:28 +02:00
for (const {default: plugin} of resolvedPlugins) {
if (plugin.isEnabled()) {
toExport.initialized = true;
2022-05-30 22:10:28 +02:00
for (const name in plugin) {
toExport[name] = plugin[name];
}
2022-05-30 22:10:28 +02:00
break;
2022-05-03 03:32:20 +02:00
}
2022-05-30 22:10:28 +02:00
}
2022-05-03 03:32:20 +02:00
2022-05-30 22:10:28 +02:00
if (!toExport.initialized) {
log.error("None of the auth plugins is enabled");
2022-05-03 03:32:20 +02:00
}
2022-05-30 22:10:28 +02:00
},
} as any;
function unimplemented(funcName: string) {
log.debug(
`Auth module ${colors.bold(toExport.moduleName)} doesn't implement function ${colors.bold(
funcName
)}`
);
}
2022-05-30 22:10:28 +02:00
// Default API implementations
export default toExport;