| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import re |
| 4 |
from typing import Any |
| 5 |
|
| 6 |
from django.conf import settings |
| 7 |
from django.http import HttpRequest, HttpResponse, HttpResponseBadRequest |
| 8 |
|
| 9 |
from allauth.core.exceptions import ImmediateHttpResponse |
| 10 |
from allauth.socialaccount.adapter import get_adapter |
| 11 |
from allauth.socialaccount.providers.oauth2.views import ( |
| 12 |
OAuth2Adapter, |
| 13 |
OAuth2CallbackView, |
| 14 |
OAuth2LoginView, |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
class ShopifyOAuth2Adapter(OAuth2Adapter): |
| 19 |
provider_id = "shopify" |
| 20 |
scope_delimiter = "," |
| 21 |
|
| 22 |
def _shop_domain(self): |
| 23 |
shop = self.request.GET.get("shop", "") |
| 24 |
if "." not in shop: |
| 25 |
shop = f"{shop}.myshopify.com" |
| 26 |
# Ensure the provided hostname parameter is a valid hostname, |
| 27 |
# ends with myshopify.com, and does not contain characters |
| 28 |
# other than letters (a-z), numbers (0-9), dots, and hyphens. |
| 29 |
if not re.match(r"^[a-z0-9-]+\.myshopify\.com$", shop): |
| 30 |
raise ImmediateHttpResponse( |
| 31 |
HttpResponseBadRequest("Invalid `shop` parameter") |
| 32 |
) |
| 33 |
return shop |
| 34 |
|
| 35 |
def _shop_url(self, path): |
| 36 |
shop = self._shop_domain() |
| 37 |
return f"https://{shop}{path}" |
| 38 |
|
| 39 |
@property |
| 40 |
def access_token_url(self): |
| 41 |
return self._shop_url("/admin/oauth/access_token") |
| 42 |
|
| 43 |
@property |
| 44 |
def authorize_url(self): |
| 45 |
return self._shop_url("/admin/oauth/authorize") |
| 46 |
|
| 47 |
@property |
| 48 |
def profile_url(self): |
| 49 |
return self._shop_url("/admin/shop.json") |
| 50 |
|
| 51 |
def complete_login(self, request: HttpRequest, app, token, **kwargs): |
| 52 |
headers = {"X-Shopify-Access-Token": f"{token.token}"} |
| 53 |
with get_adapter().get_requests_session() as sess: |
| 54 |
response = sess.get(self.profile_url, headers=headers) |
| 55 |
extra_data = response.json() |
| 56 |
associated_user = kwargs["response"].get("associated_user") |
| 57 |
if associated_user: |
| 58 |
extra_data["associated_user"] = associated_user |
| 59 |
return self.get_provider().sociallogin_from_response(request, extra_data) |
| 60 |
|
| 61 |
|
| 62 |
class ShopifyOAuth2LoginView(OAuth2LoginView): |
| 63 |
def dispatch(self, request: HttpRequest, *args: Any, **kwargs: Any): |
| 64 |
is_embedded = ( |
| 65 |
getattr(settings, "SOCIALACCOUNT_PROVIDERS", {}) |
| 66 |
.get("shopify", {}) |
| 67 |
.get("IS_EMBEDDED", False) |
| 68 |
) |
| 69 |
if is_embedded: |
| 70 |
# TODO: This bypasses LOGIN_ON_GET, but: |
| 71 |
# |
| 72 |
# The Embedded App SDK (EASDK) and backwards compatibility layer |
| 73 |
# are being removed from Shopify on January 1, 2022. |
| 74 |
# |
| 75 |
# So this needs to be dropped/revisitted anyway. |
| 76 |
response = super().dispatch(request, *args, **kwargs) |
| 77 |
""" |
| 78 |
Shopify embedded apps (that run within an iFrame) require a JS |
| 79 |
(not server) redirect for starting the oauth2 process. |
| 80 |
|
| 81 |
See Also: |
| 82 |
https://help.shopify.com/api/sdks/embedded-app-sdk/getting-started#oauth |
| 83 |
""" |
| 84 |
js = ( |
| 85 |
'<!DOCTYPE html><html><head><script type="text/javascript">' |
| 86 |
f'window.top.location.href = "{getattr(response, "url", "/")}";' |
| 87 |
"</script></head><body></body></html>" |
| 88 |
) |
| 89 |
response = HttpResponse(content=js) |
| 90 |
# Because this view will be within shopify's iframe |
| 91 |
response.xframe_options_exempt = True |
| 92 |
return response |
| 93 |
return super().dispatch(request, *args, **kwargs) |
| 94 |
|
| 95 |
|
| 96 |
oauth2_login = ShopifyOAuth2LoginView.adapter_view(ShopifyOAuth2Adapter) |
| 97 |
oauth2_callback = OAuth2CallbackView.adapter_view(ShopifyOAuth2Adapter) |