root / astakos / im / target / util.py @ 890b0eaf
History | View | Annotate | Download (3.3 kB)
1 | 64cd4730 | Antony Chazapis | # Copyright 2011 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 | 64cd4730 | Antony Chazapis | import datetime |
35 | 64cd4730 | Antony Chazapis | |
36 | 64cd4730 | Antony Chazapis | from urlparse import urlsplit, urlunsplit |
37 | 64cd4730 | Antony Chazapis | from urllib import quote |
38 | 64cd4730 | Antony Chazapis | |
39 | 64cd4730 | Antony Chazapis | from django.http import HttpResponse |
40 | 64cd4730 | Antony Chazapis | from django.utils.http import urlencode |
41 | 64cd4730 | Antony Chazapis | from django.core.urlresolvers import reverse |
42 | 0905ccd2 | Sofia Papagiannaki | from django.conf import settings |
43 | 890b0eaf | Sofia Papagiannaki | from django.contrib.auth import login |
44 | 64cd4730 | Antony Chazapis | |
45 | 64cd4730 | Antony Chazapis | def prepare_response(request, user, next='', renew=False): |
46 | 64cd4730 | Antony Chazapis | """Return the unique username and the token
|
47 | 64cd4730 | Antony Chazapis | as 'X-Auth-User' and 'X-Auth-Token' headers,
|
48 | 64cd4730 | Antony Chazapis | or redirect to the URL provided in 'next'
|
49 | 64cd4730 | Antony Chazapis | with the 'user' and 'token' as parameters.
|
50 | 64cd4730 | Antony Chazapis |
|
51 | 64cd4730 | Antony Chazapis | Reissue the token even if it has not yet
|
52 | 64cd4730 | Antony Chazapis | expired, if the 'renew' parameter is present.
|
53 | 64cd4730 | Antony Chazapis | """
|
54 | 64cd4730 | Antony Chazapis | |
55 | 0905ccd2 | Sofia Papagiannaki | auth_token = user.auth_token |
56 | 0905ccd2 | Sofia Papagiannaki | auth_token_expires = user.auth_token_expires |
57 | 0905ccd2 | Sofia Papagiannaki | if renew or auth_token_expires < datetime.datetime.now(): |
58 | 64cd4730 | Antony Chazapis | user.renew_token() |
59 | 64cd4730 | Antony Chazapis | user.save() |
60 | 64cd4730 | Antony Chazapis | |
61 | 64cd4730 | Antony Chazapis | if next: |
62 | 64cd4730 | Antony Chazapis | # TODO: Avoid redirect loops.
|
63 | 64cd4730 | Antony Chazapis | parts = list(urlsplit(next)) |
64 | 64cd4730 | Antony Chazapis | # Do not pass on user and token if we are on the same server.
|
65 | 64cd4730 | Antony Chazapis | if parts[1] and request.get_host() != parts[1]: |
66 | 0905ccd2 | Sofia Papagiannaki | parts[3] = urlencode({'user': user.username, 'token': auth_token}) |
67 | 64cd4730 | Antony Chazapis | next = urlunsplit(parts) |
68 | 64cd4730 | Antony Chazapis | |
69 | 64cd4730 | Antony Chazapis | if settings.FORCE_PROFILE_UPDATE and not user.is_verified: |
70 | 64cd4730 | Antony Chazapis | params = ''
|
71 | 64cd4730 | Antony Chazapis | if next: |
72 | 64cd4730 | Antony Chazapis | params = '?' + urlencode({'next': next}) |
73 | 890b0eaf | Sofia Papagiannaki | next = reverse('astakos.im.views.edit_profile') + params
|
74 | 890b0eaf | Sofia Papagiannaki | |
75 | 890b0eaf | Sofia Papagiannaki | # user login
|
76 | 890b0eaf | Sofia Papagiannaki | login(request, user) |
77 | 64cd4730 | Antony Chazapis | |
78 | 64cd4730 | Antony Chazapis | response = HttpResponse() |
79 | 64cd4730 | Antony Chazapis | if not next: |
80 | 0905ccd2 | Sofia Papagiannaki | response['X-Auth-User'] = user.username
|
81 | 0905ccd2 | Sofia Papagiannaki | response['X-Auth-Token'] = auth_token
|
82 | 0905ccd2 | Sofia Papagiannaki | response.content = user.username + '\n' + auth_token + '\n' |
83 | 64cd4730 | Antony Chazapis | response.status_code = 200
|
84 | 64cd4730 | Antony Chazapis | else:
|
85 | 64cd4730 | Antony Chazapis | response['Location'] = next |
86 | 64cd4730 | Antony Chazapis | response.status_code = 302
|
87 | 64cd4730 | Antony Chazapis | return response |