diff --git a/CHANGELOG.md b/CHANGELOG.md index 657a5f344..70876e7f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Unreleased ---------- +* Support Unified Admin [#1658](https://github.com/Shopify/shopify_app/pull/1658) * Set `access_scopes` column to string by default [#1636](https://github.com/Shopify/shopify_app/pull/1636) 21.4.1 (Feb 21, 2023) diff --git a/lib/shopify_app/utils.rb b/lib/shopify_app/utils.rb index 41a4d2cfd..fc248fac4 100644 --- a/lib/shopify_app/utils.rb +++ b/lib/shopify_app/utils.rb @@ -18,10 +18,10 @@ def sanitize_shop_domain(shop_domain) no_shop_name_in_subdomain = uri.host == trusted_domain from_trusted_domain = trusted_domain == uri.domain + return myshopify_domain_from_unified_admin(uri) if unified_admin?(uri) && from_trusted_domain return nil if no_shop_name_in_subdomain || uri.host&.empty? return uri.host if from_trusted_domain end - nil end @@ -65,6 +65,16 @@ def uri_from_shop_domain(shop_domain) rescue Addressable::URI::InvalidURIError nil end + + def unified_admin?(uri) + uri.host.split(".").first == "admin" + end + + def myshopify_domain_from_unified_admin(uri) + shop = uri.path.split("/").last + + "#{shop}.myshopify.com" + end end end end diff --git a/test/shopify_app/utils_test.rb b/test/shopify_app/utils_test.rb index 3fd4c6e17..d65a5b9a6 100644 --- a/test/shopify_app/utils_test.rb +++ b/test/shopify_app/utils_test.rb @@ -42,6 +42,16 @@ class UtilsTest < ActiveSupport::TestCase assert ShopifyApp::Utils.sanitize_shop_domain("some-shoppe-over-the-rainbow.myshopify.io") end + test "convert unified admin to old domain" do + trailing_forward_slash_url = "https://admin.shopify.com/store/store-name/" + unified_admin_url = "https://admin.shopify.com/store/store-name" + + expected = "store-name.myshopify.com" + + assert_equal expected, ShopifyApp::Utils.sanitize_shop_domain(trailing_forward_slash_url) + assert_equal expected, ShopifyApp::Utils.sanitize_shop_domain(unified_admin_url) + end + ["myshop.com", "myshopify.com", "shopify.com", "two words", "store.myshopify.com.evil.com", "/foo/bar", "foo.myshopify.io.evil.ru",].each do |bad_url| test "sanitize_shop_domain for a non-myshopify URL (#{bad_url})" do