OAuth2/ServiceProvider.pm

filter: Statementstmt 50.0% 91 / 182 Branchbran 19.3% 12 / 62 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 77.7% 28 / 36 Podpod 38.4% 10 / 26 totaltotal 44.7% 141 / 315 CC 42 SCAR 36.1
CC
42
Cov
41%
construct_tokens
132.0
collect_action_params
71.7
can_refresh_tokens
30.0
CRAP 36.9
↑ Summary OAuth2.pm »
1 package LWP::Authen::OAuth2::ServiceProvider;
2  
3 # ABSTRACT: ServiceProvider base class
4 12 our $VERSION = '0.20'; # 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(
Subroutine BEGIN: called CC=1 SCAR=0.0
19 extract_option copy_option assert_options_empty
20 12 );
21  
22 # Construct a new object.
23 8 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
24 8 my ($class, $opts) = @_;
25  
26 # I start as an empty hashref.
27 8 my $self = {};
28  
29 # But what class am I supposed to actually be?
30 8 ▶ if (not exists $opts->{service_provider}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opts->{'service_provider'}) { } 2 6 8
31 2 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 8 $self->init($opts);
45 }
46  
47 8 ▶sub init {
Subroutine init: called CC=6 SCAR=18.3
Pod documented
48 8 my ($self, $opts) = @_;
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 8 ▶ $self->copy_option($opts, 'use_test_urls') if defined $opts->{use_test_urls};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"use_test_urls"} 0 8 8
55  
56 # These are required, NOT provided by this class, but are by subclasses.
57 8 for my $field (qw(token_endpoint authorization_endpoint)) {
58 16 ▶ if ($self->can($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($field)) { } 16 0 16
59 16 $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 8 for my $field (
68 qw(required_init optional_init),
69 map {
70 24 ("$_\_required_params", "$_\_optional_params")
71 } qw(authorization request refresh)
72 ) {
73 64 $self->copy_option($opts, $field, [$self->$field]);
74 }
75  
76 # And hashrefs for default key/value pairs.
77 8 for my $field (
78 map "$_\_default_params", qw(authorization request refresh)
79 ) {
80 24 $self->copy_option($opts, $field, {$self->$field});
81 }
82  
83 8 return $self;
84 }
85  
86 0 ▶sub authorization_url {
Subroutine authorization_url: not called CC=1 SCAR=6.9
Pod undocumented
87 0 my ($self, $oauth2, @rest) = @_;
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 0 ▶sub request_tokens {
Subroutine request_tokens: not called CC=1 SCAR=6.9
Pod undocumented
96 0 my ($self, $oauth2, @rest) = @_;
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 0 ▶sub can_refresh_tokens {
Subroutine can_refresh_tokens: not called CC=5 SCAR=34.0
Pod undocumented
103 0 my ($self, $oauth2, %opt) = @_;
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 0 ▶sub refreshed_tokens {
Subroutine refreshed_tokens: not called CC=1 SCAR=6.9
Pod undocumented
121 0 my ($self, $oauth2, @rest) = @_;
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 1 ▶sub collect_action_params {
Subroutine collect_action_params: called CC=15 SCAR=42.7
Pod documented
129 1 my $self = shift;
130 1 my $action = shift;
131 1 my $oauth2 = shift;
132 1 my $oauth2_args = $oauth2->for_service_provider;
133 1 my @rest = @_;
134 1 my $opt = {@_};
135  
136 1 my $default = $self->{"$action\_default_params"};
137  
138 1 ▶ if ($oauth2->is_strict) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oauth2->is_strict) { } 0 1 1
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 map {
191 6 ▶ exists $oauth2_args->{$_} ? ($_, $oauth2_args->{$_}) : ()
Branch: 1 branch
BranchTrueFalseTotal
exists $oauth2_args->{$_} ? : 3 3 6
192 1 } @{ $self->{"$action\_required_params"} },
193 1 @{ $self->{"$action\_optional_params"} }
194 ),
195 %$opt
196 };
197 1 for my $key (keys %$result) {
198 4 ▶ if (not defined($result->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result->{$key}) 1 3 4
199 1 delete $result->{$key};
200 }
201 }
202 1 return $result;
203 }
204 }
205  
206 0 ▶sub post_to_token_endpoint {
Subroutine post_to_token_endpoint: not called CC=1 SCAR=6.9
Pod documented
207 0 my ($self, $oauth2, $param) = @_;
208 0 my $ua = $oauth2->user_agent();
209 0 return $ua->post($self->token_endpoint(), [%$param]);
210 }
211  
212 0 ▶sub api_url_base { return '' } # override in subclass
Subroutine api_url_base: not called CC=1 SCAR=6.9
Pod undocumented
213  
214 0 ▶sub access_token_class {
Subroutine access_token_class: not called CC=2 SCAR=17.9
Pod documented
215 0 my ($self, $type) = @_;
216  
217 0 ▶ if ("bearer" eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ('bearer' eq $type) { } 0 0 0
218 0 return "LWP::Authen::OAuth2::AccessToken::Bearer";
219 }
220 else {
221 0 return "Token type '$type' not yet implemented";
222 }
223 }
224  
225 # Attempts to construct tokens, returns the access_token (which may have a
226 # request token embedded).
227 0 ▶sub construct_tokens {
Subroutine construct_tokens: not called CC=11 SCAR=48.8
Pod undocumented
228 0 my ($self, $oauth2, $response) = @_;
229  
230 # The information that I need.
231 0 my $content = eval {$response->decoded_content};
232 0 ▶ if (not defined($content)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 0 0 0
233 0 $content = '';
234 }
235 0 my $data = eval {decode_json($content)};
236 0 my $parse_error = $@;
237 0 my $token_endpoint = $self->token_endpoint();
238  
239 # Can this have done wrong? Let me list the ways...
240 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
241 # "Should not happen", hopefully just network.
242 # Tell the programmer everything.
243 0 my $status = $response->status_line;
244 return <<"EOT"
245 Token endpoint gave invalid JSON in response.
246  
247 Endpoint: $token_endpoint
248 Status: $status
249 Parse error: $parse_error
250 JSON:
251 $content
252 EOT
253 0 }
254 elsif ($data->{error}) {
255 # Assume a valid OAuth 2 error message.
256 0 my $message = "OAuth2 error: $data->{error}";
257  
258 # Do we have a mythical service provider that gives us more?
259 0 ▶ if ($data->{error_uri}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"error_uri"}) 0 0 0
260 # They seem to have a web page with detail.
261 0 $message .= "\n$data->{error_uri} may say more.\n";
262 }
263  
264 0 ▶ if ($data->{error_description}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"error_description"}) 0 0 0
265 # Wow! Thank you!
266 0 $message .= "\n\nDescription: $data->{error_description}\n";
267 }
268 0 return $message;
269 }
270 elsif (not $data->{token_type}) {
271 # Someone failed to follow the spec...
272 0 return <<"EOT";
273 Token endpoint missing expected token_type in successful response.
274  
275 Endpoint: $token_endpoint
276 JSON:
277 $content
278 EOT
279 }
280  
281 0 my $type = $self->access_token_class(lc($data->{token_type}));
282 0 ▶ if ($type !~ /^[\w\:]+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /^[\w\:]+\z/) 0 0 0
283 # We got an error. :-(
284 0 return $type;
285 }
286  
287 0 eval {load($type)};
288 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
289 # MAKE THIS FATAL. (Clearly Perl code is simply wrong.)
290 0 confess("Loading $type for $data->{token_type} gave error: $@");
291 }
292  
293 # Try to make an access token.
294 0 my $access_token = $type->from_ref($data);
295  
296 0 ▶ if (not ref($access_token)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $access_token) { } 0 0 0
297 # This should be an error message of some sort.
298 0 return $access_token;
299 }
300 else {
301 # WE SURVIVED! EVERYTHING IS GOOD!
302 0 ▶ if ($oauth2->access_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oauth2->access_token) 0 0 0
303 0 $access_token->copy_refresh_from($oauth2->access_token);
304 }
305 0 return $access_token;
306 }
307 }
308  
309 # Override for your client_types if you have multiple.
310 5 ▶sub client_type_class {
Subroutine client_type_class: called CC=2 SCAR=7.6
Pod documented
311 5 my ($class, $name) = @_;
312 5 ▶ if ("default" eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ('default' eq $name) { } 5 0 5
313 5 return $class;
314 }
315 else {
316 0 croak("Flow '$name' not defined for '$class'");
317 }
318 }
319  
320 # Override should you need the front-end LWP::Authen::OAuth object to have
321 # methods for service provider specific functionality.
322 #
323 # This is not expected to be a common need.
324 7 ▶sub oauth2_class {
Subroutine oauth2_class: called CC=1 SCAR=0.0
Pod documented
325 7 return "LWP::Authen::OAuth2";
326 }
327  
328 12 memoize("service_provider_class");
329 6 ▶sub service_provider_class {
Subroutine service_provider_class: called CC=5 SCAR=26.1
330 6 my $short_name = shift;
331 6 eval {
332 6 load("LWP::Authen::OAuth2::ServiceProvider::$short_name");
333 };
334 6 ▶ if (not $@) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $@) { } 6 0 6
elsif ($@ =~ /Compilation failed/) { } 0 0 0
335 6 return "LWP::Authen::OAuth2::ServiceProvider::$short_name";
336 }
337 elsif ($@ =~ /Compilation failed/) {
338 0 confess($@);
339 }
340 else {
341 0 eval {
342 0 load($short_name);
343 };
344 0 ▶ if (not $@) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $@) { } 0 0 0
elsif ($@ =~ /Compilation failed/) { } 0 0 0
345 0 return $short_name;
346 }
347 elsif ($@ =~ /Compilation failed/) {
348 0 confess($@);
349 }
350 else {
351 0 croak("Service provider '$short_name' not found");
352 }
353 }
354 }
355  
356 # DEFAULTS (can be overridden)
357 2 ▶sub authorization_endpoint {
Subroutine authorization_endpoint: called CC=1 SCAR=0.0
Pod documented
358 2 my $self = shift;
359 2 return $self->{"authorization_endpoint"};
360 }
361  
362 2 ▶sub token_endpoint {
Subroutine token_endpoint: called CC=1 SCAR=0.0
Pod documented
363 2 my $self = shift;
364 2 return $self->{"token_endpoint"};
365 }
366  
367 # DEFAULTS (should be overridden)
368 6 ▶sub required_init {
Subroutine required_init: called CC=1 SCAR=0.0
Pod documented
369 6 return qw(client_id client_secret);
370 }
371  
372 7 ▶sub optional_init {
Subroutine optional_init: called CC=1 SCAR=0.0
Pod documented
373 7 return qw(redirect_uri scope);
374 }
375  
376 6 ▶sub authorization_required_params {
Subroutine authorization_required_params: called CC=1 SCAR=0.0
Pod undocumented
377 6 return qw(response_type client_id);
378 }
379  
380 8 ▶sub authorization_optional_params {
Subroutine authorization_optional_params: called CC=1 SCAR=0.0
Pod undocumented
381 8 return qw(redirect_uri state scope);
382 }
383  
384 6 ▶sub authorization_default_params {
Subroutine authorization_default_params: called CC=1 SCAR=0.0
Pod undocumented
385 6 return qw(response_type code);
386 }
387  
388 6 ▶sub request_required_params {
Subroutine request_required_params: called CC=1 SCAR=0.0
Pod undocumented
389 6 return qw(grant_type client_id client_secret code);
390 }
391  
392 8 ▶sub request_optional_params {
Subroutine request_optional_params: called CC=1 SCAR=0.0
Pod undocumented
393 8 return qw(state redirect_uri);
394 }
395  
396 6 ▶sub request_default_params {
Subroutine request_default_params: called CC=1 SCAR=0.0
Pod undocumented
397 6 return qw(grant_type authorization_code);
398 }
399  
400 7 ▶sub refresh_required_params {
Subroutine refresh_required_params: called CC=1 SCAR=0.0
Pod undocumented
401 7 return qw(grant_type refresh_token client_id client_secret);
402 }
403  
404 8 ▶sub refresh_optional_params {
Subroutine refresh_optional_params: called CC=1 SCAR=0.0
Pod undocumented
405 8 return qw();
406 }
407  
408 7 ▶sub refresh_default_params {
Subroutine refresh_default_params: called CC=1 SCAR=0.0
Pod undocumented
409 7 return qw(grant_type refresh_token);
410 }
411  
412  
413 12 1
414  
415 __END__
↑ Summary OAuth2.pm »