Example usage for javax.security.auth.callback TextOutputCallback TextOutputCallback

List of usage examples for javax.security.auth.callback TextOutputCallback TextOutputCallback

Introduction

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

Prototype

public TextOutputCallback(int messageType, String message) 

Source Link

Document

Construct a TextOutputCallback with a message type and message to be displayed.

Usage

From source file:com.ideabase.repository.core.auth.RepositoryLoginModule.java

/**
 * Perform Lucene index search by user name and user password. <br>
 * if indexer returns some value. it means the requset is from valid user.<br>
 * send another callback request, this callback request pack the user id.<br>
 * the callback handler can retrieve user object from storage.<br>
 * create new {@see RepositoryUserPrincipal}. <br>
 */// w w w  .  j  a  va2  s  .  com
private void authenticateUser(final String pUserName, final String pUserPassword)
        throws LoginException, IOException, UnsupportedCallbackException {

    final String userHashKey = DigestUtils.shaHex(pUserName + pUserPassword);
    final User user = API.giveMe().repositoryService().getItemByTitle(userHashKey, User.class);
    if (user != null) {
        final Integer userId = user.getId();
        // send other callback request with user id.
        Callback[] callbacks = new Callback[1];
        callbacks[0] = new TextOutputCallback(TextOutputCallback.INFORMATION, String.valueOf(userId));
        mCallbackHandler.handle(callbacks);

        if (DEBUG) {
            LOG.debug("Search result id - " + userId);
        }

        // create repository user principal
        if (!user.isAdmin()) {
            mUser = new RepositoryUserPrincipal(PRINCIPAL_USER, userId);
        } else {
            mUser = new RepositoryUserPrincipal(PRINCIPAL_ADMIN, userId);
        }
    } else {
        throw new LoginException("Login failed. Invalid user or password.");
    }
}

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.ja 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.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 {/* ww w.  j a  va2s .c om*/
        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;
}