Digest Authentication (Java/Android)

I have read every guide possible, from wikipedia to other implementations, but cannot for the life of me get this to work! I'm attempting to connect to my Calibre Content Server from my android application, using okhttp3 and a simple header. key[0] is the username (calibre), key[1] is the password(test).

HashMap<String, String> digestHeader = parseHeader(response.header("Www-Authenticate"));
String ha1 = FileHelper.convertToMD5(key[0] + ":" + digestHeader.get("realm") + ":" + key[1]);
String ha2 = FileHelper.convertToMD5("GET:" + uri); //Tried with CONNECT
String nc = "00000001";
String cnonce = handler.getPreferences().getUUID();

String responseString = FileHelper.convertToMD5(
        ha1 + ":" +
        digestHeader.get("nonce") + ":" +
        nc  + ":" +
        cnonce + ":" +
        digestHeader.get("qop") + ":" +
        ha2);

server.setKey("Digest " +
        "username=""+ key[0] +"", " +
        "realm="" + digestHeader.get("realm") + "", " +
        "nonce="" + digestHeader.get("nonce") + "", " +
        "uri="" + uri+ "", " +
        "qop=" + digestHeader.get("qop") + ", " +
        "nc=" + nc + ", " +
        "cnonce="" + cnonce + "", " +
        "response="" + responseString + "", " +
        "algorithm=" + digestHeader.get("algorithm"));

From there I'm simply adding an it as an "Authorization" header. It always comes back 401 Unauthorized! Here is the original response and my updated digest, the password works when I test it in a browser manually.

Www-Authenticate: Digest realm="Your calibre library", nonce="1494719624:ca232cc18dafe3a6655753aabe0c85c0", algorithm="MD5", qop="auth"

Authorization: Digest username="calibre", realm="Your calibre library", nonce="1494721002:e18358fef996544c2e7067e990806800", uri="/opds", qop=auth, nc=00000001, cnonce="1d02d071-facc-4dc2-bb95-412fbc1053b7", response="78e8b090d510e3e512fa8f77888dbdb", algorithm=MD5

Is something wrong with my code? Digest is rather confusing, and I'm not sure if I need to include nc and cnonce, or if my uri is incorrect (I've tried the full path and just everything after the port), or if it's something else?!

I attempted to use the okhttp-digest plugin, but it seems built for okhttp2, with more recent examples changing the authorization of every single request, which won't work for me. Is there another way to approach this?

链接地址: http://www.djcxy.com/p/22318.html

上一篇: Groovy摘要认证

下一篇: 摘要式身份验证(Java / Android)