Example usage for javax.security.auth.callback PasswordCallback getPassword

List of usage examples for javax.security.auth.callback PasswordCallback getPassword

Introduction

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

Prototype

public char[] getPassword() 

Source Link

Document

Get the retrieved password.

Usage

From source file:org.jasig.cas.client.jaas.CasLoginModule.java

public boolean login() throws LoginException {
    log.debug("Performing login.");
    final NameCallback serviceCallback = new NameCallback("service");
    final PasswordCallback ticketCallback = new PasswordCallback("ticket", false);
    try {//from w  ww .  ja  v a  2s . co m
        this.callbackHandler.handle(new Callback[] { ticketCallback, serviceCallback });
    } catch (final IOException e) {
        log.info("Login failed due to IO exception in callback handler: " + e);
        throw (LoginException) new LoginException("IO exception in callback handler: " + e).initCause(e);
    } catch (final UnsupportedCallbackException e) {
        log.info("Login failed due to unsupported callback: " + e);
        throw (LoginException) new LoginException(
                "Callback handler does not support PasswordCallback and TextInputCallback.").initCause(e);
    }

    if (ticketCallback.getPassword() != null) {
        this.ticket = new TicketCredential(new String(ticketCallback.getPassword()));
        final String service = CommonUtils.isNotBlank(serviceCallback.getName()) ? serviceCallback.getName()
                : this.service;

        if (this.cacheAssertions) {
            synchronized (ASSERTION_CACHE) {
                if (ASSERTION_CACHE.get(ticket) != null) {
                    log.debug("Assertion found in cache.");
                    this.assertion = (Assertion) ASSERTION_CACHE.get(ticket);
                }
            }
        }

        if (this.assertion == null) {
            log.debug("CAS assertion is null; ticket validation required.");
            if (CommonUtils.isBlank(service)) {
                log.info("Login failed because required CAS service parameter not provided.");
                throw new LoginException(
                        "Neither login module nor callback handler provided required service parameter.");
            }
            try {
                if (log.isDebugEnabled()) {
                    log.debug("Attempting ticket validation with service=" + service + " and ticket=" + ticket);
                }
                this.assertion = this.ticketValidator.validate(this.ticket.getTicket(), service);

            } catch (final Exception e) {
                log.info("Login failed due to CAS ticket validation failure: " + e);
                throw (LoginException) new LoginException("CAS ticket validation failed: " + e).initCause(e);
            }
        }
        log.info("Login succeeded.");
    } else {
        log.info("Login failed because callback handler did not provide CAS ticket.");
        throw new LoginException("Callback handler did not provide CAS ticket.");
    }
    return true;
}

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//w w w  .j  ava2  s. co  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.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.ja  v a2s . 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);
    }
}