MrShadow
MrShadow

Reputation: 171

How do I use Http Digest Auth with volley on Android

I want to use Http Digest with Volley. So far I have used the following code:

@Override
public Map<String, String> getHeaders() throws AuthFailureError {

    HashMap<String, String> params = new HashMap<String, String>();
    String creds = String.format("%s:%s","admin","mypass");
    String auth = "Digest " + Base64.encodeToString(creds.getBytes(), Base64.NO_WRAP);
    params.put("Authorization", "Digest " +auth);
    return params;

}

So far I get a response from server as wrong credentials which means that authentication is working but only wrong credentials are getting passed. But my credentials are right.

Upvotes: 0

Views: 779

Answers (1)

kalin
kalin

Reputation: 3576

You use Base64 encoding which is fine for Basic Auth but this is not how digest works. Digest & Basic Auth specs can be found here: https://www.rfc-editor.org/rfc/rfc2617

the newer Digest Specs can be found here: https://www.rfc-editor.org/rfc/rfc7616

And nice extra explanation on wikipedia here: https://en.wikipedia.org/wiki/Digest_access_authentication

For Volley implementation of Digest Auth you can use: http://www.java2s.com/Open-Source/Android_Free_Code/Framework/platform/com_gm_android_volleyHttpDigestStack_java.htm

You will just need to pass this http stack when you create your Network which you then use to create your RequestQueue:

    RequestQueue requestQueue;
    requestQueue = new RequestQueue(
            new DiskBasedCache(rootDir),
            new BasicNetwork(new HttpDigestStack())
    );
    requestQueue.start();

Upvotes: 1

Related Questions