Example usage for javax.security.auth.callback NameCallback getName

List of usage examples for javax.security.auth.callback NameCallback getName

Introduction

In this page you can find the example usage for javax.security.auth.callback NameCallback getName.

Prototype

public String getName() 

Source Link

Document

Get the retrieved name.

Usage

From source file:com.redhat.topicindex.security.FedoraAccountSystem.java

public boolean login() throws LoginException {
    if (callbackHandler == null)
        throw new LoginException("No CallbackHandler available");

    NameCallback nameCallback = new NameCallback("Username");
    PasswordCallback passwordCallback = new PasswordCallback("Password", false);

    Callback[] callbacks = new Callback[] { nameCallback, passwordCallback };

    try {/*from w  w  w .  j a v  a2  s  .co m*/
        callbackHandler.handle(callbacks);

        username = nameCallback.getName();
        password = passwordCallback.getPassword();
        passwordCallback.clearPassword();
    } catch (IOException e) {
        throw new LoginException(e.toString());
    } catch (UnsupportedCallbackException e) {
        throw new LoginException("Error: " + e.getCallback().toString() + "not available");
    }

    if (authenticate()) {
        loginSucceeded = true;
    } else {
        return false;
    }

    return true;
}

From source file:de.adorsys.oauth.loginmodule.HTTPAuthenticationLoginModule.java

@Override
public boolean login() throws LoginException {

    NameCallback nameCallback = new NameCallback("name");
    PasswordCallback passwordCallback = new PasswordCallback("password", false);
    try {/*from w ww.jav a2  s .c om*/
        callbackHandler.handle(new Callback[] { nameCallback, passwordCallback });
    } catch (Exception x) {
        throw new LoginException(x.getMessage());
    }

    String username = nameCallback.getName();
    char[] passwordChars = passwordCallback.getPassword();
    String password = passwordChars == null ? null : new String(passwordChars);

    LOG.info("login {}", username);

    try {

        return authenticate(username, password);

    } catch (Exception e) {
        throw new LoginException(e.getMessage());
    }
}

From source file:edu.vt.middleware.ldap.jaas.AbstractLoginModule.java

/**
 * This will store the supplied name, password, and entry dn in the stored
 * state map. storePass must be set for this method to have any affect.
 *
 * @param  nameCb  to store//  ww  w  .ja  v a2  s.c  om
 * @param  passCb  to store
 * @param  loginDn  to store
 */
@SuppressWarnings("unchecked")
protected void storeCredentials(final NameCallback nameCb, final PasswordCallback passCb,
        final String loginDn) {
    if (this.storePass) {
        if (nameCb != null && nameCb.getName() != null) {
            this.sharedState.put(LOGIN_NAME, nameCb.getName());
        }
        if (passCb != null && passCb.getPassword() != null) {
            this.sharedState.put(LOGIN_PASSWORD, passCb.getPassword());
        }
        if (loginDn != null) {
            this.sharedState.put(LOGIN_DN, loginDn);
        }
    }
}

From source file:org.polymap.rhei.um.auth.UmLoginModule.java

@Override
public boolean login() throws LoginException {
    Callback label = new TextOutputCallback(TextOutputCallback.INFORMATION, dialogTitle);
    NameCallback nameCallback = new NameCallback(i18n.get("username"), "default");
    PasswordCallback passwordCallback = new PasswordCallback(i18n.get("password"), false);
    try {/*from  w ww.j a  v a2  s.  c o m*/
        callbackHandler.handle(new Callback[] { label, nameCallback, passwordCallback });
    } catch (Exception e) {
        log.warn("", e);
        throw new LoginException(e.getLocalizedMessage());
    }

    String username = nameCallback.getName();
    //        if (username == null) {
    //            return false;
    //        }

    // admin
    if (username == null || username.equals("admin")) {
        // FIXME read password hash from persistent storage and check
        log.warn("!!! NO PASSWORD check for admin user yet !!!!!!");
        principal = new UserPrincipal("admin");
        return loggedIn = true;
    }

    // ordinary user
    User user = repo.findUser(username);
    log.info("username: " + user.email().get());

    if (user != null && passwordCallback.getPassword() != null) {
        String password = String.valueOf(passwordCallback.getPassword());
        if (PasswordEncryptor.instance().checkPassword(password, user.passwordHash().get())) {
            log.info("username: " + user.username().get());
            principal = new UmUserPrincipal(user);
            return loggedIn = true;
        }
    }
    return false;
}

From source file:org.sofun.core.api.security.SofunLoginModule.java

@Override
public boolean login() throws LoginException {

    NameCallback nameCallback = new NameCallback("Username");
    PasswordCallback passwordCallback = new PasswordCallback("Password", false);

    Callback[] callbacks = new Callback[] { nameCallback, passwordCallback };
    try {//from  ww  w  . j av  a2 s .co  m
        callbackHandler.handle(callbacks);
    } catch (IOException e) {
        throw new LogConfigurationException(e);
    } catch (UnsupportedCallbackException e) {
        throw new LogConfigurationException(e);
    }

    username = nameCallback.getName();

    /*
     * Member member; try { member = getMemberService().getMember(username); } catch (CoreException e) { throw new
     * LoginException(e.getMessage()); }
     * 
     * final char[] password = passwordCallback.getPassword(); passwordCallback.clearPassword();
     * 
     * 
     * if (member.getPassword() != null) { final char[] mPassword = member.getPassword().toCharArray(); if
     * (mPassword.equals(password)) { user = new SofunPrincipal(username); roles = new SofunPrincipal[] { new
     * SofunPrincipal(SofunRoles.MEMBER) }; loginSucceeded = true; } else { loginSucceeded = false; } } else {
     * loginSucceeded = false; }
     */

    // XXX: JAAS authentication. Application level responsibility for the moment
    // This is principal is not used. We rely on oauth token exchange as well application level credentials for now.
    user = new SofunPrincipal("admin@sofungaming.com");
    roles = new SofunPrincipal[] { new SofunPrincipal(SofunRoles.MEMBER) };
    loginSucceeded = true;

    return loginSucceeded;

}

From source file:net.ontopia.topicmaps.nav2.realm.TMLoginModule.java

/** 
 * Prompt the user for username and password, and verify those.
 *//* www .j a  va2 s.c  om*/
@Override
public boolean login() throws LoginException {
    log.debug("TMLoginModule: login");

    if (callbackHandler == null)
        throw new LoginException(
                "Error: no CallbackHandler available " + "to garner authentication information from the user");

    // prompt for a user name and password
    NameCallback nameCallback = new NameCallback("user name: ");
    PasswordCallback passwordCallback = new PasswordCallback("password: ", false);

    try {
        callbackHandler.handle(new Callback[] { nameCallback, passwordCallback });

        this.username = nameCallback.getName();
        char[] charpassword = passwordCallback.getPassword();
        password = (charpassword == null ? "" : new String(charpassword));
        passwordCallback.clearPassword();

    } catch (java.io.IOException ioe) {
        throw new LoginException(ioe.toString());
    } catch (UnsupportedCallbackException uce) {
        throw new LoginException("Error: " + uce.getCallback()
                + " not available to garner authentication information " + "from the user");
    }
    // verify the username/password
    loginSucceeded = verifyUsernamePassword(username, password);
    return loginSucceeded;
}

From source file:org.polymap.core.security.DummyLoginModule.java

public boolean login() throws LoginException {
    // check if there is a user with "login" password
    for (DummyUserPrincipal candidate : users.values()) {
        if (candidate.getPassword().equals("login")) {
            principal = candidate;/*from   w  ww . j  a v a2  s . c  om*/
            return loggedIn = true;
        }
    }

    try {
        Callback label = new TextOutputCallback(TextOutputCallback.INFORMATION,
                // empty if service login
                StringUtils.defaultIfEmpty(dialogTitle, "POLYMAP3 Workbench"));
        NameCallback nameCallback = new NameCallback(
                StringUtils.defaultIfEmpty(i18n.get("username"), "Username"), "default");
        PasswordCallback passwordCallback = new PasswordCallback(
                StringUtils.defaultIfEmpty(i18n.get("password"), "Password"), false);

        callbackHandler.handle(new Callback[] { label, nameCallback, passwordCallback });

        String username = nameCallback.getName();

        String password = "";
        if (passwordCallback.getPassword() != null) {
            password = String.valueOf(passwordCallback.getPassword());
        }

        DummyUserPrincipal candidate = userForName(username);
        if (candidate.getPassword().equals(password)) {
            principal = candidate;
            loggedIn = true;
            return true;
        }
        return false;
    } catch (Exception e) {
        log.warn("", e);
        throw new LoginException(e.getLocalizedMessage());
    }
}

From source file:org.getobjects.jaas.GoDefaultLoginModule.java

/**
 * This is the default JAAS Phase 1 implementation, which grabs login/pwd
 * from the CallbackHandler (eg the one provided by the GoHTTPAuthenticator)
 * and calls loginWithUsernameAndPassword() with this information.
 * /*from   w w w  .  j  av  a  2  s  . co  m*/
 * @return true if authentication was successful, false otherwise
 * @throws LoginException
 */
protected boolean loginWithUsernameAndPassword() throws LoginException {
    /* first retrieve username/password */

    NameCallback nc = new NameCallback("login");
    PasswordCallback pc = new PasswordCallback("password", false /* no echo */);

    try {
        this.handler.handle(new Callback[] { nc, pc });
    } catch (IOException ie) {
        log.error("some IO error occurred during Name/PasswordCallback retrieval", ie);
        return false;
    } catch (UnsupportedCallbackException uce) {
        /* token callbacks unsupported, this is OK */
        return false;
    }

    /* then attempt a login */

    return this.loginWithUsernameAndPassword(nc.getName(), pc.getPassword());
}

From source file:org.apache.jackrabbit.core.security.authentication.AbstractLoginModule.java

/**
 * Method supports tries to acquire a UserID in the follwing order:
 * <ol>//from w ww. j  a va 2  s .  c  o  m
 * <li>If passed credentials are {@link GuestCredentials} the anonymous user id
 * is returned.</li>
 * <li>Try to access it from the {@link Credentials} via {@link
 * SimpleCredentials#getUserID()}</li>
 * <li>Ask CallbackHandler for User-ID with use of {@link NameCallback}.</li>
 * <li>Test if the 'sharedState' contains a login name.</li>
 * <li>Fallback: return the anonymous UserID.</li>
 * </ol>
 *
 * @param credentials which, may contain a User-ID
 * @return The userId retrieved from the credentials or by any other means
 * described above.
 * @see #login()
 */
protected String getUserID(Credentials credentials) {
    String userId = null;
    if (credentials != null) {
        if (credentials instanceof GuestCredentials) {
            userId = anonymousId;
        } else if (credentials instanceof SimpleCredentials) {
            userId = ((SimpleCredentials) credentials).getUserID();
        } else {
            try {
                NameCallback callback = new NameCallback("User-ID: ");
                callbackHandler.handle(new Callback[] { callback });
                userId = callback.getName();
            } catch (UnsupportedCallbackException e) {
                log.warn("Credentials- or NameCallback must be supported");
            } catch (IOException e) {
                log.error("Name-Callback failed: " + e.getMessage());
            }
        }
    }
    if (userId == null && sharedState.containsKey(KEY_LOGIN_NAME)) {
        userId = (String) sharedState.get(KEY_LOGIN_NAME);
    }

    // still no userId -> anonymousID if its has been defined.
    // TODO: check again if correct when used with 'extendedAuth'
    if (userId == null) {
        userId = anonymousId;
    }
    return userId;
}

From source file:com.pymmasoftware.platform.login.loginmodule.DroolsLoginModule.java

@Override
public boolean login() throws LoginException {
    succeeded = false;//from  w  w  w  . ja va 2  s . c om
    QueryRunner queryRunner = null;
    try {
        userPrincipal = null;
        roles = null;
        if (callbackHandler == null)
            throw new LoginException("No callback handler");

        NameCallback nameCallback = new NameCallback("Username");
        PasswordCallback passwordCallback = new PasswordCallback("Password", false);

        Callback[] callbacks = new Callback[] { nameCallback, passwordCallback };
        try {
            callbackHandler.handle(callbacks);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedCallbackException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        username = nameCallback.getName();
        password = new String(passwordCallback.getPassword());

        queryRunner = new QueryRunner(dataSource);

        // Create a ResultSetHandler implementation to convert the
        // first row into an Object[].
        ResultSetHandler<DroolsPrincipal> h = new ResultSetHandler<DroolsPrincipal>() {
            public DroolsPrincipal handle(ResultSet rs) throws SQLException {
                if (!rs.next()) {
                    return null;
                }

                ResultSetMetaData meta = rs.getMetaData();
                String userName = rs.getString("username");

                DroolsPrincipal droolsPrincipal = new DroolsPrincipal(userName);
                droolsPrincipal.setId(rs.getInt("id"));

                return droolsPrincipal;
            }
        };
        ResultSetHandler<List<String>> hh = new ResultSetHandler<List<String>>() {
            public List<String> handle(ResultSet rs) throws SQLException {
                if (!rs.next()) {
                    return null;
                }
                List<String> droolsGroups = new ArrayList<>();
                boolean goOne = true;
                while (goOne) {
                    String groupName = rs.getString("groups");

                    droolsGroups.add(groupName);
                    if (rs.next() == false) {
                        goOne = false;
                    }
                }
                return droolsGroups;
            }
        };

        String sqlname = "select * from guvnorusers where username = ? and password = ? ";
        DroolsPrincipal user = queryRunner.query(sqlname, h, username, password);
        if (user == null) {
            succeeded = false;
            throw new FailedLoginException("The username or The password is incorrect");
        } else {

            userPrincipal = user;
            String sqlname2 = "select groups from guvnorgroups gr,guvnorusers_groups gr_user "
                    + "where gr.id = gr_user.groups_id  " + "and gr_user.guvnorusers_id= ?";
            List<String> droolsGroups = queryRunner.query(sqlname2, hh, user.getId());
            if (droolsGroups != null) {
                int i = droolsGroups.size();
                roles = new String[i];
                i = 0;
                for (String droolsGroup : droolsGroups) {
                    roles[i] = droolsGroup;
                    i++;
                }
            }
            succeeded = true;
            return true;
        }

    } catch (Exception e) {
        throw new LoginException(e.getMessage());
    } finally {
        queryRunner = null;
    }

}