2020-04-22 15:33:35 +02:00
|
|
|
"use strict";
|
|
|
|
|
2022-05-02 07:56:38 +02:00
|
|
|
import colors from "chalk";
|
|
|
|
import log from "../log";
|
2020-04-22 15:33:35 +02:00
|
|
|
|
|
|
|
// 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")];
|
2020-04-22 15:33:35 +02:00
|
|
|
|
|
|
|
function unimplemented(funcName) {
|
|
|
|
log.debug(
|
|
|
|
`Auth module ${colors.bold(
|
|
|
|
module.exports.moduleName
|
|
|
|
)} doesn't implement function ${colors.bold(funcName)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-02 07:56:38 +02:00
|
|
|
const toExport = {
|
2020-04-22 15:33:35 +02:00
|
|
|
moduleName: "<module with no name>",
|
|
|
|
|
|
|
|
// Must override: implements authentication mechanism
|
|
|
|
auth: () => unimplemented("auth"),
|
2020-04-22 15:33:09 +02:00
|
|
|
|
|
|
|
// 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-02 07:56:38 +02:00
|
|
|
// TODO: fix typing
|
|
|
|
} as any;
|
|
|
|
|
|
|
|
// Default API implementations
|
|
|
|
export default toExport;
|
2020-04-22 15:33:35 +02:00
|
|
|
|
|
|
|
// local auth should always be enabled, but check here to verify
|
|
|
|
let somethingEnabled = false;
|
|
|
|
// Override default API stubs with exports from first enabled plugin found
|
|
|
|
|
2022-05-03 03:32:20 +02:00
|
|
|
Promise.all(plugins).then((plugins) => {
|
|
|
|
for (const plugin of plugins) {
|
|
|
|
if (plugin.default.isEnabled()) {
|
|
|
|
somethingEnabled = true;
|
2020-04-22 15:33:35 +02:00
|
|
|
|
2022-05-03 03:32:20 +02:00
|
|
|
for (const name in plugin) {
|
|
|
|
toExport[name] = plugin[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2020-04-22 15:33:35 +02:00
|
|
|
}
|
|
|
|
|
2022-05-03 03:32:20 +02:00
|
|
|
if (!somethingEnabled) {
|
|
|
|
log.error("None of the auth plugins is enabled");
|
|
|
|
}
|
|
|
|
});
|