hazelcast-client
Version:
Hazelcast - open source In-Memory Data Grid - client for NodeJS
66 lines • 3.39 kB
JavaScript
;
/*
* Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var ClientAuthenticationCodec_1 = require("../codec/ClientAuthenticationCodec");
var ClientAuthenticationCustomCodec_1 = require("../codec/ClientAuthenticationCustomCodec");
var BuildInfoLoader_1 = require("../BuildInfoLoader");
var ConnectionAuthenticator = /** @class */ (function () {
function ConnectionAuthenticator(connection, client) {
this.connection = connection;
this.client = client;
this.clusterService = this.client.getClusterService();
}
ConnectionAuthenticator.prototype.authenticate = function (ownerConnection) {
var _this = this;
var credentials = this.createCredentials(ownerConnection);
return this.client.getInvocationService()
.invokeOnConnection(this.connection, credentials)
.then(function (msg) {
var authResponse = ClientAuthenticationCodec_1.ClientAuthenticationCodec.decodeResponse(msg);
if (authResponse.status === 0) {
_this.connection.address = authResponse.address;
_this.connection.setConnectedServerVersion(authResponse.serverHazelcastVersion);
if (ownerConnection) {
_this.clusterService.uuid = authResponse.uuid;
_this.clusterService.ownerUuid = authResponse.ownerUuid;
}
}
else {
throw new Error('Could not authenticate connection to ' + _this.connection.getAddress().toString());
}
});
};
ConnectionAuthenticator.prototype.createCredentials = function (ownerConnection) {
var groupConfig = this.client.getConfig().groupConfig;
var uuid = this.clusterService.uuid;
var ownerUuid = this.clusterService.ownerUuid;
var customCredentials = this.client.getConfig().customCredentials;
var clientMessage;
var clientVersion = BuildInfoLoader_1.BuildInfoLoader.getClientVersion();
if (customCredentials != null) {
var credentialsPayload = this.client.getSerializationService().toData(customCredentials);
clientMessage = ClientAuthenticationCustomCodec_1.ClientAuthenticationCustomCodec.encodeRequest(credentialsPayload, uuid, ownerUuid, ownerConnection, 'NJS', 1, clientVersion);
}
else {
clientMessage = ClientAuthenticationCodec_1.ClientAuthenticationCodec.encodeRequest(groupConfig.name, groupConfig.password, uuid, ownerUuid, ownerConnection, 'NJS', 1, clientVersion);
}
return clientMessage;
};
return ConnectionAuthenticator;
}());
exports.ConnectionAuthenticator = ConnectionAuthenticator;
//# sourceMappingURL=ConnectionAuthenticator.js.map