Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / cookie.py @ be62cfa8

History | View | Annotate | Download (3.9 kB)

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