return;
}
NameCallback nameCallBack = null;
VerifyPasswordCallback verifyPasswordCallback = null;
SubjectCallback subjectCallback = null;
for (Callback current : callbacks) {
if (current instanceof NameCallback) {
nameCallBack = (NameCallback) current;
} else if (current instanceof RealmCallback) {
} else if (current instanceof VerifyPasswordCallback) {
verifyPasswordCallback = (VerifyPasswordCallback) current;
} else if (current instanceof SubjectCallback) {
subjectCallback = (SubjectCallback) current;
} else {
throw new UnsupportedCallbackException(current);
}
}
if (nameCallBack == null) {
throw MESSAGES.noUsername();
}
final String userName = nameCallBack.getDefaultName();
if (userName == null || userName.length() == 0) {
throw MESSAGES.noUsername();
}
if (verifyPasswordCallback == null || verifyPasswordCallback.getPassword() == null) {
throw MESSAGES.noPassword();
}
final char[] password = verifyPasswordCallback.getPassword().toCharArray();
Subject subject = subjectCallback != null && subjectCallback.getSubject() != null ? subjectCallback.getSubject()
: new Subject();
ServerSecurityManager securityManager;
if ((securityManager = securityManagerValue.getOptionalValue()) != null) {
try {
securityManager.push(name, userName, password, subject);
verifyPasswordCallback.setVerified(true);
subject.getPrivateCredentials().add(new PasswordCredential(userName, password));
if (subjectCallback != null) {
// Only want to deliberately pass it back if authentication completed.
subjectCallback.setSubject(subject);
}
} catch (SecurityException e) {
verifyPasswordCallback.setVerified(false);
} finally {
securityManager.pop();
}
} else {
try {
LoginContext ctx = new LoginContext(name, subject, new CallbackHandler() {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback current : callbacks) {
if (current instanceof NameCallback) {
NameCallback ncb = (NameCallback) current;
ncb.setName(userName);
} else if (current instanceof PasswordCallback) {
PasswordCallback pcb = (PasswordCallback) current;
pcb.setPassword(password);
} else {
throw new UnsupportedCallbackException(current);
}
}
}
});
ctx.login();
verifyPasswordCallback.setVerified(true);
subject.getPrivateCredentials().add(new PasswordCredential(userName, password));
if (subjectCallback != null) {
// Only want to deliberately pass it back if authentication completed.
subjectCallback.setSubject(subject);
}
} catch (LoginException e) {
verifyPasswordCallback.setVerified(false);
}
}
}