root / pithos / im / target / local.py @ b4c241e6
History | View | Annotate | Download (4.8 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 |
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseBadRequest |
35 |
from django.conf import settings |
36 |
from django.template.loader import render_to_string |
37 |
from django.core.urlresolvers import reverse |
38 |
from django.utils.http import urlencode |
39 |
|
40 |
from pithos.im.target.util import prepare_response |
41 |
from pithos.im.models import User |
42 |
|
43 |
from urllib import unquote |
44 |
|
45 |
def login(request): |
46 |
username = '%s@local' % request.POST.get('username') |
47 |
password = request.POST.get('password')
|
48 |
|
49 |
if not username: |
50 |
return HttpResponseBadRequest('No user') |
51 |
|
52 |
if not username: |
53 |
return HttpResponseBadRequest('No password') |
54 |
|
55 |
try:
|
56 |
user = User.objects.get(uniq=username) |
57 |
except User.DoesNotExist:
|
58 |
return HttpResponseBadRequest('No such user') |
59 |
|
60 |
if not password or user.password != password: |
61 |
return HttpResponseBadRequest('Wrong password') |
62 |
|
63 |
if user.state == 'UNVERIFIED': |
64 |
return HttpResponseBadRequest('Unverified account') |
65 |
|
66 |
next = request.POST.get('next')
|
67 |
if settings.FORCE_PROFILE_UPDATE and not user.is_verified: |
68 |
profile_url = reverse('pithos.im.views.users_profile', args=(user.id,))
|
69 |
next = urlencode({'next': next}) |
70 |
profile_url = profile_url + '?' + next |
71 |
return prepare_response(request, user, profile_url)
|
72 |
|
73 |
return prepare_response(request, user, next) |
74 |
|
75 |
def activate(request): |
76 |
token = request.GET.get('auth')
|
77 |
next = request.GET.get('next')
|
78 |
try:
|
79 |
user = User.objects.get(auth_token=token) |
80 |
except User.DoesNotExist:
|
81 |
return HttpResponseBadRequest('No such user') |
82 |
|
83 |
user.state = 'ACTIVE'
|
84 |
user.save() |
85 |
return prepare_response(request, user, next, renew=True) |
86 |
|
87 |
def reset_password(request): |
88 |
if request.method == 'GET': |
89 |
cookie_value = unquote(request.COOKIES.get('_pithos2_a', '')) |
90 |
if cookie_value and '|' in cookie_value: |
91 |
token = cookie_value.split('|', 1)[1] |
92 |
else:
|
93 |
token = request.GET.get('auth')
|
94 |
next = request.GET.get('next')
|
95 |
username = request.GET.get('username')
|
96 |
kwargs = {'auth': token,
|
97 |
'next': next, |
98 |
'username' : username}
|
99 |
if not token: |
100 |
kwargs.update({'status': 'error', |
101 |
'message': 'Missing token'}) |
102 |
html = render_to_string('reset.html', kwargs)
|
103 |
return HttpResponse(html)
|
104 |
elif request.method == 'POST': |
105 |
token = request.POST.get('auth')
|
106 |
username = request.POST.get('username')
|
107 |
password = request.POST.get('password')
|
108 |
next = request.POST.get('next')
|
109 |
if not token: |
110 |
status = 'error'
|
111 |
message = 'Bad Request: missing token'
|
112 |
try:
|
113 |
user = User.objects.get(auth_token=token) |
114 |
if username != user.uniq:
|
115 |
status = 'error'
|
116 |
message = 'Bad Request: username mismatch'
|
117 |
else:
|
118 |
user.password = password |
119 |
user.status = 'NORMAL'
|
120 |
user.save() |
121 |
return prepare_response(request, user, next, renew=True) |
122 |
except User.DoesNotExist:
|
123 |
status = 'error'
|
124 |
message = 'Bad Request: invalid token'
|
125 |
|
126 |
html = render_to_string('reset.html', {
|
127 |
'status': status,
|
128 |
'message': message})
|
129 |
return HttpResponse(html)
|