1 # Copyright 2011 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
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.
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.
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.
36 from urlparse import urlsplit, urlunsplit
37 from urllib import quote
39 from django.http import HttpResponse
40 from django.utils.http import urlencode
41 from django.core.urlresolvers import reverse
42 from django.conf import settings
43 from django.contrib.auth import login
45 def prepare_response(request, user, next='', renew=False):
46 """Return the unique username and the token
47 as 'X-Auth-User' and 'X-Auth-Token' headers,
48 or redirect to the URL provided in 'next'
49 with the 'user' and 'token' as parameters.
51 Reissue the token even if it has not yet
52 expired, if the 'renew' parameter is present.
55 auth_token = user.auth_token
56 auth_token_expires = user.auth_token_expires
57 if renew or auth_token_expires < datetime.datetime.now():
62 # TODO: Avoid redirect loops.
63 parts = list(urlsplit(next))
64 # Do not pass on user and token if we are on the same server.
65 if parts[1] and request.get_host() != parts[1]:
66 parts[3] = urlencode({'user': user.username, 'token': auth_token})
67 next = urlunsplit(parts)
69 if settings.FORCE_PROFILE_UPDATE and not user.is_verified:
72 params = '?' + urlencode({'next': next})
73 next = reverse('astakos.im.views.edit_profile') + params
78 response = HttpResponse()
80 response['X-Auth-User'] = user.username
81 response['X-Auth-Token'] = auth_token
82 response.content = user.username + '\n' + auth_token + '\n'
83 response.status_code = 200
85 response['Location'] = next
86 response.status_code = 302