|
1 |
| -from social.p3 import quote |
2 |
| -from social.utils import sanitize_redirect, user_is_authenticated, \ |
3 |
| - user_is_active, partial_pipeline_data, setting_url |
4 |
| - |
5 |
| - |
6 |
| -def do_auth(backend, redirect_name='next'): |
7 |
| - # Clean any partial pipeline data |
8 |
| - backend.strategy.clean_partial_pipeline() |
9 |
| - |
10 |
| - # Save any defined next value into session |
11 |
| - data = backend.strategy.request_data(merge=False) |
12 |
| - |
13 |
| - # Save extra data into session. |
14 |
| - for field_name in backend.setting('FIELDS_STORED_IN_SESSION', []): |
15 |
| - if field_name in data: |
16 |
| - backend.strategy.session_set(field_name, data[field_name]) |
17 |
| - |
18 |
| - if redirect_name in data: |
19 |
| - # Check and sanitize a user-defined GET/POST next field value |
20 |
| - redirect_uri = data[redirect_name] |
21 |
| - if backend.setting('SANITIZE_REDIRECTS', True): |
22 |
| - allowed_hosts = backend.setting('ALLOWED_REDIRECT_HOSTS', []) + \ |
23 |
| - [backend.strategy.request_host()] |
24 |
| - redirect_uri = sanitize_redirect(allowed_hosts, redirect_uri) |
25 |
| - backend.strategy.session_set( |
26 |
| - redirect_name, |
27 |
| - redirect_uri or backend.setting('LOGIN_REDIRECT_URL') |
28 |
| - ) |
29 |
| - return backend.start() |
30 |
| - |
31 |
| - |
32 |
| -def do_complete(backend, login, user=None, redirect_name='next', |
33 |
| - *args, **kwargs): |
34 |
| - data = backend.strategy.request_data() |
35 |
| - |
36 |
| - is_authenticated = user_is_authenticated(user) |
37 |
| - user = is_authenticated and user or None |
38 |
| - |
39 |
| - partial = partial_pipeline_data(backend, user, *args, **kwargs) |
40 |
| - if partial: |
41 |
| - xargs, xkwargs = partial |
42 |
| - user = backend.continue_pipeline(*xargs, **xkwargs) |
43 |
| - else: |
44 |
| - user = backend.complete(user=user, *args, **kwargs) |
45 |
| - |
46 |
| - # pop redirect value before the session is trashed on login(), but after |
47 |
| - # the pipeline so that the pipeline can change the redirect if needed |
48 |
| - redirect_value = backend.strategy.session_get(redirect_name, '') or \ |
49 |
| - data.get(redirect_name, '') |
50 |
| - |
51 |
| - user_model = backend.strategy.storage.user.user_model() |
52 |
| - if user and not isinstance(user, user_model): |
53 |
| - return user |
54 |
| - |
55 |
| - if is_authenticated: |
56 |
| - if not user: |
57 |
| - url = setting_url(backend, redirect_value, 'LOGIN_REDIRECT_URL') |
58 |
| - else: |
59 |
| - url = setting_url(backend, redirect_value, |
60 |
| - 'NEW_ASSOCIATION_REDIRECT_URL', |
61 |
| - 'LOGIN_REDIRECT_URL') |
62 |
| - elif user: |
63 |
| - if user_is_active(user): |
64 |
| - # catch is_new/social_user in case login() resets the instance |
65 |
| - is_new = getattr(user, 'is_new', False) |
66 |
| - social_user = user.social_user |
67 |
| - login(backend, user, social_user) |
68 |
| - # store last login backend name in session |
69 |
| - backend.strategy.session_set('social_auth_last_login_backend', |
70 |
| - social_user.provider) |
71 |
| - |
72 |
| - if is_new: |
73 |
| - url = setting_url(backend, |
74 |
| - 'NEW_USER_REDIRECT_URL', |
75 |
| - redirect_value, |
76 |
| - 'LOGIN_REDIRECT_URL') |
77 |
| - else: |
78 |
| - url = setting_url(backend, redirect_value, |
79 |
| - 'LOGIN_REDIRECT_URL') |
80 |
| - else: |
81 |
| - if backend.setting('INACTIVE_USER_LOGIN', False): |
82 |
| - social_user = user.social_user |
83 |
| - login(backend, user, social_user) |
84 |
| - url = setting_url(backend, 'INACTIVE_USER_URL', 'LOGIN_ERROR_URL', |
85 |
| - 'LOGIN_URL') |
86 |
| - else: |
87 |
| - url = setting_url(backend, 'LOGIN_ERROR_URL', 'LOGIN_URL') |
88 |
| - |
89 |
| - if redirect_value and redirect_value != url: |
90 |
| - redirect_value = quote(redirect_value) |
91 |
| - url += ('?' in url and '&' or '?') + \ |
92 |
| - '{0}={1}'.format(redirect_name, redirect_value) |
93 |
| - |
94 |
| - if backend.setting('SANITIZE_REDIRECTS', True): |
95 |
| - allowed_hosts = backend.setting('ALLOWED_REDIRECT_HOSTS', []) + \ |
96 |
| - [backend.strategy.request_host()] |
97 |
| - url = sanitize_redirect(allowed_hosts, url) or \ |
98 |
| - backend.setting('LOGIN_REDIRECT_URL') |
99 |
| - return backend.strategy.redirect(url) |
100 |
| - |
101 |
| - |
102 |
| -def do_disconnect(backend, user, association_id=None, redirect_name='next', |
103 |
| - *args, **kwargs): |
104 |
| - partial = partial_pipeline_data(backend, user, *args, **kwargs) |
105 |
| - if partial: |
106 |
| - xargs, xkwargs = partial |
107 |
| - if association_id and not xkwargs.get('association_id'): |
108 |
| - xkwargs['association_id'] = association_id |
109 |
| - response = backend.disconnect(*xargs, **xkwargs) |
110 |
| - else: |
111 |
| - response = backend.disconnect(user=user, association_id=association_id, |
112 |
| - *args, **kwargs) |
113 |
| - |
114 |
| - if isinstance(response, dict): |
115 |
| - response = backend.strategy.redirect( |
116 |
| - backend.strategy.absolute_uri( |
117 |
| - backend.strategy.request_data().get(redirect_name, '') or |
118 |
| - backend.setting('DISCONNECT_REDIRECT_URL') or |
119 |
| - backend.setting('LOGIN_REDIRECT_URL') |
120 |
| - ) |
121 |
| - ) |
122 |
| - return response |
| 1 | +from social_core.actions import do_auth, do_complete, do_disconnect |
0 commit comments