Shopify/Public.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Shopify/Field/Identifier.pm ↑ Summary Shopify.pm »
1 #!/usr/bin/perl
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 0 ▶use WWW::Shopify;
Subroutine BEGIN: called CC=1 SCAR=2.6
7  
8 =head1 NAME
9  
10 WWW::Shopify::Public - Main object representing public app access to a particular Shopify store.
11  
12 =cut
13  
14 =head1 DESCRIPTION
15  
16 Inherits all methods from L<WWW::Shopify>, provides additional mechanisms to modify the used access_token, user-agent and url handler.
17  
18 =cut
19  
20 package WWW::Shopify::Public;
21 use parent 'WWW::Shopify';
22  
23 =head1 METHODS
24  
25 =head2 new($url, $api_key, $access_token)
26  
27 Creates a new WWW::Shopify::Public object, which allows you to make calls via the shopify public app interface.
28  
29 You can see an overview of the authentication workflow for a public app here: L<< http://api.shopify.com/authentication.html >>
30  
31 =cut
32  
33 sub new {
34 my $class = shift;
35 my ($shop_url, $api_key, $access_token, $api_version) = @_;
36 my $self = $class->SUPER::new($shop_url,undef,undef,$api_version);
37 # This is an optional addition; it's not necessary.
38 $self->api_key($api_key);
39 $self->access_token($access_token);
40 $self->ua->default_header("X-Shopify-Access-Token" => $access_token);
41 return $self;
42 }
43  
44 =head2 api_key([$api_key])
45  
46 Gets/sets the app's access token.
47  
48 =cut
49  
50 sub api_key { $_[0]->{_api_key} = $_[1] if defined $_[1]; return $_[0]->{_api_key}; }
51  
52 =head2 access_token([$access_token])
53  
54 Gets/sets the app's access token.
55  
56 =cut
57  
58 sub access_token { $_[0]->{_access_token} = $_[1] if defined $_[1]; return $_[0]->{_access_token}; }
59  
60 sub is_valid { my $self = shift; $self->SUPER::is_valid(@_); return $self->{_access_token}; }
61  
62 =head2 authorize_url(@$scope, $redirect)
63  
64 When the shop doesn't have an access_token, this is what you should be redirecting your client to. Inputs should be your scope, as an array, and the url you want to redirect to.
65  
66 =cut
67  
68 use URI::Escape;
69 sub authorize_url {
70 my ($self, $scope, $redirect, $nonce, $options) = @_;
71 my $hostname = $self->shop_url;
72 my %parameters = (
73 'client_id' => $self->api_key,
74 'scope' => join(",", @$scope),
75 'redirect_uri' => $redirect,
76 ($nonce ? ('state' => $nonce) : ()),
77 ($options ? ('grant_options[]' => $options) : ())
78 );
79 return "https://$hostname/admin/oauth/authorize?" . join("&", map { my $key = $_; ref($parameters{$key}) ? (map { "$key=" . uri_escape($_) } @{$parameters{$key}}) : ("$key=" . uri_escape($parameters{$key})) } keys(%parameters));
80 }
81  
82 =head2 scope_compare(@$scope1, @$scope2)
83
84 Determines whether scope1 is more or less permissive than scope2. If the scopes cannot be compared easily
85 (e.g. [write_orders, read_products], [read_products, write_script_tag]), then undef is returned. If scopes
86 can be compared, returns -1, 0 or 1 as appropriate.
87  
88 =cut
89  
90 use Exporter 'import';
91 our @EXPORT_OK = qw(scope_compare scope_clean verify_install calc_hmac_install_signature);
92  
93 use List::MoreUtils qw(uniq);
94  
95 sub scope_compare {
96 my ($scope1, $scope2) = @_;
97 die new WWW::Shopify::Exception("Invalid scopes passed to compare.") unless ref($scope1) && ref($scope2) && ref($scope1) eq "ARRAY" && ref($scope2) eq "ARRAY";
98
99 $scope1 = scope_clean($scope1);
100 $scope2 = scope_clean($scope2);
101 # If scope2 has more permission settings than scope1, we've jumped up in permissions.
102 return 1 if int(@$scope1) < int(@$scope2);
103 return -1 if (int(@$scope1) > int(@$scope2));
104 # Here, we should have exactly equal amounts of scopes in both arrays, sorted. So they should be the same types down.
105
106 for (0..int(@$scope1)-1) {
107 die new WWW::Shopify::Exception("Invalid scope: $_") unless $scope1->[$_] =~ m/(read|write)_(\w+)/;
108 my ($scope1_permission, $scope1_type) = ($1, $2);
109 die new WWW::Shopify::Exception("Invalid scope: $_") unless $scope2->[$_] =~ m/(read|write)_(\w+)/;
110 my ($scope2_permission, $scope2_type) = ($1, $2);
111 # If we have not the same type, that means we've got some weird permissions going on, so we return undef, because it's
112 # neither less nor more permissive, necessarily, just different.
113 return undef unless $scope1_type eq $scope2_type;
114 # When we've jumped down to a read in our second scope, we're less permissive.
115 return -1 if $scope2_permission eq "read" && $scope1_permission eq "write";
116 # When we've jumped up to a write in our
117 return 1 if $scope1_permission eq "read" && $scope2_permission eq "write";
118 }
119 return 0;
120 }
121  
122 # TODO There are probably still edge cases here. We should check this more thourghly
123 # namely we should probably be checking for string starts with write m/^write_.*/
124 sub scope_clean {
125 my ($scope) = @_;
126
127 my @writes = grep { $_ =~ m/write_.*/ } @$scope;
128 my @reads = grep { $_ =~ m/read_.*/ } @$scope;
129
130 my @fscopes = @writes;
131
132 for my $p (@reads) {
133 $p =~ m/read_(.*)/;
134 my $a = $1;
135 push(@fscopes, $p) unless (grep { $_ eq "write_" . $1 } @writes);
136 }
137 @fscopes = sort(@fscopes);
138 return \@fscopes;
139 }
140  
141 =head2 refresh_token($new_secret, $refresh_token)
142  
143 Refreshes a token.
144  
145 =cut
146  
147  
148 sub refresh_token {
149 my ($self, $new_secret, $refresh_token) = (@_);
150
151 my $req = HTTP::Request->new(POST => "https://" . $self->shop_url . "/admin/oauth/access_token");
152 $req->content_type('application/x-www-form-urlencoded');
153 my %parameters = (
154 'client_id' => $self->api_key,
155 'client_secret' => $new_secret,
156 'refresh_token' => $refresh_token,
157 'access_token' => $self->access_token
158 );
159 $req->content(join("&", map { "$_=" . uri_escape($parameters{$_}) } keys(%parameters)));
160 my $res;
161 my $success = undef;
162 for (my $i = 0; $i < 5 && !$success; ++$i) {
163 $res = $self->url_handler->request($req);
164 $success = $res->is_success();
165 sleep 1 unless $success;
166 }
167 die new WWW::Shopify::Exception("Unable to reach server for https://" . $self->shop_url . "/admin/oauth/access_token: " . $res->code() . ".\n" . $res->content) unless $res->is_success();
168 my $decoded = JSON::decode_json($res->content);
169 die new WWW::Shopify::Exception("Unable to refresh access token.") unless defined $decoded->{access_token};
170 return %{$decoded} if wantarray;
171 return $decoded->{access_token};
172 }
173  
174 =head2 exchange_token($shared_secret, $code)
175  
176 When you have a temporary code, which you should get from authorize_url's redirect and you want to exchange it for a token, you call this.
177  
178 =cut
179  
180 sub exchange_token {
181 my ($self, $shared_secret, $code) = (@_);
182
183 my $req = HTTP::Request->new(POST => "https://" . $self->shop_url . "/admin/oauth/access_token");
184 $req->content_type('application/x-www-form-urlencoded');
185 my %parameters = (
186 'client_id' => $self->api_key,
187 'client_secret' => $shared_secret,
188 'code' => $code
189 );
190 $req->content(join("&", map { "$_=" . uri_escape($parameters{$_}) } keys(%parameters)));
191 my $res;
192 # If you go through the install too fast, Shopify will give you an access denied thing. So in that case, let's repeat until
193 my $success = undef;
194 for (my $i = 0; $i < 5 && !$success; ++$i) {
195 $res = $self->url_handler->request($req);
196 $success = $res->is_success();
197 sleep 1 unless $success;
198 }
199 die new WWW::Shopify::Exception("Unable to reach server for https://" . $self->shop_url . "/admin/oauth/access_token: " . $res->code() . ".\n" . $res->content) unless $res->is_success();
200 my $decoded = JSON::decode_json($res->content);
201 die new WWW::Shopify::Exception("Unable to retrieve access token.") unless defined $decoded->{access_token};
202 return %{$decoded} if wantarray;
203 return $decoded->{access_token};
204 }
205  
206  
207 sub calc_hmac_install_signature { return WWW::Shopify::calc_hmac_login_signature(@_); }
208 sub verify_install {
209 my ($shared_secret, $params) = @_;
210 return undef unless $params->{hmac};
211 my $hmac = ref($params->{hmac}) eq 'ARRAY' ? $params->{hmac}->[0] : $params->{hmac};
212 return calc_hmac_install_signature($shared_secret, $params) eq $hmac;
213 }
214  
215 =head1 SEE ALSO
216  
217 L<WWW::Shopify::Item>, L<WWW::Shopify>
218  
219 =head1 AUTHOR
220  
221 Adam Harrison (adamdharrison@gmail.com)
222  
223 =head1 LICENSE
224  
225 See LICENSE in the main directory.
226  
227 =cut
228  
229 1;
« Shopify/Field/Identifier.pm ↑ Summary Shopify.pm »