Exemple #1
0
            def session_start_response(status, headers, exc_info=None):
                session_data = environ[self.environ_key]
                path = environ['SCRIPT_NAME'] or '/'
                domain = environ['HTTP_HOST'].split(':', 1)[0]

                #if there is no registered user
                #then delete the cookie on the client.
                #by setting the contents to be the empty string
                #and the expires time to be now.

                if session_data:
                    token = self.generate(session_data)
                    expires = datetime.now() + timedelta(minutes=self.lifetime)
                else:
                    token = self.generate({})
                    expires = datetime(2000, 1, 1)

                cookie = Cookie(name=self.cookie_name,
                                value=token,
                                path=path,
                                domain=domain,
                                expires=expires)
                cookie_value = str(cookie)
                self.check_cookie_size(cookie_value)
                headers.append(('Set-Cookie', cookie_value))
                return start_response(status, headers, exc_info)
Exemple #2
0
def test_with_all_attributes():
    cookie = Cookie('key', 'value', expires=FUTURE, path='/bar',
                    domain='baz.org', max_age=800, secure=True, httponly=True,
                    samesite='Strict')
    assert str(cookie) == ('key=value; Expires=Tue, 21 Sep 2027 11:22:00 GMT; '
                           'Max-Age=800; Domain=baz.org; Path=/bar; Secure; '
                           'HttpOnly; SameSite=Strict')
    def cookie(self, sid, path="/", domain="localhost"):
        """We enforce the expiration.
        """
        # Refresh the signature on the sid.
        ssid = self.refresh_id(sid)

        # Generate the expiration date using the delta
        expires = datetime.now() + timedelta(seconds=self.delta)

        # Create the cookie containing the ssid.
        cookie = Cookie(
            name=self.cookie_name, value=ssid, path=path,
            domain=domain, expires=expires)

        value = str(cookie)

        # Check value
        if len(value) > 4093:  # 4096 - 3 bytes of overhead
            raise ValueError('The Cookie is over 4093 bytes.')

        return value
Exemple #4
0
def test_create_new_cookie():
    cookie = Cookie('key', 'value')
    assert cookie.name == 'key'
    assert cookie.value == 'value'
    assert str(cookie) == 'key=value; Path=/'
Exemple #5
0
def test_value_encoding(value, expected):
    cookie = Cookie('key', value)
    assert str(cookie) == expected
    # Make sure we can read a cookie we have just encoded.
    assert parse(expected)['key'] == value
Exemple #6
0
def test_can_set_httponly():
    cookie = Cookie('key', 'value', httponly=True)
    assert str(cookie) == 'key=value; Path=/; HttpOnly'
Exemple #7
0
def test_can_set_secure():
    cookie = Cookie('key', 'value', secure=True)
    assert str(cookie) == 'key=value; Path=/; Secure'
Exemple #8
0
def test_can_set_max_age():
    cookie = Cookie('key', 'value', max_age=600)
    assert str(cookie) == 'key=value; Max-Age=600; Path=/'
Exemple #9
0
def test_can_set_domain():
    cookie = Cookie('key', 'value', domain='www.example.org')
    assert str(cookie) == 'key=value; Domain=www.example.org; Path=/'
Exemple #10
0
def test_can_change_path():
    cookie = Cookie('key', 'value', path='/foo')
    assert str(cookie) == 'key=value; Path=/foo'
Exemple #11
0
def test_cookie_expires_format():
    cookie = Cookie('key', 'value', expires=FUTURE)
    assert str(cookie) == ('key=value; Expires=Tue, 21 Sep 2027 11:22:00 GMT; '
                           'Path=/')
Exemple #12
0
 def set(self, name, *args, **kwargs):
     self[name] = Cookie(name, *args, **kwargs)
Exemple #13
0
def test_can_set_samesite():
    cookie = Cookie('key', 'value', samesite="Strict")
    assert str(cookie) == 'key=value; Path=/; SameSite=Strict'