|
| 1 | +/* |
| 2 | + * Copyright DataStax, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.datastax.oss.driver.api.core.auth; |
| 17 | + |
| 18 | +import com.datastax.oss.driver.api.core.metadata.EndPoint; |
| 19 | +import com.datastax.oss.driver.api.core.session.SessionBuilder; |
| 20 | +import com.datastax.oss.driver.internal.core.util.Strings; |
| 21 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 22 | +import java.util.Objects; |
| 23 | +import net.jcip.annotations.ThreadSafe; |
| 24 | + |
| 25 | +/** |
| 26 | + * A simple plaintext {@link AuthProvider} that receives the credentials programmatically instead of |
| 27 | + * pulling them from the configuration. |
| 28 | + * |
| 29 | + * <p>To use this class, create an instance with the appropriate credentials to use and pass it to |
| 30 | + * your session builder: |
| 31 | + * |
| 32 | + * <pre> |
| 33 | + * AuthProvider authProvider = new ProgrammaticPlainTextAuthProvider("...", "..."); |
| 34 | + * CqlSession session = |
| 35 | + * CqlSession.builder() |
| 36 | + * .addContactEndPoints(...) |
| 37 | + * .withAuthProvider(authProvider) |
| 38 | + * .build(); |
| 39 | + * </pre> |
| 40 | + * |
| 41 | + * <p>It also offers the possibility of changing the credentials at runtime. The new credentials |
| 42 | + * will be used for all connections initiated after the change. |
| 43 | + * |
| 44 | + * <p>Implementation Note: this implementation is not particularly suited for highly-sensitive |
| 45 | + * applications: it stores the credentials to use as private fields, and even if the fields are char |
| 46 | + * arrays rather than strings to make it difficult to dump their contents, they are never cleared |
| 47 | + * until the provider itself is garbage-collected, which typically only happens when the session is |
| 48 | + * closed. |
| 49 | + * |
| 50 | + * @see SessionBuilder#withAuthProvider(AuthProvider) |
| 51 | + * @see SessionBuilder#withAuthCredentials(String, String) |
| 52 | + * @see SessionBuilder#withAuthCredentials(String, String, String) |
| 53 | + */ |
| 54 | +@ThreadSafe |
| 55 | +public class ProgrammaticPlainTextAuthProvider extends PlainTextAuthProviderBase { |
| 56 | + |
| 57 | + private volatile char[] username; |
| 58 | + private volatile char[] password; |
| 59 | + private volatile char[] authorizationId; |
| 60 | + |
| 61 | + /** Builds an instance for simple username/password authentication. */ |
| 62 | + public ProgrammaticPlainTextAuthProvider(@NonNull String username, @NonNull String password) { |
| 63 | + this(username, password, ""); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Builds an instance for username/password authentication, and proxy authentication with the |
| 68 | + * given authorizationId. |
| 69 | + * |
| 70 | + * <p>This feature is only available with Datastax Enterprise. If the target server is Apache |
| 71 | + * Cassandra, use {@link #ProgrammaticPlainTextAuthProvider(String, String)} instead, or set the |
| 72 | + * authorizationId to an empty string. |
| 73 | + */ |
| 74 | + public ProgrammaticPlainTextAuthProvider( |
| 75 | + @NonNull String username, @NonNull String password, @NonNull String authorizationId) { |
| 76 | + // This will typically be built before the session so we don't know the log prefix yet. Pass an |
| 77 | + // empty string, it's only used in one log message. |
| 78 | + super(""); |
| 79 | + this.username = Strings.requireNotEmpty(username, "username").toCharArray(); |
| 80 | + this.password = Strings.requireNotEmpty(password, "password").toCharArray(); |
| 81 | + this.authorizationId = |
| 82 | + Objects.requireNonNull(authorizationId, "authorizationId cannot be null").toCharArray(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Changes the username. |
| 87 | + * |
| 88 | + * <p>The new credentials will be used for all connections initiated after this method was called. |
| 89 | + * |
| 90 | + * @param username the new name. |
| 91 | + */ |
| 92 | + public void setUsername(@NonNull String username) { |
| 93 | + this.username = Strings.requireNotEmpty(username, "username").toCharArray(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Changes the password. |
| 98 | + * |
| 99 | + * <p>The new credentials will be used for all connections initiated after this method was called. |
| 100 | + * |
| 101 | + * @param password the new password. |
| 102 | + */ |
| 103 | + public void setPassword(@NonNull String password) { |
| 104 | + this.password = Strings.requireNotEmpty(password, "password").toCharArray(); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Changes the authorization id. |
| 109 | + * |
| 110 | + * <p>The new credentials will be used for all connections initiated after this method was called. |
| 111 | + * |
| 112 | + * <p>This feature is only available with Datastax Enterprise. If the target server is Apache |
| 113 | + * Cassandra, this method should not be used. |
| 114 | + * |
| 115 | + * @param authorizationId the new authorization id. |
| 116 | + */ |
| 117 | + public void setAuthorizationId(@NonNull String authorizationId) { |
| 118 | + this.authorizationId = |
| 119 | + Objects.requireNonNull(authorizationId, "authorizationId cannot be null").toCharArray(); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * {@inheritDoc} |
| 124 | + * |
| 125 | + * <p>This implementation disregards the endpoint being connected to as well as the authenticator |
| 126 | + * class sent by the server, and always returns the same credentials. |
| 127 | + */ |
| 128 | + @NonNull |
| 129 | + @Override |
| 130 | + protected Credentials getCredentials( |
| 131 | + @NonNull EndPoint endPoint, @NonNull String serverAuthenticator) { |
| 132 | + return new Credentials(username.clone(), password.clone(), authorizationId.clone()); |
| 133 | + } |
| 134 | +} |
0 commit comments