blib/lib/Net/Facebook/Oauth2.pm

filter: Statementstmt 59.4% 82 / 138 Branchbran 33.3% 26 / 78 Conditioncond 9.3% 3 / 32 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 73.6% 14 / 19 Podpod 80.0% 8 / 10 totaltotal 44.6% 133 / 298 CC 51 SCAR 36.7
CC
51
Cov
45%
get_authorization_url
90.0
get_access_token
72.0
debug_token
72.0
CRAP 39.2
↑ Summary
1 package Net::Facebook::Oauth2;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.12';
12  
13 7 ▶sub new {
Subroutine new: called CC=13 SCAR=30.9
Pod documented
14 7 my ($class,%options) = @_;
15 7 my $self = {};
16 7 $self->{options} = \%options;
17  
18 7 ▶ my $api_version = defined $options{api_version} ? $options{api_version} : 'v4.0';
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'api_version'} ? : 0 7 7
19  
20 7 ▶ if (!defined $options{access_token}){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options{"access_token"}) 3 4 7
21 3 ▶ croak "You must provide your application id in new()\nNet::Facebook::Oauth2->new( application_id => '...' )" unless defined $self->{options}->{application_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"application_id"} 1 2 3
22 2 ▶ croak "You must provide your application secret in new()\nNet::Facebook::Oauth2->new( application_secret => '...' )" unless defined $self->{options}->{application_secret};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"}{"application_secret"} 1 1 2
23 }
24  
25 5 ▶ if (defined $options{access_token_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'access_token_url'}) { } 0 5 5
26 0 ▶ croak "cannot pass access_token_url AND api_version" if defined $options{api_version};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"api_version"} 0 0 0
27 0 $self->{access_token_url} = $options{access_token_url};
28 }
29 else {
30 5 $self->{access_token_url} = "https://graph.facebook.com/$api_version/oauth/access_token";
31 }
32  
33 5 ▶ if (defined $options{authorize_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'authorize_url'}) { } 0 5 5
34 0 ▶ croak "cannot pass authorize_url AND api_version" if defined $options{api_version};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"api_version"} 0 0 0
35 0 $self->{authorize_url} = $options{authorize_url};
36 }
37 else {
38 5 $self->{authorize_url} = "https://www.facebook.com/$api_version/dialog/oauth";
39 }
40  
41 5 ▶ if (defined $options{debug_token_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'debug_token_url'}) { } 0 5 5
42 0 ▶ croak "cannot pass debug_token_url AND api_version" if defined $options{api_version};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options{"api_version"} 0 0 0
43 0 $self->{debug_token_url} = $options{debug_token_url};
44 }
45 else {
46 5 $self->{debug_token_url} = "https://graph.facebook.com/$api_version/debug_token";
47 }
48  
49 5 ▶ $self->{browser} = $options{browser} || LWP::UserAgent->new;
Condition66%2/3
$options{'browser'} || 'LWP::UserAgent'->new
l!l&&r!l&&!r
140
MC/DC0%0/2
$options{'browser'} || 'LWP::UserAgent'->new
$options{'browser'} 'LWP::UserAgent'->new
Truth table66%2/3
A || B
A= $options{'browser'}B= 'LWP::UserAgent'->new
ABresult
1X1
011
000
50 5 ▶ $self->{display} = $options{display} || 'page'; ## other values popup and wab
Condition50%1/2
$options{'display'} || 'page'
l!l
05
MC/DC0%0/1
$options{'display'} || 'page'
$options{'display'}
Truth table50%1/2
A
A= $options{'display'}
Aresult
11
00
51 5 $self->{access_token} = $options{access_token};
52  
53 5 return bless($self, $class);
54 }
55  
56 0 ▶sub get_authorization_url {
Subroutine get_authorization_url: not called CC=9 SCAR=45.0
Pod documented
57 0 my ($self,%params) = @_;
58  
59 0 ▶ $params{callback} ||= $self->{options}->{callback};
Condition0%0/3
$params{"callback"} ||= $self->{"options"}{"callback"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"callback"} ||= $self->{"options"}{"callback"}
$params{"callback"} $self->{"options"}{"callback"}
Truth table0%0/3
A ||= B
A= $params{"callback"}B= $self->{"options"}{"callback"}
ABresult
1X1
011
000
60 0 ▶ croak "You must pass a callback parameter with Oauth v2.0" unless defined $params{callback};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"callback"} 0 0 0
61  
62 0 ▶ $params{display} = $self->{display} unless defined $params{display};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"display"} 0 0 0
63 0 $self->{options}->{callback} = $params{callback};
64  
65 my $url = $self->{authorize_url}
66 ."?client_id="
67 .uri_escape($self->{options}->{application_id})
68 ."&redirect_uri="
69 0 .uri_escape($params{callback});
70  
71 0 ▶ if ($params{scope}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"scope"}) 0 0 0
72 0 my $scope = join(',', @{$params{scope}});
73 0 ▶ $url .= '&scope=' . $scope if $scope;
Branch: 1 branch
BranchTrueFalseTotal
if $scope 0 0 0
74 }
75 # state is now required:
76 0 ▶ $url .= '&state=' . (defined $params{state} ? $params{state} : time);
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'state'} ? : 0 0 0
77  
78 0 ▶ $url .= '&response_type=' . $params{response_type} if $params{response_type};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"response_type"} 0 0 0
79 0 ▶ $url .= '&auth_type=' . $params{auth_type} if $params{auth_type};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"auth_type"} 0 0 0
80 0 $url .= "&display=".$params{display};
81  
82 0 return $url;
83 }
84  
85  
86 0 ▶sub get_access_token {
Subroutine get_access_token: not called CC=8 SCAR=42.8
Pod documented
87 0 my ($self,%params) = @_;
88 0 ▶ $params{callback} ||= $self->{options}->{callback};
Condition0%0/3
$params{"callback"} ||= $self->{"options"}{"callback"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"callback"} ||= $self->{"options"}{"callback"}
$params{"callback"} $self->{"options"}{"callback"}
Truth table0%0/3
A ||= B
A= $params{"callback"}B= $self->{"options"}{"callback"}
ABresult
1X1
011
000
89 0 ▶ $params{code} ||= $self->{options}->{code};
Condition0%0/3
$params{"code"} ||= $self->{"options"}{"code"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"code"} ||= $self->{"options"}{"code"}
$params{"code"} $self->{"options"}{"code"}
Truth table0%0/3
A ||= B
A= $params{"code"}B= $self->{"options"}{"code"}
ABresult
1X1
011
000
90  
91 0 ▶ croak "You must pass a code parameter with Oauth v2.0" unless defined $params{code};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"code"} 0 0 0
92 0 ▶ croak "You must pass callback URL" unless defined $params{callback};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"callback"} 0 0 0
93 0 $self->{options}->{code} = $params{code};
94  
95 ###generating access token URL
96 my $getURL = $self->{access_token_url}
97 ."?client_id="
98 .uri_escape($self->{options}->{application_id})
99 ."&redirect_uri="
100 .uri_escape($params{callback})
101 ."&client_secret="
102 .uri_escape($self->{options}->{application_secret})
103 0 ."&code=$params{code}";
104  
105 0 my $response = $self->{browser}->get($getURL);
106 0 my $json = decode_json($response->content());
107  
108 0 ▶ if (!$response->is_success || exists $json->{error}){
Branch: 2 branches
BranchTrueFalseTotal
if (not $response->is_success or exists $json->{'error'}) { } 0 0 0
elsif ($json->{'access_token'}) { } 0 0 0
Condition0%0/3
not $response->is_success or exists $json->{"error"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $response->is_success or exists $json->{"error"}
not $response->is_success exists $json->{"error"}
Truth table0%0/3
not(A) or B
A= not $response->is_successB= exists $json->{"error"}
ABresult
1X1
011
000
109 ##got an error response from facebook. die and display error message
110 0 croak "'" . $json->{error}->{type}. "'" . " " .$json->{error}->{message};
111 }
112 elsif ($json->{access_token}) {
113 ##everything is ok proccess response and extract access token
114 0 return $self->{access_token} = $json->{access_token};
115 }
116 else {
117 0 croak "can't get access token from " . $response->content();
118 }
119 }
120  
121 0 ▶sub get_long_lived_token {
Subroutine get_long_lived_token: not called CC=7 SCAR=40.3
Pod documented
122 0 my ($self,%params) = @_;
123  
124 0 ▶ if (!$self->{access_token}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"access_token"}) 0 0 0
125 0 ▶ croak "You must pass the access_token" unless defined $params{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"access_token"} 0 0 0
126 }
127  
128 my $getURL = $self->{access_token_url}
129 . '?grant_type=fb_exchange_token'
130 . '&client_id=' . uri_escape($self->{options}->{application_id})
131 . '&client_secret=' . uri_escape($self->{options}->{application_secret})
132 . '&fb_exchange_token=' . uri_escape($self->{access_token} || $params{access_token})
133 0 ▶ ;
Condition0%0/3
$self->{'access_token'} || $params{'access_token'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'access_token'} || $params{'access_token'}
$self->{'access_token'} $params{'access_token'}
Truth table0%0/3
A || B
A= $self->{'access_token'}B= $params{'access_token'}
ABresult
1X1
011
000
134 0 my $response = $self->{browser}->get($getURL);
135 0 my $json = decode_json($response->content());
136 0 ▶ if (!$response->is_success || exists $json->{error}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $response->is_success or exists $json->{'error'}) { } 0 0 0
elsif ($json->{'access_token'}) { } 0 0 0
Condition0%0/3
not $response->is_success or exists $json->{"error"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $response->is_success or exists $json->{"error"}
not $response->is_success exists $json->{"error"}
Truth table0%0/3
not(A) or B
A= not $response->is_successB= exists $json->{"error"}
ABresult
1X1
011
000
137 0 croak "'" . $json->{error}->{type}. "'" . " " .$json->{error}->{message};
138 }
139 elsif ($json->{access_token}) {
140 0 return $self->{access_token} = $json->{access_token};
141 }
142 else {
143 0 croak "can't get long lived access token from " . $response->content();
144 }
145 }
146  
147 0 ▶sub debug_token {
Subroutine debug_token: not called CC=8 SCAR=42.8
Pod documented
148 0 my ($self,%params) = @_;
149 croak "You must pass the 'input' access token to inspect"
150 0 ▶ unless defined $params{input};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"input"} 0 0 0
151  
152 # NOTE: according to FB's documentation, instead of passing an
153 # app token we can simply pass access_token=app_id|app_secret:
154 # https://developers.facebook.com/docs/facebook-login/access-tokens/#apptokens
155 my $getURL = $self->{debug_token_url}
156 . "?input_token=" . uri_escape($params{input})
157 . "&access_token="
158 . join('|',
159 uri_escape($self->{options}->{application_id}),
160 uri_escape($self->{options}->{application_secret})
161 0 );
162  
163 0 my $response = $self->{browser}->get($getURL);
164 0 my $json = decode_json($response->content());
165 0 ▶ if (!$response->is_success || exists $json->{error}){
Branch: 3 branches
BranchTrueFalseTotal
if (not $response->is_success or exists $json->{'error'}) { } 0 0 0
elsif (not exists $json->{'data'}{'app_id'} && exists $json->{'data'}{'user_id'}) { } 0 0 0
elsif (not $params{'skip_check'} and '' . $json->{'data'}{'app_id'} ne '' . $self->{'options'}{'application_id'}) { } 0 0 0
Condition0%0/3
not $response->is_success or exists $json->{"error"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $response->is_success or exists $json->{"error"}
not $response->is_success exists $json->{"error"}
Truth table0%0/3
not(A) or B
A= not $response->is_successB= exists $json->{"error"}
ABresult
1X1
011
000
166 ##got an error response from facebook. die and display error message
167 0 croak "'" . $json->{error}->{type}. "'" . " " .$json->{error}->{message};
168 }
169 ▶ elsif (!exists $json->{data}->{app_id} || !exists $json->{data}->{user_id}) {
Condition0%0/3
exists $json->{'data'}{'app_id'} && exists $json->{'data'}{'user_id'}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $json->{'data'}{'app_id'} && exists $json->{'data'}{'user_id'}
exists $json->{'data'}{'app_id'} exists $json->{'data'}{'user_id'}
Truth table0%0/3
A && B
A= exists $json->{'data'}{'app_id'}B= exists $json->{'data'}{'user_id'}
ABresult
0X0
100
111
170 0 return;
171 }
172 ▶ elsif (!$params{skip_check} && (''.$json->{data}->{app_id}) ne (''.$self->{options}->{application_id}) ) {
Condition0%0/3
not $params{"skip_check"} and '' . $json->{"data"}{"app_id"} ne '' . $self->{"options"}{"application_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $params{"skip_check"} and '' . $json->{"data"}{"app_id"} ne '' . $self->{"options"}{"application_id"}
not $params{"skip_check"} '' . $json->{"data"}{"app_id"} ne '' . $self->{"options"}{"application_id"}
Truth table0%0/3
not(A) and B
A= not $params{"skip_check"}B= '' . $json->{"data"}{"app_id"} ne '' . $self->{"options"}{"application_id"}
ABresult
0X0
100
111
173 0 return;
174 }
175 0 return $json->{data};
176 }
177  
178  
179 7 ▶sub get {
Subroutine get: called CC=6 SCAR=17.9
Pod documented
180 7 my ($self,$url,$params) = @_;
181 7 ▶ unless ($self->_has_access_token($url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_access_token($url)) 5 2 7
182 5 ▶ croak "You must pass access_token" unless defined $self->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"access_token"} 0 5 5
183 5 ▶ $url .= $self->{_has_query} ? '&' : '?';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_has_query'} ? : 1 4 5
184 5 $url .= "access_token=" . $self->{access_token};
185 }
186  
187 ##construct the new url
188 7 my @array;
189  
190 7 ▶ while ( my ($key, $value) = each(%{$params})){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$params;}) 5 7 12
191 5 $value = uri_escape($value);
192 5 push(@array, "$key=$value");
193 }
194  
195 7 my $string = join('&', @array);
196 7 ▶ $url .= "&".$string if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 5 2 7
197  
198 7 my $response = $self->{browser}->get($url);
199 7 my $content = $response->content();
200 7 return $self->_content($content);
201 }
202  
203 2 ▶sub post {
Subroutine post: called CC=3 SCAR=11.0
Pod documented
204 2 my ($self,$url,$params) = @_;
205 2 ▶ unless ($self->_has_access_token($url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_access_token($url)) 1 1 2
206 1 ▶ croak "You must pass access_token" unless defined $self->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"access_token"} 0 1 1
207 1 $params->{access_token} = $self->{access_token};
208 }
209 2 my $response = $self->{browser}->post($url,$params);
210 2 my $content = $response->content();
211 2 return $self->_content($content);
212 }
213  
214 1 ▶sub delete {
Subroutine delete: called CC=3 SCAR=11.2
Pod documented
215 1 my ($self,$url,$params) = @_;
216 1 ▶ unless ($self->_has_access_token($url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_access_token($url)) 1 0 1
217 1 ▶ croak "You must pass access_token" unless defined $self->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"access_token"} 0 1 1
218 1 $params->{access_token} = $self->{access_token};
219 }
220 1 my $response = $self->{browser}->delete($url,$params);
221 1 my $content = $response->content();
222 1 return $self->_content($content);
223 }
224  
225 0 ▶sub as_hash {
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod undocumented
226 0 my ($self) = @_;
227 0 return decode_json($self->{content});
228 }
229  
230 3 ▶sub as_json {
Subroutine as_json: called CC=1 SCAR=0.0
Pod undocumented
231 3 my ($self) = @_;
232 3 return $self->{content};
233 }
234  
235 10 ▶sub _content {
Subroutine _content: called CC=1 SCAR=0.0
236 10 my ($self,$content) = @_;
237 10 $self->{content} = $content;
238 10 return $self;
239 }
240  
241 10 ▶sub _has_access_token {
Subroutine _has_access_token: called CC=2 SCAR=6.9
242 10 my ($self, $url) = @_;
243 10 my $uri = URI->new($url);
244 10 my %q = $uri->query_form;
245 #also check if we have a query and save result
246 10 $self->{_has_query} = $uri->query();
247 10 ▶ if (grep { $_ eq 'access_token' } keys %q) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "access_token";} keys %q) 3 7 10
248 3 return 1;
249 }
250 7 return;
251 }
252  
253 2 1;
254 __END__
↑ Summary