All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datastax.driver.core.PlainTextAuthProvider Maven / Gradle / Ivy

Go to download

A driver for DataStax Enterprise (DSE) and Apache Cassandra 1.2+ clusters that works exclusively with the Cassandra Query Language version 3 (CQL3) and Cassandra's binary protocol, supporting DSE-specific features such as geospatial types, DSE Graph and DSE authentication.

There is a newer version: 2.4.0
Show newest version
/*
 * Copyright DataStax, Inc.
 *
 * This software can be used solely with DataStax Enterprise. Please consult the license at
 * http://www.datastax.com/terms/datastax-dse-driver-license-terms
 */
package com.datastax.driver.core;

import com.datastax.driver.core.exceptions.AuthenticationException;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import java.net.InetSocketAddress;
import java.util.Map;

/**
 * A simple {@code AuthProvider} implementation.
 *
 * 

This provider allows to programmatically define authentication information that will then * apply to all hosts. The PlainTextAuthenticator instances it returns support SASL authentication * using the PLAIN mechanism for version 2 (or above) of the CQL native protocol. */ public class PlainTextAuthProvider implements ExtendedAuthProvider { private volatile String username; private volatile String password; /** * Creates a new simple authentication information provider with the supplied credentials. * * @param username to use for authentication requests * @param password to use for authentication requests */ public PlainTextAuthProvider(String username, String password) { this.username = username; this.password = password; } /** * Changes the user name. * *

The new credentials will be used for all connections initiated after this method was called. * * @param username the new name. */ public void setUsername(String username) { this.username = username; } /** * Changes the password. * *

The new credentials will be used for all connections initiated after this method was called. * * @param password the new password. */ public void setPassword(String password) { this.password = password; } /** * Uses the supplied credentials and the SASL PLAIN mechanism to login to the server. * * @param host the Cassandra host with which we want to authenticate * @param authenticator the configured authenticator on the host * @return an Authenticator instance which can be used to perform authentication negotiations on * behalf of the client */ @Override public Authenticator newAuthenticator(EndPoint host, String authenticator) { return new PlainTextAuthenticator(username, password); } @Override public Authenticator newAuthenticator(InetSocketAddress host, String authenticator) throws AuthenticationException { throw new AssertionError( "The driver should never call this method on an object that implements ExtendedAuthProvider"); } /** * Simple implementation of {@link Authenticator} which can perform authentication against * Cassandra servers configured with PasswordAuthenticator. */ static class PlainTextAuthenticator extends ProtocolV1Authenticator implements Authenticator { private final byte[] username; private final byte[] password; public PlainTextAuthenticator(String username, String password) { this.username = username.getBytes(Charsets.UTF_8); this.password = password.getBytes(Charsets.UTF_8); } @Override public byte[] initialResponse() { byte[] initialToken = new byte[username.length + password.length + 2]; initialToken[0] = 0; System.arraycopy(username, 0, initialToken, 1, username.length); initialToken[username.length + 1] = 0; System.arraycopy(password, 0, initialToken, username.length + 2, password.length); return initialToken; } @Override public byte[] evaluateChallenge(byte[] challenge) { return null; } @Override public void onAuthenticationSuccess(byte[] token) { // no-op, the server should send nothing anyway } @Override Map getCredentials() { return ImmutableMap.of( "username", new String(username, Charsets.UTF_8), "password", new String(password, Charsets.UTF_8)); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy