Debian Codegraph @

allauth/socialaccount/providers/oauth/provider.py

raw
1 from __future__ import annotations
2
3 import logging
4 from urllib.parse import parse_qsl
5
6 from django.core.exceptions import ImproperlyConfigured
7 from django.http import HttpRequest
8 from django.urls import reverse
9 from django.utils.http import urlencode
10
11 from allauth.socialaccount.helpers import render_authentication_error
12 from allauth.socialaccount.providers.base import Provider
13 from allauth.socialaccount.providers.base.constants import AuthAction
14 from allauth.socialaccount.providers.oauth.client import OAuthError
15
16
17 logger = logging.getLogger(__name__)
18
19
20 class OAuthProvider(Provider):
21 supports_redirect = True
22
23 def get_login_url(self, request: HttpRequest, **kwargs):
24 url = reverse(f"{self.id}_login")
25 if kwargs:
26 url = f"{url}?{urlencode(kwargs)}"
27 return url
28
29 def get_auth_params(self):
30 settings = self.get_settings()
31 ret = dict(settings.get("AUTH_PARAMS", {}))
32 return ret
33
34 def get_auth_params_from_request(self, request: HttpRequest, action):
35 ret = self.get_auth_params()
36 dynamic_auth_params = request.GET.get("auth_params", None)
37 if dynamic_auth_params:
38 ret.update(dict(parse_qsl(dynamic_auth_params)))
39 return ret
40
41 def get_auth_url(self, request: HttpRequest, action):
42 # TODO: This is ugly. Move authorization_url away from the
43 # adapter into the provider. Hmpf, the line between
44 # adapter/provider is a bit too thin here.
45 return None
46
47 def get_scope_from_request(self, request: HttpRequest):
48 return self.get_scope()
49
50 def get_scope(self):
51 settings = self.get_settings()
52 scope = settings.get("SCOPE")
53 if scope is None:
54 scope = self.get_default_scope()
55 return scope
56
57 def get_default_scope(self):
58 return []
59
60 def get_oauth_adapter(self, request: HttpRequest):
61 if not hasattr(self, "oauth_adapter_class"):
62 raise ImproperlyConfigured(f"No oauth_adapter_class set for {self!r}")
63 return self.oauth_adapter_class(request)
64
65 def get_redirect_from_request_kwargs(self, request: HttpRequest):
66 kwargs = super().get_redirect_from_request_kwargs(request)
67 kwargs["scope"] = self.get_scope_from_request(request)
68 action = request.GET.get("action", AuthAction.AUTHENTICATE)
69 kwargs["action"] = action
70 kwargs["auth_params"] = self.get_auth_params_from_request(request, action)
71 return kwargs
72
73 def redirect(
74 self, request: HttpRequest, process, next_url=None, data=None, **kwargs
75 ):
76 callback_url = reverse(f"{self.id}_callback")
77 oauth_adapter = self.get_oauth_adapter(request)
78 action = kwargs.pop("action", AuthAction.AUTHENTICATE)
79 auth_url = self.get_auth_url(request, action) or oauth_adapter.authorize_url
80 auth_params = kwargs.pop("auth_params", None)
81 if auth_params is None:
82 auth_params = self.get_auth_params()
83 scope = kwargs.pop("scope", None)
84 if scope is None:
85 scope = self.get_scope()
86 self.stash_redirect_state(request, process, next_url, data, **kwargs)
87 client = oauth_adapter._get_client(request, callback_url, scope=scope)
88 try:
89 return client.get_redirect(auth_url, auth_params)
90 except OAuthError as e:
91 logger.error("OAuth authentication error", exc_info=True)
92 return render_authentication_error(request, self, exception=e)