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

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

Introduction

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

Prototype

public void setName(String name) 

Source Link

Document

Set the retrieved name.

Usage

From source file:it.cnr.icar.eric.client.xml.registry.jaas.DialogAuthenticationCallbackHandler.java

/** Implementation of the handle method specified by
 * <code> javax.security.auth.callback.CallbackHandler </code>
 * @param callbacks <code>Array of javax.security.auth.callback.CallbackHandler</code>
 *
 *//* w  w w.  ja  v a 2 s.  com*/
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
    int result = showDialog();

    for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof TextOutputCallback) {
            // Ignore this section for now. This will be used when a generic callback handler
            // is being implemented. In our current implementation, we are only expecting the
            //login type callback handler.
        } else if (callbacks[i] instanceof NameCallback) {
            // prompt the user for a username
            NameCallback nc = (NameCallback) callbacks[i];
            String strPrompt = nc.getPrompt();
            String strName = "";

            //                if (strPrompt.equals(authResBundle.getString("Keystore alias: "))) {
            if (strPrompt.equals(authResBundle.getString("Keystore.alias."))) {
                strName = txtAlias.getText();
            }

            nc.setName(strName);
        } else if (callbacks[i] instanceof PasswordCallback) {
            // prompt the user for sensitive information
            PasswordCallback pc = (PasswordCallback) callbacks[i];
            String strPrompt = pc.getPrompt();
            char[] chrPass = new char[0];

            //                if (strPrompt.equals(authResBundle.getString("Keystore password: "))) {
            if (strPrompt.equals(authResBundle.getString("Keystore.password."))) {
                /* As of now hide the Keystore password part from the user and
                   read directly from the properties file
                   chrPass = txtStorepass.getPassword() ; */
                chrPass = ProviderProperties.getInstance().getProperty("jaxr-ebxml.security.storepass")
                        .toCharArray();

                if ((chrPass == null) || (chrPass.length == 0)) {
                    log.error("Property jaxr-ebxml.security.storepass is undefined");
                }
                //                } else if (strPrompt.equals(authResBundle.getString("Private key password (optional): "))) {
            } else if (strPrompt.equals(authResBundle.getString("Private.key.password.optional."))) {
                chrPass = txtKeypass.getPassword();
            }

            pc.setPassword(chrPass);
        } else if (callbacks[i] instanceof ConfirmationCallback) {
            ConfirmationCallback cc = (ConfirmationCallback) callbacks[i];

            if (result == OK) {
                cc.setSelectedIndex(ConfirmationCallback.OK);
            } else {
                cc.setSelectedIndex(ConfirmationCallback.CANCEL);
            }
        } else {
            throw new UnsupportedCallbackException(callbacks[i],
                    JAXRResourceBundle.getInstance().getString("message.error.unrecognized.callback"));
        }
    }
}

From source file:com.fluffypeople.managesieve.ManageSieveClient.java

/**
 * Authenticate against the remote server using SAS, using the given
 * username and password./*from   w  w  w. jav a2  s . c o  m*/
 *
 * @param username String username to authenticate with.
 * @param password String password to authenticate with.
 * @return OK on success, NO otherwise.
 */
public synchronized ManageSieveResponse authenticate(final String username, final String password)
        throws SaslException, IOException, ParseException {
    CallbackHandler cbh = new CallbackHandler() {

        @Override
        public void handle(Callback[] clbcks) throws IOException, UnsupportedCallbackException {
            for (Callback cb : clbcks) {
                if (cb instanceof NameCallback) {
                    NameCallback name = (NameCallback) cb;
                    name.setName(username);
                } else if (cb instanceof PasswordCallback) {
                    PasswordCallback passwd = (PasswordCallback) cb;
                    passwd.setPassword(password.toCharArray());
                }
            }
        }
    };
    return authenticate(cbh);
}

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

/**
 * This attempts to retrieve credentials for the supplied name and password
 * callbacks. If useFirstPass or tryFirstPass is set, then name and password
 * data is retrieved from shared state. Otherwise a callback handler is used
 * to get the data. Set useCallback to force a callback handler to be used.
 *
 * @param  nameCb  to set name for/* w ww.j a  v  a 2s  .co m*/
 * @param  passCb  to set password for
 * @param  useCallback  whether to force a callback handler
 *
 * @throws  LoginException  if the callback handler fails
 */
protected void getCredentials(final NameCallback nameCb, final PasswordCallback passCb,
        final boolean useCallback) throws LoginException {
    if (this.logger.isTraceEnabled()) {
        this.logger.trace("Begin getCredentials");
        this.logger.trace("  useFistPass = " + this.useFirstPass);
        this.logger.trace("  tryFistPass = " + this.tryFirstPass);
        this.logger.trace("  useCallback = " + useCallback);
        this.logger.trace("  callbackhandler class = " + this.callbackHandler.getClass().getName());
        this.logger.trace("  name callback class = " + nameCb.getClass().getName());
        this.logger.trace("  password callback class = " + passCb.getClass().getName());
    }
    try {
        if ((this.useFirstPass || this.tryFirstPass) && !useCallback) {
            nameCb.setName((String) this.sharedState.get(LOGIN_NAME));
            passCb.setPassword((char[]) this.sharedState.get(LOGIN_PASSWORD));
        } else if (this.callbackHandler != null) {
            this.callbackHandler.handle(new Callback[] { nameCb, passCb });
        } else {
            throw new LoginException("No CallbackHandler available. "
                    + "Set useFirstPass, tryFirstPass, or provide a CallbackHandler");
        }
    } catch (IOException e) {
        if (this.logger.isErrorEnabled()) {
            this.logger.error("Error reading data from callback handler", e);
        }
        this.loginSuccess = false;
        throw new LoginException(e.getMessage());
    } catch (UnsupportedCallbackException e) {
        if (this.logger.isErrorEnabled()) {
            this.logger.error("Unsupported callback", e);
        }
        this.loginSuccess = false;
        throw new LoginException(e.getMessage());
    }
}

From source file:nl.nn.adapterframework.util.CredentialFactory.java

public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    log.info("Handling callbacks for alias [" + getAlias() + "]");
    for (int i = 0; i < callbacks.length; i++) {
        Callback cb = callbacks[i];
        //         log.info(i+") "+cb.getClass().getName()+" "+ToStringBuilder.reflectionToString(cb));
        Class cbc = cb.getClass();
        if (cbc.getName().endsWith("MappingPropertiesCallback")) { // Websphere 6
            try {
                Map mappingProperties = new HashMap();
                mappingProperties.put("com.ibm.mapping.authDataAlias", getAlias());
                ClassUtils.invokeSetter(cb, "setProperties", mappingProperties, Map.class);
                log.debug(//from w w w . j  a va 2s  . c  om
                        "MappingPropertiesCallback.properties set to entry key [com.ibm.mapping.authDataAlias], value ["
                                + getAlias() + "]");
                continue;
            } catch (Exception e) {
                log.warn("exception setting alias [" + getAlias() + "] on MappingPropertiesCallback", e);
            }
        }
        if (cbc.getName().endsWith("AuthDataAliasCallback")) { // Websphere 5
            try {
                log.info("setting alias of AuthDataAliasCallback to alias [" + getAlias() + "]");
                ClassUtils.invokeSetter(cb, "setAlias", getAlias());
                continue;
            } catch (Exception e) {
                log.warn("exception setting alias [" + getAlias() + "] on AuthDataAliasCallback", e);
            }
        }
        if (cb instanceof NameCallback) {
            NameCallback ncb = (NameCallback) cb;
            log.info("setting name of NameCallback to alias [" + getAlias() + "]");
            ncb.setName(getAlias());
            continue;
        }
        log.debug("ignoring callback of type [" + cb.getClass().getName() + "] for alias [" + getAlias() + "]");
        //         log.debug("contents of callback ["+ToStringBuilder.reflectionToString(cb)+"]");
        //         Class itf[] = cbc.getInterfaces();
        //         for (int j=0; j<itf.length; j++) {
        //            log.info("interface "+j+": "+itf[j].getName());
        //         }
        //         Method methods[] = cbc.getMethods();
        //         for (int j=0; j<methods.length; j++) {
        //            log.info("method "+j+": "+methods[j].getName()+", "+methods[j].toString());
        //         }
        //         if (cb instanceof ChoiceCallback) {
        //            ChoiceCallback ccb = (ChoiceCallback) cb;
        //            log.info("ChoiceCallback: "+ccb.getPrompt());
        //         }

    }
    log.info("Handled callbacks for alias [" + getAlias() + "]");
}

From source file:org.alfresco.filesys.auth.cifs.EnterpriseCifsAuthenticator.java

/**
 * JAAS callback handler//  www. ja v  a  2 s .  c o  m
 * 
 * @param callbacks Callback[]
 * @exception IOException
 * @exception UnsupportedCallbackException
 */
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    // Process the callback list

    for (int i = 0; i < callbacks.length; i++) {
        // Request for user name

        if (callbacks[i] instanceof NameCallback) {
            NameCallback cb = (NameCallback) callbacks[i];
            // cb.setName(m_accountName);
            cb.setName("");
        }

        // Request for password
        else if (callbacks[i] instanceof PasswordCallback) {
            PasswordCallback cb = (PasswordCallback) callbacks[i];
            cb.setPassword(m_password.toCharArray());
        }

        // Request for realm

        else if (callbacks[i] instanceof RealmCallback) {
            RealmCallback cb = (RealmCallback) callbacks[i];
            cb.setText(m_krbRealm);
        } else {
            throw new UnsupportedCallbackException(callbacks[i]);
        }
    }
}

From source file:org.alfresco.repo.webdav.auth.BaseKerberosAuthenticationFilter.java

/**
 * JAAS callback handler/*from   w w  w . j  a  v a2 s . c o  m*/
 * 
 * @param callbacks Callback[]
 * @exception IOException
 * @exception UnsupportedCallbackException
 */
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    // Process the callback list
    if (getLogger().isDebugEnabled())
        getLogger().debug("Processing the JAAS callback list of " + callbacks.length + " items.");
    for (int i = 0; i < callbacks.length; i++) {
        // Request for user name

        if (callbacks[i] instanceof NameCallback) {
            if (getLogger().isDebugEnabled())
                getLogger().debug("Request for user name.");
            NameCallback cb = (NameCallback) callbacks[i];
            cb.setName(m_accountName);
        }

        // Request for password
        else if (callbacks[i] instanceof PasswordCallback) {
            if (getLogger().isDebugEnabled())
                getLogger().debug("Request for password.");
            PasswordCallback cb = (PasswordCallback) callbacks[i];
            cb.setPassword(m_password.toCharArray());
        }

        // Request for realm

        else if (callbacks[i] instanceof RealmCallback) {
            if (getLogger().isDebugEnabled())
                getLogger().debug("Request for realm.");
            RealmCallback cb = (RealmCallback) callbacks[i];
            cb.setText(m_krbRealm);
        } else {
            throw new UnsupportedCallbackException(callbacks[i]);
        }
    }
}

From source file:org.alfresco.web.site.servlet.SSOAuthenticationFilter.java

/**
 * JAAS callback handler//w w  w .  ja v a2 s  .  c o  m
 * 
 * @param callbacks Callback[]
 * @exception IOException
 * @exception UnsupportedCallbackException
 */
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    if (logger.isDebugEnabled())
        logger.debug("Processing the JAAS callback list of " + callbacks.length + " items.");
    for (int i = 0; i < callbacks.length; i++) {
        // Request for user name

        if (callbacks[i] instanceof NameCallback) {
            if (logger.isDebugEnabled())
                logger.debug("Request for user name.");
            NameCallback cb = (NameCallback) callbacks[i];
            cb.setName(krbAccountName);
        }

        // Request for password
        else if (callbacks[i] instanceof PasswordCallback) {
            if (logger.isDebugEnabled())
                logger.debug("Request for password.");
            PasswordCallback cb = (PasswordCallback) callbacks[i];
            cb.setPassword(krbPassword.toCharArray());
        }

        // Request for realm

        else if (callbacks[i] instanceof RealmCallback) {
            if (logger.isDebugEnabled())
                logger.debug("Request for realm.");
            RealmCallback cb = (RealmCallback) callbacks[i];
            cb.setText(krbRealm);
        } else {
            throw new UnsupportedCallbackException(callbacks[i]);
        }
    }
}

From source file:org.apache.accumulo.core.rpc.SaslClientDigestCallbackHandler.java

@Override
public void handle(Callback[] callbacks) throws UnsupportedCallbackException {
    NameCallback nc = null;
    PasswordCallback pc = null;//  w  w  w.  jav a  2s .c  o  m
    RealmCallback rc = null;
    for (Callback callback : callbacks) {
        if (callback instanceof RealmChoiceCallback) {
            continue;
        } else if (callback instanceof NameCallback) {
            nc = (NameCallback) callback;
        } else if (callback instanceof PasswordCallback) {
            pc = (PasswordCallback) callback;
        } else if (callback instanceof RealmCallback) {
            rc = (RealmCallback) callback;
        } else {
            throw new UnsupportedCallbackException(callback, "Unrecognized SASL client callback");
        }
    }
    if (nc != null) {
        log.debug("SASL client callback: setting username: {}", userName);
        nc.setName(userName);
    }
    if (pc != null) {
        log.debug("SASL client callback: setting userPassword");
        pc.setPassword(userPassword);
    }
    if (rc != null) {
        log.debug("SASL client callback: setting realm: {}", rc.getDefaultText());
        rc.setText(rc.getDefaultText());
    }
}

From source file:org.apache.atlas.web.filters.AtlasAuthenticationKerberosFilterTest.java

protected Subject loginTestUser() throws LoginException, IOException {
    LoginContext lc = new LoginContext(TEST_USER_JAAS_SECTION, new CallbackHandler() {

        @Override/* ww w . j  av a2  s . c om*/
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback callback : callbacks) {
                if (callback instanceof PasswordCallback) {
                    PasswordCallback passwordCallback = (PasswordCallback) callback;
                    passwordCallback.setPassword(TESTPASS.toCharArray());
                }
                if (callback instanceof NameCallback) {
                    NameCallback nameCallback = (NameCallback) callback;
                    nameCallback.setName(TESTUSER);
                }
            }
        }
    });
    // attempt authentication
    lc.login();
    return lc.getSubject();
}

From source file:org.apache.atlas.web.filters.MetadataAuthenticationKerberosFilterIT.java

protected Subject loginTestUser() throws LoginException, IOException {
    LoginContext lc = new LoginContext(TEST_USER_JAAS_SECTION, new CallbackHandler() {

        @Override//from  ww w.j  a  va 2s  .  com
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (int i = 0; i < callbacks.length; i++) {
                if (callbacks[i] instanceof PasswordCallback) {
                    PasswordCallback passwordCallback = (PasswordCallback) callbacks[i];
                    passwordCallback.setPassword(TESTPASS.toCharArray());
                }
                if (callbacks[i] instanceof NameCallback) {
                    NameCallback nameCallback = (NameCallback) callbacks[i];
                    nameCallback.setName(TESTUSER);
                }
            }
        }
    });
    // attempt authentication
    lc.login();
    return lc.getSubject();
}