[IGN-4258]Digest auth with httpClient

Here is an example function:

def getData(client, uri, url, username, password):
		import hashlib
		import random
		
		res = client.get(url)
		if res.statusCode == 401:
			headers = res.headers
			digestParts = headers["www-authenticate"][0].replace("Digest ", "").split(",")
			digestData = {}
			for part in digestParts:		
				partParts = part.strip().split("=")
				digestData[partParts[0]] = partParts[1].replace("\"", "")
			
			realm = digestData["realm"]
			charset = digestData["charset"]
			algorithm = digestData["algorithm"]
			nonce = digestData["nonce"]
			cnonce = "".join(random.choice("0123456789abcdef") for i in range(8))
			qop = digestData["qop"]
			nc = "00000001"
			
			ha1 = hashlib.md5("%s:%s:%s" % (username, realm, password)).hexdigest()
			ha2 = hashlib.md5("GET:%s" % (uri)).hexdigest()
			response = hashlib.md5("%s:%s:%s:%s:%s:%s" % (ha1, nonce, nc, cnonce, qop, ha2)).hexdigest()
			
			authorization = "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", qop=%s, nc=%s, cnonce=\"%s\", response=\"%s\"" % (username, realm, nonce, uri, qop, nc, cnonce, response)
			res = client.get(url, headers={"Authorization":authorization})
		
		return res

You can call it like so:

ipAddress = "192.168.10.10"
uri = "/path/to/call"
url = "https://%s%s" % (ipAddress, uri)
username = "admin"
password = "password"
client = system.net.httpClient(bypassCertValidation=True)
(authorization, res) = getData(client, uri, url, username, password)
3 Likes