Revised the calc_response function for readability.
This commit is contained in:
parent
7db38c7eae
commit
8c98d35934
@ -61,6 +61,16 @@ def calc_response(
|
|||||||
cnonce or nc is missing with the auth or auth-int qop.
|
cnonce or nc is missing with the auth or auth-int qop.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def validate_required(field: t.Optional[str], error: str) -> None:
|
||||||
|
"""Validates a required field.
|
||||||
|
|
||||||
|
:param field: The field that is required.
|
||||||
|
:param error: The error message.
|
||||||
|
:return: None.
|
||||||
|
"""
|
||||||
|
if field is None:
|
||||||
|
raise UnauthorizedException(error)
|
||||||
|
|
||||||
def calc_ha1() -> str:
|
def calc_ha1() -> str:
|
||||||
"""Calculates and returns the first hash.
|
"""Calculates and returns the first hash.
|
||||||
|
|
||||||
@ -68,16 +78,13 @@ def calc_response(
|
|||||||
:raise UnauthorizedException: When the cnonce is missing with the MD5-sess
|
:raise UnauthorizedException: When the cnonce is missing with the MD5-sess
|
||||||
algorithm.
|
algorithm.
|
||||||
"""
|
"""
|
||||||
if algorithm is None or algorithm == "MD5":
|
|
||||||
return password_hash
|
|
||||||
if algorithm == "MD5-sess":
|
if algorithm == "MD5-sess":
|
||||||
if cnonce is None:
|
validate_required(
|
||||||
raise UnauthorizedException(
|
cnonce, f"Missing \"cnonce\" with algorithm=\"{algorithm}\"")
|
||||||
f"Missing \"cnonce\" with algorithm=\"{algorithm}\"")
|
|
||||||
return md5(f"{password_hash}:{nonce}:{cnonce}".encode("utf8")) \
|
return md5(f"{password_hash}:{nonce}:{cnonce}".encode("utf8")) \
|
||||||
.hexdigest()
|
.hexdigest()
|
||||||
raise UnauthorizedException(
|
# algorithm is None or algorithm == "MD5"
|
||||||
f"Unsupported algorithm=\"{algorithm}\"")
|
return password_hash
|
||||||
|
|
||||||
def calc_ha2() -> str:
|
def calc_ha2() -> str:
|
||||||
"""Calculates the second hash.
|
"""Calculates the second hash.
|
||||||
@ -86,30 +93,20 @@ def calc_response(
|
|||||||
:raise UnauthorizedException: When the body is missing with
|
:raise UnauthorizedException: When the body is missing with
|
||||||
qop="auth-int".
|
qop="auth-int".
|
||||||
"""
|
"""
|
||||||
if qop is None or qop == "auth":
|
|
||||||
return md5(f"{method}:{uri}".encode("utf8")).hexdigest()
|
|
||||||
if qop == "auth-int":
|
if qop == "auth-int":
|
||||||
if body is None:
|
validate_required(body, f"Missing \"body\" with qop=\"{qop}\"")
|
||||||
raise UnauthorizedException(
|
|
||||||
f"Missing \"body\" with qop=\"{qop}\"")
|
|
||||||
return md5(
|
return md5(
|
||||||
f"{method}:{uri}:{md5(body).hexdigest()}".encode("utf8")) \
|
f"{method}:{uri}:{md5(body).hexdigest()}".encode("utf8")) \
|
||||||
.hexdigest()
|
.hexdigest()
|
||||||
raise UnauthorizedException(f"Unsupported qop=\"{qop}\"")
|
# qop is None or qop == "auth"
|
||||||
|
return md5(f"{method}:{uri}".encode("utf8")).hexdigest()
|
||||||
|
|
||||||
ha1: str = calc_ha1()
|
ha1: str = calc_ha1()
|
||||||
ha2: str = calc_ha2()
|
ha2: str = calc_ha2()
|
||||||
if qop is None:
|
|
||||||
return md5(f"{ha1}:{nonce}:{ha2}".encode("utf8")).hexdigest()
|
|
||||||
if qop == "auth" or qop == "auth-int":
|
if qop == "auth" or qop == "auth-int":
|
||||||
if cnonce is None:
|
validate_required(cnonce, f"Missing \"cnonce\" with the qop=\"{qop}\"")
|
||||||
raise UnauthorizedException(
|
validate_required(nc, f"Missing \"nc\" with the qop=\"{qop}\"")
|
||||||
f"Missing \"cnonce\" with the qop=\"{qop}\"")
|
|
||||||
if nc is None:
|
|
||||||
raise UnauthorizedException(
|
|
||||||
f"Missing \"nc\" with the qop=\"{qop}\"")
|
|
||||||
return md5(f"{ha1}:{nonce}:{nc}:{cnonce}:{qop}:{ha2}".encode("utf8"))\
|
return md5(f"{ha1}:{nonce}:{nc}:{cnonce}:{qop}:{ha2}".encode("utf8"))\
|
||||||
.hexdigest()
|
.hexdigest()
|
||||||
if cnonce is None:
|
# qop is None
|
||||||
raise UnauthorizedException(
|
return md5(f"{ha1}:{nonce}:{ha2}".encode("utf8")).hexdigest()
|
||||||
f"Unsupported qop=\"{qop}\"")
|
|
||||||
|
Loading…
Reference in New Issue
Block a user