| 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 GiteaOAuth2Adapter(OAuth2Adapter): |
| 15 |
provider_id = "gitea" |
| 16 |
settings = app_settings.PROVIDERS.get(provider_id, {}) |
| 17 |
|
| 18 |
if "GITEA_URL" in settings: |
| 19 |
web_url = settings.get("GITEA_URL").rstrip("/") |
| 20 |
else: |
| 21 |
web_url = "https://gitea.com" |
| 22 |
api_url = f"{web_url}/api/v1" |
| 23 |
|
| 24 |
access_token_url = f"{web_url}/login/oauth/access_token" |
| 25 |
authorize_url = f"{web_url}/login/oauth/authorize" |
| 26 |
profile_url = f"{api_url}/user" |
| 27 |
|
| 28 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 29 |
headers = {"Authorization": f"token {token.token}"} |
| 30 |
with get_adapter().get_requests_session() as sess: |
| 31 |
resp = sess.get(self.profile_url, headers=headers) |
| 32 |
resp.raise_for_status() |
| 33 |
extra_data = resp.json() |
| 34 |
return self.get_provider().sociallogin_from_response(request, extra_data) |
| 35 |
|
| 36 |
|
| 37 |
oauth2_login = OAuth2LoginView.adapter_view(GiteaOAuth2Adapter) |
| 38 |
oauth2_callback = OAuth2CallbackView.adapter_view(GiteaOAuth2Adapter) |