|
| 1 | +# typed: strict |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "addressable/uri" |
| 5 | + |
| 6 | +module ShopifyAPI |
| 7 | + module Utils |
| 8 | + module ShopValidator |
| 9 | + TRUSTED_SHOPIFY_DOMAINS = T.let( |
| 10 | + [ |
| 11 | + "shopify.com", |
| 12 | + "myshopify.io", |
| 13 | + "myshopify.com", |
| 14 | + "spin.dev", |
| 15 | + "shop.dev", |
| 16 | + ].freeze, |
| 17 | + T::Array[String], |
| 18 | + ) |
| 19 | + |
| 20 | + class << self |
| 21 | + extend T::Sig |
| 22 | + |
| 23 | + sig do |
| 24 | + params( |
| 25 | + shop_domain: String, |
| 26 | + myshopify_domain: T.nilable(String), |
| 27 | + ).returns(T.nilable(String)) |
| 28 | + end |
| 29 | + def sanitize_shop_domain(shop_domain, myshopify_domain: nil) |
| 30 | + uri = uri_from_shop_domain(shop_domain, myshopify_domain) |
| 31 | + return nil if uri.nil? || uri.host.nil? || uri.host.empty? |
| 32 | + |
| 33 | + trusted_domains(myshopify_domain).each do |trusted_domain| |
| 34 | + host = T.cast(uri.host, String) |
| 35 | + uri_domain = uri.domain |
| 36 | + next if uri_domain.nil? |
| 37 | + |
| 38 | + no_shop_name_in_subdomain = host == trusted_domain |
| 39 | + from_trusted_domain = trusted_domain == uri_domain |
| 40 | + |
| 41 | + if unified_admin?(uri) && from_trusted_domain |
| 42 | + return myshopify_domain_from_unified_admin(uri) |
| 43 | + end |
| 44 | + return nil if no_shop_name_in_subdomain || host.empty? |
| 45 | + return host if from_trusted_domain |
| 46 | + end |
| 47 | + nil |
| 48 | + end |
| 49 | + |
| 50 | + sig do |
| 51 | + params( |
| 52 | + shop: String, |
| 53 | + myshopify_domain: T.nilable(String), |
| 54 | + ).returns(String) |
| 55 | + end |
| 56 | + def sanitize!(shop, myshopify_domain: nil) |
| 57 | + host = sanitize_shop_domain(shop, myshopify_domain: myshopify_domain) |
| 58 | + if host.nil? || host.empty? |
| 59 | + raise Errors::InvalidShopError, |
| 60 | + "shop must be a trusted Shopify domain (see ShopValidator::TRUSTED_SHOPIFY_DOMAINS), got: #{shop.inspect}" |
| 61 | + end |
| 62 | + |
| 63 | + host |
| 64 | + end |
| 65 | + |
| 66 | + private |
| 67 | + |
| 68 | + sig { params(myshopify_domain: T.nilable(String)).returns(T::Array[String]) } |
| 69 | + def trusted_domains(myshopify_domain) |
| 70 | + trusted = TRUSTED_SHOPIFY_DOMAINS.dup |
| 71 | + if myshopify_domain && !myshopify_domain.to_s.empty? |
| 72 | + trusted << myshopify_domain |
| 73 | + trusted.uniq! |
| 74 | + end |
| 75 | + trusted |
| 76 | + end |
| 77 | + |
| 78 | + sig do |
| 79 | + params( |
| 80 | + shop_domain: String, |
| 81 | + myshopify_domain: T.nilable(String), |
| 82 | + ).returns(T.nilable(Addressable::URI)) |
| 83 | + end |
| 84 | + def uri_from_shop_domain(shop_domain, myshopify_domain) |
| 85 | + name = shop_domain.to_s.downcase.strip |
| 86 | + return nil if name.empty? |
| 87 | + return nil if name.include?("@") |
| 88 | + |
| 89 | + if myshopify_domain && !myshopify_domain.to_s.empty? && |
| 90 | + !name.include?(myshopify_domain.to_s) && !name.include?(".") |
| 91 | + name += ".#{myshopify_domain}" |
| 92 | + end |
| 93 | + |
| 94 | + uri = Addressable::URI.parse(name) |
| 95 | + if uri.scheme.nil? |
| 96 | + name = "https://#{name}" |
| 97 | + uri = Addressable::URI.parse(name) |
| 98 | + end |
| 99 | + |
| 100 | + uri |
| 101 | + rescue Addressable::URI::InvalidURIError |
| 102 | + nil |
| 103 | + end |
| 104 | + |
| 105 | + sig { params(uri: Addressable::URI).returns(T::Boolean) } |
| 106 | + def unified_admin?(uri) |
| 107 | + T.cast(uri.host, String).split(".").first == "admin" |
| 108 | + end |
| 109 | + |
| 110 | + sig { params(uri: Addressable::URI).returns(String) } |
| 111 | + def myshopify_domain_from_unified_admin(uri) |
| 112 | + shop = uri.path.to_s.split("/").last |
| 113 | + "#{shop}.myshopify.com" |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments