Elffuss BraidHub

kikocis/java-driver public

21aa364a9b4a · 3.4 KiBRawPermalink
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 */
16package com.datastax.driver.auth;
17
18import com.datastax.driver.core.AuthProvider;
19import com.datastax.driver.core.Authenticator;
20import com.datastax.driver.core.exceptions.AuthenticationException;
21import com.google.common.base.Charsets;
22
23import java.net.InetSocketAddress;
24import java.util.Arrays;
25
26import static com.google.common.base.Preconditions.checkNotNull;
27
28/**
29 * AuthProvider that provides plain text authenticator instances for clients to connect
30 * to DSE clusters secured with the DseAuthenticator.
31 * <p/>
32 * To create a cluster using this auth provider:
33 * <pre>
34 * Cluster cluster = Cluster.builder()
35 * .addContactPoint(hostname)
36 * .withAuthProvider(new DsePlainTextAuthProvider("username", "password"))
37 * .build();
38 * </pre>
39 */
40public class DsePlainTextAuthProvider implements AuthProvider {
41 private final String username;
42 private final String password;
43
44 public DsePlainTextAuthProvider(String username, String password) {
45 checkNotNull(username, "username cannot be null");
46 checkNotNull(password, "password cannot be null");
47 this.username = username;
48 this.password = password;
49 }
50
51 public Authenticator newAuthenticator(InetSocketAddress host, String authenticator) throws AuthenticationException {
52 return new PlainTextAuthenticator(authenticator, username, password);
53 }
54
55 private static class PlainTextAuthenticator extends BaseDseAuthenticator {
56 private static final byte[] MECHANISM = "PLAIN".getBytes(Charsets.UTF_8);
57 private static final byte[] SERVER_INITIAL_CHALLENGE = "PLAIN-START".getBytes(Charsets.UTF_8);
58 private final byte[] username;
59 private final byte[] password;
60
61 public PlainTextAuthenticator(String authenticator, String username, String password) {
62 super(authenticator);
63 this.username = username.getBytes(Charsets.UTF_8);
64 this.password = password.getBytes(Charsets.UTF_8);
65 }
66
67 public byte[] getMechanism() {
68 return MECHANISM.clone();
69 }
70
71 public byte[] getInitialServerChallenge() {
72 return SERVER_INITIAL_CHALLENGE.clone();
73 }
74
75 public byte[] evaluateChallenge(byte[] challenge) {
76 if (Arrays.equals(SERVER_INITIAL_CHALLENGE, challenge)) {
77 byte[] token = new byte[username.length + password.length + 2];
78 token[0] = 0;
79 System.arraycopy(username, 0, token, 1, username.length);
80 token[username.length + 1] = 0;
81 System.arraycopy(password, 0, token, username.length + 2, password.length);
82 return token;
83 }
84 throw new RuntimeException("Incorrect challenge from server");
85 }
86 }
87}