| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.http import Http404, HttpRequest |
| 4 |
from django.urls import reverse |
| 5 |
|
| 6 |
from allauth.account.internal.decorators import login_not_required |
| 7 |
from allauth.socialaccount.adapter import get_adapter |
| 8 |
from allauth.socialaccount.internal import jwtkit |
| 9 |
from allauth.socialaccount.models import SocialApp, SocialToken |
| 10 |
from allauth.socialaccount.providers.oauth2.views import ( |
| 11 |
OAuth2Adapter, |
| 12 |
OAuth2CallbackView, |
| 13 |
OAuth2LoginView, |
| 14 |
) |
| 15 |
from allauth.utils import build_absolute_uri |
| 16 |
|
| 17 |
|
| 18 |
class OpenIDConnectOAuth2Adapter(OAuth2Adapter): |
| 19 |
def __init__(self, request: HttpRequest, provider_id) -> None: |
| 20 |
self.provider_id = provider_id |
| 21 |
super().__init__(request) |
| 22 |
|
| 23 |
@property |
| 24 |
def openid_config(self): |
| 25 |
if not hasattr(self, "_openid_config"): |
| 26 |
server_url = self.get_provider().server_url |
| 27 |
with get_adapter().get_requests_session() as sess: |
| 28 |
resp = sess.get(server_url) |
| 29 |
resp.raise_for_status() |
| 30 |
self._openid_config = resp.json() |
| 31 |
return self._openid_config |
| 32 |
|
| 33 |
@property |
| 34 |
def basic_auth(self): |
| 35 |
token_auth_method = self.get_provider().app.settings.get("token_auth_method") |
| 36 |
if token_auth_method: |
| 37 |
return token_auth_method == "client_secret_basic" # nosec |
| 38 |
methods = self.openid_config.get("token_endpoint_auth_methods_supported", []) |
| 39 |
# Basic auth is problematic, especially when client ID contains a colon. |
| 40 |
return "client_secret_post" not in methods and "client_secret_basic" in methods |
| 41 |
|
| 42 |
@property |
| 43 |
def access_token_url(self): |
| 44 |
return self.openid_config["token_endpoint"] |
| 45 |
|
| 46 |
@property |
| 47 |
def authorize_url(self): |
| 48 |
return self.openid_config["authorization_endpoint"] |
| 49 |
|
| 50 |
@property |
| 51 |
def profile_url(self): |
| 52 |
return self.openid_config["userinfo_endpoint"] |
| 53 |
|
| 54 |
def complete_login(self, request: HttpRequest, app, token: SocialToken, **kwargs): |
| 55 |
id_token_str = kwargs["response"].get("id_token") |
| 56 |
fetch_userinfo = app.settings.get("fetch_userinfo", True) |
| 57 |
data = {} |
| 58 |
if fetch_userinfo or (not id_token_str): |
| 59 |
data["userinfo"] = self._fetch_user_info(token.token) |
| 60 |
if id_token_str: |
| 61 |
data["id_token"] = self._decode_id_token(app, id_token_str) |
| 62 |
return self.get_provider().sociallogin_from_response(request, data) |
| 63 |
|
| 64 |
def _fetch_user_info(self, access_token: str) -> dict: |
| 65 |
headers = {"Authorization": f"Bearer {access_token}"} |
| 66 |
with get_adapter().get_requests_session() as sess: |
| 67 |
response = sess.get(self.profile_url, headers=headers) |
| 68 |
response.raise_for_status() |
| 69 |
return response.json() |
| 70 |
|
| 71 |
def _decode_id_token(self, app: SocialApp, id_token: str) -> dict: |
| 72 |
""" |
| 73 |
If the token was received by direct communication protected by |
| 74 |
TLS between this library and Google, we are allowed to skip checking the |
| 75 |
token signature according to the OpenID Connect Core 1.0 specification. |
| 76 |
|
| 77 |
https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation |
| 78 |
""" |
| 79 |
verify_signature = not self.did_fetch_access_token |
| 80 |
return jwtkit.verify_and_decode( |
| 81 |
credential=id_token, |
| 82 |
keys_url=self.openid_config["jwks_uri"], |
| 83 |
issuer=self.openid_config["issuer"], |
| 84 |
audience=app.client_id, |
| 85 |
lookup_kid=jwtkit.lookup_kid_jwk, |
| 86 |
verify_signature=verify_signature, |
| 87 |
) |
| 88 |
|
| 89 |
def get_callback_url(self, request: HttpRequest, app): |
| 90 |
callback_url = reverse( |
| 91 |
"openid_connect_callback", kwargs={"provider_id": self.provider_id} |
| 92 |
) |
| 93 |
protocol = self.redirect_uri_protocol |
| 94 |
return build_absolute_uri(request, callback_url, protocol) |
| 95 |
|
| 96 |
|
| 97 |
@login_not_required |
| 98 |
def login(request: HttpRequest, provider_id): |
| 99 |
try: |
| 100 |
view = OAuth2LoginView.adapter_view( |
| 101 |
OpenIDConnectOAuth2Adapter(request, provider_id) |
| 102 |
) |
| 103 |
return view(request) |
| 104 |
except SocialApp.DoesNotExist: |
| 105 |
raise Http404 |
| 106 |
|
| 107 |
|
| 108 |
@login_not_required |
| 109 |
def callback(request: HttpRequest, provider_id): |
| 110 |
try: |
| 111 |
view = OAuth2CallbackView.adapter_view( |
| 112 |
OpenIDConnectOAuth2Adapter(request, provider_id) |
| 113 |
) |
| 114 |
return view(request) |
| 115 |
except SocialApp.DoesNotExist: |
| 116 |
raise Http404 |