Debian Codegraph @

allauth/socialaccount/providers/douban/views.py

raw
1 from __future__ import annotations
2
3 from django.http import HttpRequest
4 from django.utils.translation import gettext_lazy as _
5
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 from ..base import ProviderException
14
15
16 class DoubanOAuth2Adapter(OAuth2Adapter):
17 provider_id = "douban"
18 access_token_url = "https://www.douban.com/service/auth2/token" # nosec
19 authorize_url = "https://www.douban.com/service/auth2/auth"
20 profile_url = "https://api.douban.com/v2/user/~me"
21
22 def complete_login(self, request: HttpRequest, app, token, **kwargs):
23 headers = {"Authorization": f"Bearer {token.token}"}
24 with get_adapter().get_requests_session() as sess:
25 resp = sess.get(self.profile_url, headers=headers)
26 extra_data = resp.json()
27 """
28 Douban may return data like this:
29
30 {
31 'code': 128,
32 'request': 'GET /v2/user/~me',
33 'msg': 'user_is_locked:53358092'
34 }
35
36 """
37 if "id" not in extra_data:
38 msg = extra_data.get("msg", _("Invalid profile data"))
39 raise ProviderException(msg)
40 return self.get_provider().sociallogin_from_response(request, extra_data)
41
42
43 oauth2_login = OAuth2LoginView.adapter_view(DoubanOAuth2Adapter)
44 oauth2_callback = OAuth2CallbackView.adapter_view(DoubanOAuth2Adapter)