Debian Codegraph @

allauth/socialaccount/providers/reddit/views.py

raw
1 from __future__ import annotations
2
3 from django.http import HttpRequest
4
5 from allauth.socialaccount import app_settings
6 from allauth.socialaccount.adapter import get_adapter
7 from allauth.socialaccount.providers.oauth2.views import (
8 OAuth2Adapter,
9 OAuth2CallbackView,
10 OAuth2LoginView,
11 )
12
13
14 class RedditAdapter(OAuth2Adapter):
15 provider_id = "reddit"
16 access_token_url = "https://www.reddit.com/api/v1/access_token" # nosec
17 authorize_url = "https://www.reddit.com/api/v1/authorize"
18 profile_url = "https://oauth.reddit.com/api/v1/me"
19 basic_auth = True
20
21 settings = app_settings.PROVIDERS.get(provider_id, {})
22 # Allow custom User Agent to comply with reddit API limits
23 headers = {"User-Agent": settings.get("USER_AGENT", "django-allauth-header")}
24
25 def complete_login(self, request: HttpRequest, app, token, **kwargs):
26 headers = {"Authorization": f"bearer {token.token}", **self.headers}
27 with get_adapter().get_requests_session() as sess:
28 resp = sess.get(self.profile_url, headers=headers)
29
30 # This only here because of weird response from the test suite
31 if isinstance(resp, list):
32 resp = resp[0]
33
34 extra_data = resp.json()
35 return self.get_provider().sociallogin_from_response(request, extra_data)
36
37
38 oauth2_login = OAuth2LoginView.adapter_view(RedditAdapter)
39 oauth2_callback = OAuth2CallbackView.adapter_view(RedditAdapter)