|
|
|
|
@ -20,6 +20,7 @@ import java.util.Enumeration; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.Locale; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
|
import okhttp3.OkHttp; |
|
|
|
|
import okhttp3.Request; |
|
|
|
|
@ -55,6 +56,7 @@ public class Util { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String basic(String userInfo) { |
|
|
|
|
if (!userInfo.contains(":")) userInfo += ":"; |
|
|
|
|
return "Basic " + base64(userInfo, Base64.NO_WRAP); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -154,7 +156,7 @@ public class Util { |
|
|
|
|
String[] parts = userInfo.split(":", 2); |
|
|
|
|
String nc = "00000001"; |
|
|
|
|
String username = parts[0]; |
|
|
|
|
String password = parts[1]; |
|
|
|
|
String password = parts.length > 1 ? parts[1] : ""; |
|
|
|
|
String qop = params.get("qop"); |
|
|
|
|
String realm = params.get("realm"); |
|
|
|
|
String nonce = params.get("nonce"); |
|
|
|
|
@ -162,19 +164,19 @@ public class Util { |
|
|
|
|
String uri = request.url().encodedPath(); |
|
|
|
|
String hash1 = Util.md5(username + ":" + realm + ":" + password); |
|
|
|
|
String hash2 = Util.md5(request.method() + ":" + uri); |
|
|
|
|
String cnonce = Long.toHexString(System.currentTimeMillis()); |
|
|
|
|
String response = Util.md5(hash1 + ":" + nonce + ":" + nc + ":" + cnonce + ":" + (qop != null ? qop : "") + ":" + hash2); |
|
|
|
|
StringBuilder auth = new StringBuilder("Digest "); |
|
|
|
|
auth.append("username=\"").append(username).append("\", "); |
|
|
|
|
if (realm != null) auth.append("realm=\"").append(realm).append("\", "); |
|
|
|
|
if (nonce != null) auth.append("nonce=\"").append(nonce).append("\", "); |
|
|
|
|
auth.append("uri=\"").append(uri).append("\", "); |
|
|
|
|
auth.append("cnonce=\"").append(cnonce).append("\", "); |
|
|
|
|
auth.append("nc=").append(nc).append(", "); |
|
|
|
|
if (qop != null) auth.append("qop=\"").append(qop).append("\", "); |
|
|
|
|
auth.append("response=\"").append(response).append("\""); |
|
|
|
|
if (opaque != null) auth.append(", opaque=\"").append(opaque).append("\""); |
|
|
|
|
return auth.toString(); |
|
|
|
|
String cnonce = UUID.randomUUID().toString().replace("-", ""); |
|
|
|
|
String response = Util.md5(hash1 + ":" + nonce + ":" + nc + ":" + cnonce + ":" + qop + ":" + hash2); |
|
|
|
|
StringBuilder sb = new StringBuilder("Digest "); |
|
|
|
|
sb.append("username=\"").append(username).append("\", "); |
|
|
|
|
sb.append("realm=\"").append(realm).append("\", "); |
|
|
|
|
sb.append("nonce=\"").append(nonce).append("\", "); |
|
|
|
|
sb.append("uri=\"").append(uri).append("\", "); |
|
|
|
|
sb.append("cnonce=\"").append(cnonce).append("\", "); |
|
|
|
|
sb.append("nc=").append(nc).append(", "); |
|
|
|
|
sb.append("qop=\"").append(qop).append("\", "); |
|
|
|
|
sb.append("response=\"").append(response).append("\""); |
|
|
|
|
if (opaque != null) sb.append(", opaque=\"").append(opaque).append("\""); |
|
|
|
|
return sb.toString(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static Map<String, String> parse(String header) { |
|
|
|
|
|