use of javax.security.auth.callback.NameCallback in project jdk8u_jdk by JetBrains.
the class DigestMD5Client method processChallenge.
/**
* Record information from the challengeVal array into variables/fields.
* Check directive values that are multi-valued and ensure that mandatory
* directives not missing from the digest-challenge.
*
* @throws SaslException if a sasl is a the mechanism cannot
* correcly handle a callbacks or if a violation in the
* digest challenge format is detected.
*/
private void processChallenge(byte[][] challengeVal, List<byte[]> realmChoices) throws SaslException, UnsupportedEncodingException {
/* CHARSET: optional atmost once */
if (challengeVal[CHARSET] != null) {
if (!"utf-8".equals(new String(challengeVal[CHARSET], encoding))) {
throw new SaslException("DIGEST-MD5: digest-challenge format " + "violation. Unrecognised charset value: " + new String(challengeVal[CHARSET]));
} else {
encoding = "UTF8";
useUTF8 = true;
}
}
/* ALGORITHM: required exactly once */
if (challengeVal[ALGORITHM] == null) {
throw new SaslException("DIGEST-MD5: Digest-challenge format " + "violation: algorithm directive missing");
} else if (!"md5-sess".equals(new String(challengeVal[ALGORITHM], encoding))) {
throw new SaslException("DIGEST-MD5: Digest-challenge format " + "violation. Invalid value for 'algorithm' directive: " + challengeVal[ALGORITHM]);
}
/* NONCE: required exactly once */
if (challengeVal[NONCE] == null) {
throw new SaslException("DIGEST-MD5: Digest-challenge format " + "violation: nonce directive missing");
} else {
nonce = challengeVal[NONCE];
}
try {
/* REALM: optional, if multiple, stored in realmChoices */
String[] realmTokens = null;
if (challengeVal[REALM] != null) {
if (realmChoices == null || realmChoices.size() <= 1) {
// Only one realm specified
negotiatedRealm = new String(challengeVal[REALM], encoding);
} else {
realmTokens = new String[realmChoices.size()];
for (int i = 0; i < realmTokens.length; i++) {
realmTokens[i] = new String(realmChoices.get(i), encoding);
}
}
}
NameCallback ncb = authzid == null ? new NameCallback("DIGEST-MD5 authentication ID: ") : new NameCallback("DIGEST-MD5 authentication ID: ", authzid);
PasswordCallback pcb = new PasswordCallback("DIGEST-MD5 password: ", false);
if (realmTokens == null) {
// Server specified <= 1 realm
// If 0, RFC 2831: the client SHOULD solicit a realm from the user.
RealmCallback tcb = (negotiatedRealm == null ? new RealmCallback("DIGEST-MD5 realm: ") : new RealmCallback("DIGEST-MD5 realm: ", negotiatedRealm));
cbh.handle(new Callback[] { tcb, ncb, pcb });
/* Acquire realm from RealmCallback */
negotiatedRealm = tcb.getText();
if (negotiatedRealm == null) {
negotiatedRealm = "";
}
} else {
RealmChoiceCallback ccb = new RealmChoiceCallback("DIGEST-MD5 realm: ", realmTokens, 0, false);
cbh.handle(new Callback[] { ccb, ncb, pcb });
// Acquire realm from RealmChoiceCallback
int[] selected = ccb.getSelectedIndexes();
if (selected == null || selected[0] < 0 || selected[0] >= realmTokens.length) {
throw new SaslException("DIGEST-MD5: Invalid realm chosen");
}
negotiatedRealm = realmTokens[selected[0]];
}
passwd = pcb.getPassword();
pcb.clearPassword();
username = ncb.getName();
} catch (SaslException se) {
throw se;
} catch (UnsupportedCallbackException e) {
throw new SaslException("DIGEST-MD5: Cannot perform callback to " + "acquire realm, authentication ID or password", e);
} catch (IOException e) {
throw new SaslException("DIGEST-MD5: Error acquiring realm, authentication ID or password", e);
}
if (username == null || passwd == null) {
throw new SaslException("DIGEST-MD5: authentication ID and password must be specified");
}
/* MAXBUF: optional atmost once */
int srvMaxBufSize = (challengeVal[MAXBUF] == null) ? DEFAULT_MAXBUF : Integer.parseInt(new String(challengeVal[MAXBUF], encoding));
sendMaxBufSize = (sendMaxBufSize == 0) ? srvMaxBufSize : Math.min(sendMaxBufSize, srvMaxBufSize);
}
use of javax.security.auth.callback.NameCallback in project jdk8u_jdk by JetBrains.
the class NegotiateCallbackHandler method handle.
public void handle(Callback[] callbacks) throws UnsupportedCallbackException, IOException {
for (int i = 0; i < callbacks.length; i++) {
Callback callBack = callbacks[i];
if (callBack instanceof NameCallback) {
getAnswer();
((NameCallback) callBack).setName(username);
} else if (callBack instanceof PasswordCallback) {
getAnswer();
((PasswordCallback) callBack).setPassword(password);
if (password != null)
Arrays.fill(password, ' ');
} else {
throw new UnsupportedCallbackException(callBack, "Call back not supported");
}
}
}
use of javax.security.auth.callback.NameCallback in project CorfuDB by CorfuDB.
the class PlainTextLoginModule method login.
@Override
public boolean login() throws LoginException {
if (callbackHandler == null) {
throw new LoginException("CallbackHandler not registered");
}
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("Username");
callbacks[1] = new PasswordCallback("Password", false);
try {
callbackHandler.handle(callbacks);
} catch (IOException ie) {
throw new LoginException("IOException: " + ie.toString());
} catch (UnsupportedCallbackException uce) {
throw new LoginException("UnsupportedCallbackException: " + uce.getCallback().toString());
}
String username = ((NameCallback) callbacks[0]).getName();
if (options.containsKey(PLAIN_TEXT_USER_PREFIX + username)) {
String expectedPassword = (String) options.get(PLAIN_TEXT_USER_PREFIX + username);
String password = new String(((PasswordCallback) callbacks[1]).getPassword());
if (!expectedPassword.equals(password)) {
throw new LoginException("Incorrect password for: " + username);
}
} else {
throw new LoginException("User: " + username + " not found");
}
return true;
}
use of javax.security.auth.callback.NameCallback in project tomee by apache.
the class ServiceProviderLoginModule method getUserData.
private UserData getUserData() throws LoginException {
final Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("Username: ");
callbacks[1] = new PasswordCallback("Password: ", false);
try {
this.callbackHandler.handle(callbacks);
} catch (final IOException ioe) {
throw new LoginException(ioe.getMessage());
} catch (final UnsupportedCallbackException uce) {
throw new LoginException(uce.getMessage() + " not available to obtain information from user");
}
final String user = ((NameCallback) callbacks[0]).getName();
char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
if (tmpPassword == null) {
tmpPassword = new char[0];
}
final String password = new String(tmpPassword);
return new UserData(user, password);
}
use of javax.security.auth.callback.NameCallback in project tomee by apache.
the class TestLoginModule method login.
public boolean login() throws LoginException {
final Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("Username:");
callbacks[1] = new PasswordCallback("Password:", false);
try {
callbackHandler.handle(callbacks);
} catch (final Exception e) {
throw new LoginException("Failed to perform emulated login: " + e.getMessage());
}
user = ((NameCallback) callbacks[0]).getName();
return true;
}
Aggregations