Search in sources :

Example 1 with RealmChoiceCallback

use of org.apache.harmony.javax.security.sasl.RealmChoiceCallback 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)

Aggregations

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