| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from http import HTTPStatus |
| 4 |
|
| 5 |
from django.http import HttpRequest |
| 6 |
|
| 7 |
from allauth.core import context |
| 8 |
from allauth.socialaccount import app_settings |
| 9 |
from allauth.socialaccount.adapter import get_adapter |
| 10 |
from allauth.socialaccount.providers.oauth2.client import OAuth2Error |
| 11 |
from allauth.socialaccount.providers.oauth2.views import ( |
| 12 |
OAuth2Adapter, |
| 13 |
OAuth2CallbackView, |
| 14 |
OAuth2LoginView, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
def _check_errors(response): |
| 19 |
# 403 error's are presented as user-facing errors |
| 20 |
if response.status_code == HTTPStatus.FORBIDDEN: |
| 21 |
msg = response.content |
| 22 |
raise OAuth2Error(f"Invalid data from GitLab API: {msg!r}") |
| 23 |
|
| 24 |
try: |
| 25 |
data = response.json() |
| 26 |
except ValueError: # JSONDecodeError on py3 |
| 27 |
raise OAuth2Error(f"Invalid JSON from GitLab API: {response.text!r}") |
| 28 |
|
| 29 |
if response.status_code >= HTTPStatus.BAD_REQUEST or "error" in data: |
| 30 |
# For errors, we expect the following format: |
| 31 |
# {"error": "error_name", "error_description": "Oops!"} |
| 32 |
# For example, if the token is not valid, we will get: |
| 33 |
# {"message": "status_code - message"} |
| 34 |
error = data.get("error", "") or response.status_code |
| 35 |
desc = data.get("error_description", "") or data.get("message", "") |
| 36 |
|
| 37 |
raise OAuth2Error(f"GitLab error: {error} ({desc})") |
| 38 |
|
| 39 |
# The expected output from the API follows this format: |
| 40 |
# {"id": 12345, ...} |
| 41 |
if "id" not in data: |
| 42 |
# If the id is not present, the output is not usable (no UID) |
| 43 |
raise OAuth2Error(f"Invalid data from GitLab API: {data!r}") |
| 44 |
|
| 45 |
return data |
| 46 |
|
| 47 |
|
| 48 |
class GitLabOAuth2Adapter(OAuth2Adapter): |
| 49 |
provider_id = "gitlab" |
| 50 |
provider_default_url = "https://gitlab.com" |
| 51 |
provider_api_version = "v4" |
| 52 |
|
| 53 |
def _build_url(self, path): |
| 54 |
settings = app_settings.PROVIDERS.get(self.provider_id, {}) |
| 55 |
gitlab_url = settings.get("GITLAB_URL", self.provider_default_url) |
| 56 |
# Prefer app based setting. |
| 57 |
app = get_adapter().get_app(context.request, provider=self.provider_id) |
| 58 |
gitlab_url = app.settings.get("gitlab_url", gitlab_url) |
| 59 |
return f"{gitlab_url}{path}" |
| 60 |
|
| 61 |
@property |
| 62 |
def access_token_url(self): |
| 63 |
return self._build_url("/oauth/token") |
| 64 |
|
| 65 |
@property |
| 66 |
def authorize_url(self): |
| 67 |
return self._build_url("/oauth/authorize") |
| 68 |
|
| 69 |
@property |
| 70 |
def profile_url(self): |
| 71 |
return self._build_url(f"/api/{self.provider_api_version}/user") |
| 72 |
|
| 73 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 74 |
with get_adapter().get_requests_session() as sess: |
| 75 |
response = sess.get(self.profile_url, params={"access_token": token.token}) |
| 76 |
data = _check_errors(response) |
| 77 |
return self.get_provider().sociallogin_from_response(request, data) |
| 78 |
|
| 79 |
|
| 80 |
oauth2_login = OAuth2LoginView.adapter_view(GitLabOAuth2Adapter) |
| 81 |
oauth2_callback = OAuth2CallbackView.adapter_view(GitLabOAuth2Adapter) |