diffusion
Version:
Diffusion JavaScript client
354 lines (353 loc) • 15.8 kB
JavaScript
;
/**
* @module Security
*/
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecurityImpl = void 0;
var CommandService = require("./../client/command-service");
var control_group_1 = require("./../control/control-group");
var registration_1 = require("./../control/registration");
var security_script_builder_1 = require("./../features/security/security-script-builder");
var system_authentication_script_builder_1 = require("./../features/security/system-authentication-script-builder");
var security_command_script_1 = require("./../services/authentication/security-command-script");
var change_principal_request_1 = require("./../services/change-principal/change-principal-request");
var Services = require("./../services/security-services");
var authenticator_response_1 = require("./../services/security/authenticator-response");
var logger = require("./../util/logger");
var require_non_null_1 = require("./../util/require-non-null");
var error_reason_1 = require("../../errors/error-reason");
var security_1 = require("../../features/security");
var log = logger.create('Session.Security');
/**
* Implementation of the {@link Security} feature
*/
var SecurityImpl = /** @class */ (function () {
function SecurityImpl(internal) {
/**
* The global permission enum
*/ // eslint-disable-next-line @typescript-eslint/naming-convention
this.GlobalPermission = security_1.GlobalPermission;
/**
* The path permission enum
*/ // eslint-disable-next-line @typescript-eslint/naming-convention
this.PathPermission = security_1.PathPermission;
this.internal = internal;
var serviceLocator = internal.getServiceLocator();
this.CHANGE_PRINCIPAL = serviceLocator.obtain(Services.CHANGE_PRINCIPAL);
this.GET_SECURITY_CONFIGURATION = serviceLocator.obtain(Services.GET_SECURITY_CONFIGURATION);
this.GET_SYSTEM_AUTHENTICATION = serviceLocator.obtain(Services.GET_SYSTEM_AUTHENTICATION);
this.UPDATE_SECURITY_CONFIGURATION = serviceLocator.obtain(Services.UPDATE_SECURITY_CONFIGURATION);
this.UPDATE_SYSTEM_AUTHENTICATION = serviceLocator.obtain(Services.UPDATE_SYSTEM_AUTHENTICATION);
this.AUTHENTICATOR_DEREGISTRATION = serviceLocator.obtain(Services.AUTHENTICATOR_DEREGISTRATION);
this.LIST_GLOBAL_PERMISSIONS = serviceLocator.obtain(Services.LIST_GLOBAL_PERMISSIONS);
this.LIST_PATH_PERMISSIONS = serviceLocator.obtain(Services.LIST_PATH_PERMISSIONS);
internal.getServiceRegistry().add(Services.AUTHENTICATOR, CommandService.create(function (internalSession, req, callback) {
internalSession.getConversationSet().respondIfPresent(req.cid, { req: req, callback: callback });
}));
}
/**
* @inheritdoc
*/
SecurityImpl.prototype.getPrincipal = function () {
return this.internal.getPrincipal();
};
/**
* @inheritdoc
*/
SecurityImpl.prototype.changePrincipal = function (principal, credentials) {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this.internal.checkConnected(reject)) {
log.debug('Changing principal', principal);
_this.CHANGE_PRINCIPAL.send(new change_principal_request_1.ChangePrincipalRequest(principal, credentials), function (err, response) {
if (err) {
reject(err);
}
else {
if (response) {
_this.internal.setPrincipal(principal);
resolve();
}
else {
reject(new Error('Unable to change principal due to authentication failure'));
}
}
});
}
});
};
/**
* @inheritdoc
*/
SecurityImpl.prototype.getSecurityConfiguration = function () {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this.internal.checkConnected(reject)) {
log.debug('Getting security configuration');
_this.GET_SECURITY_CONFIGURATION.send(null, function (err, response) {
if (err) {
reject(err);
}
else {
resolve(response);
}
});
}
});
};
/**
* @inheritdoc
*/
SecurityImpl.prototype.getSystemAuthenticationConfiguration = function () {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this.internal.checkConnected(reject)) {
log.debug('Getting system authentication');
_this.GET_SYSTEM_AUTHENTICATION.send(null, function (err, response) {
if (err) {
reject(err);
}
else {
resolve(response);
}
});
}
});
};
/**
* @inheritdoc
*/
SecurityImpl.prototype.setAuthenticator = function (handlerName, authenticator) {
var _this = this;
return new Promise(function (resolve, reject) {
var closingRegistration = false;
if (_this.internal.checkConnected(reject)) {
try {
require_non_null_1.requireNonNull(handlerName, 'handlerName');
require_non_null_1.requireNonNull(authenticator, 'authenticator');
}
catch (e) {
reject(e);
return;
}
// tslint:disable:strict-type-predicates
if (typeof authenticator.authenticate !== 'function'
|| typeof authenticator.onError !== 'function'
|| typeof authenticator.onClose !== 'function') {
// tslint:enable:strict-type-predicates
reject(new Error('authenticator does not implement the Authenticator interface'));
}
log.debug('Adding authenticator', handlerName);
var params_1 = {
name: handlerName,
definition: Services.AUTHENTICATOR,
group: control_group_1.DEFAULT
};
var adapter = {
active: function (close, cid) {
log.debug('Authenticator active', handlerName);
var registration = {
close: function () {
return new Promise(function (resolveRegistration) {
closingRegistration = true;
_this.AUTHENTICATOR_DEREGISTRATION.send(params_1, function (err, response) {
if (err) {
_this.internal.getConversationSet().discard(cid, err);
log.debug('Error with authenticator deregistration: ', err);
}
else {
_this.internal.getConversationSet().respondIfPresent(cid, response);
authenticator.onClose();
}
resolveRegistration();
});
});
}
};
resolve(registration);
},
respond: function (msg) {
if (closingRegistration) {
return true;
}
var req = msg.req;
var callback = msg.callback;
try {
authenticator.authenticate(req.principal, req.credentials, req.sessionProperties, req.proposedProperties, {
allow: function (properties) {
var e_1, _a;
if (properties) {
properties = Object.assign({}, properties);
try {
for (var _b = __values(Object.getOwnPropertyNames(req.sessionProperties)), _c = _b.next(); !_c.done; _c = _b.next()) {
var key = _c.value;
if (properties[key] === req.sessionProperties[key]) {
delete properties[key];
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
}
callback.respond(new authenticator_response_1.AuthenticatorResponse(authenticator_response_1.TypeCode.ALLOW, properties));
},
abstain: function () {
callback.respond(authenticator_response_1.ABSTAIN);
},
deny: function () {
callback.respond(authenticator_response_1.DENY);
}
});
}
catch (err) {
authenticator.onError(err);
callback.fail(error_reason_1.ErrorReason.CALLBACK_EXCEPTION, err && err.message);
// rethrow so that the conversation can be discarded
throw err;
}
return false;
},
close: function (err) {
if (err) {
log.debug("'Authenticator closed with error for \"" + handlerName + "\"'");
authenticator.onError(err);
}
else {
log.debug("'Authentication Handler closed for \"" + handlerName + "\"'");
authenticator.onClose();
}
}
};
registration_1.registerHandler(_this.internal, params_1, adapter, Services.AUTHENTICATOR_REGISTRATION, Services.AUTHENTICATOR_DEREGISTRATION).then(undefined, function (err) {
reject(err);
});
}
});
};
/**
* Callback for processing the server response from a command script request.
*
* @param err any errors from the server
* @param response the response from the server
* @param resolve a callback to resolve the request
* @param reject a callback to signal an error
*/
SecurityImpl.prototype.updateStoreCallback = function (err, response, resolve, reject) {
if (err) {
reject(err);
}
else if (response.errors.length > 0) {
reject(response.errors);
}
else {
resolve();
}
};
/**
* Send a command script to the server and process the result
*
* @param updater the command service to send the script to
* @param script the command script
* @return the {@link Result} that resolves when the operation
* completes successfully
*/
SecurityImpl.prototype.updateStore = function (updater, script) {
var _this = this;
return new Promise(function (resolve, reject) {
if (script === '') {
resolve();
// tslint:disable-next-line:strict-type-predicates
}
else if (!script || typeof script !== 'string') {
reject(new Error("Invalid argument for script: " + script));
}
else if (_this.internal.checkConnected(reject)) {
updater.send(new security_command_script_1.SecurityCommandScript(script), function (err, response) {
_this.updateStoreCallback(err, response, resolve, reject);
});
}
});
};
/**
* @inheritdoc
*/
SecurityImpl.prototype.updateSecurityStore = function (script) {
log.debug('Updating security store');
return this.updateStore(this.UPDATE_SECURITY_CONFIGURATION, script);
};
/**
* @inheritdoc
*/
SecurityImpl.prototype.updateAuthenticationStore = function (script) {
log.debug('Updating authentication store');
return this.updateStore(this.UPDATE_SYSTEM_AUTHENTICATION, script);
};
/**
* @inheritdoc
*/
SecurityImpl.prototype.securityScriptBuilder = function () {
return new security_script_builder_1.SecurityScriptBuilderImpl();
};
/**
* @inheritdoc
*/
SecurityImpl.prototype.authenticationScriptBuilder = function () {
return new system_authentication_script_builder_1.SystemAuthenticationScriptBuilderImpl();
};
/**
* @inheritdoc
*/
SecurityImpl.prototype.getGlobalPermissions = function () {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this.internal.checkConnected(reject)) {
log.debug('Getting global permissions');
_this.LIST_GLOBAL_PERMISSIONS.send(null, function (err, response) {
if (err) {
reject(err);
}
else {
resolve(response.permissions);
}
});
}
});
};
/**
* @inheritdoc
*/
SecurityImpl.prototype.getPathPermissions = function (path) {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this.internal.checkConnected(reject)) {
log.debug('Getting path permissions');
_this.LIST_PATH_PERMISSIONS.send(path, function (err, response) {
if (err) {
reject(err);
}
else {
resolve(response.permissions);
}
});
}
});
};
return SecurityImpl;
}());
exports.SecurityImpl = SecurityImpl;