Class ProgrammaticPlainTextAuthProvider

  • All Implemented Interfaces:
    AuthProvider, AutoCloseable

    @ThreadSafe
    public class ProgrammaticPlainTextAuthProvider
    extends PlainTextAuthProviderBase
    A simple plaintext AuthProvider that receives the credentials programmatically instead of pulling them from the configuration.

    To use this class, create an instance with the appropriate credentials to use and pass it to your session builder:

     AuthProvider authProvider = new ProgrammaticPlainTextAuthProvider("...", "...");
     CqlSession session =
         CqlSession.builder()
             .addContactEndPoints(...)
             .withAuthProvider(authProvider)
             .build();
     

    It also offers the possibility of changing the credentials at runtime. The new credentials will be used for all connections initiated after the change.

    Implementation Note: this implementation is not particularly suited for highly-sensitive applications: it stores the credentials to use as private fields, and even if the fields are char arrays rather than strings to make it difficult to dump their contents, they are never cleared until the provider itself is garbage-collected, which typically only happens when the session is closed.

    See Also:
    SessionBuilder.withAuthProvider(AuthProvider), SessionBuilder.withAuthCredentials(String, String), SessionBuilder.withAuthCredentials(String, String, String)
    • Constructor Detail

      • ProgrammaticPlainTextAuthProvider

        public ProgrammaticPlainTextAuthProvider​(@NonNull
                                                 String username,
                                                 @NonNull
                                                 String password)
        Builds an instance for simple username/password authentication.
      • ProgrammaticPlainTextAuthProvider

        public ProgrammaticPlainTextAuthProvider​(@NonNull
                                                 String username,
                                                 @NonNull
                                                 String password,
                                                 @NonNull
                                                 String authorizationId)
        Builds an instance for username/password authentication, and proxy authentication with the given authorizationId.

        This feature is only available with DataStax Enterprise. If the target server is Apache Cassandra, use ProgrammaticPlainTextAuthProvider(String, String) instead, or set the authorizationId to an empty string.

    • Method Detail

      • setUsername

        public void setUsername​(@NonNull
                                String username)
        Changes the username.

        The new credentials will be used for all connections initiated after this method was called.

        Parameters:
        username - the new name.
      • setPassword

        public void setPassword​(@NonNull
                                String password)
        Changes the password.

        The new credentials will be used for all connections initiated after this method was called.

        Parameters:
        password - the new password.
      • setAuthorizationId

        public void setAuthorizationId​(@NonNull
                                       String authorizationId)
        Changes the authorization id.

        The new credentials will be used for all connections initiated after this method was called.

        This feature is only available with DataStax Enterprise. If the target server is Apache Cassandra, this method should not be used.

        Parameters:
        authorizationId - the new authorization id.
      • getCredentials

        @NonNull
        protected PlainTextAuthProviderBase.Credentials getCredentials​(@NonNull
                                                                       EndPoint endPoint,
                                                                       @NonNull
                                                                       String serverAuthenticator)
        Retrieves the credentials from the underlying source.

        This is invoked every time the driver opens a new connection.

        This implementation disregards the endpoint being connected to as well as the authenticator class sent by the server, and always returns the same credentials.

        Specified by:
        getCredentials in class PlainTextAuthProviderBase
        Parameters:
        endPoint - The endpoint being contacted.
        serverAuthenticator - The authenticator class sent by the endpoint.