Skip to content

Commit

Permalink
support unified admin
Browse files Browse the repository at this point in the history
  • Loading branch information
klenotiw committed Mar 15, 2023
1 parent f889f25 commit a462388
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/shopify_app/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions test/shopify_app/utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a462388

Please sign in to comment.