Returns the given signed cookie if it validates, or None. The decoded cookie value is returned as a byte string (unlike `get_cookie`). Similar to `get_cookie`, this method only returns cookies that were present in the request. It does not see outgoing cookies set by
(
self,
name: str,
value: Optional[str] = None,
max_age_days: float = 31,
min_version: Optional[int] = None,
)
| 891 | ) |
| 892 | |
| 893 | def get_signed_cookie( |
| 894 | self, |
| 895 | name: str, |
| 896 | value: Optional[str] = None, |
| 897 | max_age_days: float = 31, |
| 898 | min_version: Optional[int] = None, |
| 899 | ) -> Optional[bytes]: |
| 900 | """Returns the given signed cookie if it validates, or None. |
| 901 | |
| 902 | The decoded cookie value is returned as a byte string (unlike |
| 903 | `get_cookie`). |
| 904 | |
| 905 | Similar to `get_cookie`, this method only returns cookies that |
| 906 | were present in the request. It does not see outgoing cookies set by |
| 907 | `set_signed_cookie` in this handler. |
| 908 | |
| 909 | .. versionchanged:: 3.2.1 |
| 910 | |
| 911 | Added the ``min_version`` argument. Introduced cookie version 2; |
| 912 | both versions 1 and 2 are accepted by default. |
| 913 | |
| 914 | .. versionchanged:: 6.3 |
| 915 | |
| 916 | Renamed from ``get_secure_cookie`` to ``get_signed_cookie`` to |
| 917 | avoid confusion with other uses of "secure" in cookie attributes |
| 918 | and prefixes. The old name remains as an alias. |
| 919 | |
| 920 | """ |
| 921 | self.require_setting("cookie_secret", "secure cookies") |
| 922 | if value is None: |
| 923 | value = self.get_cookie(name) |
| 924 | return decode_signed_value( |
| 925 | self.application.settings["cookie_secret"], |
| 926 | name, |
| 927 | value, |
| 928 | max_age_days=max_age_days, |
| 929 | min_version=min_version, |
| 930 | ) |
| 931 | |
| 932 | get_secure_cookie = get_signed_cookie |
| 933 |