Debian Codegraph @

allauth/socialaccount/providers/lichess/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.app_settings import QUERY_EMAIL
8 from allauth.socialaccount.providers.oauth2.views import (
9 OAuth2Adapter,
10 OAuth2CallbackView,
11 OAuth2LoginView,
12 )
13
14
15 class LichessOAuth2Adapter(OAuth2Adapter):
16 provider_id = "lichess"
17
18 settings = app_settings.PROVIDERS.get(provider_id, {})
19 provider_base_url = settings.get("API_URL", "https://lichess.org")
20
21 access_token_url = f"{provider_base_url}/api/token"
22 authorize_url = f"{provider_base_url}/oauth"
23
24 profile_url = f"{provider_base_url}/api/account"
25 email_address_url = f"{provider_base_url}/api/account/email"
26
27 def complete_login(self, request: HttpRequest, app, token, **kwargs):
28 headers = {"Authorization": f"Bearer {token.token}"}
29 with get_adapter().get_requests_session() as sess:
30 profile_res = sess.get(
31 self.profile_url,
32 params={"access_token": token.token},
33 headers=headers,
34 )
35 profile_res.raise_for_status()
36 extra_data = profile_res.json()
37
38 user_profile = (
39 extra_data["result"] if "result" in extra_data else extra_data
40 )
41
42 # retrieve email address if requested
43 if QUERY_EMAIL:
44 email_resp = sess.get(self.email_address_url, headers=headers)
45
46 email_resp.raise_for_status()
47 email_data = email_resp.json()
48
49 # extract email address from response
50
51 email = email_data.get("email", None)
52
53 if email:
54 user_profile["email"] = email
55
56 return self.get_provider().sociallogin_from_response(request, user_profile)
57
58
59 oauth2_login = OAuth2LoginView.adapter_view(LichessOAuth2Adapter)
60 oauth2_callback = OAuth2CallbackView.adapter_view(LichessOAuth2Adapter)