root / snf-astakos-app / astakos / im / views / target / local.py @ 933d3219
History | View | Annotate | Download (8 kB)
1 |
# Copyright 2011-2012 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 HttpResponseRedirect |
35 |
from django.shortcuts import render_to_response |
36 |
from django.template import RequestContext |
37 |
from django.contrib import messages |
38 |
from django.utils.translation import ugettext as _ |
39 |
from django.views.decorators.csrf import csrf_exempt |
40 |
from django.views.decorators.http import require_http_methods |
41 |
from django.core.urlresolvers import reverse |
42 |
from django.contrib.auth.decorators import login_required |
43 |
|
44 |
import django.contrib.auth.views as django_auth_views |
45 |
|
46 |
from astakos.im.util import prepare_response, get_query |
47 |
from astakos.im.models import PendingThirdPartyUser |
48 |
from astakos.im.forms import LoginForm, ExtendedPasswordChangeForm, \ |
49 |
ExtendedSetPasswordForm
|
50 |
from astakos.im import settings |
51 |
import astakos.im.messages as astakos_messages |
52 |
from astakos.im import auth_providers as auth |
53 |
from astakos.im.views.decorators import cookie_fix, requires_anonymous, \ |
54 |
signed_terms_required, requires_auth_provider |
55 |
|
56 |
from ratelimit.decorators import ratelimit |
57 |
|
58 |
retries = settings.RATELIMIT_RETRIES_ALLOWED - 1
|
59 |
rate = str(retries) + '/m' |
60 |
|
61 |
|
62 |
@requires_auth_provider('local') |
63 |
@require_http_methods(["GET", "POST"]) |
64 |
@csrf_exempt
|
65 |
@requires_anonymous
|
66 |
@cookie_fix
|
67 |
@ratelimit(field='username', method='POST', rate=rate) |
68 |
def login(request, on_failure='im/login.html'): |
69 |
"""
|
70 |
on_failure: the template name to render on login failure
|
71 |
"""
|
72 |
was_limited = getattr(request, 'limited', False) |
73 |
form = LoginForm(data=request.POST, |
74 |
was_limited=was_limited, |
75 |
request=request) |
76 |
next = get_query(request).get('next', '') |
77 |
third_party_token = get_query(request).get('key', False) |
78 |
provider = auth.get_provider('local')
|
79 |
|
80 |
if not form.is_valid(): |
81 |
if third_party_token:
|
82 |
messages.info(request, provider.get_login_to_add_msg) |
83 |
|
84 |
return render_to_response(
|
85 |
on_failure, |
86 |
{'login_form':form,
|
87 |
'next':next, |
88 |
'key': third_party_token},
|
89 |
context_instance=RequestContext(request)) |
90 |
|
91 |
# get the user from the cache
|
92 |
user = form.user_cache |
93 |
provider = auth.get_provider('local', user)
|
94 |
|
95 |
if not provider.get_login_policy: |
96 |
message = provider.get_login_disabled_msg |
97 |
messages.error(request, message) |
98 |
return HttpResponseRedirect(reverse('login')) |
99 |
|
100 |
message = None
|
101 |
if not user: |
102 |
message = provider.get_authentication_failed_msg |
103 |
elif not user.is_active: |
104 |
message = user.get_inactive_message('local')
|
105 |
|
106 |
elif not user.has_auth_provider('local'): |
107 |
# valid user logged in with no auth providers set, add local provider
|
108 |
# and let him log in
|
109 |
if not user.get_available_auth_providers(): |
110 |
user.add_auth_provider('local')
|
111 |
else:
|
112 |
message = _(astakos_messages.NO_LOCAL_AUTH) |
113 |
|
114 |
if message:
|
115 |
messages.error(request, message) |
116 |
return render_to_response(on_failure,
|
117 |
{'login_form': form},
|
118 |
context_instance=RequestContext(request)) |
119 |
|
120 |
response = prepare_response(request, user, next)
|
121 |
if third_party_token:
|
122 |
# use requests to assign the account he just authenticated with with
|
123 |
# a third party provider account
|
124 |
try:
|
125 |
request.user.add_pending_auth_provider(third_party_token) |
126 |
except PendingThirdPartyUser.DoesNotExist:
|
127 |
provider = auth.get_provider('local', request.user)
|
128 |
messages.error(request, provider.get_add_failed_msg) |
129 |
|
130 |
provider = user.get_auth_provider('local')
|
131 |
messages.success(request, provider.get_login_success_msg) |
132 |
response.set_cookie('astakos_last_login_method', 'local') |
133 |
return response
|
134 |
|
135 |
|
136 |
@require_http_methods(["GET"]) |
137 |
@cookie_fix
|
138 |
def password_reset_done(request, *args, **kwargs): |
139 |
messages.success(request, _(astakos_messages.PASSWORD_RESET_DONE)) |
140 |
return HttpResponseRedirect(reverse('index')) |
141 |
|
142 |
|
143 |
@require_http_methods(["GET"]) |
144 |
@cookie_fix
|
145 |
def password_reset_confirm_done(request, *args, **kwargs): |
146 |
messages.success(request, _(astakos_messages.PASSWORD_RESET_CONFIRM_DONE)) |
147 |
return HttpResponseRedirect(reverse('index')) |
148 |
|
149 |
|
150 |
@cookie_fix
|
151 |
def password_reset(request, *args, **kwargs): |
152 |
kwargs['post_reset_redirect'] = reverse(
|
153 |
'astakos.im.views.target.local.password_reset_done')
|
154 |
return django_auth_views.password_reset(request, *args, **kwargs)
|
155 |
|
156 |
|
157 |
@cookie_fix
|
158 |
def password_reset_confirm(request, *args, **kwargs): |
159 |
kwargs['post_reset_redirect'] = reverse(
|
160 |
'astakos.im.views.target.local.password_reset_complete')
|
161 |
return django_auth_views.password_reset_confirm(request, *args, **kwargs)
|
162 |
|
163 |
|
164 |
@cookie_fix
|
165 |
def password_reset_complete(request, *args, **kwargs): |
166 |
return django_auth_views.password_reset_complete(request, *args, **kwargs)
|
167 |
|
168 |
|
169 |
@require_http_methods(["GET", "POST"]) |
170 |
@signed_terms_required
|
171 |
@login_required
|
172 |
@cookie_fix
|
173 |
@requires_auth_provider('local', login=True) |
174 |
def password_change(request, template_name='registration/password_change_form.html', |
175 |
post_change_redirect=None, password_change_form=ExtendedPasswordChangeForm):
|
176 |
|
177 |
create_password = False
|
178 |
|
179 |
provider = auth.get_provider('local', request.user)
|
180 |
|
181 |
# no local backend user wants to create a password
|
182 |
if not request.user.has_auth_provider('local'): |
183 |
if not provider.get_add_policy: |
184 |
messages.error(request, provider.get_add_disabled_msg) |
185 |
return HttpResponseRedirect(reverse('edit_profile')) |
186 |
|
187 |
create_password = True
|
188 |
password_change_form = ExtendedSetPasswordForm |
189 |
|
190 |
if post_change_redirect is None: |
191 |
post_change_redirect = reverse('edit_profile')
|
192 |
|
193 |
if request.method == "POST": |
194 |
form_kwargs = dict(
|
195 |
user=request.user, |
196 |
data=request.POST, |
197 |
) |
198 |
if not create_password: |
199 |
form_kwargs['session_key'] = session_key=request.session.session_key
|
200 |
|
201 |
form = password_change_form(**form_kwargs) |
202 |
if form.is_valid():
|
203 |
form.save() |
204 |
if create_password:
|
205 |
provider = auth.get_provider('local', request.user)
|
206 |
messages.success(request, provider.get_added_msg) |
207 |
else:
|
208 |
messages.success(request, |
209 |
astakos_messages.PASSWORD_RESET_CONFIRM_DONE) |
210 |
return HttpResponseRedirect(post_change_redirect)
|
211 |
else:
|
212 |
form = password_change_form(user=request.user) |
213 |
return render_to_response(template_name, {
|
214 |
'form': form,
|
215 |
}, context_instance=RequestContext(request, {'create_password':
|
216 |
create_password})) |
217 |
|