| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.http import HttpRequest |
| 4 |
|
| 5 |
from allauth.socialaccount.adapter import get_adapter |
| 6 |
from allauth.socialaccount.providers.oauth2.views import ( |
| 7 |
OAuth2Adapter, |
| 8 |
OAuth2CallbackView, |
| 9 |
OAuth2LoginView, |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
class QuickBooksOAuth2Adapter(OAuth2Adapter): |
| 14 |
provider_id = "quickbooks" |
| 15 |
access_token_url = ( |
| 16 |
"https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer" # nosec |
| 17 |
) |
| 18 |
authorize_url = "https://appcenter.intuit.com/connect/oauth2" |
| 19 |
profile_test = "https://sandbox-accounts.platform.intuit.com/v1/openid_connect/userinfo" # NOQA |
| 20 |
profile_url = "https://accounts.platform.intuit.com/v1/openid_connect/userinfo" |
| 21 |
profile_url_method = "GET" |
| 22 |
access_token_method = "POST" # nosec |
| 23 |
|
| 24 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 25 |
realm_id = request.GET.get("realmId") |
| 26 |
extra_data = self.get_user_info(token) |
| 27 |
if realm_id: |
| 28 |
extra_data["realmId"] = realm_id |
| 29 |
return self.get_provider().sociallogin_from_response(request, extra_data) |
| 30 |
|
| 31 |
def get_user_info(self, token): |
| 32 |
auth_header = f"Bearer {token.token}" |
| 33 |
headers = { |
| 34 |
"Accept": "application/json", |
| 35 |
"Authorization": auth_header, |
| 36 |
"accept": "application/json", |
| 37 |
} |
| 38 |
is_sandbox = self.get_provider().get_settings().get("SANDBOX", False) |
| 39 |
url = self.profile_test if is_sandbox else self.profile_url |
| 40 |
with get_adapter().get_requests_session() as sess: |
| 41 |
resp = sess.get(url, headers=headers) |
| 42 |
resp.raise_for_status() |
| 43 |
return resp.json() |
| 44 |
|
| 45 |
|
| 46 |
oauth2_login = OAuth2LoginView.adapter_view(QuickBooksOAuth2Adapter) |
| 47 |
oauth2_callback = OAuth2CallbackView.adapter_view(QuickBooksOAuth2Adapter) |