blib/lib/OAuth/Lite2/Client/WebServer.pm

filter: Statementstmt 86.2% 119 / 138 Branchbran 53.8% 14 / 26 Conditioncond 34.6% 9 / 26 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 86.2% 25 / 29 Podpod 85.7% 6 / 7 totaltotal 72.3% 175 / 242 SCAR 35.1
CC
24
Cov
75%
uri_to_redirect
72.0
get_access_token
7.1
refresh_access_token
6.1
CRAP 33.3
« blib/lib/OAuth/Lite2/Agent/Strict.pm ↑ Summary blib/lib/OAuth/Lite2/Client/ExternalService.pm »
1 package OAuth::Lite2::Client::WebServer;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use base 'Class::ErrorHandler';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use bytes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Try::Tiny qw/try catch/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Params::Validate qw(HASHREF);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use OAuth::Lite2;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use OAuth::Lite2::Util qw(build_content);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use OAuth::Lite2::Client::TokenResponseParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use OAuth::Lite2::Client::StateResponseParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 OAuth::Lite2::Client::WebServer - OAuth 2.0 Web Server Profile Client
23  
24 =head1 SYNOPSIS
25  
26 my $client = OAuth::Lite2::Client::WebServer->new(
27 id => q{my_client_id},
28 secret => q{my_client_secret},
29 authorize_uri => q{http://example.org/authorize},
30 access_token_uri => q{http://example.org/token},
31 );
32  
33 # redirect user to authorize page.
34 sub start_authorize {
35 my $your_app = shift;
36 my $redirect_url = $client->uri_to_redirect(
37 redirect_uri => q{http://yourapp/callback},
38 scope => q{photo},
39 state => q{optional_state},
40 );
41  
42 $your_app->res->redirect( $redirect_url );
43 }
44  
45 # this method corresponds to the url 'http://yourapp/callback'
46 sub callback {
47 my $your_app = shift;
48  
49 my $code = $your_app->request->param("code");
50  
51 my $access_token = $client->get_access_token(
52 code => $code,
53 redirect_uri => q{http://yourapp/callback},
54 ) or return $your_app->error( $client->errstr );
55  
56 $your_app->store->save( access_token => $access_token->access_token );
57 $your_app->store->save( expires_at => time() + $access_token->expires_in );
58 $your_app->store->save( refresh_token => $access_token->refresh_token );
59 }
60  
61 sub refresh_access_token {
62 my $your_app = shift;
63  
64 my $access_token = $client->refresh_access_token(
65 refresh_token => $refresh_token,
66 ) or return $your_app->error( $client->errstr );
67  
68 $your_app->store->save( access_token => $access_token->access_token );
69 $your_app->store->save( expires_at => time() + $access_token->expires_in );
70 $your_app->store->save( refresh_token => $access_token->refresh_token );
71 }
72  
73  
74 sub access_to_protected_resource {
75 my $your_app = shift;
76  
77 my $access_token = $your_app->store->get("access_token");
78 my $expires_at = $your_app->store->get("expires_at");
79 my $refresh_token = $your_app->store->get("refresh_token");
80  
81 unless ($access_token) {
82 $your_app->start_authorize();
83 return;
84 }
85  
86 if ($expires_at < time()) {
87 $your_app->refresh_access_token();
88 return;
89 }
90  
91 my $req = HTTP::Request->new( GET => q{http://example.org/photo} );
92 $req->header( Authorization => sprintf(q{OAuth %s}, $access_token) );
93 my $agent = LWP::UserAgent->new;
94 my $res = $agent->request($req);
95 ...
96 }
97  
98  
99 =head1 DESCRIPTION
100  
101 Client library for OAuth 2.0 Web Server Profile.
102  
103 =head1 METHODS
104  
105 =head2 new( %params )
106  
107 =over 4
108  
109 =item id
110  
111 Client ID
112  
113 =item secret
114  
115 Client secret
116  
117 =item authorize_uri
118  
119 authorization page uri on auth-server.
120  
121 =item access_token_uri
122  
123 token endpoint uri on auth-server.
124  
125 =item refresh_token_uri
126  
127 refresh-token endpoint uri on auth-server.
128 if you omit this, access_token_uri is used instead.
129  
130 =item agent
131  
132 user agent. if you omit this, LWP::UserAgent's object is set by default.
133 You can use your custom agent or preset-agents.
134  
135 See also
136  
137 L<OAuth::Lite2::Agent::Dump>
138 L<OAuth::Lite2::Agent::Strict>
139 L<OAuth::Lite2::Agent::PSGIMock>
140  
141 =back
142  
143 =cut
144  
145 sub new {
146  
147 4 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.7
Pod documented
148  
149 4 my %args = Params::Validate::validate(@_, {
150 id => 1,
151 secret => 1,
152 # format => { optional => 1 },
153 authorize_uri => { optional => 1 },
154 access_token_uri => { optional => 1 },
155 refresh_token_uri => { optional => 1 },
156 agent => { optional => 1 },
157 });
158  
159 4 my $self = bless {
160 id => undef,
161 secret => undef,
162 authorize_uri => undef,
163 access_token_uri => undef,
164 refresh_token_uri => undef,
165 last_request => undef,
166 last_response => undef,
167 %args,
168 }, $class;
169  
170 4 ▶ unless ($self->{agent}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"agent"}) 0 4 4
171 0 $self->{agent} = LWP::UserAgent->new;
172 $self->{agent}->agent(
173 0 join "/", __PACKAGE__, $OAuth::Lite2::VERSION);
174 }
175  
176 4 ▶ $self->{format} ||= 'json';
Condition50%1/2
$self->{"format"} ||= "json"
l!l
04
MC/DC0%0/1
$self->{"format"} ||= "json"
$self->{"format"}
Truth table50%1/2
A
A= $self->{"format"}
Aresult
11
00
177 4 $self->{response_parser} = OAuth::Lite2::Client::TokenResponseParser->new;
178 4 $self->{state_response_parser} = OAuth::Lite2::Client::StateResponseParser->new;
179  
180 4 return $self;
181 }
182  
183 =head2 uri_to_redirect( %params )
184  
185 =cut
186  
187 sub uri_to_redirect {
188 0 ▶ my $self = shift;
Subroutine uri_to_redirect: not called CC=8 SCAR=42.8
Pod documented
189 0 my %args = Params::Validate::validate(@_, {
190 redirect_uri => 1,
191 state => { optional => 1 },
192 scope => { optional => 1 },
193 immediate => { optional => 1 },
194 uri => { optional => 1 },
195 extra => { optional => 1, type => HASHREF },
196 });
197  
198 my %params = (
199 response_type => 'code',
200 client_id => $self->{id},
201 redirect_uri => $args{redirect_uri},
202 0 );
203 0 ▶ $params{state} = $args{state} if $args{state};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"state"} 0 0 0
204 0 ▶ $params{scope} = $args{scope} if $args{scope};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"scope"} 0 0 0
205 0 ▶ $params{immediate} = $args{immediate} if $args{immediate};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"immediate"} 0 0 0
206  
207 0 ▶ if ($args{extra}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"extra"}) 0 0 0
208 0 for my $key ( keys %{$args{extra}} ) {
209 0 $params{$key} = $args{extra}{$key};
210 }
211 }
212  
213 my $uri = $args{uri}
214 || $self->{authorize_uri}
215 0 ▶ || Carp::croak "uri not found";
Condition0%0/6
$args{'uri'} || $self->{'authorize_uri'} || Carp::croak('uri not found')
l!l&&r!l&&!r
000
$args{"uri"} or $self->{"authorize_uri"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$args{'uri'} || $self->{'authorize_uri'} || Carp::croak('uri not found')
$args{"uri"} $self->{"authorize_uri"} Carp::croak('uri not found')
Truth table0%0/4
A or B || C
A= $args{"uri"}B= $self->{"authorize_uri"}C= Carp::croak('uri not found')
ABCresult
1XX1
01X1
0011
0000
216  
217 0 $uri = URI->new($uri);
218 0 $uri->query_form(%params);
219 0 return $uri->as_string;
220 }
221  
222 =head2 get_access_token( %params )
223  
224 execute verification,
225 and returns L<OAuth::Lite2::Client::Token> object.
226  
227 =over 4
228  
229 =item code
230  
231 Authorization-code that is issued beforehand by server
232  
233 =item redirect_uri
234  
235 The URL that has used for user authorization's callback
236  
237 =back
238  
239 =cut
240  
241 sub get_access_token {
242 15 ▶ my $self = shift;
Subroutine get_access_token: called CC=7 SCAR=19.5
Pod documented
243  
244 15 my %args = Params::Validate::validate(@_, {
245 code => 1,
246 redirect_uri => 1,
247 server_state => { optional => 1 },
248 uri => { optional => 1 },
249 use_basic_schema => { optional => 1 },
250 # secret_type => { optional => 1 },
251 # format => { optional => 1 },
252 });
253  
254 15 ▶ unless (exists $args{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"uri"}) 15 0 15
255 $args{uri} = $self->{access_token_uri}
256 15 ▶ || Carp::croak "uri not found";
Condition33%1/3
$self->{'access_token_uri'} || Carp::croak('uri not found')
l!l&&r!l&&!r
1500
MC/DC0%0/2
$self->{'access_token_uri'} || Carp::croak('uri not found')
$self->{'access_token_uri'} Carp::croak('uri not found')
Truth table33%1/3
A || B
A= $self->{'access_token_uri'}B= Carp::croak('uri not found')
ABresult
1X1
011
000
257 }
258  
259 # $args{format} ||= $self->{format};
260  
261 my %params = (
262 grant_type => 'authorization_code',
263 code => $args{code},
264 redirect_uri => $args{redirect_uri},
265 # format => $args{format},
266 15 );
267 15 ▶ $params{server_state} = $args{server_state} if $args{server_state};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"server_state"} 2 13 15
268  
269 15 ▶ unless ($args{use_basic_schema}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"use_basic_schema"}) 9 6 15
270 9 $params{client_id} = $self->{id};
271 9 $params{client_secret} = $self->{secret};
272 }
273  
274 # $params{secret_type} = $args{secret_type}
275 # if $args{secret_type};
276  
277 15 my $content = build_content(\%params);
278 15 my $headers = HTTP::Headers->new;
279 15 $headers->header("Content-Type" => q{application/x-www-form-urlencoded});
280 15 $headers->header("Content-Length" => bytes::length($content));
281 $headers->authorization_basic($self->{id}, $self->{secret})
282 15 ▶ if($args{use_basic_schema});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"use_basic_schema"} 6 9 15
283 15 my $req = HTTP::Request->new( POST => $args{uri}, $headers, $content );
284  
285 15 my $res = $self->{agent}->request($req);
286 15 $self->{last_request} = $req;
287 15 $self->{last_response} = $res;
288  
289 15 my ($token, $errmsg);
290 try {
291 15 ▶ $token = $self->{response_parser}->parse($res);
Subroutine __ANON__: called CC=1 SCAR=0.0
292 } catch {
293 12 ▶ $errmsg = "$_";
Subroutine __ANON__: called CC=1 SCAR=0.0
294 15 };
295 15 ▶ return $token || $self->error($errmsg);
Condition66%2/3
$token || $self->error($errmsg)
l!l&&r!l&&!r
3012
MC/DC50%1/2
$token || $self->error($errmsg)
$token $self->error($errmsg)
Truth table66%2/3
A || B
A= $tokenB= $self->error($errmsg)
ABresult
1X1
011
000
296 }
297  
298 =head2 refresh_access_token( %params )
299  
300 Refresh access token by refresh_token,
301 returns L<OAuth::Lite2::Client::Token> object.
302  
303 =over 4
304  
305 =item refresh_token
306  
307 =back
308  
309 =cut
310  
311 sub refresh_access_token {
312 4 ▶ my $self = shift;
Subroutine refresh_access_token: called CC=6 SCAR=18.0
Pod documented
313  
314 4 my %args = Params::Validate::validate(@_, {
315 refresh_token => 1,
316 # secret_type => { optional => 1 },
317 # format => { optional => 1 },
318 uri => { optional => 1 },
319 use_basic_schema => { optional => 1 },
320 });
321  
322 4 ▶ unless (exists $args{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"uri"}) 4 0 4
323 $args{uri} = $self->{access_token_uri}
324 4 ▶ || Carp::croak "uri not found";
Condition33%1/3
$self->{'access_token_uri'} || Carp::croak('uri not found')
l!l&&r!l&&!r
400
MC/DC0%0/2
$self->{'access_token_uri'} || Carp::croak('uri not found')
$self->{'access_token_uri'} Carp::croak('uri not found')
Truth table33%1/3
A || B
A= $self->{'access_token_uri'}B= Carp::croak('uri not found')
ABresult
1X1
011
000
325 }
326  
327 # $args{format} ||= $self->{format};
328  
329 my %params = (
330 grant_type => 'refresh_token',
331 refresh_token => $args{refresh_token},
332 # format => $args{format},
333 4 );
334  
335 4 ▶ unless ($args{use_basic_schema}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"use_basic_schema"}) 2 2 4
336 2 $params{client_id} = $self->{id};
337 2 $params{client_secret} = $self->{secret};
338 }
339  
340 # $params{secret_type} = $args{secret_type}
341 # if $args{secret_type};
342  
343 4 my $content = build_content(\%params);
344 4 my $headers = HTTP::Headers->new;
345 4 $headers->header("Content-Type" => q{application/x-www-form-urlencoded});
346 4 $headers->header("Content-Length" => bytes::length($content));
347 $headers->authorization_basic($self->{id}, $self->{secret})
348 4 ▶ if($args{use_basic_schema});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"use_basic_schema"} 2 2 4
349 4 my $req = HTTP::Request->new( POST => $args{uri}, $headers, $content );
350  
351 4 my $res = $self->{agent}->request($req);
352 4 $self->{last_request} = $req;
353 4 $self->{last_response} = $res;
354  
355 4 my ($token, $errmsg);
356 try {
357 4 ▶ $token = $self->{response_parser}->parse($res);
Subroutine __ANON__: called CC=1 SCAR=0.0
358 } catch {
359 2 ▶ $errmsg = "$_";
Subroutine __ANON__: called CC=1 SCAR=0.0
360 4 };
361 4 ▶ return $token || $self->error($errmsg);
Condition66%2/3
$token || $self->error($errmsg)
l!l&&r!l&&!r
202
MC/DC50%1/2
$token || $self->error($errmsg)
$token $self->error($errmsg)
Truth table66%2/3
A || B
A= $tokenB= $self->error($errmsg)
ABresult
1X1
011
000
362  
363 }
364  
365 =head2 get_server_state
366  
367 Obtain L<OAuth::Lite2::Client::ServerState> object.
368  
369 =cut
370  
371 sub get_server_state {
372 1 ▶ my $self = shift;
Subroutine get_server_state: called CC=4 SCAR=14.3
Pod documented
373  
374 1 my %args = Params::Validate::validate(@_, {
375 uri => { optional => 1 },
376 });
377  
378 1 ▶ unless (exists $args{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"uri"}) 1 0 1
379 $args{uri} = $self->{access_token_uri}
380 1 ▶ || Carp::croak "uri not found";
Condition33%1/3
$self->{'access_token_uri'} || Carp::croak('uri not found')
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{'access_token_uri'} || Carp::croak('uri not found')
$self->{'access_token_uri'} Carp::croak('uri not found')
Truth table33%1/3
A || B
A= $self->{'access_token_uri'}B= Carp::croak('uri not found')
ABresult
1X1
011
000
381 }
382  
383 my %params = (
384 grant_type => 'server_state',
385 client_id => $self->{id},
386 1 );
387  
388 1 my $content = build_content(\%params);
389 1 my $headers = HTTP::Headers->new;
390 1 $headers->header("Content-Type" => q{application/x-www-form-urlencoded});
391 1 $headers->header("Content-Length" => bytes::length($content));
392 1 my $req = HTTP::Request->new( POST => $args{uri}, $headers, $content );
393  
394 1 my $res = $self->{agent}->request($req);
395 1 $self->{last_request} = $req;
396 1 $self->{last_response} = $res;
397  
398 1 my ($state, $errmsg);
399 try {
400 1 ▶ $state = $self->{state_response_parser}->parse($res);
Subroutine __ANON__: called CC=1 SCAR=0.0
401 } catch {
402 0 ▶ $errmsg = "$_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
403 1 };
404 1 ▶ return $state || $self->error($errmsg);
Condition33%1/3
$state || $self->error($errmsg)
l!l&&r!l&&!r
100
MC/DC0%0/2
$state || $self->error($errmsg)
$state $self->error($errmsg)
Truth table33%1/3
A || B
A= $stateB= $self->error($errmsg)
ABresult
1X1
011
000
405  
406 }
407  
408 =head2 last_request
409  
410 Returns a HTTP::Request object that is used
411 when you obtain or refresh access token last time internally.
412  
413 =head2 last_request
414  
415 Returns a HTTP::Response object that is used
416 when you obtain or refresh access token last time internally.
417  
418 =cut
419  
420 0 ▶sub last_request { $_[0]->{last_request} }
Subroutine last_request: not called CC=1 SCAR=6.9
Pod documented
421 0 ▶sub last_response { $_[0]->{last_response} }
Subroutine last_response: not called CC=1 SCAR=6.9
Pod undocumented
422  
423 =head1 AUTHOR
424  
425 Ryo Ito, E<lt>ritou.06@gmail.comE<gt>
426  
427 Lyo Kato, E<lt>lyo.kato@gmail.comE<gt>
428  
429 =head1 COPYRIGHT AND LICENSE
430  
431 Copyright (C) 2010 by Lyo Kato
432  
433 This library is free software; you can redistribute it and/or modify
434 it under the same terms as Perl itself, either Perl version 5.8.8 or,
435 at your option, any later version of Perl 5 you may have available.
436  
437 =cut
438  
439 3 1;
« blib/lib/OAuth/Lite2/Agent/Strict.pm ↑ Summary blib/lib/OAuth/Lite2/Client/ExternalService.pm »