use of javax.security.auth.callback.NameCallback in project wildfly by wildfly.
the class RealmDirectLoginModule method getUsersPassword.
/**
* @see org.jboss.security.auth.spi.UsernamePasswordLoginModule#getUsersPassword()
*/
@Override
protected String getUsersPassword() throws LoginException {
if (validationMode == ValidationMode.VALIDATION) {
return null;
}
RealmCallback rcb = new RealmCallback("Realm", securityRealm.getName());
NameCallback ncb = new NameCallback("User Name", getUsername());
String password = null;
switch(validationMode) {
case DIGEST:
CredentialCallback cc = new CredentialCallback(PasswordCredential.class, ALGORITHM_DIGEST_MD5);
handle(new Callback[] { rcb, ncb, cc });
PasswordCredential passwordCredential = (PasswordCredential) cc.getCredential();
DigestPassword digestPassword = passwordCredential.getPassword(DigestPassword.class);
password = ByteIterator.ofBytes(digestPassword.getDigest()).hexEncode().drainToString();
break;
case PASSWORD:
PasswordCallback pcb = new PasswordCallback("Password", false);
handle(new Callback[] { rcb, ncb, pcb });
password = String.valueOf(pcb.getPassword());
break;
}
return password;
}
use of javax.security.auth.callback.NameCallback in project wildfly by wildfly.
the class RealmDirectLoginModule method validatePassword.
@Override
protected boolean validatePassword(String inputPassword, String expectedPassword) {
if (digestCredential != null) {
return digestCredential.verifyHA1(expectedPassword.getBytes(UTF_8));
}
switch(validationMode) {
case DIGEST:
String inputHashed = hashUtil.generateHashedHexURP(getUsername(), securityRealm.getName(), inputPassword.toCharArray());
return expectedPassword.equals(inputHashed);
case PASSWORD:
return expectedPassword.equals(inputPassword);
case VALIDATION:
RealmCallback rcb = new RealmCallback("Realm", securityRealm.getName());
NameCallback ncb = new NameCallback("User Name", getUsername());
EvidenceVerifyCallback evc = new EvidenceVerifyCallback(new PasswordGuessEvidence(inputPassword.toCharArray()));
try {
handle(new Callback[] { rcb, ncb, evc });
return evc.isVerified();
} catch (LoginException e) {
return false;
}
default:
return false;
}
}
use of javax.security.auth.callback.NameCallback in project wildfly by wildfly.
the class CustomEjbAccessingLoginModule method getUsernameAndPassword.
protected void getUsernameAndPassword() throws LoginException {
// prompt for a username and password
if (callbackHandler == null) {
throw new LoginException("Error: no CallbackHandler available " + "to collect authentication information");
}
NameCallback nc = new NameCallback("User name: ", "guest");
PasswordCallback pc = new PasswordCallback("Password: ", false);
Callback[] callbacks = { nc, pc };
try {
callbackHandler.handle(callbacks);
username = nc.getName();
char[] tmpPassword = pc.getPassword();
if (tmpPassword != null) {
pc.clearPassword();
password = new String(tmpPassword);
}
} catch (IOException e) {
LoginException le = new LoginException("Failed to get username/password");
le.initCause(e);
throw le;
} catch (UnsupportedCallbackException e) {
LoginException le = new LoginException("CallbackHandler does not support: " + e.getCallback());
le.initCause(e);
throw le;
}
}
use of javax.security.auth.callback.NameCallback in project wildfly by wildfly.
the class CustomTestLoginModule method getUsernameAndPassword.
protected String[] getUsernameAndPassword() throws LoginException {
String[] info = { null, null };
// prompt for a username and password
if (callbackHandler == null) {
throw new LoginException("Error: no CallbackHandler available " + "to collect authentication information");
}
NameCallback nc = new NameCallback("User name: ", "guest");
PasswordCallback pc = new PasswordCallback("Password: ", false);
Callback[] callbacks = { nc, pc };
String username = null;
String password = null;
try {
callbackHandler.handle(callbacks);
username = nc.getName();
char[] tmpPassword = pc.getPassword();
if (tmpPassword != null) {
pc.clearPassword();
password = new String(tmpPassword);
}
} catch (IOException e) {
LoginException le = new LoginException("Failed to get username/password");
le.initCause(e);
throw le;
} catch (UnsupportedCallbackException e) {
LoginException le = new LoginException("CallbackHandler does not support: " + e.getCallback());
le.initCause(e);
throw le;
}
info[0] = username;
info[1] = password;
return info;
}
use of javax.security.auth.callback.NameCallback in project wildfly by wildfly.
the class GuestDelegationLoginModule method login.
// Public methods --------------------------------------------------------
@SuppressWarnings("unchecked")
@Override
public boolean login() throws LoginException {
if (super.login() == true) {
log.debug("super.login()==true");
return true;
}
// Time to see if this is a delegation request.
NameCallback ncb = new NameCallback("Username:");
ObjectCallback ocb = new ObjectCallback("Password:");
try {
callbackHandler.handle(new Callback[] { ncb, ocb });
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
// If the CallbackHandler can not handle the required callbacks then no chance.
return false;
}
String name = ncb.getName();
Object credential = ocb.getCredential();
if (credential instanceof CurrentUserCredential) {
// This credential type will only be seen for a delegation request, if not seen then the request is not for us.
final CurrentUserCredential cuCredential = (CurrentUserCredential) credential;
// only the "guest" can be switched to another identity
if ("guest".equals(cuCredential.getUser())) {
identity = new SimplePrincipal(name);
if (getUseFirstPass()) {
String userName = identity.getName();
if (log.isDebugEnabled())
log.debug("Storing username '" + userName + "' and empty password");
// Add the username and an empty password to the shared state map
sharedState.put("javax.security.auth.login.name", identity);
sharedState.put("javax.security.auth.login.password", "");
}
loginOk = true;
return true;
}
}
// Attempted login but not successful.
return false;
}
Aggregations