use of org.eclipse.jetty.jaas.callback.ObjectCallback in project jetty.project by eclipse.
the class AbstractLoginModule method configureCallbacks.
public Callback[] configureCallbacks() {
Callback[] callbacks = new Callback[3];
callbacks[0] = new NameCallback("Enter user name");
callbacks[1] = new ObjectCallback();
//only used if framework does not support the ObjectCallback
callbacks[2] = new PasswordCallback("Enter password", false);
return callbacks;
}
use of org.eclipse.jetty.jaas.callback.ObjectCallback in project jetty.project by eclipse.
the class LdapLoginModule method login.
/**
* since ldap uses a context bind for valid authentication checking, we override login()
* <p>
* if credentials are not available from the users context or if we are forcing the binding check
* then we try a binding authentication check, otherwise if we have the users encoded password then
* we can try authentication via that mechanic
*
* @return true if authenticated, false otherwise
* @throws LoginException if unable to login
*/
public boolean login() throws LoginException {
try {
if (getCallbackHandler() == null) {
throw new LoginException("No callback handler");
}
Callback[] callbacks = configureCallbacks();
getCallbackHandler().handle(callbacks);
String webUserName = ((NameCallback) callbacks[0]).getName();
Object webCredential = ((ObjectCallback) callbacks[1]).getObject();
if (webUserName == null || webCredential == null) {
setAuthenticated(false);
return isAuthenticated();
}
boolean authed = false;
if (_forceBindingLogin) {
authed = bindingLogin(webUserName, webCredential);
} else {
// This sets read and the credential
UserInfo userInfo = getUserInfo(webUserName);
if (userInfo == null) {
setAuthenticated(false);
return false;
}
setCurrentUser(new JAASUserInfo(userInfo));
if (webCredential instanceof String)
authed = credentialLogin(Credential.getCredential((String) webCredential));
else
authed = credentialLogin(webCredential);
}
//only fetch roles if authenticated
if (authed)
getCurrentUser().fetchRoles();
return authed;
} catch (UnsupportedCallbackException e) {
throw new LoginException("Error obtaining callback information.");
} catch (IOException e) {
if (_debug) {
e.printStackTrace();
}
throw new LoginException("IO Error performing login.");
} catch (Exception e) {
if (_debug) {
e.printStackTrace();
}
throw new LoginException("Error obtaining user info.");
}
}
use of org.eclipse.jetty.jaas.callback.ObjectCallback in project jetty.project by eclipse.
the class JAASLoginService method login.
/* ------------------------------------------------------------ */
@Override
public UserIdentity login(final String username, final Object credentials, final ServletRequest request) {
try {
CallbackHandler callbackHandler = null;
if (_callbackHandlerClass == null) {
callbackHandler = new CallbackHandler() {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
((NameCallback) callback).setName(username);
} else if (callback instanceof PasswordCallback) {
((PasswordCallback) callback).setPassword((char[]) credentials.toString().toCharArray());
} else if (callback instanceof ObjectCallback) {
((ObjectCallback) callback).setObject(credentials);
} else if (callback instanceof RequestParameterCallback) {
RequestParameterCallback rpc = (RequestParameterCallback) callback;
if (request != null)
rpc.setParameterValues(Arrays.asList(request.getParameterValues(rpc.getParameterName())));
} else
throw new UnsupportedCallbackException(callback);
}
}
};
} else {
Class<?> clazz = Loader.loadClass(_callbackHandlerClass);
callbackHandler = (CallbackHandler) clazz.newInstance();
}
//set up the login context
//TODO jaspi requires we provide the Configuration parameter
Subject subject = new Subject();
LoginContext loginContext = new LoginContext(_loginModuleName, subject, callbackHandler);
loginContext.login();
//login success
JAASUserPrincipal userPrincipal = new JAASUserPrincipal(getUserName(callbackHandler), subject, loginContext);
subject.getPrincipals().add(userPrincipal);
return _identityService.newUserIdentity(subject, userPrincipal, getGroups(subject));
} catch (LoginException e) {
LOG.warn(e);
} catch (IOException e) {
LOG.warn(e);
} catch (UnsupportedCallbackException e) {
LOG.warn(e);
} catch (InstantiationException e) {
LOG.warn(e);
} catch (IllegalAccessException e) {
LOG.warn(e);
} catch (ClassNotFoundException e) {
LOG.warn(e);
}
return null;
}
use of org.eclipse.jetty.jaas.callback.ObjectCallback in project jetty.project by eclipse.
the class AbstractLoginModule method login.
/**
* @see javax.security.auth.spi.LoginModule#login()
* @return true if is authenticated, false otherwise
* @throws LoginException if unable to login
*/
public boolean login() throws LoginException {
try {
if (isIgnored())
return false;
if (callbackHandler == null)
throw new LoginException("No callback handler");
Callback[] callbacks = configureCallbacks();
callbackHandler.handle(callbacks);
String webUserName = ((NameCallback) callbacks[0]).getName();
Object webCredential = null;
//first check if ObjectCallback has the credential
webCredential = ((ObjectCallback) callbacks[1]).getObject();
if (webCredential == null)
//use standard PasswordCallback
webCredential = ((PasswordCallback) callbacks[2]).getPassword();
if ((webUserName == null) || (webCredential == null)) {
setAuthenticated(false);
throw new FailedLoginException();
}
UserInfo userInfo = getUserInfo(webUserName);
if (userInfo == null) {
setAuthenticated(false);
throw new FailedLoginException();
}
currentUser = new JAASUserInfo(userInfo);
setAuthenticated(currentUser.checkCredential(webCredential));
if (isAuthenticated()) {
currentUser.fetchRoles();
return true;
} else
throw new FailedLoginException();
} catch (IOException e) {
throw new LoginException(e.toString());
} catch (UnsupportedCallbackException e) {
throw new LoginException(e.toString());
} catch (Exception e) {
if (e instanceof LoginException)
throw (LoginException) e;
throw new LoginException(e.toString());
}
}
Aggregations