« OAuth/Lite2/Server/GrantHandler/GroupingRefreshToken.pm
↑ Summary
OAuth/Lite2/Client/StateResponseParser.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 | 4 | ▶ | sub new { | ||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||
| 147 | 4 | my $class = shift; | |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$self->{"format"} ||= "json"
$self->{"format"}
Truth table50%1/2
A
A= $self->{"format"}
| |||||||||||||||||||||||||
| 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 | 0 | ▶ | sub uri_to_redirect { | ||||||||||||||||||||||
|
Subroutine
uri_to_redirect: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||
| 188 | 0 | my $self = shift; | |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 204 | 0 | ▶ | $params{scope} = $args{scope} if $args{scope}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 205 | 0 | ▶ | $params{immediate} = $args{immediate} if $args{immediate}; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||
| 207 | 0 | ▶ | if ($args{extra}) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 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/5
$args{'uri'} || $self->{'authorize_uri'} || Carp::croak('uri not found')
$args{"uri"} or $self->{"authorize_uri"}
MC/DC0%0/2
$args{'uri'} || $self->{'authorize_uri'} || Carp::croak('uri not found')
$args{"uri"}
$self->{"authorize_uri"}
Truth table0%0/3
A or B
A= $args{"uri"}B= $self->{"authorize_uri"}
| |||||||||||||||||||||||||
| 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 | 15 | ▶ | sub get_access_token { | ||||||||||||||||||||||
|
Subroutine
get_access_token: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||
| 242 | 15 | my $self = shift; | |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 255 | $args{uri} = $self->{access_token_uri} | ||||||||||||||||||||||||
| 256 | 15 | ▶ | || Carp::croak "uri not found"; | ||||||||||||||||||||||
|
Condition50%1/2
$self->{'access_token_uri'} || Carp::croak('uri not found')
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'}
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||
| 269 | 15 | ▶ | unless ($args{use_basic_schema}){ | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 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 | 15 | ▶ | try { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 291 | 15 | $token = $self->{response_parser}->parse($res); | |||||||||||||||||||||||
| 292 | 12 | ▶ | } catch { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 293 | 12 | $errmsg = "$_"; | |||||||||||||||||||||||
| 294 | 15 | }; | |||||||||||||||||||||||
| 295 | 15 | ▶ | return $token || $self->error($errmsg); | ||||||||||||||||||||||
|
Condition66%2/3
$token || $self->error($errmsg)
MC/DC50%1/2
$token || $self->error($errmsg)
$token
$self->error($errmsg)
Truth table66%2/3
A || B
A= $tokenB= $self->error($errmsg)
| |||||||||||||||||||||||||
| 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 | 4 | ▶ | sub refresh_access_token { | ||||||||||||||||||||||
|
Subroutine
refresh_access_token: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||
| 312 | 4 | my $self = shift; | |||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 323 | $args{uri} = $self->{access_token_uri} | ||||||||||||||||||||||||
| 324 | 4 | ▶ | || Carp::croak "uri not found"; | ||||||||||||||||||||||
|
Condition50%1/2
$self->{'access_token_uri'} || Carp::croak('uri not found')
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'}
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 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 | 4 | ▶ | try { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 357 | 4 | $token = $self->{response_parser}->parse($res); | |||||||||||||||||||||||
| 358 | 2 | ▶ | } catch { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 359 | 2 | $errmsg = "$_"; | |||||||||||||||||||||||
| 360 | 4 | }; | |||||||||||||||||||||||
| 361 | 4 | ▶ | return $token || $self->error($errmsg); | ||||||||||||||||||||||
|
Condition66%2/3
$token || $self->error($errmsg)
MC/DC50%1/2
$token || $self->error($errmsg)
$token
$self->error($errmsg)
Truth table66%2/3
A || B
A= $tokenB= $self->error($errmsg)
| |||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||
| 365 | =head2 get_server_state | ||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||
| 367 | Obtain L<OAuth::Lite2::Client::ServerState> object. | ||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||
| 369 | =cut | ||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||
| 371 | 1 | ▶ | sub get_server_state { | ||||||||||||||||||||||
|
Subroutine
get_server_state: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||
| 372 | 1 | my $self = shift; | |||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||
| 374 | 1 | my %args = Params::Validate::validate(@_, { | |||||||||||||||||||||||
| 375 | uri => { optional => 1 }, | ||||||||||||||||||||||||
| 376 | }); | ||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||
| 378 | 1 | ▶ | unless (exists $args{uri}) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 379 | $args{uri} = $self->{access_token_uri} | ||||||||||||||||||||||||
| 380 | 1 | ▶ | || Carp::croak "uri not found"; | ||||||||||||||||||||||
|
Condition50%1/2
$self->{'access_token_uri'} || Carp::croak('uri not found')
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'}
| |||||||||||||||||||||||||
| 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 | 1 | ▶ | try { | ||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||
| 400 | 1 | $state = $self->{state_response_parser}->parse($res); | |||||||||||||||||||||||
| 401 | 0 | ▶ | } catch { | ||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||
| 402 | 0 | $errmsg = "$_"; | |||||||||||||||||||||||
| 403 | 1 | }; | |||||||||||||||||||||||
| 404 | 1 | ▶ | return $state || $self->error($errmsg); | ||||||||||||||||||||||
|
Condition33%1/3
$state || $self->error($errmsg)
MC/DC0%0/2
$state || $self->error($errmsg)
$state
$self->error($errmsg)
Truth table33%1/3
A || B
A= $stateB= $self->error($errmsg)
| |||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||