blib/lib/OIDC/Lite/Client/WebServer.pm

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