root / snf-astakos-app / astakos / im / cookie.py @ 53764fd9
History | View | Annotate | Download (3.4 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
import logging |
35 |
|
36 |
from urllib import quote, unquote |
37 |
|
38 |
from django.http import HttpRequest |
39 |
from django.contrib.auth.models import AnonymousUser |
40 |
|
41 |
from astakos.im.settings import ( |
42 |
COOKIE_NAME, COOKIE_DOMAIN, COOKIE_SECURE, LOGGING_LEVEL |
43 |
) |
44 |
|
45 |
logger = logging.getLogger(__name__) |
46 |
|
47 |
class Cookie(): |
48 |
def __init__(self, request, response): |
49 |
cookies = getattr(request, 'COOKIES', {}) |
50 |
cookie = unquote(cookies.get(COOKIE_NAME, ''))
|
51 |
self.email, sep, self.auth_token = cookie.partition('|') |
52 |
self.request = request
|
53 |
self.response = response
|
54 |
|
55 |
@property
|
56 |
def is_set(self): |
57 |
no_token = not self.auth_token |
58 |
return not no_token |
59 |
|
60 |
@property
|
61 |
def is_valid(self): |
62 |
return self.email == getattr(self.user, 'email', '') and \ |
63 |
self.auth_token == getattr(self.user, 'auth_token', '') |
64 |
|
65 |
@property
|
66 |
def user(self): |
67 |
return getattr(self.request, 'user', AnonymousUser()) |
68 |
|
69 |
def __set(self): |
70 |
user = self.user
|
71 |
expire_fmt = user.auth_token_expires.strftime('%a, %d-%b-%Y %H:%M:%S %Z')
|
72 |
cookie_value = quote(user.email + '|' + user.auth_token)
|
73 |
self.response.set_cookie(
|
74 |
COOKIE_NAME, value=cookie_value, expires=expire_fmt, path='/',
|
75 |
domain=COOKIE_DOMAIN, secure=COOKIE_SECURE |
76 |
) |
77 |
msg = 'Cookie [expiring %(auth_token_expires)s] set for %(email)s' % user.__dict__
|
78 |
logger._log(LOGGING_LEVEL, msg, []) |
79 |
|
80 |
def __delete(self): |
81 |
self.response.delete_cookie(COOKIE_NAME, path='/', domain=COOKIE_DOMAIN) |
82 |
msg = 'Cookie deleted for %(email)s' % self.__dict__ |
83 |
logger._log(LOGGING_LEVEL, msg, []) |
84 |
|
85 |
def fix(self): |
86 |
if self.user.is_authenticated(): |
87 |
if not self.is_set or not self.is_valid: |
88 |
self.__set()
|
89 |
else:
|
90 |
if self.is_set: |
91 |
self.__delete()
|