Elffuss BraidHub

kikocis/java-driver public

cdd9bb755010 · 5.6 KiBRawPermalink
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package com.datastax.driver.auth;
19
20import org.slf4j.Logger;
21import org.slf4j.LoggerFactory;
22
23import javax.security.auth.Subject;
24import javax.security.auth.callback.CallbackHandler;
25import javax.security.sasl.Sasl;
26import javax.security.sasl.SaslClient;
27import javax.security.sasl.SaslException;
28import java.security.PrivilegedAction;
29import java.util.HashMap;
30import java.util.Map;
31
32/**
33 * Wrapper around {@code SaslClient}, which performs necessary parts of
34 * the authentication protocol as {@code PrivilegedAction}s with the
35 * assumed identity of the {@code Subject} supplied in the constructor.
36 */
37public class PrivilegedSaslClient
38{
39 private static final Logger logger = LoggerFactory.getLogger(PrivilegedSaslClient.class);
40
41 public static final Map<String, String> DEFAULT_PROPERTIES = new HashMap<String, String>()
42 {{
43 put(Sasl.SERVER_AUTH, "true");
44 put(Sasl.QOP, "auth");
45 }};
46 private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
47
48 /**
49 * Used to respond to server challenges with SASL tokens
50 * represented as byte arrays.
51 */
52 private final SaslClient saslClient;
53
54 /**
55 * The identity we intend to use when we authenticate with the server
56 */
57 private Subject clientIdentity;
58
59 public PrivilegedSaslClient(final Subject clientIdentity,
60 final String[] mechanisms,
61 final String authzId,
62 final String protocol,
63 final String hostname,
64 final Map<String, String> properties,
65 final CallbackHandler callbackHandler)
66 {
67 logger.trace("Initalising SASL client");
68 this.clientIdentity = clientIdentity;
69 saslClient = Subject.doAs(this.clientIdentity, new PrivilegedAction<SaslClient>()
70 {
71 @Override
72 public javax.security.sasl.SaslClient run()
73 {
74 try
75 {
76 return Sasl.createSaslClient(
77 mechanisms,
78 authzId,
79 protocol,
80 hostname,
81 properties,
82 callbackHandler);
83 } catch (Exception e)
84 {
85 logger.error("Error initialising SASL client", e);
86 throw new RuntimeException(e);
87 }
88 }
89 });
90 logger.trace("SASL client initialised");
91 }
92
93
94 /**
95 * Generate an initial token to start the SASL handshake with server.
96 * @return SaslTokenRequestMessage message to be sent to server.
97 */
98 public byte[] getInitialResponse()
99 {
100 logger.trace("Initialising SASL handshake from client");
101 byte[] initialResponse = null;
102 if (saslClient.hasInitialResponse())
103 {
104 initialResponse = Subject.doAs(clientIdentity, new PrivilegedAction<byte[]>()
105 {
106 @Override
107 public byte[] run()
108 {
109 try
110 {
111 return saslClient.evaluateChallenge(EMPTY_BYTE_ARRAY);
112 } catch (Exception e)
113 {
114 throw new RuntimeException(e);
115 }
116 }
117 });
118 }
119 return initialResponse;
120 }
121
122 /**
123 * Respond to server's SASL token
124 * @param challenge contains server's SASL token
125 * @return client's response SASL token, which will be null if authentication is complete
126 */
127 public byte[] evaluateChallenge(final byte[] challenge)
128 {
129 logger.trace("Evaluating SASL server token");
130 if (saslClient.isComplete())
131 {
132 return null;
133 }
134 byte[] retval = Subject.doAs(clientIdentity, new PrivilegedAction<byte[]>()
135 {
136 @Override
137 public byte[] run()
138 {
139 try
140 {
141 logger.trace("SASL client evaluating challenge");
142 byte[] retval = saslClient.evaluateChallenge(challenge);
143 logger.trace("SASL client is complete = " + saslClient.isComplete());
144 return retval;
145 }
146 catch (SaslException e)
147 {
148 logger.error("Failed to generate response to SASL server's token:", e);
149 throw new RuntimeException(e);
150 }
151 }
152 });
153 return retval;
154 }
155
156 /**
157 * Is the SASL negotiation with the server complete
158 * @return true iff SASL authentication has successfully completed
159 */
160 public boolean isComplete()
161 {
162 return saslClient.isComplete();
163 }
164}