root / pithos / im / target / util.py @ 0778f7e1
History | View | Annotate | Download (3.9 kB)
1 |
# Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
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.
|
15 |
#
|
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.
|
28 |
#
|
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.
|
33 |
|
34 |
import logging |
35 |
import datetime |
36 |
|
37 |
from urlparse import urlsplit, urlunsplit |
38 |
from urllib import quote |
39 |
|
40 |
from django.conf import settings |
41 |
from django.http import HttpResponse |
42 |
from django.utils.http import urlencode |
43 |
from django.core.urlresolvers import reverse |
44 |
|
45 |
from pithos.im.models import User |
46 |
|
47 |
|
48 |
def get_or_create_user(uniq, realname, affiliation, level): |
49 |
"""Find or register a user into the internal database
|
50 |
and issue a token for subsequent requests.
|
51 |
"""
|
52 |
|
53 |
user, created = User.objects.get_or_create(uniq=uniq, |
54 |
defaults={ |
55 |
'realname': realname,
|
56 |
'affiliation': affiliation,
|
57 |
'level': level,
|
58 |
'invitations': settings.INVITATIONS_PER_LEVEL[level]
|
59 |
}) |
60 |
if created:
|
61 |
user.renew_token() |
62 |
user.save() |
63 |
logging.info('Created user %s', user)
|
64 |
|
65 |
return user
|
66 |
|
67 |
def prepare_response(request, user, next='', renew=False): |
68 |
"""Return the unique username and the token
|
69 |
as 'X-Auth-User' and 'X-Auth-Token' headers,
|
70 |
or redirect to the URL provided in 'next'
|
71 |
with the 'user' and 'token' as parameters.
|
72 |
|
73 |
Reissue the token even if it has not yet
|
74 |
expired, if the 'renew' parameter is present.
|
75 |
"""
|
76 |
|
77 |
if renew or user.auth_token_expires < datetime.datetime.now(): |
78 |
user.renew_token() |
79 |
user.save() |
80 |
|
81 |
if next: |
82 |
# TODO: Avoid redirect loops.
|
83 |
parts = list(urlsplit(next)) |
84 |
# Do not pass on user and token if we are on the same server.
|
85 |
if parts[1] and request.get_host() != parts[1]: |
86 |
parts[3] = urlencode({'user': user.uniq, 'token': user.auth_token}) |
87 |
next = urlunsplit(parts) |
88 |
|
89 |
if settings.FORCE_PROFILE_UPDATE and not user.is_verified: |
90 |
params = ''
|
91 |
if next: |
92 |
params = '?' + urlencode({'next': next}) |
93 |
next = reverse('pithos.im.views.users_profile') + params
|
94 |
|
95 |
response = HttpResponse() |
96 |
expire_fmt = user.auth_token_expires.strftime('%a, %d-%b-%Y %H:%M:%S %Z')
|
97 |
cookie_value = quote(user.uniq + '|' + user.auth_token)
|
98 |
response.set_cookie('_pithos2_a', value=cookie_value, expires=expire_fmt, path='/') |
99 |
|
100 |
if not next: |
101 |
response['X-Auth-User'] = user.uniq
|
102 |
response['X-Auth-Token'] = user.auth_token
|
103 |
response.content = user.uniq + '\n' + user.auth_token + '\n' |
104 |
response.status_code = 200
|
105 |
else:
|
106 |
response['Location'] = next |
107 |
response.status_code = 302
|
108 |
return response
|