Search in sources :

Example 1 with RealmCallback

use of org.apache.harmony.javax.security.sasl.RealmCallback in project AsmackService by rtreffer.

the class DigestMD5SaslClient method createDigestResponse.

/**
     * Creates the intial response to be sent to the server.
     *
     * @param challenge  Challenge in bytes recived form the Server
     *
     * @return Initial response to be sent to the server
     */
private String createDigestResponse(byte[] challenge) throws SaslException {
    char[] response;
    StringBuffer digestResponse = new StringBuffer(512);
    int realmSize;
    m_dc = new DigestChallenge(challenge);
    m_digestURI = m_protocol + "/" + m_serverName;
    if ((m_dc.getQop() & DigestChallenge.QOP_AUTH) == DigestChallenge.QOP_AUTH)
        m_qopValue = "auth";
    else
        throw new SaslException("Client only supports qop of 'auth'");
    //get call back information
    Callback[] callbacks = new Callback[3];
    ArrayList realms = m_dc.getRealms();
    realmSize = realms.size();
    if (realmSize == 0) {
        callbacks[0] = new RealmCallback("Realm");
    } else if (realmSize == 1) {
        callbacks[0] = new RealmCallback("Realm", (String) realms.get(0));
    } else {
        callbacks[0] = new RealmChoiceCallback("Realm", (String[]) realms.toArray(new String[realmSize]), //the default choice index
        0, //no multiple selections
        false);
    }
    callbacks[1] = new javax.security.auth.callback.PasswordCallback("Password", false);
    if (m_authorizationId == null || m_authorizationId.length() == 0)
        callbacks[2] = new NameCallback("Name");
    else
        callbacks[2] = new NameCallback("Name", m_authorizationId);
    try {
        m_cbh.handle(callbacks);
    } catch (UnsupportedCallbackException e) {
        throw new SaslException("Handler does not support" + " necessary callbacks", e);
    } catch (IOException e) {
        throw new SaslException("IO exception in CallbackHandler.", e);
    }
    if (realmSize > 1) {
        int[] selections = ((RealmChoiceCallback) callbacks[0]).getSelectedIndexes();
        if (selections.length > 0)
            m_realm = ((RealmChoiceCallback) callbacks[0]).getChoices()[selections[0]];
        else
            m_realm = ((RealmChoiceCallback) callbacks[0]).getChoices()[0];
    } else
        m_realm = ((RealmCallback) callbacks[0]).getText();
    m_clientNonce = getClientNonce();
    m_name = ((NameCallback) callbacks[2]).getName();
    if (m_name == null)
        m_name = ((NameCallback) callbacks[2]).getDefaultName();
    if (m_name == null)
        throw new SaslException("No user name was specified.");
    m_HA1 = DigestCalcHA1(m_dc.getAlgorithm(), m_name, m_realm, new String(((javax.security.auth.callback.PasswordCallback) callbacks[1]).getPassword()), m_dc.getNonce(), m_clientNonce);
    response = DigestCalcResponse(m_HA1, m_dc.getNonce(), "00000001", m_clientNonce, m_qopValue, "AUTHENTICATE", m_digestURI, true);
    digestResponse.append("username=\"");
    digestResponse.append(m_authorizationId);
    if (0 != m_realm.length()) {
        digestResponse.append("\",realm=\"");
        digestResponse.append(m_realm);
    }
    digestResponse.append("\",cnonce=\"");
    digestResponse.append(m_clientNonce);
    digestResponse.append("\",nc=");
    //nounce count
    digestResponse.append("00000001");
    digestResponse.append(",qop=");
    digestResponse.append(m_qopValue);
    digestResponse.append(",digest-uri=\"");
    digestResponse.append(m_digestURI);
    digestResponse.append("\",response=");
    digestResponse.append(response);
    digestResponse.append(",charset=utf-8,nonce=\"");
    digestResponse.append(m_dc.getNonce());
    digestResponse.append("\"");
    return digestResponse.toString();
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) SaslException(org.apache.harmony.javax.security.sasl.SaslException) NameCallback(org.apache.harmony.javax.security.auth.callback.NameCallback) RealmChoiceCallback(org.apache.harmony.javax.security.sasl.RealmChoiceCallback) RealmCallback(org.apache.harmony.javax.security.sasl.RealmCallback) Callback(javax.security.auth.callback.Callback) NameCallback(org.apache.harmony.javax.security.auth.callback.NameCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RealmChoiceCallback(org.apache.harmony.javax.security.sasl.RealmChoiceCallback) RealmCallback(org.apache.harmony.javax.security.sasl.RealmCallback)

Example 2 with RealmCallback

use of org.apache.harmony.javax.security.sasl.RealmCallback in project AsmackService by rtreffer.

the class AccountCallbackHander method handle.

/**
     * Answer callbacks of the type
     * <ul>
     *   <li>{@link NameCallback} with the user account jid</li>
     *   <li>{@link PasswordCallback} with the user password</li>
     *   <li>{@link RealmCallback} with the user account domain</li>
     * </ul>
     * @param callback A set of callbacks to be answered.
     * @throws IOException Never.
     * @throws UnsupportedCallbackException Whenever an unhandled callback is
     *                                      received. Note that this may be
     *                                      thrown after some callbacks were
     *                                      handled.
     */
@Override
public void handle(Callback[] callback) throws IOException, UnsupportedCallbackException {
    for (Callback cb : callback) {
        if (cb instanceof NameCallback) {
            NameCallback nameCallback = (NameCallback) cb;
            nameCallback.setName(XMPPUtils.getUser(account.getJid()));
            continue;
        }
        if (cb instanceof PasswordCallback) {
            PasswordCallback passwordCallback = (PasswordCallback) cb;
            passwordCallback.setPassword(account.getPassword().toCharArray());
            continue;
        }
        if (cb instanceof RealmCallback) {
            RealmCallback realmCallback = (RealmCallback) cb;
            realmCallback.setText(XMPPUtils.getDomain(account.getJid()));
            continue;
        }
        Log.d("CALLBACK", "" + cb);
        throw new UnsupportedCallbackException(cb);
    }
}
Also used : PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(org.apache.harmony.javax.security.auth.callback.NameCallback) RealmCallback(org.apache.harmony.javax.security.sasl.RealmCallback) Callback(javax.security.auth.callback.Callback) NameCallback(org.apache.harmony.javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RealmCallback(org.apache.harmony.javax.security.sasl.RealmCallback)

Aggregations

Callback (javax.security.auth.callback.Callback)2 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)2 NameCallback (org.apache.harmony.javax.security.auth.callback.NameCallback)2 RealmCallback (org.apache.harmony.javax.security.sasl.RealmCallback)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 PasswordCallback (javax.security.auth.callback.PasswordCallback)1 RealmChoiceCallback (org.apache.harmony.javax.security.sasl.RealmChoiceCallback)1 SaslException (org.apache.harmony.javax.security.sasl.SaslException)1