| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import json |
| 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 |
try: |
| 20 |
data = response.json() |
| 21 |
except json.decoder.JSONDecodeError: |
| 22 |
raise OAuth2Error(f"Invalid JSON from Microsoft Graph API: {response.text}") |
| 23 |
|
| 24 |
if "id" not in data: |
| 25 |
error_message = "Error retrieving Microsoft profile" |
| 26 |
microsoft_error_message = data.get("error", {}).get("message") |
| 27 |
if microsoft_error_message: |
| 28 |
error_message = f"{error_message}: {microsoft_error_message}" |
| 29 |
raise OAuth2Error(error_message) |
| 30 |
|
| 31 |
return data |
| 32 |
|
| 33 |
|
| 34 |
class MicrosoftGraphOAuth2Adapter(OAuth2Adapter): |
| 35 |
provider_id = "microsoft" |
| 36 |
|
| 37 |
def _build_tenant_url(self, path): |
| 38 |
settings = app_settings.PROVIDERS.get(self.provider_id, {}) |
| 39 |
# Lower case "tenant" for backwards compatibility |
| 40 |
tenant = settings.get("TENANT", settings.get("tenant", "common")) |
| 41 |
# Prefer app based tenant setting. |
| 42 |
app = get_adapter().get_app(context.request, provider=self.provider_id) |
| 43 |
tenant = app.settings.get("tenant", tenant) |
| 44 |
login_url = app.settings.get("login_url", "https://login.microsoftonline.com") |
| 45 |
return f"{login_url}/{tenant}{path}" |
| 46 |
|
| 47 |
@property |
| 48 |
def access_token_url(self): |
| 49 |
return self._build_tenant_url("/oauth2/v2.0/token") |
| 50 |
|
| 51 |
@property |
| 52 |
def authorize_url(self): |
| 53 |
return self._build_tenant_url("/oauth2/v2.0/authorize") |
| 54 |
|
| 55 |
@property |
| 56 |
def profile_url(self): |
| 57 |
app = get_adapter().get_app(context.request, provider=self.provider_id) |
| 58 |
graph_url = app.settings.get("graph_url", "https://graph.microsoft.com") |
| 59 |
return f"{graph_url}/v1.0/me" |
| 60 |
|
| 61 |
user_properties = ( |
| 62 |
"businessPhones", |
| 63 |
"displayName", |
| 64 |
"givenName", |
| 65 |
"id", |
| 66 |
"jobTitle", |
| 67 |
"mail", |
| 68 |
"mobilePhone", |
| 69 |
"officeLocation", |
| 70 |
"preferredLanguage", |
| 71 |
"surname", |
| 72 |
"userPrincipalName", |
| 73 |
"mailNickname", |
| 74 |
"companyName", |
| 75 |
) |
| 76 |
profile_url_params = {"$select": ",".join(user_properties)} |
| 77 |
|
| 78 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 79 |
headers = {"Authorization": f"Bearer {token.token}"} |
| 80 |
with get_adapter().get_requests_session() as sess: |
| 81 |
response = sess.get( |
| 82 |
self.profile_url, params=self.profile_url_params, headers=headers |
| 83 |
) |
| 84 |
extra_data = _check_errors(response) |
| 85 |
return self.get_provider().sociallogin_from_response(request, extra_data) |
| 86 |
|
| 87 |
|
| 88 |
oauth2_login = OAuth2LoginView.adapter_view(MicrosoftGraphOAuth2Adapter) |
| 89 |
oauth2_callback = OAuth2CallbackView.adapter_view(MicrosoftGraphOAuth2Adapter) |