photo26/allauth_oauth/views.py
krek0 a01af1e3fa
All checks were successful
Docker / build (release) Successful in 9s
fix oauth
2026-05-16 23:06:16 +02:00

58 lines
1.7 KiB
Python

# From https://gitlab.crans.org/bde/allauth-note-kfet
# Copyright (C) 2022 Amicale des élèves de l'ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
import requests
from allauth.socialaccount import app_settings
from django.core.exceptions import ImproperlyConfigured
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import OAuthProvider
class OAuthAdapter(OAuth2Adapter):
provider_id = OAuthProvider.id
def complete_login(self, request, app, token, **kwargs):
headers = {
"Authorization": f"Bearer {token.token}",
"Content-Type": "application/json",
}
extra_data = requests.get(self.profile_url, headers=headers)
return self.get_provider().sociallogin_from_response(request, extra_data.json())
@property
def settings(self):
return app_settings.PROVIDERS.get(self.provider_id, {})
@property
def domain(self):
domain = self.settings.get("DOMAIN", "")
if not domain:
raise ImproperlyConfigured(
"OAUTH_SERVER_URL is not configured. Set it in your .env file."
)
return domain
@property
def access_token_url(self):
return f"https://{self.domain}/application/o/token/"
@property
def authorize_url(self):
return f"https://{self.domain}/application/o/authorize/"
@property
def profile_url(self):
return f"https://{self.domain}/application/o/userinfo/"
OAuthProvider.oauth2_adapter_class = OAuthAdapter
oauth2_login = OAuth2LoginView.adapter_view(OAuthAdapter)
oauth2_callback = OAuth2CallbackView.adapter_view(OAuthAdapter)