Search in sources :

Example 1 with ObjectCallback

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;
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) ObjectCallback(org.eclipse.jetty.jaas.callback.ObjectCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) ObjectCallback(org.eclipse.jetty.jaas.callback.ObjectCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback)

Example 2 with ObjectCallback

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.");
    }
}
Also used : ObjectCallback(org.eclipse.jetty.jaas.callback.ObjectCallback) IOException(java.io.IOException) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) NamingException(javax.naming.NamingException) IOException(java.io.IOException) ObjectCallback(org.eclipse.jetty.jaas.callback.ObjectCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Example 3 with ObjectCallback

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;
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) ObjectCallback(org.eclipse.jetty.jaas.callback.ObjectCallback) IOException(java.io.IOException) Subject(javax.security.auth.Subject) ObjectCallback(org.eclipse.jetty.jaas.callback.ObjectCallback) RequestParameterCallback(org.eclipse.jetty.jaas.callback.RequestParameterCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) LoginContext(javax.security.auth.login.LoginContext) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RequestParameterCallback(org.eclipse.jetty.jaas.callback.RequestParameterCallback)

Example 4 with ObjectCallback

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());
    }
}
Also used : IOException(java.io.IOException) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) FailedLoginException(javax.security.auth.login.FailedLoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) ObjectCallback(org.eclipse.jetty.jaas.callback.ObjectCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) FailedLoginException(javax.security.auth.login.FailedLoginException) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException)

Aggregations

Callback (javax.security.auth.callback.Callback)4 NameCallback (javax.security.auth.callback.NameCallback)4 ObjectCallback (org.eclipse.jetty.jaas.callback.ObjectCallback)4 IOException (java.io.IOException)3 PasswordCallback (javax.security.auth.callback.PasswordCallback)3 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)3 LoginException (javax.security.auth.login.LoginException)3 NamingException (javax.naming.NamingException)1 Subject (javax.security.auth.Subject)1 CallbackHandler (javax.security.auth.callback.CallbackHandler)1 FailedLoginException (javax.security.auth.login.FailedLoginException)1 LoginContext (javax.security.auth.login.LoginContext)1 RequestParameterCallback (org.eclipse.jetty.jaas.callback.RequestParameterCallback)1