Search in sources :

Example 1 with RequestParameterCallback

use of org.eclipse.jetty.plus.jaas.callback.RequestParameterCallback in project cdap by caskdata.

the class JAASLoginService method login.

/* ------------------------------------------------------------ */
public UserIdentity login(final String username, final Object credentials) {
    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(credentials.toString().toCharArray());
                        } else if (callback instanceof ObjectCallback) {
                            ((ObjectCallback) callback).setObject(credentials);
                        } else if (callback instanceof RequestParameterCallback) {
                            AbstractHttpConnection connection = AbstractHttpConnection.getCurrentConnection();
                            Request request = (connection == null ? null : connection.getRequest());
                            if (request != null) {
                                RequestParameterCallback rpc = (RequestParameterCallback) callback;
                                rpc.setParameterValues(Arrays.asList(request.getParameterValues(rpc.getParameterName())));
                            }
                        } else {
                            throw new UnsupportedCallbackException(callback);
                        }
                    }
                }
            };
        } else {
            Class clazz = Loader.loadClass(getClass(), 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, configuration);
        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.debug(e);
    } catch (IOException e) {
        LOG.info(e.getMessage());
        LOG.debug(e);
    } catch (UnsupportedCallbackException e) {
        LOG.info(e.getMessage());
        LOG.debug(e);
    } catch (InstantiationException e) {
        LOG.info(e.getMessage());
        LOG.debug(e);
    } catch (IllegalAccessException e) {
        LOG.info(e.getMessage());
        LOG.debug(e);
    } catch (ClassNotFoundException e) {
        LOG.info(e.getMessage());
        LOG.debug(e);
    }
    return null;
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) ObjectCallback(org.eclipse.jetty.plus.jaas.callback.ObjectCallback) Request(org.eclipse.jetty.server.Request) IOException(java.io.IOException) Subject(javax.security.auth.Subject) ObjectCallback(org.eclipse.jetty.plus.jaas.callback.ObjectCallback) RequestParameterCallback(org.eclipse.jetty.plus.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.plus.jaas.callback.RequestParameterCallback) AbstractHttpConnection(org.eclipse.jetty.server.AbstractHttpConnection)

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.plus.jaas.callback.ObjectCallback)1 RequestParameterCallback (org.eclipse.jetty.plus.jaas.callback.RequestParameterCallback)1 AbstractHttpConnection (org.eclipse.jetty.server.AbstractHttpConnection)1 Request (org.eclipse.jetty.server.Request)1