Debian Codegraph @

allauth/socialaccount/providers/dwolla/views.py

raw
1 from __future__ import annotations
2
3 from django.conf import settings
4 from django.http import HttpRequest
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
14 ENVIRONMENTS = {
15 "production": {
16 "auth_url": "https://www.dwolla.com/oauth/v2/authenticate",
17 "token_url": "https://www.dwolla.com/oauth/v2/token", # nosec
18 },
19 "sandbox": {
20 "auth_url": "https://uat.dwolla.com/oauth/v2/authenticate",
21 "token_url": "https://uat.dwolla.com/oauth/v2/token", # nosec
22 },
23 }
24
25 ENV = (
26 getattr(settings, "SOCIALACCOUNT_PROVIDERS", {})
27 .get("dwolla", {})
28 .get("ENVIRONMENT", "production")
29 )
30
31 AUTH_URL = ENVIRONMENTS[ENV]["auth_url"]
32 TOKEN_URL = ENVIRONMENTS[ENV]["token_url"]
33
34
35 class DwollaOAuth2Adapter(OAuth2Adapter):
36 """Dwolla Views Adapter"""
37
38 scope_delimiter = "|"
39
40 provider_id = "dwolla"
41 access_token_url = TOKEN_URL
42 authorize_url = AUTH_URL
43
44 def complete_login(self, request: HttpRequest, app, token, **kwargs):
45 response = kwargs["response"]
46 with get_adapter().get_requests_session() as sess:
47 resp = sess.get(
48 response["_links"]["account"]["href"],
49 headers={
50 "authorization": f"Bearer {token.token}",
51 "accept": "application/vnd.dwolla.v1.hal+json",
52 },
53 )
54 extra_data = resp.json()
55
56 return self.get_provider().sociallogin_from_response(request, extra_data)
57
58
59 oauth2_login = OAuth2LoginView.adapter_view(DwollaOAuth2Adapter)
60 oauth2_callback = OAuth2CallbackView.adapter_view(DwollaOAuth2Adapter)