| 1 | /* |
| 2 | * Copyright (C) 2012-2015 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.driver.auth; |
| 17 | |
| 18 | import com.datastax.driver.core.Authenticator; |
| 19 | |
| 20 | /** |
| 21 | * Base class for {@link Authenticator} implementations that want to make use of |
| 22 | * the authentication scheme negotiation in the DseAuthenticator |
| 23 | */ |
| 24 | abstract class BaseDseAuthenticator implements Authenticator { |
| 25 | private static final String DSE_AUTHENTICATOR = "com.datastax.bdp.cassandra.auth.DseAuthenticator"; |
| 26 | private final String authenticator; |
| 27 | |
| 28 | protected BaseDseAuthenticator(String authenticator) { |
| 29 | this.authenticator = authenticator; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Return a byte array containing the required SASL mechanism. |
| 34 | * This should be one of: |
| 35 | * <ul> |
| 36 | * <li>"PLAIN".getBytes(Charsets.UTF_8);</li> |
| 37 | * <li>"GSSAPI".getBytes(Charsets.UTF_8);</li> |
| 38 | * </ul> |
| 39 | * |
| 40 | * @return a byte array containing the SASL mechanism |
| 41 | */ |
| 42 | public abstract byte[] getMechanism(); |
| 43 | |
| 44 | /** |
| 45 | * Return a byte array containing the expected successful server challenge. |
| 46 | * This should be one of: |
| 47 | * <ul> |
| 48 | * <li>"PLAIN-START".getBytes(Charsets.UTF_8);</li> |
| 49 | * <li>"GSSAPI-START".getBytes(Charsets.UTF_8);</li> |
| 50 | * </ul> |
| 51 | * |
| 52 | * @return a byte array containing the server challenge |
| 53 | */ |
| 54 | public abstract byte[] getInitialServerChallenge(); |
| 55 | |
| 56 | public byte[] initialResponse() { |
| 57 | if (isDseAuthenticator()) |
| 58 | return getMechanism(); |
| 59 | else |
| 60 | return evaluateChallenge(getInitialServerChallenge()); |
| 61 | } |
| 62 | |
| 63 | public void onAuthenticationSuccess(byte[] token) { |
| 64 | } |
| 65 | |
| 66 | private boolean isDseAuthenticator() { |
| 67 | return authenticator.equals(DSE_AUTHENTICATOR); |
| 68 | } |
| 69 | } |