use of javax.security.sasl.SaslClient in project drill by apache.
the class AuthenticationOutcomeListener method initiate.
public void initiate(final String mechanismName) {
logger.trace("Initiating SASL exchange.");
try {
final ByteString responseData;
final SaslClient saslClient = connection.getSaslClient();
if (saslClient.hasInitialResponse()) {
responseData = ByteString.copyFrom(evaluateChallenge(ugi, saslClient, new byte[0]));
} else {
responseData = ByteString.EMPTY;
}
client.send(new AuthenticationOutcomeListener<>(client, connection, saslRpcType, ugi, completionListener), connection, saslRpcType, SaslMessage.newBuilder().setMechanism(mechanismName).setStatus(SaslStatus.SASL_START).setData(responseData).build(), SaslMessage.class, true);
logger.trace("Initiated SASL exchange.");
} catch (final Exception e) {
completionListener.failed(RpcException.mapException(e));
}
}
use of javax.security.sasl.SaslClient in project drill by apache.
the class KerberosFactory method createSaslClient.
@Override
public SaslClient createSaslClient(final UserGroupInformation ugi, final Map<String, ?> properties) throws SaslException {
final String servicePrincipal = getServicePrincipal(properties);
final String[] parts = KerberosUtil.splitPrincipalIntoParts(servicePrincipal);
final String serviceName = parts[0];
final String serviceHostName = parts[1];
final String qopValue = properties.containsKey(Sasl.QOP) ? properties.get(Sasl.QOP).toString() : "auth";
// ignore parts[2]; GSSAPI gets the realm info from the ticket
try {
final SaslClient saslClient = ugi.doAs(new PrivilegedExceptionAction<SaslClient>() {
@Override
public SaslClient run() throws Exception {
return FastSaslClientFactory.getInstance().createSaslClient(new String[] { KerberosUtil.KERBEROS_SASL_NAME }, null, /** authorization ID */
serviceName, serviceHostName, properties, new CallbackHandler() {
@Override
public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException {
throw new UnsupportedCallbackException(callbacks[0]);
}
});
}
});
logger.debug("GSSAPI SaslClient created to authenticate to {} running on {} with QOP value {}", serviceName, serviceHostName, qopValue);
return saslClient;
} catch (final UndeclaredThrowableException e) {
logger.debug("Authentication failed.", e);
throw new SaslException(String.format("Unexpected failure trying to authenticate to %s using GSSAPI with QOP %s", serviceHostName, qopValue), e.getCause());
} catch (final IOException | InterruptedException e) {
logger.debug("Authentication failed.", e);
if (e instanceof SaslException) {
throw (SaslException) e;
}
throw new SaslException(String.format("Unexpected failure trying to authenticate to %s using GSSAPI with QOP %s", serviceHostName, qopValue), e);
}
}
use of javax.security.sasl.SaslClient in project jdk8u_jdk by JetBrains.
the class SampleCallbackHandler method main.
public static void main(String[] args) throws Exception {
Map<String, String> props = new TreeMap<String, String>();
props.put(Sasl.QOP, "auth");
// client
SaslClient client = Sasl.createSaslClient(new String[] { DIGEST_MD5 }, "user1", "xmpp", "127.0.0.1", props, authCallbackHandler);
if (client == null) {
throw new Exception("Unable to find client implementation for: " + DIGEST_MD5);
}
byte[] response = client.hasInitialResponse() ? client.evaluateChallenge(EMPTY) : EMPTY;
logger.info("initial: " + new String(response));
// server
byte[] challenge = null;
SaslServer server = Sasl.createSaslServer(DIGEST_MD5, "xmpp", "127.0.0.1", props, authCallbackHandler);
if (server == null) {
throw new Exception("Unable to find server implementation for: " + DIGEST_MD5);
}
if (!client.isComplete() || !server.isComplete()) {
challenge = server.evaluateResponse(response);
logger.info("challenge: " + new String(challenge));
if (challenge != null) {
response = client.evaluateChallenge(challenge);
}
}
String challengeString = new String(challenge, "UTF-8").toLowerCase();
if (challengeString.indexOf("\"md5-sess\"") > 0 || challengeString.indexOf("\"utf-8\"") > 0) {
throw new Exception("The challenge string's charset and " + "algorithm values must not be enclosed within quotes");
}
client.dispose();
server.dispose();
}
Aggregations