| 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 AgaveAdapter(OAuth2Adapter): |
| 15 |
provider_id = "agave" |
| 16 |
|
| 17 |
settings = app_settings.PROVIDERS.get(provider_id, {}) |
| 18 |
provider_base_url = settings.get("API_URL", "https://public.agaveapi.co") |
| 19 |
|
| 20 |
access_token_url = f"{provider_base_url}/token" |
| 21 |
authorize_url = f"{provider_base_url}/authorize" |
| 22 |
profile_url = f"{provider_base_url}/profiles/v2/me" |
| 23 |
|
| 24 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 25 |
with get_adapter().get_requests_session() as sess: |
| 26 |
extra_data = sess.get( |
| 27 |
self.profile_url, |
| 28 |
params={"access_token": token.token}, |
| 29 |
headers={"Authorization": f"Bearer {token.token}"}, |
| 30 |
) |
| 31 |
user_profile = extra_data.json().get("result", {}) |
| 32 |
|
| 33 |
return self.get_provider().sociallogin_from_response(request, user_profile) |
| 34 |
|
| 35 |
|
| 36 |
oauth2_login = OAuth2LoginView.adapter_view(AgaveAdapter) |
| 37 |
oauth2_callback = OAuth2CallbackView.adapter_view(AgaveAdapter) |