def __init__(
        self,
        auth_header,
        http_method,
        debug=False,
        accept_charset=DEFAULT_CHARSET[:],
    ):
        """Initialize an HTTP Digest Authorization parser."""
        self.http_method = http_method
        self.debug = debug

        if not self.matches(auth_header):
            raise ValueError('Authorization scheme is not "Digest"')

        self.auth_header = _try_decode_header(auth_header, accept_charset)

        scheme, params = self.auth_header.split(' ', 1)

        # make a dict of the params
        items = parse_http_list(params)
        paramsd = parse_keqv_list(items)

        self.realm = paramsd.get('realm')
        self.username = paramsd.get('username')
        self.nonce = paramsd.get('nonce')
        self.uri = paramsd.get('uri')
        self.method = paramsd.get('method')
        self.response = paramsd.get('response')  # the response digest
        self.algorithm = paramsd.get('algorithm', 'MD5').upper()
        self.cnonce = paramsd.get('cnonce')
        self.opaque = paramsd.get('opaque')
        self.qop = paramsd.get('qop')  # qop
        self.nc = paramsd.get('nc')  # nonce count

        # perform some correctness checks
        if self.algorithm not in valid_algorithms:
            raise ValueError(
                self.errmsg(
                    "Unsupported value for algorithm: '%s'" % self.algorithm,
                ),
            )

        has_reqd = (
            self.username
            and self.realm
            and self.nonce
            and self.uri
            and self.response
        )
        if not has_reqd:
            raise ValueError(
                self.errmsg('Not all required parameters are present.'),
            )

        if self.qop:
            if self.qop not in valid_qops:
                raise ValueError(
                    self.errmsg("Unsupported value for qop: '%s'" % self.qop),
                )
            if not (self.cnonce and self.nc):
                raise ValueError(
                    self.errmsg(
                        'If qop is sent then cnonce and nc MUST be present',
                    ),
                )
        else:
            if self.cnonce or self.nc:
                raise ValueError(
                    self.errmsg(
                        'If qop is not sent, '
                        'neither cnonce nor nc can be present',
                    ),
                )
Initialize an HTTP Digest Authorization parser.
cherrypy/lib/auth_digest.py