-
Notifications
You must be signed in to change notification settings - Fork 153
/
views.py
340 lines (260 loc) · 12.2 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import socket
from django.conf import settings
from django.contrib.auth import logout
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from django.views.generic.base import View, TemplateView
from socialregistration.clients.oauth import OAuthError
from socialregistration.mixins import SocialRegistration
from socialregistration.contrib.openid.client import OpenIDClient
GENERATE_USERNAME = getattr(settings, 'SOCIALREGISTRATION_GENERATE_USERNAME', False)
USERNAME_FUNCTION = getattr(settings, 'SOCIALREGISTRATION_GENERATE_USERNAME_FUNCTION',
'socialregistration.utils.generate_username')
FORM_CLASS = getattr(settings, 'SOCIALREGISTRATION_SETUP_FORM',
'socialregistration.forms.UserForm')
INITAL_DATA_FUNCTION = getattr(settings, 'SOCIALREGISTRATION_INITIAL_DATA_FUNCTION',
None)
CONTEXT_FUNCTION = getattr(settings, 'SOCIALREGISTRATION_SETUP_CONTEXT_FUNCTION',
None)
ALLOW_OPENID_SIGNUPS = getattr(settings, 'SOCIALREGISTRATION_ALLOW_OPENID_SIGNUPS',
True)
class Setup(SocialRegistration, View):
"""
Setup view to create new Django users from third party APIs.
"""
template_name = 'socialregistration/setup.html'
def get_form(self):
"""
Return the form to be used. The return form is controlled
with ``SOCIALREGISTRATION_SETUP_FORM``.
"""
return self.import_attribute(FORM_CLASS)
def get_username_function(self):
"""
Return a function that can generate a username. The function
is controlled with ``SOCIALREGISTRATION_GENERATE_USERNAME_FUNCTION``.
"""
return self.import_attribute(USERNAME_FUNCTION)
def get_initial_data(self, request, user, profile, client):
"""
Return initial data for the setup form. The function can be
controlled with ``SOCIALREGISTRATION_INITIAL_DATA_FUNCTION``.
:param request: The current request object
:param user: The unsaved user object
:param profile: The unsaved profile object
:param client: The API client
"""
if INITAL_DATA_FUNCTION:
func = self.import_attribute(INITAL_DATA_FUNCTION)
return func(request, user, profile, client)
return {}
def get_context(self, request, user, profile, client):
"""
Return additional context for the setup view. The function can
be controlled with ``SOCIALREGISTRATION_SETUP_CONTEXT_FUNCTION``.
:param request: The current request object
:param user: The unsaved user object
:param profile: The unsaved profile object
:param client: The API client
"""
if CONTEXT_FUNCTION:
func = self.import_attribute(CONTEXT_FUNCTION)
return func(request, user, profile, client)
return {}
def generate_username_and_redirect(self, request, user, profile, client):
"""
Generate a username and then redirect the user to the correct place.
This method is called when ``SOCIALREGISTRATION_GENERATE_USERNAME``
is set.
:param request: The current request object
:param user: The unsaved user object
:param profile: The unsaved profile object
:param client: The API client
"""
func = self.get_username_function()
user.username = func(user, profile, client)
user.set_unusable_password()
user.save()
profile.user = user
profile.save()
user = profile.authenticate()
self.send_connect_signal(request, user, profile, client)
self.login(request, user)
self.send_login_signal(request, user, profile, client)
self.delete_session_data(request)
return HttpResponseRedirect(self.get_next(request))
def get(self, request):
"""
When signing a new user up - either display a setup form, or
generate the username automatically.
"""
if request.user.is_authenticated():
return HttpResponseRedirect(self.get_next(request))
try:
user, profile, client = self.get_session_data(request)
except KeyError:
return self.error_to_response(request, dict(
error=_("Social profile is missing from your session.")))
if GENERATE_USERNAME:
return self.generate_username_and_redirect(request, user, profile, client)
form = self.get_form()(initial=self.get_initial_data(request, user, profile, client))
additional_context = self.get_context(request, user, profile, client)
return self.render_to_response(dict({'form': form}, **additional_context))
def post(self, request):
"""
Save the user and profile, login and send the right signals.
"""
if request.user.is_authenticated():
return self.error_to_response(request, dict(
error=_("You are already logged in.")))
try:
user, profile, client = self.get_session_data(request)
except KeyError:
return self.error_to_response(request, dict(
error=_("A social profile is missing from your session.")))
form = self.get_form()(request.POST, request.FILES,
initial=self.get_initial_data(request, user, profile, client))
if not form.is_valid():
additional_context = self.get_context(request, user, profile, client)
return self.render_to_response(dict({'form': form}, **additional_context))
user, profile = form.save(request, user, profile, client)
user = profile.authenticate()
self.send_connect_signal(request, user, profile, client)
self.login(request, user)
self.send_login_signal(request, user, profile, client)
self.delete_session_data(request)
return HttpResponseRedirect(self.get_next(request))
class Logout(View):
"""
Log the user out of Django. This **does not** log the user out
of third party sites.
"""
def get(self, request):
logout(request)
url = getattr(settings, 'LOGOUT_REDIRECT_URL', '/')
return HttpResponseRedirect(url)
class OAuthRedirect(SocialRegistration, View):
"""
Base class for both OAuth and OAuth2 redirects.
:param client: The API client class that should be used.
:param template_name: The error template.
"""
# The OAuth{1,2} client to be used
client = None
# The template to render in case of errors
template_name = None
def post(self, request):
"""
Create a client, store it in the user's session and redirect the user
to the API provider to authorize our app and permissions.
"""
request.session['next'] = self.get_next(request)
client = self.get_client()()
request.session[self.get_client().get_session_key()] = client
try:
return HttpResponseRedirect(client.get_redirect_url(request=request))
except OAuthError, error:
return self.error_to_response(request, {'error': error})
except socket.timeout:
return self.error_to_response(request, {'error':
_('Could not connect to service (timed out)')})
class OAuthCallback(SocialRegistration, View):
"""
Base class for OAuth and OAuth2 callback views.
:param client: The API client class that should be used.
:param template_name: The error template.
"""
# The OAuth{1,2} client to be used
client = None
# The template to render in case of errors
template_name = None
def get_redirect(self):
"""
Return a URL that will set up the correct models if the
OAuth flow succeeded. Subclasses **must** override this
method.
"""
raise NotImplementedError
def get(self, request):
"""
Called after the user is redirected back to our application.
Tries to:
- Complete the OAuth / OAuth2 flow
- Redirect the user to another view that deals with login, connecting
or user creation.
"""
try:
client = request.session[self.get_client().get_session_key()]
client.complete(dict(request.GET.items()))
request.session[self.get_client().get_session_key()] = client
return HttpResponseRedirect(self.get_redirect())
except KeyError:
return self.error_to_response(request, {'error': "Session expired."})
except OAuthError, error:
return self.error_to_response(request, {'error': error})
except socket.timeout:
return self.error_to_response(request, {'error':
_('Could not connect to service (timed out)')})
class SetupCallback(SocialRegistration, TemplateView):
"""
Base class for OAuth and OAuth2 login / connects / registration.
"""
template_name = 'socialregistration/setup.error.html'
def get(self, request):
"""
Called after authorization was granted and the OAuth flow
successfully completed.
Tries to:
- Connect the remote account if the user is logged in already
- Log the user in if a local profile of the remote account
exists already
- Create a user and profile object if none of the above succeed
and redirect the user further to either capture some data via
form or generate a username automatically
"""
try:
client = request.session[self.get_client().get_session_key()]
except KeyError:
return self.error_to_response(request, {'error': "Session expired."})
# Get the lookup dictionary to find the user's profile
lookup_kwargs = self.get_lookup_kwargs(request, client)
# Logged in user (re-)connecting an account
if request.user.is_authenticated():
try:
profile = self.get_profile(**lookup_kwargs)
# Make sure that there is only *one* account per profile.
if not profile.user == request.user:
self.delete_session_data(request)
return self.error_to_response(request, {
'error': _('This profile is already connected to another user account.')
})
except self.get_model().DoesNotExist:
profile, created = self.get_or_create_profile(request.user,
save=True, **lookup_kwargs)
self.send_connect_signal(request, request.user, profile, client)
return self.redirect(request)
# Logged out user - let's see if we've got the identity saved already.
# If so - just log the user in. If not, create profile and redirect
# to the setup view
user = self.authenticate(**lookup_kwargs)
# No user existing - create a new one and redirect to the final setup view
if user is None:
if not ALLOW_OPENID_SIGNUPS and self.client is OpenIDClient:
return self.error_to_response(request, {
'error': _('We are not currently accepting new OpenID signups.')
})
user = self.create_user()
profile = self.create_profile(user, **lookup_kwargs)
self.store_user(request, user)
self.store_profile(request, profile)
self.store_client(request, client)
return HttpResponseRedirect(reverse('socialregistration:setup'))
# Inactive user - displaying / redirect to the appropriate place.
if not user.is_active:
return self.inactive_response(request)
# Active user with existing profile: login, send signal and redirect
self.login(request, user)
profile = self.get_profile(user=user, **lookup_kwargs)
self.send_login_signal(request, user, profile, client)
return self.redirect(request)