Search in sources :

Example 1 with RequestParameterCallback

use of org.eclipse.jetty.jaas.callback.RequestParameterCallback 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)

Aggregations

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