MCPcopy
hub / github.com/psf/requests / build_digest_header

Method build_digest_header

src/requests/auth.py:157–266  ·  view source on GitHub ↗

:rtype: str

(self, method: str, url: str)

Source from the content-addressed store, hash-verified

155 self._thread_local.num_401_calls = None
156
157 def build_digest_header(self, method: str, url: str) -> str | None:
158 """
159 :rtype: str
160 """
161
162 realm = self._thread_local.chal["realm"]
163 nonce = self._thread_local.chal["nonce"]
164 qop = self._thread_local.chal.get("qop")
165 algorithm = self._thread_local.chal.get("algorithm")
166 opaque = self._thread_local.chal.get("opaque")
167 hash_utf8 = None
168
169 if algorithm is None:
170 _algorithm = "MD5"
171 else:
172 _algorithm = algorithm.upper()
173 # lambdas assume digest modules are imported at the top level
174 if _algorithm == "MD5" or _algorithm == "MD5-SESS":
175
176 def md5_utf8(x: str | bytes) -> str:
177 if isinstance(x, str):
178 x = x.encode("utf-8")
179 return hashlib.md5(x, usedforsecurity=False).hexdigest()
180
181 hash_utf8 = md5_utf8
182 elif _algorithm == "SHA":
183
184 def sha_utf8(x: str | bytes) -> str:
185 if isinstance(x, str):
186 x = x.encode("utf-8")
187 return hashlib.sha1(x, usedforsecurity=False).hexdigest()
188
189 hash_utf8 = sha_utf8
190 elif _algorithm == "SHA-256":
191
192 def sha256_utf8(x: str | bytes) -> str:
193 if isinstance(x, str):
194 x = x.encode("utf-8")
195 return hashlib.sha256(x, usedforsecurity=False).hexdigest()
196
197 hash_utf8 = sha256_utf8
198 elif _algorithm == "SHA-512":
199
200 def sha512_utf8(x: str | bytes) -> str:
201 if isinstance(x, str):
202 x = x.encode("utf-8")
203 return hashlib.sha512(x, usedforsecurity=False).hexdigest()
204
205 hash_utf8 = sha512_utf8
206
207 if hash_utf8 is None:
208 return None
209
210 def KD(s: str, d: str) -> str:
211 return hash_utf8(f"{s}:{d}")
212
213 # XXX not implemented yet
214 entdig = None

Callers 2

handle_401Method · 0.95
__call__Method · 0.95

Calls 1

getMethod · 0.45

Tested by

no test coverage detected