| 1 |
"""Views for Drip API.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from django.http import HttpRequest |
| 6 |
|
| 7 |
from allauth.socialaccount.adapter import get_adapter |
| 8 |
from allauth.socialaccount.providers.oauth2.views import ( |
| 9 |
OAuth2Adapter, |
| 10 |
OAuth2CallbackView, |
| 11 |
OAuth2LoginView, |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
class DripOAuth2Adapter(OAuth2Adapter): |
| 16 |
"""OAuth2Adapter for Drip API v3.""" |
| 17 |
|
| 18 |
provider_id = "drip" |
| 19 |
|
| 20 |
authorize_url = "https://www.getdrip.com/oauth/authorize" |
| 21 |
access_token_url = "https://www.getdrip.com/oauth/token" # nosec |
| 22 |
profile_url = "https://api.getdrip.com/v2/user" |
| 23 |
|
| 24 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 25 |
"""Complete login, ensuring correct OAuth header.""" |
| 26 |
headers = {"Authorization": f"Bearer {token.token}"} |
| 27 |
with get_adapter().get_requests_session() as sess: |
| 28 |
response = sess.get(self.profile_url, headers=headers) |
| 29 |
response.raise_for_status() |
| 30 |
extra_data = response.json()["users"][0] |
| 31 |
return self.get_provider().sociallogin_from_response(request, extra_data) |
| 32 |
|
| 33 |
|
| 34 |
oauth2_login = OAuth2LoginView.adapter_view(DripOAuth2Adapter) |
| 35 |
oauth2_callback = OAuth2CallbackView.adapter_view(DripOAuth2Adapter) |