Пример #1
0
def test_date_to_unix():
    """Date to UNIX timestamp conversions."""
    assert internal._date_to_unix(datetime(1970, 1, 1)) == 0
    assert internal._date_to_unix(datetime(1970, 1, 1, 1, 0, 0)) == 3600
    assert internal._date_to_unix(datetime(1970, 1, 1, 1, 1, 1)) == 3661
    x = datetime(2010, 2, 15, 16, 15, 39)
    assert internal._date_to_unix(x) == 1266250539
Пример #2
0
    def serialize(self, expires=None):
        """Serialize the secure cookie into a string.

        If expires is provided, the session will be automatically invalidated
        after expiration when you unseralize it. This provides better
        protection against session cookie theft.

        :param expires: an optional expiration date for the cookie (a
                        :class:`datetime.datetime` object)
        """
        if self.secret_key is None:
            raise RuntimeError('no secret key defined')
        if expires:
            self['_expires'] = _date_to_unix(expires)
        result = []
        mac = hmac(self.secret_key, None, self.hash_method)
        for key, value in sorted(self.items()):
            result.append('%s=%s' % (
                url_quote_plus(key),
                self.quote(value)
            ))
            mac.update('|' + result[-1])
        return '%s?%s' % (
            mac.digest().encode('base64').strip(),
            '&'.join(result)
        )
Пример #3
0
    def serialize(self, expires=None):
        """Serialize the secure cookie into a string.

        If expires is provided, the session will be automatically invalidated
        after expiration when you unseralize it. This provides better
        protection against session cookie theft.

        :param expires: an optional expiration date for the cookie (a
                        :class:`datetime.datetime` object)
        """
        if self.secret_key is None:
            raise RuntimeError('no secret key defined')
        if expires:
            self['_expires'] = _date_to_unix(expires)
        result = []
        mac = hmac(self.secret_key, None, self.hash_method)
        for key, value in sorted(self.items()):
            result.append(('%s=%s' % (
                url_quote_plus(key),
                self.quote(value).decode('ascii')
            )).encode('ascii'))
            mac.update(b'|' + result[-1])
        return b'?'.join([
            base64.b64encode(mac.digest()).strip(),
            b'&'.join(result)
        ])
Пример #4
0
    def serialize(self, expires = None):
        if self.secret_key is None:
            raise RuntimeError('no secret key defined')
        if expires:
            self['_expires'] = _date_to_unix(expires)
        result = []
        mac = hmac(self.secret_key, None, self.hash_method)
        for key, value in sorted(self.items()):
            result.append('%s=%s' % (url_quote_plus(key), self.quote(value)))
            mac.update('|' + result[-1])

        return '%s?%s' % (mac.digest().encode('base64').strip(), '&'.join(result))
Пример #5
0
    def serialize(self, expires=None):
        if self.secret_key is None:
            raise RuntimeError('no secret key defined')
        if expires:
            self['_expires'] = _date_to_unix(expires)
        result = []
        mac = hmac(self.secret_key, None, self.hash_method)
        for key, value in sorted(self.items()):
            result.append('%s=%s' % (url_quote_plus(key), self.quote(value)))
            mac.update('|' + result[-1])

        return '%s?%s' % (mac.digest().encode('base64').strip(),
                          '&'.join(result))
    def serialize(self, expires=None):
        """Serialize the cookie into a string and encrypt.

        If expires is provided, the session will be automatically invalidated
        after expiration when you unseralize it. This provides better
        protection against session cookie theft.

        :param expires: an optional expiration date for the cookie (a
                        :class:`datetime.datetime` object)
        """
        if expires:
            self["_expires"] = _date_to_unix(expires)
        result = self.serialization_method.dumps(dict(self))
        return self.crypter.Encrypt(result)
Пример #7
0
    def serialize(self, expires=None):
        if self.secret_key is None:
            raise RuntimeError('no secret key defined')

        data = dict(self)
        if expires:
            data['_expires'] = _date_to_unix(expires)

        payload = self.dumps(data)

        string = self.encrypt(payload, self.secret_key)

        if self.quote_base64:
            # bytes -> ascii bytes
            string = ''.join(string.encode('base64').splitlines()).strip()

        return string
Пример #8
0
 def test_date_to_unix(self):
     assert internal._date_to_unix(datetime(1970, 1, 1)) == 0
     assert internal._date_to_unix(datetime(1970, 1, 1, 1, 0, 0)) == 3600
     assert internal._date_to_unix(datetime(1970, 1, 1, 1, 1, 1)) == 3661
     x = datetime(2010, 2, 15, 16, 15, 39)
     assert internal._date_to_unix(x) == 1266250539
Пример #9
0
 def test_date_to_unix(self):
     assert internal._date_to_unix(datetime(1970, 1, 1)) == 0
     assert internal._date_to_unix(datetime(1970, 1, 1, 1, 0, 0)) == 3600
     assert internal._date_to_unix(datetime(1970, 1, 1, 1, 1, 1)) == 3661
     x = datetime(2010, 2, 15, 16, 15, 39)
     assert internal._date_to_unix(x) == 1266250539