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

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

Introduction

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

Prototype

public NameCallback(String prompt, String defaultName) 

Source Link

Document

Construct a NameCallback with a prompt and default name.

Usage

From source file:com.delphix.session.impl.sasl.PlainSaslServer.java

@Override
protected byte[] evaluate(byte[] message) throws SaslException {
    // Parse the SASL message
    String[] userInfo = parse(message);

    // Perform authentication
    String prompt = getMechanismName() + " authentication ID: ";
    NameCallback nc = new NameCallback(prompt, userInfo[1]);
    AuthenticateCallback ac = new AuthenticateCallback(userInfo[2]);

    invokeCallbacks(nc, ac);// w ww .jav  a  2s .  c  om

    if (!ac.isAuthenticated()) {
        throw new SaslException("sasl authentication failed");
    }

    // Perform authorization
    AuthorizeCallback az = new AuthorizeCallback(userInfo[1], userInfo[0]);

    invokeCallbacks(az);

    if (az.isAuthorized()) {
        authorizationId = az.getAuthorizedID();
    } else {
        throw new SaslException();
    }

    // Mark the SASL server completed
    setComplete();

    return null;
}

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  av a  2 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.nuxeo.ecm.platform.login.NuxeoLoginModule.java

@SuppressWarnings({ "unchecked" })
protected NuxeoPrincipal getPrincipal() throws LoginException {
    UserIdentificationInfo userIdent = null;

    // **** init the callbacks
    // Std login/password callbacks
    NameCallback nc = new NameCallback("Username: ", SecurityConstants.ANONYMOUS);
    PasswordCallback pc = new PasswordCallback("Password: ", false);

    // Nuxeo specific cb : handle LoginPlugin initialization
    UserIdentificationInfoCallback uic = new UserIdentificationInfoCallback();

    // JBoss specific cb : handle web=>ejb propagation
    // SecurityAssociationCallback ac = new SecurityAssociationCallback();
    // ObjectCallback oc = new ObjectCallback("UserInfo:");

    // **** handle callbacks
    // We can't check the callback handler class to know what will be
    // supported/*w w w . jav  a 2s.c o m*/
    // because the cbh is wrapped by JAAS
    // => just try and swalow exceptions
    // => will be externalised to plugins via EP to avoid JBoss dependency
    boolean cb_handled = false;

    try {
        // only try this cbh when called from the web layer
        if (useUserIdentificationInfoCB) {
            callbackHandler.handle(new Callback[] { uic });
            // First check UserInfo CB return
            userIdent = uic.getUserInfo();
            cb_handled = true;
        }
    } catch (UnsupportedCallbackException e) {
        log.debug("UserIdentificationInfoCallback is not supported");
    } catch (IOException e) {
        log.warn("Error calling callback handler with UserIdentificationInfoCallback : " + e.getMessage());
    }

    Principal principal = null;
    Object credential = null;

    if (!cb_handled) {
        CallbackResult result = loginPluginManager.handleSpecifcCallbacks(callbackHandler);

        if (result != null && result.cb_handled) {
            if (result.userIdent != null && result.userIdent.containsValidIdentity()) {
                userIdent = result.userIdent;
                cb_handled = true;
            } else {
                principal = result.principal;
                credential = result.credential;
                if (principal != null) {
                    cb_handled = true;
                }
            }
        }
    }

    if (!cb_handled) {
        try {
            // Std CBH : will only works for L/P
            callbackHandler.handle(new Callback[] { nc, pc });
            cb_handled = true;
        } catch (UnsupportedCallbackException e) {
            LoginException le = new LoginException("Authentications Failure - " + e.getMessage());
            le.initCause(e);
        } catch (IOException e) {
            LoginException le = new LoginException("Authentications Failure - " + e.getMessage());
            le.initCause(e);
        }
    }

    // Login via the Web Interface : may be using a plugin
    if (userIdent != null && userIdent.containsValidIdentity()) {
        NuxeoPrincipal nxp = validateUserIdentity(userIdent);

        if (nxp != null) {
            sharedState.put("javax.security.auth.login.name", nxp.getName());
            sharedState.put("javax.security.auth.login.password", userIdent);
        }
        return nxp;
    }

    if (LoginComponent.isSystemLogin(principal)) {
        return new SystemPrincipal(principal.getName());
    }
    // if (principal instanceof NuxeoPrincipal) { // a nuxeo principal
    // return validatePrincipal((NuxeoPrincipal) principal);
    // } else
    if (principal != null) { // a non null principal
        String password = null;
        if (credential instanceof char[]) {
            password = new String((char[]) credential);
        } else if (credential != null) {
            password = credential.toString();
        }
        return validateUsernamePassword(principal.getName(), password);
    } else { // we don't have a principal - try the username &
        // password
        String username = nc.getName();
        if (username == null) {
            return null;
        }
        char[] password = pc.getPassword();
        return validateUsernamePassword(username, password != null ? new String(password) : null);
    }
}

From source file:org.nuxeo.ecm.platform.login.test.DummyNuxeoLoginModule.java

@SuppressWarnings({ "unchecked" })
protected NuxeoPrincipal getPrincipal() throws LoginException {
    UserIdentificationInfo userIdent = null;

    // **** init the callbacks
    // Std login/password callbacks
    NameCallback nc = new NameCallback("Username: ", SecurityConstants.ANONYMOUS);
    PasswordCallback pc = new PasswordCallback("Password: ", false);

    // Nuxeo specific cb : handle LoginPlugin initialization
    UserIdentificationInfoCallback uic = new UserIdentificationInfoCallback();

    // JBoss specific cb : handle web=>ejb propagation
    // SecurityAssociationCallback ac = new SecurityAssociationCallback();
    // ObjectCallback oc = new ObjectCallback("UserInfo:");

    // **** handle callbacks
    // We can't check the callback handler class to know what will be
    // supported//from  w  ww .j ava  2  s.  c o  m
    // because the cbh is wrapped by JAAS
    // => just try and swalow exceptions
    // => will be externalised to plugins via EP to avoid JBoss dependency
    boolean cb_handled = false;

    try {
        // only try this cbh when called from the web layer
        if (useUserIdentificationInfoCB) {
            callbackHandler.handle(new Callback[] { uic });
            // First check UserInfo CB return
            userIdent = uic.getUserInfo();
            cb_handled = true;
        }
    } catch (UnsupportedCallbackException e) {
        log.debug("UserIdentificationInfoCallback is not supported");
    } catch (IOException e) {
        log.warn("Error calling callback handler with UserIdentificationInfoCallback : " + e.getMessage());
    }

    Principal principal = null;
    Object credential = null;

    if (!cb_handled) {
        CallbackResult result = loginPluginManager.handleSpecifcCallbacks(callbackHandler);

        if (result != null && result.cb_handled) {
            if (result.userIdent != null && result.userIdent.containsValidIdentity()) {
                userIdent = result.userIdent;
                cb_handled = true;
            } else {
                principal = result.principal;
                credential = result.credential;
                if (principal != null) {
                    cb_handled = true;
                }
            }
        }
    }

    if (!cb_handled) {
        try {
            // Std CBH : will only works for L/P
            callbackHandler.handle(new Callback[] { nc, pc });
            cb_handled = true;
        } catch (UnsupportedCallbackException e) {
            LoginException le = new LoginException("Authentications Failure - " + e.getMessage());
            le.initCause(e);
        } catch (IOException e) {
            LoginException le = new LoginException("Authentications Failure - " + e.getMessage());
            le.initCause(e);
        }
    }

    try {
        // Login via the Web Interface : may be using a plugin
        if (userIdent != null && userIdent.containsValidIdentity()) {
            NuxeoPrincipal nxp = validateUserIdentity(userIdent);

            if (nxp != null) {
                sharedState.put("javax.security.auth.login.name", nxp.getName());
                sharedState.put("javax.security.auth.login.password", userIdent);
            }
            return nxp;
        }

        if (LoginComponent.isSystemLogin(principal)) {
            return new SystemPrincipal(principal.getName());
        }

        if (principal != null) { // a non null principal
            String password = null;
            if (credential instanceof char[]) {
                password = new String((char[]) credential);
            } else if (credential != null) {
                password = credential.toString();
            }
            return validateUsernamePassword(principal.getName(), password);
        } else { // we don't have a principal - try the username &
            // password
            String username = nc.getName();
            if (username == null) {
                return null;
            }
            char[] password = pc.getPassword();
            return validateUsernamePassword(username, password != null ? new String(password) : null);
        }
    } catch (LoginException e) {
        throw e;
    } catch (Exception e) {
        // jboss catches LoginException, so show it at least in the logs
        String msg = "Authentication failed: " + e.getMessage();
        log.error(msg, e);
        throw (LoginException) new LoginException(msg).initCause(e);
    }
}

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;/*  w w  w . ja va 2  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:gov.nih.nci.ncicb.cadsr.common.security.jboss.DBLoginModule.java

protected String[] getUsernameAndPassword() throws LoginException {
    String[] info = { null, null };
    if (callbackHandler == null) {
        throw new LoginException("Error: no CallbackHandler available to collect authentication information");
    }//from   w  w  w .  ja  v a  2s .c  o m
    NameCallback nc = new NameCallback("User name: ", "guest");
    PasswordCallback pc = new PasswordCallback("Password: ", false);
    Callback[] callbacks = { nc, pc };
    String username = null;
    String password = null;
    try {
        callbackHandler.handle(callbacks);
        username = nc.getName();
        char[] tmpPassword = pc.getPassword();
        if (tmpPassword != null) {
            credential = new char[tmpPassword.length];
            System.arraycopy(tmpPassword, 0, credential, 0, tmpPassword.length);
            pc.clearPassword();
            password = new String(credential);
        }
    } catch (IOException ioe) {
        throw new LoginException(ioe.toString());
    } catch (UnsupportedCallbackException uce) {
        throw new LoginException("CallbackHandler does not support: " + uce.getCallback());
    }
    info[0] = username;
    info[1] = password;
    logger.debug("Username=" + username);
    return info;
}

From source file:org.betaconceptframework.astroboa.engine.service.security.AstroboaLogin.java

/**
 * /* w  ww . jav a 2  s  . co m*/
 * TAKEN FROM Jboss class
 *  
 * org.jboss.security.auth.spi.UsernamePasswordLoginModule
 * 
 * and adjust it to Astroboa requirements
 * 
 * @return
 * @throws LoginException
 */
private String[] getAuthenticationInformation() throws LoginException {
    String[] info = { null, null, null, null, null };
    // prompt for a username and password
    if (callbackHandler == null) {
        throw new LoginException(
                "Error: no CallbackHandler available " + "to collect authentication information");
    }

    NameCallback nc = new NameCallback("User name: ", "guest");
    PasswordCallback pc = new PasswordCallback("Password: ", false);
    AstroboaAuthenticationCallback authenticationCallback = new AstroboaAuthenticationCallback(
            "Astroboa authentication info");

    Callback[] callbacks = { nc, pc, authenticationCallback };
    String username = null;
    String password = null;
    String identityStoreLocation = null;
    String userSecretKey = null;
    String repositoryId = null;

    try {
        callbackHandler.handle(callbacks);
        username = nc.getName();
        char[] tmpPassword = pc.getPassword();
        if (tmpPassword != null) {
            char[] credential = new char[tmpPassword.length];
            System.arraycopy(tmpPassword, 0, credential, 0, tmpPassword.length);
            pc.clearPassword();
            password = new String(credential);
        }

        identityStoreLocation = authenticationCallback.getIdentityStoreLocation();

        useExternalIdentity = authenticationCallback.isExternalIdentityStore();

        userSecretKey = authenticationCallback.getSecretKey();

        repositoryId = authenticationCallback.getRepositoryId();
    } catch (IOException e) {
        LoginException le = new LoginException("Failed to get username/password");
        le.initCause(e);
        throw le;
    } catch (UnsupportedCallbackException e) {
        LoginException le = new LoginException("CallbackHandler does not support: " + e.getCallback());
        le.initCause(e);
        throw le;
    }
    info[0] = username;
    info[1] = password;
    info[2] = userSecretKey;
    info[3] = identityStoreLocation;
    info[4] = repositoryId;

    return info;
}