@mcma/client
Version:
Node module with classes and functions used to access services in an MCMA environment
37 lines (36 loc) • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthProvider = void 0;
const core_1 = require("@mcma/core");
class AuthProvider {
registeredAuthTypes = {};
add(authTypeOrRegistration, authenticator) {
let authType;
if (typeof authTypeOrRegistration === "string") {
if (Object.keys(this.registeredAuthTypes).find((k) => k.toLowerCase() === authTypeOrRegistration.toLowerCase())) {
throw new core_1.McmaException("Auth type '" + authTypeOrRegistration + "' has already been registered.");
}
if (typeof authenticator?.sign !== "function") {
throw new core_1.McmaException("authenticator must have a sign function.");
}
authType = authTypeOrRegistration;
}
else {
authType = authTypeOrRegistration.authType;
authenticator = authTypeOrRegistration.authenticator;
}
this.registeredAuthTypes[authType] = authenticator;
return this;
}
get(authType) {
authType = Object.keys(this.registeredAuthTypes).find((k) => k.toLowerCase() === (authType || "").toLowerCase());
return authType && this.registeredAuthTypes[authType] && this.registeredAuthTypes[authType];
}
getDefault() {
if (Object.keys(this.registeredAuthTypes).length === 1) {
return this.registeredAuthTypes[Object.keys(this.registeredAuthTypes)[0]];
}
return null;
}
}
exports.AuthProvider = AuthProvider;