Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / target / redirect.py @ 3b258643

History | View | Annotate | Download (5.5 kB)

1 aba1e498 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 64cd4730 Antony Chazapis
#
3 64cd4730 Antony Chazapis
# Redistribution and use in source and binary forms, with or
4 64cd4730 Antony Chazapis
# without modification, are permitted provided that the following
5 64cd4730 Antony Chazapis
# conditions are met:
6 64cd4730 Antony Chazapis
#
7 64cd4730 Antony Chazapis
#   1. Redistributions of source code must retain the above
8 64cd4730 Antony Chazapis
#      copyright notice, this list of conditions and the following
9 64cd4730 Antony Chazapis
#      disclaimer.
10 64cd4730 Antony Chazapis
#
11 64cd4730 Antony Chazapis
#   2. Redistributions in binary form must reproduce the above
12 64cd4730 Antony Chazapis
#      copyright notice, this list of conditions and the following
13 64cd4730 Antony Chazapis
#      disclaimer in the documentation and/or other materials
14 64cd4730 Antony Chazapis
#      provided with the distribution.
15 64cd4730 Antony Chazapis
#
16 64cd4730 Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 64cd4730 Antony Chazapis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 64cd4730 Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 64cd4730 Antony Chazapis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 64cd4730 Antony Chazapis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 64cd4730 Antony Chazapis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 64cd4730 Antony Chazapis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 64cd4730 Antony Chazapis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 64cd4730 Antony Chazapis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 64cd4730 Antony Chazapis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 64cd4730 Antony Chazapis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 64cd4730 Antony Chazapis
# POSSIBILITY OF SUCH DAMAGE.
28 64cd4730 Antony Chazapis
#
29 64cd4730 Antony Chazapis
# The views and conclusions contained in the software and
30 64cd4730 Antony Chazapis
# documentation are those of the authors and should not be
31 64cd4730 Antony Chazapis
# interpreted as representing official policies, either expressed
32 64cd4730 Antony Chazapis
# or implied, of GRNET S.A.
33 64cd4730 Antony Chazapis
34 148b0cde Sofia Papagiannaki
from django.core.urlresolvers import reverse
35 a196eb7e Sofia Papagiannaki
from django.utils.translation import ugettext as _
36 a196eb7e Sofia Papagiannaki
from django.utils.http import urlencode
37 111f3da6 Sofia Papagiannaki
from django.contrib.auth import authenticate
38 217994f8 Sofia Papagiannaki
from django.http import (
39 217994f8 Sofia Papagiannaki
    HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
40 217994f8 Sofia Papagiannaki
)
41 27e26a41 Sofia Papagiannaki
from django.core.exceptions import ValidationError
42 9a06d96f Olga Brani
from django.views.decorators.http import require_http_methods
43 a196eb7e Sofia Papagiannaki
44 aab4d540 Sofia Papagiannaki
from urlparse import urlunsplit, urlsplit, parse_qsl
45 64cd4730 Antony Chazapis
46 b2ffa772 Sofia Papagiannaki
from astakos.im.settings import COOKIE_DOMAIN
47 b2ffa772 Sofia Papagiannaki
from astakos.im.util import restrict_next
48 111f3da6 Sofia Papagiannaki
from astakos.im.functions import login as auth_login, logout
49 860b37f0 Sofia Papagiannaki
50 ae497612 Olga Brani
import astakos.im.messages as astakos_messages
51 ae497612 Olga Brani
52 e015e9e6 Sofia Papagiannaki
import logging
53 e015e9e6 Sofia Papagiannaki
54 e015e9e6 Sofia Papagiannaki
logger = logging.getLogger(__name__)
55 e015e9e6 Sofia Papagiannaki
56 5ce3ce4f Sofia Papagiannaki
57 9a06d96f Olga Brani
@require_http_methods(["GET", "POST"])
58 64cd4730 Antony Chazapis
def login(request):
59 a196eb7e Sofia Papagiannaki
    """
60 09e7393c Sofia Papagiannaki
    If there is no ``next`` request parameter redirects to astakos index page
61 09e7393c Sofia Papagiannaki
    displaying an error message.
62 09e7393c Sofia Papagiannaki
    If the request user is authenticated and has signed the approval terms,
63 09e7393c Sofia Papagiannaki
    redirects to `next` request parameter. If not, redirects to approval terms
64 09e7393c Sofia Papagiannaki
    in order to return back here after agreeing with the terms.
65 ebd369d0 Sofia Papagiannaki
    Otherwise, redirects to login in order to return back here after successful login.
66 a196eb7e Sofia Papagiannaki
    """
67 ebd369d0 Sofia Papagiannaki
    next = request.GET.get('next')
68 ebd369d0 Sofia Papagiannaki
    if not next:
69 ae497612 Olga Brani
        return HttpResponseBadRequest(_(astakos_messages.MISSING_NEXT_PARAMETER))
70 217994f8 Sofia Papagiannaki
    if not restrict_next(
71 217994f8 Sofia Papagiannaki
        next, domain=COOKIE_DOMAIN, allowed_schemes=('pithos',)
72 217994f8 Sofia Papagiannaki
    ):
73 c0b26605 Sofia Papagiannaki
        return HttpResponseForbidden(_(astakos_messages.NOT_ALLOWED_NEXT_PARAM))
74 ebd369d0 Sofia Papagiannaki
    force = request.GET.get('force', None)
75 ebd369d0 Sofia Papagiannaki
    response = HttpResponse()
76 ebd369d0 Sofia Papagiannaki
    if force == '':
77 ebd369d0 Sofia Papagiannaki
        logout(request)
78 148b0cde Sofia Papagiannaki
    if request.user.is_authenticated():
79 e510fae4 Sofia Papagiannaki
        # if user has not signed the approval terms
80 e510fae4 Sofia Papagiannaki
        # redirect to approval terms with next the request path
81 fcf90160 Sofia Papagiannaki
        if not request.user.signed_terms:
82 e510fae4 Sofia Papagiannaki
            # first build next parameter
83 e510fae4 Sofia Papagiannaki
            parts = list(urlsplit(request.build_absolute_uri()))
84 e510fae4 Sofia Papagiannaki
            params = dict(parse_qsl(parts[3], keep_blank_values=True))
85 e510fae4 Sofia Papagiannaki
            # delete force parameter
86 e510fae4 Sofia Papagiannaki
            parts[3] = urlencode(params)
87 e510fae4 Sofia Papagiannaki
            next = urlunsplit(parts)
88 5ce3ce4f Sofia Papagiannaki
89 e510fae4 Sofia Papagiannaki
            # build url location
90 e510fae4 Sofia Papagiannaki
            parts[2] = reverse('latest_terms')
91 5ce3ce4f Sofia Papagiannaki
            params = {'next': next}
92 e510fae4 Sofia Papagiannaki
            parts[3] = urlencode(params)
93 e510fae4 Sofia Papagiannaki
            url = urlunsplit(parts)
94 e510fae4 Sofia Papagiannaki
            response['Location'] = url
95 e510fae4 Sofia Papagiannaki
            response.status_code = 302
96 e510fae4 Sofia Papagiannaki
            return response
97 860b37f0 Sofia Papagiannaki
        renew = request.GET.get('renew', None)
98 ebd369d0 Sofia Papagiannaki
        if renew == '':
99 bf0c6de5 Sofia Papagiannaki
            request.user.renew_token(
100 bf0c6de5 Sofia Papagiannaki
                flush_sessions=True,
101 bf0c6de5 Sofia Papagiannaki
                current_key=request.session.session_key
102 bf0c6de5 Sofia Papagiannaki
            )
103 27e26a41 Sofia Papagiannaki
            try:
104 27e26a41 Sofia Papagiannaki
                request.user.save()
105 27e26a41 Sofia Papagiannaki
            except ValidationError, e:
106 27e26a41 Sofia Papagiannaki
                return HttpResponseBadRequest(e)
107 ebd369d0 Sofia Papagiannaki
            # authenticate before login
108 aab4d540 Sofia Papagiannaki
            user = authenticate(email=request.user.email,
109 5ce3ce4f Sofia Papagiannaki
                                auth_token=request.user.auth_token
110 5ce3ce4f Sofia Papagiannaki
                                )
111 ebd369d0 Sofia Papagiannaki
            auth_login(request, user)
112 ebd369d0 Sofia Papagiannaki
            logger.info('Token reset for %s' % request.user.email)
113 ebd369d0 Sofia Papagiannaki
        parts = list(urlsplit(next))
114 aab4d540 Sofia Papagiannaki
        parts[3] = urlencode({'user': request.user.email,
115 5ce3ce4f Sofia Papagiannaki
                              'token': request.user.auth_token
116 5ce3ce4f Sofia Papagiannaki
                              }
117 5ce3ce4f Sofia Papagiannaki
                             )
118 ebd369d0 Sofia Papagiannaki
        url = urlunsplit(parts)
119 ebd369d0 Sofia Papagiannaki
        response['Location'] = url
120 ebd369d0 Sofia Papagiannaki
        response.status_code = 302
121 ebd369d0 Sofia Papagiannaki
        return response
122 148b0cde Sofia Papagiannaki
    else:
123 e510fae4 Sofia Papagiannaki
        # redirect to login with next the request path
124 5ce3ce4f Sofia Papagiannaki
125 ebd369d0 Sofia Papagiannaki
        # first build next parameter
126 ebd369d0 Sofia Papagiannaki
        parts = list(urlsplit(request.build_absolute_uri()))
127 ebd369d0 Sofia Papagiannaki
        params = dict(parse_qsl(parts[3], keep_blank_values=True))
128 ebd369d0 Sofia Papagiannaki
        # delete force parameter
129 ebd369d0 Sofia Papagiannaki
        if 'force' in params:
130 ebd369d0 Sofia Papagiannaki
            del params['force']
131 ebd369d0 Sofia Papagiannaki
        parts[3] = urlencode(params)
132 ebd369d0 Sofia Papagiannaki
        next = urlunsplit(parts)
133 5ce3ce4f Sofia Papagiannaki
134 ebd369d0 Sofia Papagiannaki
        # build url location
135 6ff7a7ca Sofia Papagiannaki
        parts[2] = reverse('index')
136 5ce3ce4f Sofia Papagiannaki
        params = {'next': next}
137 ebd369d0 Sofia Papagiannaki
        parts[3] = urlencode(params)
138 ebd369d0 Sofia Papagiannaki
        url = urlunsplit(parts)
139 ebd369d0 Sofia Papagiannaki
        response['Location'] = url
140 ebd369d0 Sofia Papagiannaki
        response.status_code = 302
141 5ce3ce4f Sofia Papagiannaki
        return response