OAuth/Lite2/Client/ExternalService.pm

filter: Statementstmt 74.2% 75 / 101 Branchbran 50.0% 11 / 22 Conditioncond 30.0% 3 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 78.2% 18 / 23 Podpod 100% 5 / 5 totaltotal 67.6% 113 / 167 CC 16 SCAR 23.1
CC
16
Cov
67%
refresh_access_token
42.0
get_access_token
10.2
new
2.1
CRAP 10.1
« OAuth/Lite2/Server/GrantHandler/ExternalService.pm ↑ Summary OAuth/Lite2/Server/GrantHandler/Password.pm »
1 package OAuth::Lite2::Client::ExternalService;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use base 'Class::ErrorHandler';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use bytes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Try::Tiny qw/try catch/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Params::Validate qw(HASHREF);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use OAuth::Lite2;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use OAuth::Lite2::Util qw(build_content);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use OAuth::Lite2::Client::TokenResponseParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 OAuth::Lite2::Client::ExternalService - OAuth 2.0 Federated Assertion Profile Client
21  
22 =head1 SYNOPSIS
23  
24 my $client = OAuth::Lite2::Client::ExternalService->new(
25 id => q{my_client_id},
26 secret => q{my_client_secret},
27 access_token_uri => q{http://example.org/token},
28 );
29  
30 sub get_access_token {
31 my $your_app = shift;
32  
33 my $access_token = $client->get_access_token(
34 assertion => $your_app->request->param("assertion"),
35 scope => q{photo},
36 ) or return $your_app->error( $client->errstr );
37  
38 $your_app->store->save( access_token => $access_token->access_token );
39 $your_app->store->save( expires_at => time() + $access_token->expires_in );
40 $your_app->store->save( refresh_token => $access_token->refresh_token );
41 }
42  
43 sub refresh_access_token {
44 my $your_app = shift;
45  
46 my $access_token = $client->refresh_access_token(
47 refresh_token => $refresh_token,
48 ) or return $your_app->error( $client->errstr );
49  
50 $your_app->store->save( access_token => $access_token->access_token );
51 $your_app->store->save( expires_at => time() + $access_token->expires_in );
52 $your_app->store->save( refresh_token => $access_token->refresh_token );
53 }
54  
55 sub access_to_protected_resource {
56 my $your_app = shift;
57  
58 my $access_token = $your_app->store->get("access_token");
59 my $expires_at = $your_app->store->get("expires_at");
60 my $refresh_token = $your_app->store->get("refresh_token");
61  
62 unless ($access_token) {
63 $your_app->show_reauthorize_page();
64 return;
65 }
66  
67 if ($expires_at < time()) {
68 $your_app->refresh_access_token();
69 return;
70 }
71  
72 my $req = HTTP::Request->new( GET => q{http://example.org/photo} );
73 $req->header( Authorization => sprintf(q{OAuth %s}, $access_token) );
74 my $agent = LWP::UserAgent->new;
75 my $res = $agent->request($req);
76 ...
77 }
78  
79  
80 =head1 DESCRIPTION
81  
82 OAuth 2.0 Federated Assertion Profile Client.
83  
84  
85 =head2 new( %params )
86  
87 =over 4
88  
89 =item id
90  
91 Client ID
92  
93 =item secret
94  
95 Client secret
96  
97 =item access_token_uri
98  
99 token endpoint uri on auth-server.
100  
101 =item refresh_token_uri
102  
103 refresh-token endpoint uri on auth-server.
104 if you omit this, access_token_uri is used instead.
105  
106 =item agent
107  
108 user agent. if you omit this, LWP::UserAgent's object is set by default.
109 You can use your custom agent or preset-agents.
110  
111 See also
112  
113 L<OAuth::Lite2::Agent::Dump>
114 L<OAuth::Lite2::Agent::Strict>
115 L<OAuth::Lite2::Agent::PSGIMock>
116  
117 =back
118  
119 =cut
120  
121 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.5
Pod documented
122 1 my $class = shift;
123  
124 1 my %args = Params::Validate::validate(@_, {
125 id => 1,
126 secret => 1,
127 access_token_uri => { optional => 1 },
128 refresh_token_uri => { optional => 1 },
129 agent => { optional => 1 },
130 });
131  
132 1 my $self = bless {
133 id => undef,
134 secret => undef,
135 access_token_uri => undef,
136 refresh_token_uri => undef,
137 last_request => undef,
138 last_response => undef,
139 %args,
140 }, $class;
141  
142 1 ▶ unless ($self->{agent}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"agent"}) 0 1 1
143 0 $self->{agent} = LWP::UserAgent->new;
144 $self->{agent}->agent(
145 0 join "/", __PACKAGE__, $OAuth::Lite2::VERSION);
146 }
147  
148 # $self->{format} ||= 'json';
149 1 $self->{response_parser} = OAuth::Lite2::Client::TokenResponseParser->new;
150  
151 1 return $self;
152 }
153  
154 =head2 get_access_token( %params )
155  
156 =over 4
157  
158 =item assertion
159  
160 =item type
161  
162 =item iss
163  
164 =item aud
165  
166 =item scope
167  
168 =back
169  
170 =cut
171  
172 12 ▶sub get_access_token {
Subroutine get_access_token: called CC=10 SCAR=23.3
Pod documented
173 12 my $self = shift;
174  
175 12 my %args = Params::Validate::validate(@_, {
176 assertion => 1,
177 type => { optional => 1 },
178 iss => { optional => 1 },
179 aud => { optional => 1 },
180 scope => { optional => 1 },
181 uri => { optional => 1 },
182 use_basic_schema => { optional => 1 },
183 });
184  
185 12 ▶ unless (exists $args{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"uri"}) 12 0 12
186 $args{uri} = $self->{access_token_uri}
187 12 ▶ || Carp::croak "uri not found";
Condition50%1/2
$self->{'access_token_uri'} || Carp::croak('uri not found')
l!l
120
MC/DC0%0/1
$self->{'access_token_uri'} || Carp::croak('uri not found')
$self->{'access_token_uri'}
Truth table50%1/2
A
A= $self->{'access_token_uri'}
Aresult
11
00
188 }
189  
190 my %params = (
191 grant_type => 'external_service',
192 assertion => $args{assertion},
193 12 );
194  
195 12 ▶ unless ($args{use_basic_schema}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"use_basic_schema"}) 12 0 12
196 12 $params{client_id} = $self->{id};
197 12 $params{client_secret} = $self->{secret};
198 }
199  
200 # optional params
201 $params{type} = $args{type}
202 12 ▶ if $args{type};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"type"} 2 10 12
203 $params{iss} = $args{iss}
204 12 ▶ if $args{iss};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"iss"} 3 9 12
205 $params{aud} = $args{aud}
206 12 ▶ if $args{aud};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"aud"} 1 11 12
207 $params{scope} = $args{scope}
208 12 ▶ if $args{scope};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"scope"} 0 12 12
209  
210 12 my $content = build_content(\%params);
211 12 my $headers = HTTP::Headers->new;
212 12 $headers->header("Content-Type" => q{application/x-www-form-urlencoded});
213 12 $headers->header("Content-Length" => bytes::length($content));
214 $headers->authorization_basic($self->{id}, $self->{secret})
215 12 ▶ if($args{use_basic_schema});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"use_basic_schema"} 0 12 12
216 12 my $req = HTTP::Request->new( POST => $args{uri}, $headers, $content );
217  
218 12 my $res = $self->{agent}->request($req);
219 12 $self->{last_request} = $req;
220 12 $self->{last_response} = $res;
221  
222 12 my ($token, $errmsg);
223 12 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
224 12 $token = $self->{response_parser}->parse($res);
225 8 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
226 8 $errmsg = "$_";
227 12 };
228 12 ▶ return $token || $self->error($errmsg);
Condition66%2/3
$token || $self->error($errmsg)
l!l&&r!l&&!r
408
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
229  
230 }
231  
232 =head2 refresh_access_token( %params )
233  
234 =over 4
235  
236 =item refresh_token
237  
238 =back
239  
240 =cut
241  
242 0 ▶sub refresh_access_token {
Subroutine refresh_access_token: not called CC=6 SCAR=37.4
Pod documented
243 0 my $self = shift;
244  
245 0 my %args = Params::Validate::validate(@_, {
246 refresh_token => 1,
247 uri => { optional => 1 },
248 use_basic_schema => { optional => 1 },
249 });
250  
251 0 ▶ unless (exists $args{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"uri"}) 0 0 0
252 $args{uri} = $self->{access_token_uri}
253 0 ▶ || Carp::croak "uri not found";
Condition0%0/2
$self->{'access_token_uri'} || Carp::croak('uri not found')
l!l
00
MC/DC0%0/1
$self->{'access_token_uri'} || Carp::croak('uri not found')
$self->{'access_token_uri'}
Truth table0%0/2
A
A= $self->{'access_token_uri'}
Aresult
11
00
254 }
255  
256 my %params = (
257 grant_type => 'refresh_token',
258 refresh_token => $args{refresh_token},
259 0 );
260  
261 0 ▶ unless ($args{use_basic_schema}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"use_basic_schema"}) 0 0 0
262 0 $params{client_id} = $self->{id};
263 0 $params{client_secret} = $self->{secret};
264 }
265  
266 0 my $content = build_content(\%params);
267 0 my $headers = HTTP::Headers->new;
268 0 $headers->header("Content-Type" => q{application/x-www-form-urlencoded});
269 0 $headers->header("Content-Length" => bytes::length($content));
270 $headers->authorization_basic($self->{id}, $self->{secret})
271 0 ▶ if($args{use_basic_schema});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"use_basic_schema"} 0 0 0
272 0 my $req = HTTP::Request->new( POST => $args{uri}, $headers, $content );
273  
274 0 my $res = $self->{agent}->request($req);
275 0 $self->{last_request} = $req;
276 0 $self->{last_response} = $res;
277  
278 0 my ($token, $errmsg);
279 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
280 0 $token = $self->{response_parser}->parse($res);
281 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
282 0 $errmsg = "$_";
283 0 };
284 0 ▶ return $token || $self->error($errmsg);
Condition0%0/3
$token || $self->error($errmsg)
l!l&&r!l&&!r
000
MC/DC0%0/2
$token || $self->error($errmsg)
$token $self->error($errmsg)
Truth table0%0/3
A || B
A= $tokenB= $self->error($errmsg)
ABresult
1X1
011
000
285 }
286  
287 =head2 last_request
288  
289 Returns a HTTP::Request object that is used
290 when you obtain or refresh access token last time internally.
291  
292 =head2 last_response
293  
294 Returns a HTTP::Response object that is used
295 when you obtain or refresh access token last time internally.
296  
297 =cut
298  
299 0 ▶sub last_request { $_[0]->{last_request} }
Subroutine last_request: not called CC=1 SCAR=6.9
Pod documented
300 0 ▶sub last_response { $_[0]->{last_response} }
Subroutine last_response: not called CC=1 SCAR=6.9
Pod documented
301  
302 =head1 AUTHOR
303  
304 Ryo Ito, E<lt>ritou.06@gmail.comE<gt>
305  
306 =head1 COPYRIGHT AND LICENSE
307  
308 Copyright (C) 2014 by Ryo Ito
309  
310 This library is free software; you can redistribute it and/or modify
311 it under the same terms as Perl itself, either Perl version 5.8.8 or,
312 at your option, any later version of Perl 5 you may have available.
313  
314 =cut
315  
316 2 1;
« OAuth/Lite2/Server/GrantHandler/ExternalService.pm ↑ Summary OAuth/Lite2/Server/GrantHandler/Password.pm »