Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / middleware.py @ 304acb60

History | View | Annotate | Download (2.5 kB)

1 68ed1683 Sofia Papagiannaki
# Copyright 2011 GRNET S.A. All rights reserved.
2 68ed1683 Sofia Papagiannaki
#
3 68ed1683 Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
4 68ed1683 Sofia Papagiannaki
# without modification, are permitted provided that the following
5 68ed1683 Sofia Papagiannaki
# conditions are met:
6 68ed1683 Sofia Papagiannaki
#
7 68ed1683 Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
8 68ed1683 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
9 68ed1683 Sofia Papagiannaki
#      disclaimer.
10 68ed1683 Sofia Papagiannaki
#
11 68ed1683 Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
12 68ed1683 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
13 68ed1683 Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
14 68ed1683 Sofia Papagiannaki
#      provided with the distribution.
15 68ed1683 Sofia Papagiannaki
#
16 68ed1683 Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 68ed1683 Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 68ed1683 Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 68ed1683 Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 68ed1683 Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 68ed1683 Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 68ed1683 Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 68ed1683 Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 68ed1683 Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 68ed1683 Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 68ed1683 Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 68ed1683 Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
28 68ed1683 Sofia Papagiannaki
#
29 68ed1683 Sofia Papagiannaki
# The views and conclusions contained in the software and
30 68ed1683 Sofia Papagiannaki
# documentation are those of the authors and should not be
31 68ed1683 Sofia Papagiannaki
# interpreted as representing official policies, either expressed
32 68ed1683 Sofia Papagiannaki
# or implied, of GRNET S.A.
33 68ed1683 Sofia Papagiannaki
34 68ed1683 Sofia Papagiannaki
from urllib import unquote
35 111f3da6 Sofia Papagiannaki
from django.contrib.auth import authenticate
36 68ed1683 Sofia Papagiannaki
37 68ed1683 Sofia Papagiannaki
from astakos.im.settings import COOKIE_NAME
38 68ed1683 Sofia Papagiannaki
from astakos.im.models import AstakosUser
39 111f3da6 Sofia Papagiannaki
from astakos.im.functions import login
40 68ed1683 Sofia Papagiannaki
41 68ed1683 Sofia Papagiannaki
class CookieAuthenticationMiddleware(object):
42 68ed1683 Sofia Papagiannaki
    def process_request(self, request):
43 68ed1683 Sofia Papagiannaki
        assert hasattr(request, 'session'), "The Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'."
44 68ed1683 Sofia Papagiannaki
        if request.user.is_authenticated():
45 68ed1683 Sofia Papagiannaki
            return None
46 68ed1683 Sofia Papagiannaki
        
47 68ed1683 Sofia Papagiannaki
        cookie = unquote(request.COOKIES.get(COOKIE_NAME, ''))
48 68ed1683 Sofia Papagiannaki
        email, sep, auth_token = cookie.partition('|')
49 68ed1683 Sofia Papagiannaki
        if not sep:
50 68ed1683 Sofia Papagiannaki
            return None
51 68ed1683 Sofia Papagiannaki
        
52 68ed1683 Sofia Papagiannaki
        try:
53 68ed1683 Sofia Papagiannaki
            user = authenticate(email=email, auth_token=auth_token)
54 68ed1683 Sofia Papagiannaki
            if user:
55 68ed1683 Sofia Papagiannaki
                request.user = user
56 68ed1683 Sofia Papagiannaki
            login(request, user)
57 68ed1683 Sofia Papagiannaki
        except:
58 68ed1683 Sofia Papagiannaki
            pass
59 68ed1683 Sofia Papagiannaki
        return None