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 cas by apereo.
the class MockLoginModule method login.
@Override
public boolean login() throws LoginException {
final Callback[] callbacks = new Callback[] { new NameCallback("f"), new PasswordCallback("f", false) };
try {
this.callbackHandler.handle(callbacks);
} catch (final Exception e) {
throw new LoginException();
}
final String userName = ((NameCallback) callbacks[0]).getName();
final String password = new String(((PasswordCallback) callbacks[1]).getPassword());
if ("test".equals(userName) && "test".equals(password)) {
this.subject.getPrincipals().add(new BasicUserPrincipal(userName));
return true;
}
throw new LoginException();
}
use of javax.security.auth.callback.NameCallback in project uPortal by Jasig.
the class JAASInlineCallbackHandler method handle.
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
NameCallback nc = (NameCallback) callbacks[i];
nc.setName(username);
} else if (callbacks[i] instanceof PasswordCallback) {
PasswordCallback pc = (PasswordCallback) callbacks[i];
pc.setPassword(password);
} else {
throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
}
}
}
use of javax.security.auth.callback.NameCallback in project Payara by payara.
the class AuthenticationServiceImpl method setupPasswordCredential.
/**
* A PasswordCredential object is needed when using the existing Realm LoginModules.
*
* Unless the CallbackHandler is from the AuthenticationService obtain the name
* and password from the supplied JAAS CallbackHandler directly. Establishing the
* PasswordCredential in the Subject is determined by service configuration.
*
* @throws LoginException when unable to obtain data from the CallbackHandler
*/
private void setupPasswordCredential(Subject subject, CallbackHandler callbackHandler) throws LoginException {
String username = null;
char[] password = null;
// Obtain the username and password for the PasswordCredential
if (callbackHandler instanceof AuthenticationCallbackHandler) {
username = ((AuthenticationCallbackHandler) callbackHandler).getUsername();
password = ((AuthenticationCallbackHandler) callbackHandler).getPassword();
} else {
// Use the supplied callback handler to obtain the PasswordCredential information
// TODO - How does this impact Audit ability to get name?
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("username: ");
callbacks[1] = new PasswordCallback("password: ", false);
try {
callbackHandler.handle(callbacks);
username = ((NameCallback) callbacks[0]).getName();
password = ((PasswordCallback) callbacks[1]).getPassword();
} catch (IOException ioe) {
throw (LoginException) new LoginException("AuthenticationService unable to create PasswordCredential: " + ioe.getMessage()).initCause(ioe);
} catch (UnsupportedCallbackException uce) {
throw (LoginException) new LoginException("AuthenticationService unable to create PasswordCredential: " + uce.getMessage()).initCause(uce);
}
}
// Add the PasswordCredential to the Subject
final Subject s = subject;
final PasswordCredential pc = new PasswordCredential(username, password, realmName);
AppservAccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
s.getPrivateCredentials().add(pc);
return null;
}
});
}
use of javax.security.auth.callback.NameCallback in project camel by apache.
the class MyLoginModule method login.
@Override
public boolean login() throws LoginException {
// get username and password
Callback[] callbacks = new Callback[2];
callbacks[0] = new NameCallback("username");
callbacks[1] = new PasswordCallback("password", false);
try {
callbackHandler.handle(callbacks);
String username = ((NameCallback) callbacks[0]).getName();
char[] tmpPassword = ((PasswordCallback) callbacks[1]).getPassword();
String password = new String(tmpPassword);
((PasswordCallback) callbacks[1]).clearPassword();
// as this is just for testing purpose
if (!"secret".equals(password)) {
throw new LoginException("Login denied");
}
// add roles
if ("scott".equals(username)) {
subject.getPrincipals().add(new MyRolePrincipal("admin"));
subject.getPrincipals().add(new MyRolePrincipal("guest"));
} else if ("guest".equals(username)) {
subject.getPrincipals().add(new MyRolePrincipal("guest"));
}
} catch (IOException ioe) {
LoginException le = new LoginException(ioe.toString());
le.initCause(ioe);
throw le;
} catch (UnsupportedCallbackException uce) {
LoginException le = new LoginException("Error: " + uce.getCallback().toString() + " not available to gather authentication information from the user");
le.initCause(uce);
throw le;
}
return true;
}
Aggregations