Module: ShopifyAPI::Utils::ShopValidator

Extended by:
T::Sig
Defined in:
lib/shopify_api/utils/shop_validator.rb

Constant Summary collapse

TRUSTED_SHOPIFY_DOMAINS =
T.let(
  [
    "shopify.com",
    "myshopify.io",
    "myshopify.com",
    "spin.dev",
    "shop.dev",
  ].freeze,
  T::Array[String],
)

Class Method Summary collapse

Class Method Details

.sanitize!(shop, myshopify_domain: nil) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/shopify_api/utils/shop_validator.rb', line 56

def sanitize!(shop, myshopify_domain: nil)
  host = sanitize_shop_domain(shop, myshopify_domain: myshopify_domain)
  if host.nil? || host.empty?
    raise Errors::InvalidShopError,
      "shop must be a trusted Shopify domain (see ShopValidator::TRUSTED_SHOPIFY_DOMAINS), got: #{shop.inspect}"
  end

  host
end

.sanitize_shop_domain(shop_domain, myshopify_domain: nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/shopify_api/utils/shop_validator.rb', line 29

def sanitize_shop_domain(shop_domain, myshopify_domain: nil)
  uri = uri_from_shop_domain(shop_domain, myshopify_domain)
  return nil if uri.nil? || uri.host.nil? || uri.host.empty?

  trusted_domains(myshopify_domain).each do |trusted_domain|
    host = T.cast(uri.host, String)
    uri_domain = uri.domain
    next if uri_domain.nil?

    no_shop_name_in_subdomain = host == trusted_domain
    from_trusted_domain = trusted_domain == uri_domain

    if unified_admin?(uri) && from_trusted_domain
      return myshopify_domain_from_unified_admin(uri)
    end
    return nil if no_shop_name_in_subdomain || host.empty?
    return host if from_trusted_domain
  end
  nil
end