lib/LWP/Authen/OAuth2/ServiceProvider.pm

filter: Statementstmt 38.4% 70 / 182 Branchbran 10.0% 6 / 60 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 69.4% 25 / 36 Podpod 38.4% 10 / 26 totaltotal 35.4% 111 / 313 SCAR 64.0
CC
41
Cov
31%
collect_action_params
210.0
construct_tokens
132.0
can_refresh_tokens
30.0
CRAP 601.8
« lib/LWP/Authen/OAuth2.pm ↑ Summary blib/lib/LWP/Authen/OAuth2/Args.pm »
1 package LWP::Authen::OAuth2::ServiceProvider;
2  
3 # ABSTRACT: ServiceProvider base class
4 12 our $VERSION = '0.19'; # VERSION
5  
6 12 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 ▶use Carp qw(confess croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use Memoize qw(memoize);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Module::Load qw(load);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 our @CARP_NOT = qw(LWP::Authen::OAuth2 LWP::Authen::OAuth2::Args);
17  
18 12 use LWP::Authen::OAuth2::Args qw(
19 extract_option copy_option assert_options_empty
20 12 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # Construct a new object.
23 sub new {
24 6 ▶ my ($class, $opts) = @_;
Subroutine new: called CC=3 SCAR=11.3
Pod undocumented
25  
26 # I start as an empty hashref.
27 6 my $self = {};
28  
29 # But what class am I supposed to actually be?
30 6 ▶ if (not exists $opts->{service_provider}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opts->{'service_provider'}) { } 0 6 6
31 0 bless $self, $class;
32 }
33 else {
34 # Convert "Google" to "LWP::Authen::OAuth2::ServiceProvider::Google"
35 # Not a method because no object yet exists.
36 6 $class = service_provider_class(delete $opts->{service_provider});
37 6 my $client_type = delete $opts->{client_type};
38 6 ▶ if (not defined($client_type)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $client_type) 6 0 6
39 6 $client_type = "default";
40 }
41 6 bless $self, $class->client_type_class($client_type);
42 }
43  
44 6 $self->init($opts);
45 }
46  
47 sub init {
48 6 ▶ my ($self, $opts) = @_;
Subroutine init: called CC=6 SCAR=18.3
Pod documented
49  
50 # Now let us consume options. 2 args = required, 3 = defaulted.
51 # In general subclasses should Just Work.
52  
53 # need to read this first, since the later opts depend on it
54 6 ▶ $self->copy_option($opts, 'use_test_urls') if defined $opts->{use_test_urls};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"use_test_urls"} 0 6 6
55  
56 # These are required, NOT provided by this class, but are by subclasses.
57 6 for my $field (qw(token_endpoint authorization_endpoint)) {
58 12 ▶ if ($self->can($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($field)) { } 12 0 12
59 12 $self->copy_option($opts, $field, $self->$field);
60 }
61 else {
62 0 $self->copy_option($opts, $field);
63 }
64 }
65  
66 # These are defaulted by this class, maybe overridden by subclasses.
67 6 for my $field (
68 qw(required_init optional_init),
69 map {
70 18 ("$_\_required_params", "$_\_optional_params")
71 } qw(authorization request refresh)
72 ) {
73 48 $self->copy_option($opts, $field, [$self->$field]);
74 }
75  
76 # And hashrefs for default key/value pairs.
77 6 for my $field (
78 map "$_\_default_params", qw(authorization request refresh)
79 ) {
80 18 $self->copy_option($opts, $field, {$self->$field});
81 }
82  
83 6 return $self;
84 }
85  
86 sub authorization_url {
87 0 ▶ my ($self, $oauth2, @rest) = @_;
Subroutine authorization_url: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my $param
89 = $self->collect_action_params("authorization", $oauth2, @rest);
90 0 my $uri = URI->new($self->authorization_endpoint());
91 0 $uri->query_form(%$param);
92 0 return $uri->as_string;
93 }
94  
95 sub request_tokens {
96 0 ▶ my ($self, $oauth2, @rest) = @_;
Subroutine request_tokens: not called CC=1 SCAR=6.9
Pod undocumented
97 0 my $param = $self->collect_action_params("request", $oauth2, @rest);
98 0 my $response = $self->post_to_token_endpoint($oauth2, $param);
99 0 return $self->construct_tokens($oauth2, $response);
100 }
101  
102 sub can_refresh_tokens {
103 0 ▶ my ($self, $oauth2, %opt) = @_;
Subroutine can_refresh_tokens: not called CC=5 SCAR=34.0
Pod undocumented
104 0 my %default = $self->refresh_default_params;
105 0 my $oauth2_args = $oauth2->for_service_provider;
106 0 for my $param ($self->refresh_required_params) {
107 0 ▶ if ( exists $default{$param}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $default{$param} or exists $oauth2_args->{$param} or exists $opt{$param}) { } 0 0 0
Condition0%0/6
exists $default{$param} or exists $oauth2_args->{$param} or exists $opt{$param}
l!l&&r!l&&!r
000
exists $default{$param} or exists $oauth2_args->{$param}
l!l&&r!l&&!r
000
MC/DC0%0/3
exists $default{$param} or exists $oauth2_args->{$param} or exists $opt{$param}
exists $default{$param} exists $oauth2_args->{$param} exists $opt{$param}
Truth table0%0/4
A or B or C
A= exists $default{$param}B= exists $oauth2_args->{$param}C= exists $opt{$param}
ABCresult
1XX1
01X1
0011
0000
108 or exists $oauth2_args->{$param}
109 or exists $opt{$param}
110 ) {
111 0 next;
112 }
113 else {
114 0 return 0;
115 }
116 }
117 0 return 1;
118 }
119  
120 sub refreshed_tokens {
121 0 ▶ my ($self, $oauth2, @rest) = @_;
Subroutine refreshed_tokens: not called CC=1 SCAR=6.9
Pod undocumented
122 0 my $param = $self->collect_action_params("refresh", $oauth2, @rest);
123 0 my $response = $self->post_to_token_endpoint($oauth2, $param);
124 # Error message or object, this is what we return.
125 0 return $self->construct_tokens($oauth2, $response);
126 }
127  
128 sub collect_action_params {
129 0 ▶ my $self = shift;
Subroutine collect_action_params: not called CC=14 SCAR=53.5
Pod documented
130 0 my $action = shift;
131 0 my $oauth2 = shift;
132 0 my $oauth2_args = $oauth2->for_service_provider;
133 0 my @rest = @_;
134 0 my $opt = {@_};
135  
136 0 my $default = $self->{"$action\_default_params"};
137  
138 0 ▶ if ($oauth2->is_strict) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oauth2->is_strict) { } 0 0 0
139 # We copy one by one with testing.
140 0 my $result = {};
141 0 for my $param (@{ $self->{"$action\_required_params"}}) {
142 0 ▶ if (exists $opt->{$param}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $opt->{$param}) { } 0 0 0
elsif (defined $oauth2_args->{$param}) { } 0 0 0
elsif (defined $default->{$param}) { } 0 0 0
143 0 ▶ if (defined $opt->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt->{$param}) { } 0 0 0
144 0 $result->{$param} = delete $opt->{$param};
145 }
146 else {
147 0 croak("Cannot pass undef for required param '$param'");
148 }
149 }
150 elsif (defined $oauth2_args->{$param}) {
151 0 $result->{$param} = $oauth2_args->{$param};
152 }
153 elsif (defined $default->{$param}) {
154 0 $result->{$param} = $default->{$param};
155 }
156 else {
157 0 croak("Missing required param '$param'");
158 }
159 }
160  
161 0 for my $param (@{ $self->{"$action\_optional_params"} }) {
162 0 for my $source ($result, $opt, $oauth2_args, $default) {
163 0 ▶ if (exists $source->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $source->{$param}) 0 0 0
164 # Only add it if it is not undef. Else hide.
165 0 ▶ if (defined $source->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $source->{$param}) 0 0 0
166 0 $result->{$param} = $source->{$param};
167 }
168  
169 # For opt only, delete if it was found.
170 0 ▶ if ($opt == $source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt == $source) 0 0 0
171 0 delete $opt->{$param};
172 }
173  
174 0 last; # source
175 # (undef is deliberate override, which is OK)
176 }
177 }
178 }
179  
180 0 $self->assert_options_empty($opt);
181  
182 # End of strict section.
183 0 return $result;
184 }
185 else {
186 # Not strict just bulk copy.
187 my $result = {
188 %$default,
189 (
190 0 map {($_, $oauth2_args->{$_})}
191 0 @{ $self->{"$action\_required_params"} },
192 0 @{ $self->{"$action\_optional_params"} }
193 ),
194 %$opt
195 };
196 0 for my $key (keys %$result) {
197 0 ▶ if (not defined($result->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result->{$key}) 0 0 0
198 0 delete $result->{$key};
199 }
200 }
201 0 return $result;
202 }
203 }
204  
205 sub post_to_token_endpoint {
206 0 ▶ my ($self, $oauth2, $param) = @_;
Subroutine post_to_token_endpoint: not called CC=1 SCAR=6.9
Pod documented
207 0 my $ua = $oauth2->user_agent();
208 0 return $ua->post($self->token_endpoint(), [%$param]);
209 }
210  
211 0 ▶sub api_url_base { return '' } # override in subclass
Subroutine api_url_base: not called CC=1 SCAR=6.9
Pod undocumented
212  
213 sub access_token_class {
214 0 ▶ my ($self, $type) = @_;
Subroutine access_token_class: not called CC=2 SCAR=17.9
Pod documented
215  
216 0 ▶ if ("bearer" eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ('bearer' eq $type) { } 0 0 0
217 0 return "LWP::Authen::OAuth2::AccessToken::Bearer";
218 }
219 else {
220 0 return "Token type '$type' not yet implemented";
221 }
222 }
223  
224 # Attempts to construct tokens, returns the access_token (which may have a
225 # request token embedded).
226 sub construct_tokens {
227 0 ▶ my ($self, $oauth2, $response) = @_;
Subroutine construct_tokens: not called CC=11 SCAR=48.8
Pod undocumented
228  
229 # The information that I need.
230 0 my $content = eval {$response->decoded_content};
231 0 ▶ if (not defined($content)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 0 0 0
232 0 $content = '';
233 }
234 0 my $data = eval {decode_json($content)};
235 0 my $parse_error = $@;
236 0 my $token_endpoint = $self->token_endpoint();
237  
238 # Can this have done wrong? Let me list the ways...
239 0 ▶ if ($parse_error) {
Branch: 3 branches
BranchTrueFalseTotal
if ($parse_error) { } 0 0 0
elsif ($data->{'error'}) { } 0 0 0
elsif (not $data->{'token_type'}) { } 0 0 0
240 # "Should not happen", hopefully just network.
241 # Tell the programmer everything.
242 0 my $status = $response->status_line;
243 return <<"EOT"
244 Token endpoint gave invalid JSON in response.
245  
246 Endpoint: $token_endpoint
247 Status: $status
248 Parse error: $parse_error
249 JSON:
250 $content
251 EOT
252 0 }
253 elsif ($data->{error}) {
254 # Assume a valid OAuth 2 error message.
255 0 my $message = "OAuth2 error: $data->{error}";
256  
257 # Do we have a mythical service provider that gives us more?
258 0 ▶ if ($data->{error_uri}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"error_uri"}) 0 0 0
259 # They seem to have a web page with detail.
260 0 $message .= "\n$data->{error_uri} may say more.\n";
261 }
262  
263 0 ▶ if ($data->{error_description}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"error_description"}) 0 0 0
264 # Wow! Thank you!
265 0 $message .= "\n\nDescription: $data->{error_description}\n";
266 }
267 0 return $message;
268 }
269 elsif (not $data->{token_type}) {
270 # Someone failed to follow the spec...
271 0 return <<"EOT";
272 Token endpoint missing expected token_type in successful response.
273  
274 Endpoint: $token_endpoint
275 JSON:
276 $content
277 EOT
278 }
279  
280 0 my $type = $self->access_token_class(lc($data->{token_type}));
281 0 ▶ if ($type !~ /^[\w\:]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /^[\w\:]+\z/) 0 0 0
282 # We got an error. :-(
283 0 return $type;
284 }
285  
286 0 eval {load($type)};
287 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
288 # MAKE THIS FATAL. (Clearly Perl code is simply wrong.)
289 0 confess("Loading $type for $data->{token_type} gave error: $@");
290 }
291  
292 # Try to make an access token.
293 0 my $access_token = $type->from_ref($data);
294  
295 0 ▶ if (not ref($access_token)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $access_token) { } 0 0 0
296 # This should be an error message of some sort.
297 0 return $access_token;
298 }
299 else {
300 # WE SURVIVED! EVERYTHING IS GOOD!
301 0 ▶ if ($oauth2->access_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oauth2->access_token) 0 0 0
302 0 $access_token->copy_refresh_from($oauth2->access_token);
303 }
304 0 return $access_token;
305 }
306 }
307  
308 # Override for your client_types if you have multiple.
309 sub client_type_class {
310 5 ▶ my ($class, $name) = @_;
Subroutine client_type_class: called CC=2 SCAR=7.6
Pod documented
311 5 ▶ if ("default" eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ('default' eq $name) { } 5 0 5
312 5 return $class;
313 }
314 else {
315 0 croak("Flow '$name' not defined for '$class'");
316 }
317 }
318  
319 # Override should you need the front-end LWP::Authen::OAuth object to have
320 # methods for service provider specific functionality.
321 #
322 # This is not expected to be a common need.
323 sub oauth2_class {
324 6 ▶ return "LWP::Authen::OAuth2";
Subroutine oauth2_class: called CC=1 SCAR=0.0
Pod documented
325 }
326  
327 12 memoize("service_provider_class");
328 sub service_provider_class {
329 6 ▶ my $short_name = shift;
Subroutine service_provider_class: called CC=5 SCAR=26.1
330 6 eval {
331 6 load("LWP::Authen::OAuth2::ServiceProvider::$short_name");
332 };
333 6 ▶ if (not $@) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $@) { } 6 0 6
elsif ($@ =~ /Compilation failed/) { } 0 0 0
334 6 return "LWP::Authen::OAuth2::ServiceProvider::$short_name";
335 }
336 elsif ($@ =~ /Compilation failed/) {
337 0 confess($@);
338 }
339 else {
340 0 eval {
341 0 load($short_name);
342 };
343 0 ▶ if (not $@) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $@) { } 0 0 0
elsif ($@ =~ /Compilation failed/) { } 0 0 0
344 0 return $short_name;
345 }
346 elsif ($@ =~ /Compilation failed/) {
347 0 confess($@);
348 }
349 else {
350 0 croak("Service provider '$short_name' not found");
351 }
352 }
353 }
354  
355 # DEFAULTS (can be overridden)
356 sub authorization_endpoint {
357 0 ▶ my $self = shift;
Subroutine authorization_endpoint: not called CC=1 SCAR=6.9
Pod documented
358 0 return $self->{"authorization_endpoint"};
359 }
360  
361 sub token_endpoint {
362 0 ▶ my $self = shift;
Subroutine token_endpoint: not called CC=1 SCAR=6.9
Pod documented
363 0 return $self->{"token_endpoint"};
364 }
365  
366 # DEFAULTS (should be overridden)
367 sub required_init {
368 4 ▶ return qw(client_id client_secret);
Subroutine required_init: called CC=1 SCAR=0.0
Pod documented
369 }
370  
371 sub optional_init {
372 5 ▶ return qw(redirect_uri scope);
Subroutine optional_init: called CC=1 SCAR=0.0
Pod documented
373 }
374  
375 sub authorization_required_params {
376 4 ▶ return qw(response_type client_id);
Subroutine authorization_required_params: called CC=1 SCAR=0.0
Pod undocumented
377 }
378  
379 sub authorization_optional_params {
380 6 ▶ return qw(redirect_uri state scope);
Subroutine authorization_optional_params: called CC=1 SCAR=0.0
Pod undocumented
381 }
382  
383 sub authorization_default_params {
384 4 ▶ return qw(response_type code);
Subroutine authorization_default_params: called CC=1 SCAR=0.0
Pod undocumented
385 }
386  
387 sub request_required_params {
388 4 ▶ return qw(grant_type client_id client_secret code);
Subroutine request_required_params: called CC=1 SCAR=0.0
Pod undocumented
389 }
390  
391 sub request_optional_params {
392 6 ▶ return qw(state redirect_uri);
Subroutine request_optional_params: called CC=1 SCAR=0.0
Pod undocumented
393 }
394  
395 sub request_default_params {
396 4 ▶ return qw(grant_type authorization_code);
Subroutine request_default_params: called CC=1 SCAR=0.0
Pod undocumented
397 }
398  
399 sub refresh_required_params {
400 5 ▶ return qw(grant_type refresh_token client_id client_secret);
Subroutine refresh_required_params: called CC=1 SCAR=0.0
Pod undocumented
401 }
402  
403 sub refresh_optional_params {
404 6 ▶ return qw();
Subroutine refresh_optional_params: called CC=1 SCAR=0.0
Pod undocumented
405 }
406  
407 sub refresh_default_params {
408 5 ▶ return qw(grant_type refresh_token);
Subroutine refresh_default_params: called CC=1 SCAR=0.0
Pod undocumented
409 }
410  
411  
412 12 1
413  
414 __END__
« lib/LWP/Authen/OAuth2.pm ↑ Summary blib/lib/LWP/Authen/OAuth2/Args.pm »