Python tornado.web.create_signed_value() Examples

The following are 27 code examples of tornado.web.create_signed_value(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tornado.web , or try the search function .
Example #1
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_key_versioning_read_write_non_default_key(self):
        value = b"\xe9"
        signed = create_signed_value(SignedValueTest.SECRET_DICT,
                                     "key", value, clock=self.present,
                                     key_version=1)
        decoded = decode_signed_value(SignedValueTest.SECRET_DICT,
                                      "key", signed, clock=self.present)
        self.assertEqual(value, decoded) 
Example #2
Source File: web_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_key_version_retrieval(self):
        value = b"\xe9"
        signed = create_signed_value(SignedValueTest.SECRET_DICT,
                                     "key", value, clock=self.present,
                                     key_version=1)
        key_version = get_signature_key_version(signed)
        self.assertEqual(1, key_version) 
Example #3
Source File: web_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_key_versioning_invalid_key(self):
        value = b"\xe9"
        signed = create_signed_value(SignedValueTest.SECRET_DICT,
                                     "key", value, clock=self.present,
                                     key_version=0)
        newkeys = SignedValueTest.SECRET_DICT.copy()
        newkeys.pop(0)
        decoded = decode_signed_value(newkeys,
                                      "key", signed, clock=self.present)
        self.assertEqual(None, decoded) 
Example #4
Source File: web_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_key_versioning_read_write_non_default_key(self):
        value = b"\xe9"
        signed = create_signed_value(SignedValueTest.SECRET_DICT,
                                     "key", value, clock=self.present,
                                     key_version=1)
        decoded = decode_signed_value(SignedValueTest.SECRET_DICT,
                                      "key", signed, clock=self.present)
        self.assertEqual(value, decoded) 
Example #5
Source File: web_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_non_ascii(self):
        value = b"\xe9"
        signed = create_signed_value(SignedValueTest.SECRET, "key", value,
                                     clock=self.present)
        decoded = decode_signed_value(SignedValueTest.SECRET, "key", signed,
                                      clock=self.present)
        self.assertEqual(value, decoded) 
Example #6
Source File: web_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_expired(self):
        signed = create_signed_value(SignedValueTest.SECRET, "key1", "value",
                                     clock=self.past)
        decoded_past = decode_signed_value(SignedValueTest.SECRET, "key1",
                                           signed, clock=self.past)
        self.assertEqual(decoded_past, b"value")
        decoded_present = decode_signed_value(SignedValueTest.SECRET, "key1",
                                              signed, clock=self.present)
        self.assertIs(decoded_present, None) 
Example #7
Source File: web_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_name_swap(self):
        signed1 = create_signed_value(SignedValueTest.SECRET, "key1", "value",
                                      clock=self.present)
        signed2 = create_signed_value(SignedValueTest.SECRET, "key2", "value",
                                      clock=self.present)
        # Try decoding each string with the other's "name"
        decoded1 = decode_signed_value(SignedValueTest.SECRET, "key2", signed1,
                                       clock=self.present)
        self.assertIs(decoded1, None)
        decoded2 = decode_signed_value(SignedValueTest.SECRET, "key1", signed2,
                                       clock=self.present)
        self.assertIs(decoded2, None) 
Example #8
Source File: web_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_known_values(self):
        signed_v1 = create_signed_value(SignedValueTest.SECRET, "key", "value",
                                        version=1, clock=self.present)
        self.assertEqual(
            signed_v1,
            b"dmFsdWU=|1300000000|31c934969f53e48164c50768b40cbd7e2daaaa4f")

        signed_v2 = create_signed_value(SignedValueTest.SECRET, "key", "value",
                                        version=2, clock=self.present)
        self.assertEqual(
            signed_v2,
            b"2|1:0|10:1300000000|3:key|8:dmFsdWU=|"
            b"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152")

        signed_default = create_signed_value(SignedValueTest.SECRET,
                                             "key", "value", clock=self.present)
        self.assertEqual(signed_default, signed_v2)

        decoded_v1 = decode_signed_value(SignedValueTest.SECRET, "key",
                                         signed_v1, min_version=1,
                                         clock=self.present)
        self.assertEqual(decoded_v1, b"value")

        decoded_v2 = decode_signed_value(SignedValueTest.SECRET, "key",
                                         signed_v2, min_version=2,
                                         clock=self.present)
        self.assertEqual(decoded_v2, b"value") 
Example #9
Source File: web_test.py    From EventGhost with GNU General Public License v2.0 5 votes vote down vote up
def test_types(self):
        cookie_value = to_unicode(create_signed_value(self.COOKIE_SECRET,
                                                      "asdf", "qwer"))
        response = self.fetch("/typecheck/asdf?foo=bar",
                              headers={"Cookie": "asdf=" + cookie_value})
        data = json_decode(response.body)
        self.assertEqual(data, {})

        response = self.fetch("/typecheck/asdf?foo=bar", method="POST",
                              headers={"Cookie": "asdf=" + cookie_value},
                              body="foo=bar") 
Example #10
Source File: web_test.py    From honeything with GNU General Public License v3.0 5 votes vote down vote up
def test_types(self):
        cookie_value = to_unicode(create_signed_value(self.COOKIE_SECRET,
                                                      "asdf", "qwer"))
        response = self.fetch("/typecheck/asdf?foo=bar",
                              headers={"Cookie": "asdf=" + cookie_value})
        data = json_decode(response.body)
        self.assertEqual(data, {})

        response = self.fetch("/typecheck/asdf?foo=bar", method="POST",
                              headers={"Cookie": "asdf=" + cookie_value},
                              body="foo=bar") 
Example #11
Source File: web_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_types(self):
        cookie_value = to_unicode(create_signed_value(self.COOKIE_SECRET,
                                                      "asdf", "qwer"))
        response = self.fetch("/typecheck/asdf?foo=bar",
                              headers={"Cookie": "asdf=" + cookie_value})
        data = json_decode(response.body)
        self.assertEqual(data, {})

        response = self.fetch("/typecheck/asdf?foo=bar", method="POST",
                              headers={"Cookie": "asdf=" + cookie_value},
                              body="foo=bar") 
Example #12
Source File: web_test.py    From viewfinder with Apache License 2.0 5 votes vote down vote up
def test_types(self):
        cookie_value = to_unicode(create_signed_value(self.COOKIE_SECRET,
                                                      "asdf", "qwer"))
        response = self.fetch("/typecheck/asdf?foo=bar",
                              headers={"Cookie": "asdf=" + cookie_value})
        data = json_decode(response.body)
        self.assertEqual(data, {})

        response = self.fetch("/typecheck/asdf?foo=bar", method="POST",
                              headers={"Cookie": "asdf=" + cookie_value},
                              body="foo=bar") 
Example #13
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_key_versioning_invalid_key(self):
        value = b"\xe9"
        signed = create_signed_value(SignedValueTest.SECRET_DICT,
                                     "key", value, clock=self.present,
                                     key_version=0)
        newkeys = SignedValueTest.SECRET_DICT.copy()
        newkeys.pop(0)
        decoded = decode_signed_value(newkeys,
                                      "key", signed, clock=self.present)
        self.assertEqual(None, decoded) 
Example #14
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_types(self):
        cookie_value = to_unicode(create_signed_value(self.COOKIE_SECRET,
                                                      "asdf", "qwer"))
        response = self.fetch("/typecheck/asdf?foo=bar",
                              headers={"Cookie": "asdf=" + cookie_value})
        data = json_decode(response.body)
        self.assertEqual(data, {})

        response = self.fetch("/typecheck/asdf?foo=bar", method="POST",
                              headers={"Cookie": "asdf=" + cookie_value},
                              body="foo=bar") 
Example #15
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_key_versioning_read_write_default_key(self):
        value = b"\xe9"
        signed = create_signed_value(SignedValueTest.SECRET_DICT,
                                     "key", value, clock=self.present,
                                     key_version=0)
        decoded = decode_signed_value(SignedValueTest.SECRET_DICT,
                                      "key", signed, clock=self.present)
        self.assertEqual(value, decoded) 
Example #16
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_non_ascii(self):
        value = b"\xe9"
        signed = create_signed_value(SignedValueTest.SECRET, "key", value,
                                     clock=self.present)
        decoded = decode_signed_value(SignedValueTest.SECRET, "key", signed,
                                      clock=self.present)
        self.assertEqual(value, decoded) 
Example #17
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_expired(self):
        signed = create_signed_value(SignedValueTest.SECRET, "key1", "value",
                                     clock=self.past)
        decoded_past = decode_signed_value(SignedValueTest.SECRET, "key1",
                                           signed, clock=self.past)
        self.assertEqual(decoded_past, b"value")
        decoded_present = decode_signed_value(SignedValueTest.SECRET, "key1",
                                              signed, clock=self.present)
        self.assertIs(decoded_present, None) 
Example #18
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_known_values(self):
        signed_v1 = create_signed_value(SignedValueTest.SECRET, "key", "value",
                                        version=1, clock=self.present)
        self.assertEqual(
            signed_v1,
            b"dmFsdWU=|1300000000|31c934969f53e48164c50768b40cbd7e2daaaa4f")

        signed_v2 = create_signed_value(SignedValueTest.SECRET, "key", "value",
                                        version=2, clock=self.present)
        self.assertEqual(
            signed_v2,
            b"2|1:0|10:1300000000|3:key|8:dmFsdWU=|"
            b"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152")

        signed_default = create_signed_value(SignedValueTest.SECRET,
                                             "key", "value", clock=self.present)
        self.assertEqual(signed_default, signed_v2)

        decoded_v1 = decode_signed_value(SignedValueTest.SECRET, "key",
                                         signed_v1, min_version=1,
                                         clock=self.present)
        self.assertEqual(decoded_v1, b"value")

        decoded_v2 = decode_signed_value(SignedValueTest.SECRET, "key",
                                         signed_v2, min_version=2,
                                         clock=self.present)
        self.assertEqual(decoded_v2, b"value") 
Example #19
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_types(self):
        cookie_value = to_unicode(create_signed_value(self.COOKIE_SECRET,
                                                      "asdf", "qwer"))
        response = self.fetch("/typecheck/asdf?foo=bar",
                              headers={"Cookie": "asdf=" + cookie_value})
        data = json_decode(response.body)
        self.assertEqual(data, {})

        response = self.fetch("/typecheck/asdf?foo=bar", method="POST",
                              headers={"Cookie": "asdf=" + cookie_value},
                              body="foo=bar") 
Example #20
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_key_version_retrieval(self):
        value = b"\xe9"
        signed = create_signed_value(SignedValueTest.SECRET_DICT,
                                     "key", value, clock=self.present,
                                     key_version=1)
        key_version = get_signature_key_version(signed)
        self.assertEqual(1, key_version) 
Example #21
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_key_versioning_invalid_key(self):
        value = b"\xe9"
        signed = create_signed_value(SignedValueTest.SECRET_DICT,
                                     "key", value, clock=self.present,
                                     key_version=0)
        newkeys = SignedValueTest.SECRET_DICT.copy()
        newkeys.pop(0)
        decoded = decode_signed_value(newkeys,
                                      "key", signed, clock=self.present)
        self.assertEqual(None, decoded) 
Example #22
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_key_versioning_read_write_non_default_key(self):
        value = b"\xe9"
        signed = create_signed_value(SignedValueTest.SECRET_DICT,
                                     "key", value, clock=self.present,
                                     key_version=1)
        decoded = decode_signed_value(SignedValueTest.SECRET_DICT,
                                      "key", signed, clock=self.present)
        self.assertEqual(value, decoded) 
Example #23
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_non_ascii(self):
        value = b"\xe9"
        signed = create_signed_value(SignedValueTest.SECRET, "key", value,
                                     clock=self.present)
        decoded = decode_signed_value(SignedValueTest.SECRET, "key", signed,
                                      clock=self.present)
        self.assertEqual(value, decoded) 
Example #24
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_expired(self):
        signed = create_signed_value(SignedValueTest.SECRET, "key1", "value",
                                     clock=self.past)
        decoded_past = decode_signed_value(SignedValueTest.SECRET, "key1",
                                           signed, clock=self.past)
        self.assertEqual(decoded_past, b"value")
        decoded_present = decode_signed_value(SignedValueTest.SECRET, "key1",
                                              signed, clock=self.present)
        self.assertIs(decoded_present, None) 
Example #25
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_name_swap(self):
        signed1 = create_signed_value(SignedValueTest.SECRET, "key1", "value",
                                      clock=self.present)
        signed2 = create_signed_value(SignedValueTest.SECRET, "key2", "value",
                                      clock=self.present)
        # Try decoding each string with the other's "name"
        decoded1 = decode_signed_value(SignedValueTest.SECRET, "key2", signed1,
                                       clock=self.present)
        self.assertIs(decoded1, None)
        decoded2 = decode_signed_value(SignedValueTest.SECRET, "key1", signed2,
                                       clock=self.present)
        self.assertIs(decoded2, None) 
Example #26
Source File: web_test.py    From tornado-zh with MIT License 5 votes vote down vote up
def test_known_values(self):
        signed_v1 = create_signed_value(SignedValueTest.SECRET, "key", "value",
                                        version=1, clock=self.present)
        self.assertEqual(
            signed_v1,
            b"dmFsdWU=|1300000000|31c934969f53e48164c50768b40cbd7e2daaaa4f")

        signed_v2 = create_signed_value(SignedValueTest.SECRET, "key", "value",
                                        version=2, clock=self.present)
        self.assertEqual(
            signed_v2,
            b"2|1:0|10:1300000000|3:key|8:dmFsdWU=|"
            b"3d4e60b996ff9c5d5788e333a0cba6f238a22c6c0f94788870e1a9ecd482e152")

        signed_default = create_signed_value(SignedValueTest.SECRET,
                                             "key", "value", clock=self.present)
        self.assertEqual(signed_default, signed_v2)

        decoded_v1 = decode_signed_value(SignedValueTest.SECRET, "key",
                                         signed_v1, min_version=1,
                                         clock=self.present)
        self.assertEqual(decoded_v1, b"value")

        decoded_v2 = decode_signed_value(SignedValueTest.SECRET, "key",
                                         signed_v2, min_version=2,
                                         clock=self.present)
        self.assertEqual(decoded_v2, b"value") 
Example #27
Source File: _session.py    From flexx with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def set_cookie(self, name, value, expires_days=30, version=None,
                   domain=None, expires=None, path="/", **kwargs):
        """ Sets the given cookie name/value with the given options. Set value
        to None to clear. The cookie value is secured using
        `flexx.config.cookie_secret`; don't forget to set that config
        value in your server. Additional keyword arguments are set on
        the Cookie.Morsel directly.
        """
        # This code is taken (in modified form) from the Tornado project
        # Copyright 2009 Facebook
        # Licensed under the Apache License, Version 2.0

        # Assume tornado is available ...
        from tornado.escape import native_str
        from tornado.httputil import format_timestamp
        from tornado.web import create_signed_value

        # Clear cookie?
        if value is None:
            value = ""
            expires = datetime.datetime.utcnow() - datetime.timedelta(days=365)
        else:
            secret = config.cookie_secret
            value = create_signed_value(secret, name, value, version=version,
                                        key_version=None)

        # The cookie library only accepts type str, in both python 2 and 3
        name = native_str(name)
        value = native_str(value)
        if re.search(r"[\x00-\x20]", name + value):
            # Don't let us accidentally inject bad stuff
            raise ValueError("Invalid cookie %r: %r" % (name, value))
        if name in self._cookies:
            del self._cookies[name]
        self._cookies[name] = value
        morsel = self._cookies[name]
        if domain:
            morsel["domain"] = domain
        if expires_days is not None and not expires:
            expires = datetime.datetime.utcnow() + datetime.timedelta(
                days=expires_days)
        if expires:
            morsel["expires"] = format_timestamp(expires)
        if path:
            morsel["path"] = path
        for k, v in kwargs.items():
            if k == 'max_age':
                k = 'max-age'
            # skip falsy values for httponly and secure flags because
            # SimpleCookie sets them regardless
            if k in ['httponly', 'secure'] and not v:
                continue
            morsel[k] = v

        self.send_command('EXEC', 'document.cookie = "%s";' %
                   morsel.OutputString().replace('"', '\\"'))

    ## Data