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

Method _decode_xsrf_token

python/python3/tornado/web.py:1458–1492  ·  view source on GitHub ↗

Convert a cookie string into a the tuple form returned by _get_raw_xsrf_token.

(
        self, cookie: str
    )

Source from the content-addressed store, hash-verified

1456 return self._raw_xsrf_token
1457
1458 def _decode_xsrf_token(
1459 self, cookie: str
1460 ) -> Tuple[Optional[int], Optional[bytes], Optional[float]]:
1461 """Convert a cookie string into a the tuple form returned by
1462 _get_raw_xsrf_token.
1463 """
1464
1465 try:
1466 m = _signed_value_version_re.match(utf8(cookie))
1467
1468 if m:
1469 version = int(m.group(1))
1470 if version == 2:
1471 _, mask_str, masked_token, timestamp_str = cookie.split("|")
1472
1473 mask = binascii.a2b_hex(utf8(mask_str))
1474 token = _websocket_mask(mask, binascii.a2b_hex(utf8(masked_token)))
1475 timestamp = int(timestamp_str)
1476 return version, token, timestamp
1477 else:
1478 # Treat unknown versions as not present instead of failing.
1479 raise Exception("Unknown xsrf cookie version")
1480 else:
1481 version = 1
1482 try:
1483 token = binascii.a2b_hex(utf8(cookie))
1484 except (binascii.Error, TypeError):
1485 token = utf8(cookie)
1486 # We don't have a usable timestamp in older versions.
1487 timestamp = int(time.time())
1488 return (version, token, timestamp)
1489 except Exception:
1490 # Catch exceptions and return nothing instead of failing.
1491 gen_log.debug("Uncaught exception in _decode_xsrf_token", exc_info=True)
1492 return None, None, None
1493
1494 def check_xsrf_cookie(self) -> None:
1495 """Verifies that the ``_xsrf`` cookie matches the ``_xsrf`` argument.

Callers 2

_get_raw_xsrf_tokenMethod · 0.95
check_xsrf_cookieMethod · 0.95

Calls 5

utf8Function · 0.90
splitMethod · 0.80
timeMethod · 0.80
debugMethod · 0.80
matchMethod · 0.45

Tested by

no test coverage detected