本文整理匯總了Java中javax.security.sasl.SaslClient.evaluateChallenge方法的典型用法代碼示例。如果您正苦於以下問題:Java SaslClient.evaluateChallenge方法的具體用法?Java SaslClient.evaluateChallenge怎麽用?Java SaslClient.evaluateChallenge使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.security.sasl.SaslClient
的用法示例。
在下文中一共展示了SaslClient.evaluateChallenge方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: runNegotiation
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
private void runNegotiation(CallbackHandler clientCbh,
CallbackHandler serverCbh)
throws SaslException {
String mechanism = AuthMethod.PLAIN.getMechanismName();
SaslClient saslClient = Sasl.createSaslClient(
new String[]{ mechanism }, null, null, null, null, clientCbh);
assertNotNull(saslClient);
SaslServer saslServer = Sasl.createSaslServer(
mechanism, null, "localhost", null, serverCbh);
assertNotNull("failed to find PLAIN server", saslServer);
byte[] response = saslClient.evaluateChallenge(new byte[0]);
assertNotNull(response);
assertTrue(saslClient.isComplete());
response = saslServer.evaluateResponse(response);
assertNull(response);
assertTrue(saslServer.isComplete());
assertNotNull(saslServer.getAuthorizationID());
}
示例2: handleSaslStartMessage
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
/**
* Performs the client side of the initial portion of the Thrift SASL
* protocol. Generates and sends the initial response to the server, including
* which mechanism this client wants to use.
*/
@Override
protected void handleSaslStartMessage() throws TTransportException, SaslException {
SaslClient saslClient = getSaslClient();
byte[] initialResponse = new byte[0];
if (saslClient.hasInitialResponse())
initialResponse = saslClient.evaluateChallenge(initialResponse);
LOGGER.debug("Sending mechanism name {} and initial response of length {}", mechanism,
initialResponse.length);
byte[] mechanismBytes = mechanism.getBytes();
sendSaslMessage(NegotiationStatus.START,
mechanismBytes);
// Send initial response
sendSaslMessage(saslClient.isComplete() ? NegotiationStatus.COMPLETE : NegotiationStatus.OK,
initialResponse);
underlyingTransport.flush();
}
示例3: startAuthentication
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
/**
* Starts to authenticate the user with the specified credentials.
*
* @param credentials
* The credentials to use to login to the database.
* @param connection
* The connection to authenticate the user with.
* @throws MongoDbAuthenticationException
* On a failure in the protocol to authenticate the user on the
* connection.
*/
public void startAuthentication(final Credential credentials,
final Connection connection) throws MongoDbAuthenticationException {
try {
final SaslClient client = createSaslClient(credentials, connection);
if (client != null) {
byte[] payload = EMPTY_BYTES;
if (client.hasInitialResponse()) {
payload = client.evaluateChallenge(payload);
}
sendStart(payload, connection, new SaslResponseCallback(client,
connection, myResults));
}
else {
throw new MongoDbAuthenticationException(
"Could not locate a SASL provider.");
}
}
catch (final SaslException e) {
throw new MongoDbAuthenticationException(e);
}
}
示例4: testSaslServerClient
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
private void testSaslServerClient(SaslServer server, SaslClient client) throws SaslException {
byte[] message = new byte[]{};
if (client.hasInitialResponse()) message = client.evaluateChallenge(message);
while(!server.isComplete() || !client.isComplete()) {
if (!server.isComplete()) message = server.evaluateResponse(message);
if (!client.isComplete()) message = client.evaluateChallenge(message);
}
}
示例5: methodReceived
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
public void methodReceived(AMQProtocolSession session, ConnectionSecureBody body, int channelId)
throws AMQException
{
SaslClient client = session.getSaslClient();
if (client == null)
{
throw new AMQException(null, "No SASL client set up - cannot proceed with authentication", null);
}
try
{
// Evaluate server challenge
byte[] response = client.evaluateChallenge(body.getChallenge());
ConnectionSecureOkBody secureOkBody = session.getMethodRegistry().createConnectionSecureOkBody(response);
session.writeFrame(secureOkBody.generateFrame(channelId));
}
catch (SaslException e)
{
throw new AMQException(null, "Error processing SASL challenge: " + e, e);
}
}
示例6: connectionSecure
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
@Override
public void connectionSecure(Connection conn, ConnectionSecure secure)
{
SaslClient sc = conn.getSaslClient();
try
{
byte[] response = sc.evaluateChallenge(secure.getChallenge());
conn.connectionSecureOk(response);
}
catch (SaslException e)
{
conn.exception(e);
}
}
示例7: buildResponse
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
@Override
protected byte[] buildResponse(SaslClient sc) throws SaslException {
return sc.hasInitialResponse() ?
sc.evaluateChallenge(challenge)
: EMPTY_BYTES;
}
示例8: nextAuthenticationStep
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
@Override
public boolean nextAuthenticationStep(final Buffer fromServer, final List<Buffer> toServer) throws SQLException {
try {
toServer.clear();
if (fromServer == null) {
throw SQLError.createSQLException("Unexpected empty challenge ", SQLError.SQL_STATE_GENERAL_ERROR, null);
}
if (firstChallenge) {
firstChallenge = false;
toServer.add(new Buffer(new byte[0]));
return true;
}
ByteBuffer byteBuffer = ByteBuffer.wrap(fromServer.getByteBuffer(), 0, fromServer.getBufLength());
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
if (saslClients.isEmpty()) {
String mechanism = readString(byteBuffer);
int iterations = byteBuffer.getInt();
for (int i = 0; i < iterations; i++) {
saslClients.add(createSaslClient(mechanism));
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (SaslClient saslClient : saslClients) {
byte[] response = saslClient.evaluateChallenge(getNextChallenge(byteBuffer));
writeByte(baos, (byte) (saslClient.isComplete() ? 1 : 0));
writeInt(baos, response.length);
writeBytes(baos, response);
}
toServer.add(new Buffer(baos.toByteArray()));
return true; // The implementation of the authentication handshake requires that this method always returns true
} catch (SaslException e) {
throw SQLError.createSQLException("mongosql_auth authentication exception ", SQLError.SQL_STATE_GENERAL_ERROR, e, null);
}
}
示例9: shouldAuthenticate
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
@Test
public void shouldAuthenticate() throws SaslException {
// given
String user = "user";
String password = "pencil";
RandomStringGenerator randomStringGenerator = new RandomStringGenerator() {
@Override
public String generate(final int length) {
return "fyko+d2lbbFgONRv9qkxdawL";
}
};
SaslClient saslClient = ScramSha1.createSaslClient(user, password, randomStringGenerator);
// then
assertFalse(saslClient.isComplete());
// when
byte[] response = saslClient.evaluateChallenge(new byte[0]);
// then
assertFalse(saslClient.isComplete());
String expectedResponseHex = "biwsbj11c2VyLHI9ZnlrbytkMmxiYkZnT05Sdjlxa3hkYXdM";
assertArrayEquals(parseBase64Binary(expectedResponseHex), response);
//when
String challengeHex = "cj1meWtvK2QybGJiRmdPTlJ2OXFreGRhd0xIbytWZ2s3cXZVT0tVd3V"
+ "XTElXZzRsLzlTcmFHTUhFRSxzPXJROVpZM01udEJldVAzRTFURFZDNHc9PSxpPTEwMDAw";
response = saslClient.evaluateChallenge(parseBase64Binary(challengeHex));
// then
assertFalse(saslClient.isComplete());
expectedResponseHex = "Yz1iaXdzLHI9ZnlrbytkMmxiYkZnT05Sdjlxa3hkYXdMSG8rVmdrN3F"
+ "2VU9LVXd1V0xJV2c0bC85U3JhR01IRUUscD1NQzJUOEJ2Ym1XUmNrRHc4b1dsNUlWZ2h3Q1k9";
assertArrayEquals(parseBase64Binary(expectedResponseHex), response);
// when
challengeHex = "dj1VTVdlSTI1SkQxeU5ZWlJNcFo0Vkh2aFo5ZTA9";
response = saslClient.evaluateChallenge(parseBase64Binary(challengeHex));
// then
assertTrue(saslClient.isComplete());
assertArrayEquals(new byte[0], response);
// when
try {
saslClient.evaluateChallenge(new byte[0]);
fail();
} catch (SaslException e) {
// all good
}
}
示例10: main
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
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();
}
示例11: run
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
public void run() throws Exception {
System.out.println("Host:" + host + " port: "
+ port);
try (SaslEndpoint endpoint = SaslEndpoint.create(host, port)) {
negotiateMechanism(endpoint);
SaslClient client = createSaslClient();
byte[] data = new byte[0];
if (client.hasInitialResponse()) {
data = client.evaluateChallenge(data);
}
endpoint.send(new Message(SaslStatus.CONTINUE, data));
Message msg = getMessage(endpoint.receive());
while (!client.isComplete()
&& msg.getStatus() != SaslStatus.FAILURE) {
switch (msg.getStatus()) {
case CONTINUE:
System.out.println("client continues");
data = client.evaluateChallenge(msg.getData());
endpoint.send(new Message(SaslStatus.CONTINUE,
data));
msg = getMessage(endpoint.receive());
break;
case SUCCESS:
System.out.println("client succeeded");
data = client.evaluateChallenge(msg.getData());
if (data != null) {
throw new SaslException("data should be null");
}
break;
default:
throw new RuntimeException("Wrong status:"
+ msg.getStatus());
}
}
if (msg.getStatus() == SaslStatus.FAILURE) {
throw new RuntimeException("Status is FAILURE");
}
}
System.out.println("Done");
}
示例12: main
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
if (args.length == 0) {
pwfile = "pw.properties";
namesfile = "names.properties";
auto = true;
} else {
int i = 0;
if (args[i].equals("-m")) {
i++;
auto = false;
}
if (args.length > i) {
pwfile = args[i++];
if (args.length > i) {
namesfile = args[i++];
}
} else {
pwfile = "pw.properties";
namesfile = "names.properties";
}
}
CallbackHandler clntCbh = new ClientCallbackHandler(auto);
CallbackHandler srvCbh =
new PropertiesFileCallbackHandler(pwfile, namesfile, null);
SaslClient clnt = Sasl.createSaslClient(
new String[]{MECH}, null, PROTOCOL, SERVER_FQDN, null, clntCbh);
SaslServer srv = Sasl.createSaslServer(MECH, PROTOCOL, SERVER_FQDN, null,
srvCbh);
if (clnt == null) {
throw new IllegalStateException(
"Unable to find client impl for " + MECH);
}
if (srv == null) {
throw new IllegalStateException(
"Unable to find server impl for " + MECH);
}
byte[] response = (clnt.hasInitialResponse()?
clnt.evaluateChallenge(EMPTY) : EMPTY);
byte[] challenge;
while (!clnt.isComplete() || !srv.isComplete()) {
challenge = srv.evaluateResponse(response);
if (challenge != null) {
response = clnt.evaluateChallenge(challenge);
}
}
if (clnt.isComplete() && srv.isComplete()) {
if (verbose) {
System.out.println("SUCCESS");
System.out.println("authzid is " + srv.getAuthorizationID());
}
} else {
throw new IllegalStateException("FAILURE: mismatched state:" +
" client complete? " + clnt.isComplete() +
" server complete? " + srv.isComplete());
}
}
示例13: buildResponse
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
@Override
protected byte[] buildResponse(SaslClient sc) throws SaslException {
return sc.hasInitialResponse() ? sc.evaluateChallenge(challenge)
: EMPTY_BYTES;
}
示例14: buildResponse
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
@Override
protected byte[] buildResponse(SaslClient sc) throws SaslException {
return sc.evaluateChallenge(challenge);
}
示例15: connectionStart
import javax.security.sasl.SaslClient; //導入方法依賴的package包/類
@Override
public void connectionStart(Connection conn, ConnectionStart start)
{
Map<String,Object> clientProperties = new HashMap<String,Object>();
if(this.conSettings.getClientProperties() != null)
{
clientProperties.putAll(this.conSettings.getClientProperties());
}
clientProperties.put("qpid.session_flow", 1);
clientProperties.put("qpid.client_pid",getPID());
clientProperties.put("qpid.client_process",
System.getProperty("qpid.client_process","Qpid Java Client"));
List<Object> brokerMechs = start.getMechanisms();
if (brokerMechs == null || brokerMechs.isEmpty())
{
conn.connectionStartOk
(clientProperties, null, null, conn.getLocale());
return;
}
List<String> choosenMechs = new ArrayList<String>();
for (String mech:clientMechs)
{
if (brokerMechs.contains(mech))
{
choosenMechs.add(mech);
}
}
if (choosenMechs.size() == 0)
{
conn.exception(new ConnectionException("The following SASL mechanisms " +
clientMechs.toString() +
" specified by the client are not supported by the broker"));
return;
}
String[] mechs = new String[choosenMechs.size()];
choosenMechs.toArray(mechs);
conn.setServerProperties(start.getServerProperties());
try
{
Map<String,Object> saslProps = new HashMap<String,Object>();
if (conSettings.isUseSASLEncryption())
{
saslProps.put(Sasl.QOP, "auth-conf");
}
UsernamePasswordCallbackHandler handler =
new UsernamePasswordCallbackHandler();
handler.initialise(conSettings.getUsername(), conSettings.getPassword());
SaslClient sc = Sasl.createSaslClient
(mechs, null, conSettings.getSaslProtocol(), conSettings.getSaslServerName(), saslProps, handler);
conn.setSaslClient(sc);
byte[] response = sc.hasInitialResponse() ?
sc.evaluateChallenge(new byte[0]) : null;
conn.connectionStartOk
(clientProperties, sc.getMechanismName(), response,
conn.getLocale());
}
catch (SaslException e)
{
conn.exception(e);
}
}