Search in sources :

Example 1 with ClientClusterServiceImpl

use of com.hazelcast.client.spi.impl.ClientClusterServiceImpl in project hazelcast by hazelcast.

the class ClientConnectionManagerImpl method authenticate.

private void authenticate(final Address target, final ClientConnection connection, final boolean asOwner, final AuthenticationFuture callback) {
    SerializationService ss = client.getSerializationService();
    final ClientClusterServiceImpl clusterService = (ClientClusterServiceImpl) client.getClientClusterService();
    final ClientPrincipal principal = clusterService.getPrincipal();
    byte serializationVersion = ((InternalSerializationService) client.getSerializationService()).getVersion();
    String uuid = null;
    String ownerUuid = null;
    if (principal != null) {
        uuid = principal.getUuid();
        ownerUuid = principal.getOwnerUuid();
    }
    ClientMessage clientMessage = encodeAuthenticationRequest(asOwner, ss, serializationVersion, uuid, ownerUuid);
    ClientInvocation clientInvocation = new ClientInvocation(client, clientMessage, connection);
    ClientInvocationFuture future = clientInvocation.invokeUrgent();
    if (asOwner && clientInvocation.getSendConnection() != null) {
        correlationIddOfLastAuthentication.set(clientInvocation.getClientMessage().getCorrelationId());
    }
    future.andThen(new ExecutionCallback<ClientMessage>() {

        @Override
        public void onResponse(ClientMessage response) {
            ClientAuthenticationCodec.ResponseParameters result = ClientAuthenticationCodec.decodeResponse(response);
            AuthenticationStatus authenticationStatus = AuthenticationStatus.getById(result.status);
            switch(authenticationStatus) {
                case AUTHENTICATED:
                    connection.setConnectedServerVersion(result.serverHazelcastVersion);
                    connection.setRemoteEndpoint(result.address);
                    if (asOwner) {
                        if (!(correlationIddOfLastAuthentication.get() == response.getCorrelationId())) {
                            //if not same, client already gave up on this and send another authentication.
                            onFailure(new AuthenticationException("Owner authentication response from address " + target + " is late. Dropping the response. Principal : " + principal));
                            return;
                        }
                        connection.setIsAuthenticatedAsOwner();
                        ClientPrincipal principal = new ClientPrincipal(result.uuid, result.ownerUuid);
                        clusterService.setPrincipal(principal);
                        clusterService.setOwnerConnectionAddress(connection.getEndPoint());
                        logger.info("Setting " + connection + " as owner  with principal " + principal);
                    }
                    onAuthenticated(target, connection);
                    callback.onSuccess(connection, asOwner);
                    break;
                case CREDENTIALS_FAILED:
                    onFailure(new AuthenticationException("Invalid credentials! Principal: " + principal));
                    break;
                default:
                    onFailure(new AuthenticationException("Authentication status code not supported. status: " + authenticationStatus));
            }
        }

        @Override
        public void onFailure(Throwable t) {
            onAuthenticationFailed(target, connection, t);
            callback.onFailure(t);
        }
    });
}
Also used : AuthenticationException(com.hazelcast.client.AuthenticationException) SerializationService(com.hazelcast.spi.serialization.SerializationService) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) ClientPrincipal(com.hazelcast.client.impl.client.ClientPrincipal) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture) AuthenticationStatus(com.hazelcast.client.impl.protocol.AuthenticationStatus) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClientClusterServiceImpl(com.hazelcast.client.spi.impl.ClientClusterServiceImpl)

Aggregations

AuthenticationException (com.hazelcast.client.AuthenticationException)1 ClientPrincipal (com.hazelcast.client.impl.client.ClientPrincipal)1 AuthenticationStatus (com.hazelcast.client.impl.protocol.AuthenticationStatus)1 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)1 ClientClusterServiceImpl (com.hazelcast.client.spi.impl.ClientClusterServiceImpl)1 ClientInvocation (com.hazelcast.client.spi.impl.ClientInvocation)1 ClientInvocationFuture (com.hazelcast.client.spi.impl.ClientInvocationFuture)1 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)1 SerializationService (com.hazelcast.spi.serialization.SerializationService)1