Home / Class/ HTTPDigestAuth Class — requests Architecture

HTTPDigestAuth Class — requests Architecture

Architecture documentation for the HTTPDigestAuth class in auth.py from the requests codebase.

Entity Profile

Dependency Diagram

graph TD
  aa32083c_418b_f1ff_9e66_cf1a12e8a8ee["HTTPDigestAuth"]
  c7a3ca15_217a_21f9_87ee_74879ebf9720["AuthBase"]
  aa32083c_418b_f1ff_9e66_cf1a12e8a8ee -->|extends| c7a3ca15_217a_21f9_87ee_74879ebf9720
  d7a739b0_e73b_9565_f5ed_5e8c24943504["auth.py"]
  aa32083c_418b_f1ff_9e66_cf1a12e8a8ee -->|defined in| d7a739b0_e73b_9565_f5ed_5e8c24943504
  0b34d884_648d_0780_4909_af9251a17f26["__init__()"]
  aa32083c_418b_f1ff_9e66_cf1a12e8a8ee -->|method| 0b34d884_648d_0780_4909_af9251a17f26
  cb809e14_f57d_9c3d_4f4e_7538910c95a0["init_per_thread_state()"]
  aa32083c_418b_f1ff_9e66_cf1a12e8a8ee -->|method| cb809e14_f57d_9c3d_4f4e_7538910c95a0
  7a5e3fcd_3d53_bb5a_33e1_2ec03991bb2c["build_digest_header()"]
  aa32083c_418b_f1ff_9e66_cf1a12e8a8ee -->|method| 7a5e3fcd_3d53_bb5a_33e1_2ec03991bb2c
  2db52d5a_260e_6a08_d0f8_f4487818074e["handle_redirect()"]
  aa32083c_418b_f1ff_9e66_cf1a12e8a8ee -->|method| 2db52d5a_260e_6a08_d0f8_f4487818074e
  351a1e00_cb4e_af8e_1ed9_e33664efe05e["handle_401()"]
  aa32083c_418b_f1ff_9e66_cf1a12e8a8ee -->|method| 351a1e00_cb4e_af8e_1ed9_e33664efe05e
  19624f9a_1446_1a92_8704_bca792b9b1fa["__call__()"]
  aa32083c_418b_f1ff_9e66_cf1a12e8a8ee -->|method| 19624f9a_1446_1a92_8704_bca792b9b1fa
  2073b6fa_6876_e257_beef_507c03186bb9["__eq__()"]
  aa32083c_418b_f1ff_9e66_cf1a12e8a8ee -->|method| 2073b6fa_6876_e257_beef_507c03186bb9
  24a82227_f1bf_68a2_cb0f_677e4fb3d2bd["__ne__()"]
  aa32083c_418b_f1ff_9e66_cf1a12e8a8ee -->|method| 24a82227_f1bf_68a2_cb0f_677e4fb3d2bd

Relationship Graph

Source Code

src/requests/auth.py lines 107–314

class HTTPDigestAuth(AuthBase):
    """Attaches HTTP Digest Authentication to the given Request object."""

    def __init__(self, username, password):
        self.username = username
        self.password = password
        # Keep state in per-thread local storage
        self._thread_local = threading.local()

    def init_per_thread_state(self):
        # Ensure state is initialized just once per-thread
        if not hasattr(self._thread_local, "init"):
            self._thread_local.init = True
            self._thread_local.last_nonce = ""
            self._thread_local.nonce_count = 0
            self._thread_local.chal = {}
            self._thread_local.pos = None
            self._thread_local.num_401_calls = None

    def build_digest_header(self, method, url):
        """
        :rtype: str
        """

        realm = self._thread_local.chal["realm"]
        nonce = self._thread_local.chal["nonce"]
        qop = self._thread_local.chal.get("qop")
        algorithm = self._thread_local.chal.get("algorithm")
        opaque = self._thread_local.chal.get("opaque")
        hash_utf8 = None

        if algorithm is None:
            _algorithm = "MD5"
        else:
            _algorithm = algorithm.upper()
        # lambdas assume digest modules are imported at the top level
        if _algorithm == "MD5" or _algorithm == "MD5-SESS":

            def md5_utf8(x):
                if isinstance(x, str):
                    x = x.encode("utf-8")
                return hashlib.md5(x).hexdigest()

            hash_utf8 = md5_utf8
        elif _algorithm == "SHA":

            def sha_utf8(x):
                if isinstance(x, str):
                    x = x.encode("utf-8")
                return hashlib.sha1(x).hexdigest()

            hash_utf8 = sha_utf8
        elif _algorithm == "SHA-256":

            def sha256_utf8(x):
                if isinstance(x, str):
                    x = x.encode("utf-8")
                return hashlib.sha256(x).hexdigest()

            hash_utf8 = sha256_utf8
        elif _algorithm == "SHA-512":

            def sha512_utf8(x):
                if isinstance(x, str):
                    x = x.encode("utf-8")
                return hashlib.sha512(x).hexdigest()

            hash_utf8 = sha512_utf8

        KD = lambda s, d: hash_utf8(f"{s}:{d}")  # noqa:E731

        if hash_utf8 is None:
            return None

        # XXX not implemented yet
        entdig = None
        p_parsed = urlparse(url)
        #: path is request-uri defined in RFC 2616 which should not be empty
        path = p_parsed.path or "/"
        if p_parsed.query:
            path += f"?{p_parsed.query}"

Domain

Extends

Frequently Asked Questions

What is the HTTPDigestAuth class?
HTTPDigestAuth is a class in the requests codebase, defined in src/requests/auth.py.
Where is HTTPDigestAuth defined?
HTTPDigestAuth is defined in src/requests/auth.py at line 107.
What does HTTPDigestAuth extend?
HTTPDigestAuth extends AuthBase.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free