Convert a cookie string into a the tuple form returned by _get_raw_xsrf_token.
(
self, cookie: str
)
| 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. |
no test coverage detected