blib/lib/OAuth/Lite2/Server/Endpoint/Token.pm

filter: Statementstmt 92.6% 76 / 82 Branchbran 69.2% 18 / 26 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 91.3% 21 / 23 Podpod 100% 7 / 7 totaltotal 84.4% 125 / 148 SCAR 28.9
CC
17
Cov
85%
__ANON__
9.1
data_handler
6.0
__ANON__
4.2
CRAP 18.0
« blib/lib/OAuth/Lite2/Server/GrantHandler/GroupingRefreshToken.pm ↑ Summary blib/lib/OAuth/Lite2/Client/StateResponseParser.pm »
1 package OAuth::Lite2::Server::Endpoint::Token;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 use overload
5 54 ▶ q(&{}) => sub { shift->psgi_app },
Subroutine __ANON__: called CC=1 SCAR=0.0
6 7 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use Try::Tiny qw/try catch/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use OAuth::Lite2::Formatters;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use OAuth::Lite2::Server::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use OAuth::Lite2::Server::GrantHandlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use OAuth::Lite2::ParamMethod::AuthHeader;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub new {
17 6 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 6 my %args = Params::Validate::validate(@_, {
19 data_handler => 1,
20 error_uri => { optional => 1 },
21 });
22 my $self = bless {
23 data_handler => $args{data_handler},
24 error_uri => $args{error_uri},
25 6 grant_handlers => {},
26 }, $class;
27 6 return $self;
28 }
29  
30 sub support_grant_type {
31 12 ▶ my ($self, $type) = @_;
Subroutine support_grant_type: called CC=2 SCAR=7.1
Pod documented
32 12 ▶ my $handler = OAuth::Lite2::Server::GrantHandlers->get_handler($type)
Branch: 1 branch
BranchTrueFalseTotal
my $handler = "OAuth::Lite2::Server::GrantHandlers"->get_handler($type) or "OAuth::Lite2::Server::Error::UnsupportedGrantType"->throw 0 12 12
33 or OAuth::Lite2::Server::Error::UnsupportedGrantType->throw;
34 12 $self->{grant_handlers}{$type} = $handler;
35 }
36  
37 sub support_grant_types {
38 6 ▶ my $self = shift;
Subroutine support_grant_types: called CC=2 SCAR=6.9
Pod documented
39 6 $self->support_grant_type($_) for @_;
40 }
41  
42 sub data_handler {
43 0 ▶ my ($self, $handler) = @_;
Subroutine data_handler: not called CC=2 SCAR=17.9
Pod documented
44 0 ▶ $self->{data_handler} = $handler if $handler;
Branch: 1 branch
BranchTrueFalseTotal
if $handler 0 0 0
45 0 $self->{data_handler};
46 }
47  
48 sub psgi_app {
49 54 ▶ my $self = shift;
Subroutine psgi_app: called CC=2 SCAR=7.1
Pod documented
50 return $self->{psgi_app}
51 54 ▶ ||= $self->compile_psgi_app;
Condition66%2/3
$self->{"psgi_app"} ||= $self->compile_psgi_app
l!l&&r!l&&!r
4860
MC/DC0%0/2
$self->{"psgi_app"} ||= $self->compile_psgi_app
$self->{"psgi_app"} $self->compile_psgi_app
Truth table66%2/3
A ||= B
A= $self->{"psgi_app"}B= $self->compile_psgi_app
ABresult
1X1
011
000
52 }
53  
54 sub compile_psgi_app {
55 6 ▶ my $self = shift;
Subroutine compile_psgi_app: called CC=1 SCAR=0.1
Pod documented
56  
57 my $app = sub {
58 54 ▶ my $env = shift;
Subroutine __ANON__: called CC=1 SCAR=0.2
59 54 my $req = Plack::Request->new($env);
60 54 my $res; try {
61 54 ▶ $res = $self->handle_request($req);
Subroutine __ANON__: called CC=1 SCAR=0.0
62 } catch {
63 # Internal Server Error
64 0 ▶ warn $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 $res = $req->new_response(500);
66 54 };
67 54 return $res->finalize;
68 6 };
69  
70 6 return $app;
71 }
72  
73 sub handle_request {
74 54 ▶ my ($self, $request) = @_;
Subroutine handle_request: called CC=2 SCAR=7.0
Pod documented
75  
76 # from draft-v8, format is specified to JSON only.
77 54 my $format = "json";
78 # my $format = $request->param("format") || "json";
79 54 ▶ my $formatter = OAuth::Lite2::Formatters->get_formatter_by_name($format)
Condition33%1/3
'OAuth::Lite2::Formatters'->get_formatter_by_name($format) || 'OAuth::Lite2::Formatters'->get_formatter_by_name('json')
l!l&&r!l&&!r
5400
MC/DC0%0/2
'OAuth::Lite2::Formatters'->get_formatter_by_name($format) || 'OAuth::Lite2::Formatters'->get_formatter_by_name('json')
'OAuth::Lite2::Formatters'->get_formatter_by_name($format) 'OAuth::Lite2::Formatters'->get_formatter_by_name('json')
Truth table33%1/3
A || B
A= 'OAuth::Lite2::Formatters'->get_formatter_by_name($format)B= 'OAuth::Lite2::Formatters'->get_formatter_by_name('json')
ABresult
1X1
011
000
80 || OAuth::Lite2::Formatters->get_formatter_by_name("json");
81  
82 my $res = try {
83  
84 54 ▶ my $type = $request->param("grant_type")
Subroutine __ANON__: called CC=9 SCAR=22.1
Branch: 1 branch
BranchTrueFalseTotal
my $type = $request->param("grant_type") or "OAuth::Lite2::Server::Error::InvalidRequest"->throw("description", "'grant_type' not found") 0 54 54
85 or OAuth::Lite2::Server::Error::InvalidRequest->throw(
86 description => q{'grant_type' not found},
87 );
88  
89 54 ▶ my $handler = $self->{grant_handlers}{$type}
Branch: 1 branch
BranchTrueFalseTotal
my $handler = $self->{"grant_handlers"}{$type} or "OAuth::Lite2::Server::Error::UnsupportedGrantType"->throw 2 52 54
90 or OAuth::Lite2::Server::Error::UnsupportedGrantType->throw;
91  
92 52 my $data_handler = $self->{data_handler}->new(request => $request);
93  
94 # If Authorization Header is set, it is decoded and overwrite form encoded parameters.
95 52 my $parser = OAuth::Lite2::ParamMethod::AuthHeader->new;
96 52 my $header_credentials = $parser->basic_credentials($request);
97  
98 52 ▶ my $client_id = ($header_credentials->{client_id}) ? $header_credentials->{client_id} : $request->param("client_id");
Branch: 1 branch
BranchTrueFalseTotal
$header_credentials->{'client_id'} ? : 17 35 52
99 52 ▶ OAuth::Lite2::Server::Error::InvalidRequest->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $client_id 0 52 52
100 description => q{'client_id' not found},
101 )unless($client_id);
102  
103 52 ▶ my $client_secret = ($header_credentials->{client_secret}) ? $header_credentials->{client_secret} : $request->param("client_secret");
Branch: 1 branch
BranchTrueFalseTotal
$header_credentials->{'client_secret'} ? : 17 35 52
104  
105 # The grant type which are defined in spec require client authentication,
106 # but additional grant type may not.
107 52 ▶ if ( $handler->is_required_client_authentication ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler->is_required_client_authentication) 51 1 52
108 51 ▶ OAuth::Lite2::Server::Error::InvalidRequest->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $client_secret 0 51 51
109 description => q{'client_secret' not found},
110 )unless($client_secret);
111 }
112  
113 52 ▶ $data_handler->validate_client($client_id, $client_secret, $type)
Branch: 1 branch
BranchTrueFalseTotal
$data_handler->validate_client($client_id, $client_secret, $type) or "OAuth::Lite2::Server::Error::InvalidClient"->throw 6 46 52
114 or OAuth::Lite2::Server::Error::InvalidClient->throw;
115  
116 46 $handler->{client_id} = $client_id;
117 46 $handler->{client_secret} = $client_secret;
118 46 my $result = $handler->handle_request($data_handler);
119  
120 21 return $request->new_response(200,
121 [ "Content-Type" => $formatter->type,
122 "Cache-Control" => "no-store" ],
123 [ $formatter->format($result) ]);
124  
125 } catch {
126  
127 33 ▶ if ($_->isa("OAuth::Lite2::Server::Error")) {
Subroutine __ANON__: called CC=4 SCAR=14.5
Branch: 1 branch
BranchTrueFalseTotal
if ($_->isa('OAuth::Lite2::Server::Error')) { } 33 0 33
128  
129 33 my $error_params = { error => $_->type };
130 33 ▶ $error_params->{error_description} = $_->description
Branch: 1 branch
BranchTrueFalseTotal
if $_->description 1 32 33
131 if $_->description;
132 $error_params->{error_uri} = $self->{error_uri}
133 33 ▶ if $self->{error_uri};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error_uri"} 0 33 33
134  
135 33 return $request->new_response($_->code,
136 [ "Content-Type" => $formatter->type,
137 "Cache-Control" => "no-store" ],
138 [ $formatter->format($error_params) ]);
139  
140 } else {
141  
142 0 die $_;
143  
144 }
145  
146 54 };
147 }
148  
149 =head1 NAME
150  
151 OAuth::Lite2::Server::Endpoint::Token - token endpoint PSGI application
152  
153 =head1 SYNOPSIS
154  
155 token_endpoint.psgi
156  
157 use strict;
158 use warnings;
159 use Plack::Builder;
160 use OAuth::Lite2::Server::Endpoint::Token;
161 use MyDataHandlerClass;
162  
163 builder {
164 my $app = OAuth::Lite2::Server::Endpoint::Token->new(
165 data_handler => 'MyDataHandlerClass',
166 );
167 $app->support_grant_types(qw(authorization_code refresh_token));
168 $app;
169 };
170  
171 =head1 DESCRIPTION
172  
173 The instance of this class behaves as a PSGI application (subroutine reference).
174 This is for the OAuth 2.0 token-endpoint.
175  
176 The first thing you need to do is make your custom class, which inherits L<OAuth::Lite2::Server::DataHandler>, and then setup the PSGI file referencing it.
177  
178 =head1 METHODS
179  
180 =head2 new( %params )
181  
182 =over 4
183  
184 =item data_handler
185  
186 The name of your custom class that inherits the L<OAuth::Lite2::Server::DataHandler> package.
187  
188 =item error_uri
189  
190 Optional. This URI indicates the page that should be presented on an error. This will be included in error responses.
191  
192 =back
193  
194 =head2 support_grant_type( $type )
195  
196 Indicates support for a specific grant type. This does not remove previously supported grant types. The available values are:
197  
198 =over 4
199  
200 =item authorization_code
201  
202 =item password
203  
204 =item client_credentials
205  
206 =item refresh_token
207  
208 =back
209  
210 =head2 support_grant_types( @types )
211  
212 Allows specification of multiple grant types at once. This is equivalent to calling support_grant_type once for each type in the list. The available values are:
213  
214 =over 4
215  
216 =item authorization_code
217  
218 =item password
219  
220 =item client_credentials
221  
222 =item refresh_token
223  
224 =back
225  
226 =head2 data_handler
227  
228 This returns the class that inherits the L<OAuth::Lite2::Server::DataHandler> package. This is defined by the data_handler parameter of the constructor.
229  
230 =head2 psgi_app
231  
232 This returns a PSGI application.
233  
234 =head2 compile_psgi_app
235  
236 This will compile the PSGI application.
237  
238 =head2 handle_request( $req )
239  
240 This will parse the access token request and call the data handler's method.
241  
242 =head1 TEST
243  
244 You can test with L<OAuth::Lite2::Agent::PSGIMock> and some of the client classes.
245  
246 my $app = OAuth::Lite2::Server::Endpoint::Token->new(
247 data_handler => 'MyDataHandlerClass',
248 );
249 $app->support_grant_types(qw(authorization_code refresh_token));
250 my $mock_agent = OAuth::Lite2::Agent::PSGIMock->new(app => $app);
251 my $client = OAuth::Lite2::Client::UsernameAndPassword->new(
252 id => q{my_client_id},
253 secret => q{my_client_secret},
254 agent => $mock_agent,
255 );
256 my $token = $client->get_access_token(
257 username => q{foo},
258 password => q{bar},
259 );
260 ok($token);
261 is($token->access_token, q{access_token_value});
262  
263 =head1 AUTHOR
264  
265 Ryo Ito, E<lt>ritou.06@gmail.comE<gt>
266  
267 Lyo Kato, E<lt>lyo.kato@gmail.comE<gt>
268  
269 =head1 COPYRIGHT AND LICENSE
270  
271 Copyright (C) 2010 by Lyo Kato
272  
273 This library is free software; you can redistribute it and/or modify
274 it under the same terms as Perl itself, either Perl version 5.8.8 or,
275 at your option, any later version of Perl 5 you may have available.
276  
277 =cut
278  
279 7 1;
« blib/lib/OAuth/Lite2/Server/GrantHandler/GroupingRefreshToken.pm ↑ Summary blib/lib/OAuth/Lite2/Client/StateResponseParser.pm »