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)
    • Constructor Detail

      • ProgrammaticPlainTextAuthProvider

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

        @Deprecated
        public ProgrammaticPlainTextAuthProvider​(@NonNull
                                                 String username,
                                                 @NonNull
                                                 String password,
                                                 @NonNull
                                                 String authorizationId)
        Deprecated.
        Only an empty authorization ID is supported for ordinary authentication.
    • 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

        @Deprecated
        public void setAuthorizationId​(@NonNull
                                       String authorizationId)
        Deprecated.
        Only an empty authorization ID is supported for ordinary authentication.
      • 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.