Example usage for javax.security.auth.login LoginException toString

List of usage examples for javax.security.auth.login LoginException toString

Introduction

In this page you can find the example usage for javax.security.auth.login LoginException toString.

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:com.tethrnet.manage.util.ExternalAuthUtil.java

/**
 * external auth login method//from   w  w  w . ja  v  a2  s. c om
 *
 * @param auth contains username and password
 * @return auth token if success
 */
public static String login(final Auth auth) {

    String authToken = null;
    if (externalAuthEnabled && auth != null && StringUtils.isNotEmpty(auth.getUsername())
            && StringUtils.isNotEmpty(auth.getPassword())) {

        Connection con = null;
        try {
            CallbackHandler handler = new CallbackHandler() {

                @Override
                public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                    for (Callback callback : callbacks) {
                        if (callback instanceof NameCallback) {
                            ((NameCallback) callback).setName(auth.getUsername());
                        } else if (callback instanceof PasswordCallback) {
                            ((PasswordCallback) callback).setPassword(auth.getPassword().toCharArray());
                        }
                    }
                }
            };

            try {
                LoginContext loginContext = new LoginContext(JAAS_MODULE, handler);
                //will throw exception if login fail
                loginContext.login();
                Subject subject = loginContext.getSubject();

                con = DBUtils.getConn();
                User user = AuthDB.getUserByUID(con, auth.getUsername());

                if (user == null) {
                    user = new User();

                    user.setUserType(User.ADMINISTRATOR);
                    user.setUsername(auth.getUsername());

                    //set email
                    if (auth.getUsername().contains("@")) {
                        user.setEmail(auth.getUsername());
                    }

                    user.setId(UserDB.insertUser(con, user));
                }

                authToken = UUID.randomUUID().toString();
                user.setAuthToken(authToken);
                user.setAuthType(Auth.AUTH_EXTERNAL);
                //set auth token
                AuthDB.updateLogin(con, user);

            } catch (LoginException e) {
                //auth failed return empty
                authToken = null;
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
        }

        DBUtils.closeConn(con);
    }

    return authToken;
}

From source file:com.keybox.manage.util.ExternalAuthUtil.java

/**
 * external auth login method/*from  w  w  w  . j av a  2  s  . c  o m*/
 *
 * @param auth contains username and password
 * @return auth token if success
 */
public static String login(final Auth auth) {

    String authToken = null;
    if (externalAuthEnabled && auth != null && StringUtils.isNotEmpty(auth.getUsername())
            && StringUtils.isNotEmpty(auth.getPassword())) {

        Connection con = null;
        try {
            CallbackHandler handler = new CallbackHandler() {

                @Override
                public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                    for (Callback callback : callbacks) {
                        if (callback instanceof NameCallback) {
                            ((NameCallback) callback).setName(auth.getUsername());
                        } else if (callback instanceof PasswordCallback) {
                            ((PasswordCallback) callback).setPassword(auth.getPassword().toCharArray());
                        }
                    }
                }
            };

            try {
                LoginContext loginContext = new LoginContext(JAAS_MODULE, handler);
                //will throw exception if login fail
                loginContext.login();
                Subject subject = loginContext.getSubject();

                con = DBUtils.getConn();
                User user = AuthDB.getUserByUID(con, auth.getUsername());

                if (user == null) {
                    user = new User();

                    user.setUserType(User.ADMINISTRATOR);
                    user.setUsername(auth.getUsername());

                    //if it looks like name is returned default it 
                    for (Principal p : subject.getPrincipals()) {
                        if (p.getName().contains(" ")) {
                            String[] name = p.getName().split(" ");
                            if (name.length > 1) {
                                user.setFirstNm(name[0]);
                                user.setLastNm(name[name.length - 1]);
                            }
                        }
                    }

                    //set email
                    if (auth.getUsername().contains("@")) {
                        user.setEmail(auth.getUsername());
                    }

                    user.setId(UserDB.insertUser(con, user));
                }

                authToken = UUID.randomUUID().toString();
                user.setAuthToken(authToken);
                user.setAuthType(Auth.AUTH_EXTERNAL);
                //set auth token
                AuthDB.updateLogin(con, user);

            } catch (LoginException e) {
                //auth failed return empty
                authToken = null;
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
        }

        DBUtils.closeConn(con);
    }

    return authToken;
}

From source file:org.marketcetera.modules.remote.receiver.ClientLoginModuleTest.java

/**
 * Attempt login and test for failure / success conditions
 *
 * @param name the user name//from ww  w .ja v  a2 s  . c o  m
 * @param password the password
 * @param failure expected failure
 * @param failureMsg expected failure message
 *
 * @return the failure exception if any
 *
 * @throws Exception if there was unexpected failure
 */
private LoginException attemptLogin(String name, char[] password, Class<? extends LoginException> failure,
        String failureMsg) throws Exception {
    MockCallbackHandler ch = null;
    loginContext = null;
    try {
        ch = new MockCallbackHandler(name, password);
        loginContext = new LoginContext(JaasConfiguration.REMOTING_LOGIN_DOMAIN, ch);
        loginContext.login();
        assertNull("Expected failure:" + failure + failureMsg, failure);
        //verify that the appropriate principals are set in the subject
        assertTrue(loginContext.getSubject().getPrincipals().toString(),
                loginContext.getSubject().getPrincipals().contains(new UserPrincipal(getTestUsername())));
    } catch (LoginException e) {
        assertNotNull("Unexpected failure:" + e, failure);
        assertTrue("Expected:" + failure + ":Actual:" + e.getClass().getName() + e.toString(),
                failure.isInstance(e));
        if (failureMsg != null) {
            assertEquals(failureMsg, e.getMessage());
        }
        assertNotNull(loginContext);
        //verify that the appropriate principals are not set in the subject
        if (loginContext.getSubject() != null && loginContext.getSubject().getPrincipals() != null) {
            assertFalse(loginContext.getSubject().getPrincipals().toString(),
                    loginContext.getSubject().getPrincipals().contains(new UserPrincipal(getTestUsername())));
        }
        assertEquals(2, ch.getNumCallbacks());
        //These values are only set if call back handler doesn't throw
        //exceptions
        if (callbackException == null && !doNotHandleCallbacks) {
            assertEquals(Messages.PROMPT_USERNAME.getText(), ch.getNamePrompt());
            assertEquals(Messages.PROMPT_PASSWORD.getText(), ch.getPasswordPrompt());
            assertNull(ch.getDefaultName());
        }
        return e;
    }
    return null;
}

From source file:org.sakaiproject.component.kerberos.user.KerberosUserDirectoryProvider.java

/**
 * Check if the user id is known to kerberos.
 * //  w ww  .  j a  va  2 s. c o m
 * @param user
 *        The user id.
 * @return true if successful, false if not.
 */
private boolean userKnownToKerberos(String user) {
    // use a dummy password
    String pw = "dummy";

    // Obtain a LoginContext, needed for authentication.
    // Tell it to use the LoginModule implementation specified
    // in the JAAS login configuration file and to use
    // use the specified CallbackHandler.
    LoginContext lc = null;
    try {
        CallbackHandler t = new UsernamePasswordCallback(user, pw);
        lc = new LoginContext(m_logincontext, t);
    } catch (LoginException le) {
        if (M_log.isDebugEnabled())
            M_log.debug("useKnownToKerberos(): " + le.toString());
        return false;
    } catch (SecurityException se) {
        if (M_log.isDebugEnabled())
            M_log.debug("useKnownToKerberos(): " + se.toString());
        return false;
    }

    try {
        // attempt authentication
        lc.login();
        lc.logout();

        if (M_log.isDebugEnabled())
            M_log.debug("useKnownToKerberos(" + user + "): Kerberos auth success");

        return true;
    } catch (LoginException le) {
        String msg = le.getMessage();

        // if this is the message, the user was good, the password was bad
        if (msg.startsWith(m_knownusermsg)) {
            if (M_log.isDebugEnabled())
                M_log.debug("userKnownToKerberos(" + user + "): Kerberos user known (bad pw)");

            return true;
        }

        // the other message is when the user is bad:
        if (M_log.isDebugEnabled())
            M_log.debug("userKnownToKerberos(" + user + "): Kerberos user unknown or invalid");

        return false;
    }

}