| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use utf8; | ||
| 6 | |||
| 7 | # All the extra functionality relating to the admin panel should go in here. | ||
| 8 | package WWW::Shopify::Login; | ||
| 9 | use parent 'WWW::Shopify'; | ||
| 10 | |||
| 11 | # Used for the login version. | ||
| 12 | # X-Shopify-Api-Features:pagination-headers, embed-metafields, include-image-token. | ||
| 13 | sub embed_metafields { if (defined $_[1]) { $_[0]->{embed_metafields} = $_[1]; $_[0]->update_x_shopify_api_features; } return $_[0]->{embed_metafields}; } | ||
| 14 | sub include_image_token { if (defined $_[1]) { $_[0]->{include_image_token} = $_[1]; $_[0]->update_x_shopify_api_features; } return $_[0]->{include_image_token}; } | ||
| 15 | sub pagination_headers { if (defined $_[1]) { $_[0]->{pagination_headers} = $_[1]; $_[0]->update_x_shopify_api_features; } return $_[0]->{pagination_headers}; } | ||
| 16 | sub include_variant_summary { if (defined $_[1]) { $_[0]->{include_variant_summary} = $_[1]; $_[0]->update_x_shopify_api_features; } return $_[0]->{include_variant_summary}; } | ||
| 17 | sub include_gift_cards { if (defined $_[1]) { $_[0]->{include_gift_cards} = $_[1]; $_[0]->update_x_shopify_api_features; } return $_[0]->{include_gift_cards}; } | ||
| 18 | sub future_publishables { if (defined $_[1]) { $_[0]->{future_publishables} = $_[1]; $_[0]->update_x_shopify_api_features; } return $_[0]->{future_publishables}; } | ||
| 19 | |||
| 20 | sub two_factor_secret { if (defined $_[1]) { $_[0]->{two_factor_secret} = $_[1]; } return $_[0]->{two_factor_secret}; } | ||
| 21 | |||
| 22 | sub x_shopify_api_headers { | ||
| 23 | $_[0]->embed_metafields($_[1]); | ||
| 24 | $_[0]->include_image_token($_[1]); | ||
| 25 | $_[0]->pagination_headers($_[1]); | ||
| 26 | $_[0]->include_variant_summary($_[1]); | ||
| 27 | $_[0]->include_gift_cards($_[1]); | ||
| 28 | $_[0]->future_publishables($_[1]); | ||
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | sub new { | ||
| 33 | my ($package, $shop_url, $email, $password, $two_factor_secret) = @_; | ||
| 34 | my $self = $package->SUPER::new($shop_url, $email, $password); | ||
| 35 | $self->two_factor_secret($two_factor_secret) if $two_factor_secret; | ||
| 36 | $self->login_admin($email, $password, $two_factor_secret) if defined $email && defined $password; | ||
| 37 | return $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | sub get_all_limit { | ||
| 42 | my ($self, $package, $specs) = @_; | ||
| 43 | if ($package =~ m/File$/) { | ||
| 44 | $package = $self->translate_model($package); | ||
| 45 | $specs->{"limit"} = $package->max_per_page unless exists $specs->{"limit"}; | ||
| 46 | my $response = $self->use_url('get', "/admin/api/" . $self->api_version . "/settings/files", { limit => $specs->{limit} }, "text/html"); | ||
| 47 | my $contents = $response->decoded_content; | ||
| 48 | my @images = (); | ||
| 49 | return () if ($specs->{limit} == 0); | ||
| 50 | while ($contents =~ m/<tr bind-class="\{selected: bulk.selected\[(\d+)\]}".*?<td[^>]*>.*?<\/td>.*?<td[^>]*>.*?<\/td>.*?<td[^>]*>(.*?)<\/td>.*?id="text-\w+" value="([^"]+)".*?<\/tr>/gmis) { | ||
| 51 | my ($id, $key, $public_url) = ($1, $2, $3); | ||
| 52 | $key =~ s/\<[^\>]+>//g; | ||
| 53 | $key =~ s/^\s*//; | ||
| 54 | $key =~ s/\s*$//; | ||
| 55 | push(@images, WWW::Shopify::Model::File->new({ id => $id, key => $key, public_url => $public_url })); | ||
| 56 | } | ||
| 57 | return (undef,@images); | ||
| 58 | } | ||
| 59 | return shift->SUPER::get_all_limit(@_); | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | sub get_all { | ||
| 64 | my ($self, $package, $hash) = @_; | ||
| 65 | if ($package =~ m/LocaleTranslation/) { | ||
| 66 | $hash->{locale_id} = $hash->{parent}->id if $hash->{parent}; | ||
| 67 | } | ||
| 68 | return shift->SUPER::get_all(@_); | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | sub update_x_shopify_api_features { | ||
| 73 | my ($self) = @_; | ||
| 74 | my @list = ( | ||
| 75 | ($_[0]->embed_metafields ? ('embed-metafields') : ()), | ||
| 76 | ($_[0]->pagination_headers ? ('pagination-headers') : ()), | ||
| 77 | ($_[0]->include_image_token ? ('include-image-token') : ()), | ||
| 78 | ($_[0]->include_variant_summary ? ('include-variant-summary') : ()), | ||
| 79 | ($_[0]->include_gift_cards ? ('include-gift-cards') : ()), | ||
| 80 | ($_[0]->future_publishables ? ('future-publishables') : ()), | ||
| 81 | ); | ||
| 82 | if (int(@list) == 0 && $self->url_handler->{_default_headers}->{'X-Shopify-Api-Features'}) { | ||
| 83 | delete $self->url_handler->{_default_headers}->{'X-Shopify-Api-Features'}; | ||
| 84 | delete $self->url_handler->{_default_headers}->{'X-Requested-With'}; | ||
| 85 | } elsif (int(@list) > 0) { | ||
| 86 | $self->url_handler->{_default_headers}->{'X-Shopify-Api-Features'} = join(", ", @list); | ||
| 87 | $self->url_handler->{_default_headers}->{'X-Requested-With'} = "XMLHttpRequest"; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | # Takes in a list of locales. | ||
| 91 | sub create_update_locale_translation { | ||
| 92 | my ($self, @locales) = @_; | ||
| 93 | my @english_translation = $self->get_english_translation; | ||
| 94 | my %hash = map { $_->english => $_->text } @locales; | ||
| 95 | my %mapping = (); | ||
| 96 | foreach my $english (@english_translation) { | ||
| 97 | $mapping{$english->id} = exists $hash{$english->english} ? $hash{$english->english} : ""; | ||
| 98 | } | ||
| 99 | my $locale_id = $locales[0]->locale_id; | ||
| 100 | my ($decoded, $response) = $self->use_url('put', "/admin/api/" . $self->api_version . "/locales/$locale_id.json", { s => \%mapping }); | ||
| 101 | my $package = "WWW::Shopify::Model::Locale"; | ||
| 102 | my $object = $package->from_json($decoded->{$package->singular}, $self); | ||
| 103 | return $object; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub get_english_translation { | ||
| 107 | my ($self) = @_; | ||
| 108 | my $package = "WWW::Shopify::Model::LocaleTranslation"; | ||
| 109 | my ($decoded, $response) = $self->use_url('get', "/admin/api/" . $self->api_version . "/locale_translations/english_translations.json"); | ||
| 110 | return map { my $object = $package->from_json($_, $self); $object; } @{$decoded->{$package->plural}}; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub update { | ||
| 114 | my ($self, $item, $hash) = @_; | ||
| 115 | |||
| 116 | if ($item->needs_form_encoding_update) { | ||
| 117 | my $response = $self->use_url("POST", $self->resolve_trailing_url($item, "update", $item->associated_parent) . "/" . $item->id, { | ||
| 118 | authenticity_token => $self->{authenticity_token}, | ||
| 119 | utf8 => "✓", | ||
| 120 | _method => "patch", | ||
| 121 | $item->singular => $item | ||
| 122 | }, 1, "application/x-www-form-urlencoded", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); | ||
| 123 | return (undef, $response); | ||
| 124 | } | ||
| 125 | return shift->SUPER::update(@_) unless ref($item) =~ m/LocaleTranslation/; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub create { | ||
| 129 | my ($self, $item, $hash) = @_; | ||
| 130 | |||
| 131 | if ($item->needs_form_encoding_create) { | ||
| 132 | my $response = $self->use_url("POST", $self->resolve_trailing_url($item, "create", $item->associated_parent), { | ||
| 133 | authenticity_token => $self->{authenticity_token}, | ||
| 134 | utf8 => "✓", | ||
| 135 | $item->singular => $item | ||
| 136 | }, 1, "application/x-www-form-urlencoded", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); | ||
| 137 | return (undef, $response); | ||
| 138 | } | ||
| 139 | if (ref($item) && ref($item) =~ m/File$/) { | ||
| 140 | use MIME::Base64 qw(decode_base64); | ||
| 141 | die new WWW::Shopify::Exception("Requires attachment, and key.") unless $item->attachment && $item->key; | ||
| 142 | my $attachment = decode_base64($item->attachment); | ||
| 143 | my $response = $self->url_handler->upload_url("POST", 'https://' . $self->shop_url . '/admin/settings/files.json', 'file', $item->key, ($item->content_type || "application/octet-stream"), $attachment); | ||
| 144 | die new WWW::Shopify::Exception($response) unless $response->is_success; | ||
| 145 | return WWW::Shopify::Model::File->new({ content_type => $item->content_type, key => $item->key, size => length($attachment) }); | ||
| 146 | } | ||
| 147 | return shift->SUPER::create(@_) unless ref($item) =~ m/LocaleTranslation/; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub resolve_trailing_url { | ||
| 151 | my ($self, $class, $action, $parent) = @_; | ||
| 152 | return "/admin/api/settings/" . $class->url_plural if ((ref($class) || $class) =~ m/File$/) ; | ||
| 153 | return shift->SUPER::resolve_trailing_url(@_); | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | sub delete { | ||
| 158 | my ($self, $class) = @_; | ||
| 159 | $self->validate_item(ref($class)); | ||
| 160 | if ($class->needs_form_encoding_delete) { | ||
| 161 | my $url; | ||
| 162 | if (ref($class) =~ m/File$/) { | ||
| 163 | $url = $self->resolve_trailing_url($class, "delete", $class->associated_parent); | ||
| 164 | $self->use_url("POST", $url, { | ||
| 165 | _method => "delete", | ||
| 166 | authenticity_token => $self->{authenticity_token}, | ||
| 167 | utf8 => "✓", | ||
| 168 | file => $class->key | ||
| 169 | }, 1, "application/x-www-form-urlencoded", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); | ||
| 170 | } else { | ||
| 171 | $url = $self->resolve_trailing_url($class, "delete", $class->associated_parent) . "/" . $class->id; | ||
| 172 | $self->use_url("POST", $url, { | ||
| 173 | _method => "delete", | ||
| 174 | authenticity_token => $self->{authenticity_token}, | ||
| 175 | utf8 => "✓" | ||
| 176 | }, 1, "application/x-www-form-urlencoded", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); | ||
| 177 | } | ||
| 178 | |||
| 179 | } else { | ||
| 180 | $self->SUPER::delete($class); | ||
| 181 | } | ||
| 182 | return 1; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub extract_alt_text { | ||
| 186 | my ($self, $image, $content) = @_; | ||
| 187 | my $product_id = $image->associated_parent->id; | ||
| 188 | my $image_id = $image->id; | ||
| 189 | my ($form) = ($content =~ m/<form[^>]+action="\/admin\/products\/$product_id\/images\/$image_id"(.*?)<\/form>/ms); | ||
| 190 | die new WWW::Shopify::Exception() unless $form =~ m/<input[^>]+name="image\[alt\]" type="text"\s*(?:value="([^"]+)")?\s*\/>/; | ||
| 191 | return $1; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub send_activation_links { | ||
| 195 | my ($self, @customers) = @_; | ||
| 196 | my ($decoded, $response) = $self->use_url('put', "/admin/api/" . $self->api_version . "/customers/set", { operation => "invite", 'customer_ids[]' => [map { $_->id } @customers] }, 1, "application/x-www-form-urlencoded"); | ||
| 197 | die new WWW::Shopify::Exception("Unexpected Response.") unless $decoded->{message} =~ m/Invited (\d+) customer/; | ||
| 198 | return $1; | ||
| 199 | } | ||
| 200 | |||
| 201 | sub get_activation_link { | ||
| 202 | my ($self, $customer) = @_; | ||
| 203 | my ($decoded, $reponse) = $self->use_url('get', "/admin/api/" . $self->api_version . "/customers/" . $customer->id, { }, "text/html"); | ||
| 204 | die new WWW::Shopify::Exception("Unable to find activation link for " . $customer->id . ".") unless $decoded =~ m/http:\/\/[\w\.]+\/account\/activate\/\w+/; | ||
| 205 | return $&; | ||
| 206 | } | ||
| 207 | |||
| 208 | sub get_reset_token { | ||
| 209 | my ($self, $customer) = @_; | ||
| 210 | my ($decoded, $response) = $self->use_url('get', "/admin/api/" . $self->api_version . "/customers/" . $customer->id, { }, "text/html"); | ||
| 211 | die new WWW::Shopify::Exception("Unable to find activation link for " . $customer->id . ".") unless $decoded =~ m/http:\/\/[\w\.]+\/account\/activate\/(\w+)/; | ||
| 212 | return $1; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub send_activation_link { | ||
| 216 | my ($self, $customer, $from, $subject, $body, $reset_token) = @_; | ||
| 217 | my ($decoded, $response) = $self->use_url('post', "/admin/api/" . $self->api_version . "/customers/" . $customer->id . "/invite", { | ||
| 218 | utf => '✓', | ||
| 219 | source => 'adminnext', | ||
| 220 | 'customer_invite_message[from]' => $from, | ||
| 221 | 'customer_invite_message[subject]' => $subject, | ||
| 222 | 'customer_invite_message[body]' => $body, | ||
| 223 | 'customer_invite_message[reset_token]' => $reset_token | ||
| 224 | }, "application/x-www-form-urlencoded"); | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | =head2 login_admin($self, $email, $password) | ||
| 229 | |||
| 230 | Logs you in to the shop as an admin, allowing you to create and manipulate discount codes, as well as upload files into user-space (not theme space). | ||
| 231 | |||
| 232 | Doens't get around the API call limit, unfortunately. | ||
| 233 | |||
| 234 | =cut | ||
| 235 | |||
| 236 | use HTTP::Request::Common; | ||
| 237 | sub login_admin { | ||
| 238 | my ($self, $username, $password, $secret) = @_; | ||
| 239 | $secret = $self->two_factor_secret unless $secret; | ||
| 240 | return 1 if $self->{last_login_check} && (time - $self->{last_login_check}) < 1000; | ||
| 241 | my $ua = $self->ua; | ||
| 242 | die new WWW::Shopify::Exception("Unable to login as admin without a cookie jar.") unless defined $ua->cookie_jar; | ||
| 243 | my $res = $ua->get("https://" . $self->shop_url . "/admin/auth/login"); | ||
| 244 | die new WWW::Shopify::Exception("Unable to get login page.") unless $res->is_success; | ||
| 245 | die new WWW::Shopify::Exception("Unable to find authenticity token.") unless $res->decoded_content =~ m/name="authenticity_token".*?value="(\S+)"/ms; | ||
| 246 | my $authenticity_token = $1; | ||
| 247 | my $req = POST "https://" . $self->shop_url . "/admin/auth/login", [ | ||
| 248 | login => $username, | ||
| 249 | password => $password, | ||
| 250 | redirect => "", | ||
| 251 | commit => "Log in", | ||
| 252 | authenticity_token => $authenticity_token | ||
| 253 | ]; | ||
| 254 | $res = $ua->request($req); | ||
| 255 | die new WWW::Shopify::Exception("Unable to complete request: " . $res->decoded_content) unless $res->is_success || $res->code == 302; | ||
| 256 | if ($res->decoded_content =~ m/Please enter the six/) { | ||
| 257 | die new WWW::Shopify::Exception($res) unless $res->decoded_content =~ m/name="authenticity_token" value="(.*?)"/; | ||
| 258 | $authenticity_token = $1; | ||
| 259 | $self->{authenticity_token} = $authenticity_token; | ||
| 260 | $req = POST "https://" . $self->shop_url . "/admin/auth/tfa", [ | ||
| 261 | code => calc_onetime_code($secret), | ||
| 262 | redirect => "", | ||
| 263 | commit => "Log in", | ||
| 264 | authenticity_token => $authenticity_token | ||
| 265 | ]; | ||
| 266 | $res = $ua->request($req); | ||
| 267 | } | ||
| 268 | die new WWW::Shopify::Exception("Unable to login: $1.") if $res->decoded_content =~ m/class="status system-error">(.*?)<\/div>/; | ||
| 269 | $self->{last_login_check} = time; | ||
| 270 | $self->{authenticity_token} = $authenticity_token; | ||
| 271 | $res = $self->ua->request(GET "https://" . $self->shop_url . "/admin"); | ||
| 272 | die new WWW::Shopify::Exception($res) unless $res->decoded_content =~ m/meta name="csrf-token" content="(.*?)"/; | ||
| 273 | $self->{authenticity_token} = $1; | ||
| 274 | $ua->default_header('X-CSRF-Token' => $self->{authenticity_token}); | ||
| 275 | return 1; | ||
| 276 | } | ||
| 277 | |||
| 278 | =head2 logged_in_admin($self) | ||
| 279 | |||
| 280 | Determines whether or not you're logged in to the Shopify store as an admin. | ||
| 281 | |||
| 282 | =cut | ||
| 283 | |||
| 284 | sub logged_in_admin { | ||
| 285 | my ($self) = @_; | ||
| 286 | return undef unless $self->{authenticity_token}; | ||
| 287 | return 1 if $self->{last_login_check} && (time - $self->{last_login_check}) < 1000; | ||
| 288 | my $ua = $self->ua; | ||
| 289 | return undef unless $ua->cookie_jar; | ||
| 290 | my $res = $ua->get('https://' . $self->shop_url . '/admin/api/" . $self->api_version . "/discounts/count.json'); | ||
| 291 | return undef unless $res->is_success; | ||
| 292 | $self->{last_login_check} = time; | ||
| 293 | return 1; | ||
| 294 | } | ||
| 295 | |||
| 296 | =head2 menu_or_linklists | ||
| 297 | |||
| 298 | As of 2017-08-29, some stores will go for Menus, some for LinkList. This figures out which. | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | sub menu_or_linklists { | ||
| 303 | my ($self) = @_; | ||
| 304 | my $ua = $self->ua; | ||
| 305 | my $res = $ua->get('https://' . $self->shop_url . '/admin/api/" . $self->api_version . "/link_lists.json'); | ||
| 306 | return $res->request->uri->path =~ m/menu/ ? "WWW::Shopify::Model::Menu" : "WWW::Shopify::Model::LinkList"; | ||
| 307 | } | ||
| 308 | |||
| 309 | sub convert_linklist_to_menu { | ||
| 310 | my ($self, $linklist) = @_; | ||
| 311 | return WWW::Shopify::Model::Menu->new({ | ||
| 312 | id => $linklist->id, | ||
| 313 | handle => $linklist->handle, | ||
| 314 | title => $linklist->title, | ||
| 315 | items => [ | ||
| 316 | (map { | ||
| 317 | WWW::Shopify::Model::Menu::Item->new({ | ||
| 318 | title => $_->title, | ||
| 319 | subject => $_->subject, | ||
| 320 | subject_id => $_->subject_id, | ||
| 321 | subject_params => $_->subject_params, | ||
| 322 | type => $_->link_type, | ||
| 323 | }) | ||
| 324 | } $linklist->links) | ||
| 325 | ] | ||
| 326 | }); | ||
| 327 | } | ||
| 328 | |||
| 329 | use Exporter 'import'; | ||
| 330 | our @EXPORT_OK = qw(calc_onetime_code); | ||
| 331 | use Digest::SHA qw(hmac_sha1); | ||
| 332 | use Convert::Base32 qw(decode_base32); | ||
| 333 | use Math::BigInt; | ||
| 334 | |||
| 335 | sub calc_onetime_code { | ||
| 336 | my ($secret, $time) = @_; | ||
| 337 | $secret = decode_base32($secret); | ||
| 338 | $time = $time || time; | ||
| 339 | my $step = 30; | ||
| 340 | |||
| 341 | my $T = Math::BigInt->new( int( $time / $step ) ); | ||
| 342 | ( my $hex = $T->as_hex ) =~ s/^0x(.*)/"0"x(16 - length $1) . $1/e; | ||
| 343 | my $bin_code = join( "", map chr hex, $hex =~ /(..)/g ); | ||
| 344 | my $hash = hmac_sha1($bin_code, $secret); | ||
| 345 | my $offset = hex substr unpack( "H*" => $hash ), -1; | ||
| 346 | my $dt = unpack("N", substr $hash, $offset, 4); | ||
| 347 | return sprintf("%06d", ($dt & 0x7fffffff) % (10 ** 6)); | ||
| 348 | } | ||
| 349 | |||
| 350 | 1; |