MCPcopy Create free account
hub / github.com/EasyIME/PIME / _decode_signed_value_v2

Function _decode_signed_value_v2

python/python3/tornado/web.py:3556–3593  ·  view source on GitHub ↗
(
    secret: _CookieSecretTypes,
    name: str,
    value: bytes,
    max_age_days: float,
    clock: Callable[[], float],
)

Source from the content-addressed store, hash-verified

3554
3555
3556def _decode_signed_value_v2(
3557 secret: _CookieSecretTypes,
3558 name: str,
3559 value: bytes,
3560 max_age_days: float,
3561 clock: Callable[[], float],
3562) -> Optional[bytes]:
3563 try:
3564 (
3565 key_version,
3566 timestamp_bytes,
3567 name_field,
3568 value_field,
3569 passed_sig,
3570 ) = _decode_fields_v2(value)
3571 except ValueError:
3572 return None
3573 signed_string = value[: -len(passed_sig)]
3574
3575 if isinstance(secret, dict):
3576 try:
3577 secret = secret[key_version]
3578 except KeyError:
3579 return None
3580
3581 expected_sig = _create_signature_v2(secret, signed_string)
3582 if not hmac.compare_digest(passed_sig, expected_sig):
3583 return None
3584 if name_field != utf8(name):
3585 return None
3586 timestamp = int(timestamp_bytes)
3587 if timestamp < clock() - max_age_days * 86400:
3588 # The signature has expired.
3589 return None
3590 try:
3591 return base64.b64decode(value_field)
3592 except Exception:
3593 return None
3594
3595
3596def get_signature_key_version(value: Union[str, bytes]) -> Optional[int]:

Callers 1

decode_signed_valueFunction · 0.85

Calls 3

utf8Function · 0.90
_decode_fields_v2Function · 0.85
_create_signature_v2Function · 0.85

Tested by

no test coverage detected