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