blib/lib/Mojolicious/Plugin/OAuth2/Server.pm

filter: Statementstmt 96.5% 168 / 174 Branchbran 84.0% 79 / 94 Conditioncond 78.5% 66 / 84 MC/DCmcdc 53.1% 25 / 47 Subroutinesub 100% 19 / 19 Podpod 100% 1 / 1 totaltotal 85.4% 358 / 419 CC 80 SCAR 26.5
CC
80
Cov
89%
_authorization_request
24.3
_access_token_request
20.3
_access_token_request_check_params
15.1
CRAP 14.2
↑ Summary
1 package Mojolicious::Plugin::OAuth2::Server;
2  
3 =head1 NAME
4  
5 Mojolicious::Plugin::OAuth2::Server - Easier implementation of an OAuth2
6 Authorization Server / Resource Server with Mojolicious
7  
8 =for html
9 <a href='https://travis-ci.org/Humanstate/mojolicious-plugin-oauth2-server?branch=master'><img src='https://travis-ci.org/Humanstate/mojolicious-plugin-oauth2-server.svg?branch=master' alt='Build Status' /></a>
10 <a href='https://coveralls.io/r/Humanstate/mojolicious-plugin-oauth2-server?branch=master'><img src='https://coveralls.io/repos/Humanstate/mojolicious-plugin-oauth2-server/badge.png?branch=master' alt='Coverage Status' /></a>
11  
12 =head1 VERSION
13  
14 0.53
15  
16 =head1 SYNOPSIS
17  
18 use Mojolicious::Lite;
19  
20 plugin 'OAuth2::Server' => {
21 ... # see SYNOPSIS in Net::OAuth2::AuthorizationServer::Manual
22 };
23  
24 group {
25 # /api - must be authorized
26 under '/api' => sub {
27 my ( $c ) = @_;
28  
29 return 1 if $c->oauth; # must be authorized via oauth
30  
31 $c->render( status => 401, text => 'Unauthorized' );
32 return undef;
33 };
34  
35 any '/annoy_friends' => sub { shift->render( text => "Annoyed Friends" ); };
36 any '/post_image' => sub { shift->render( text => "Posted Image" ); };
37 };
38  
39 any '/track_location' => sub {
40 my ( $c ) = @_;
41  
42 my $oauth_details = $c->oauth( 'track_location' )
43 || return $c->render( status => 401, text => 'You cannot track location' );
44  
45 $c->render( text => "Target acquired: @{[$oauth_details->{user_id}]}" );
46 };
47  
48 app->start;
49  
50 Or full fat app:
51  
52 use Mojo::Base 'Mojolicious';
53  
54 ...
55  
56 sub startup {
57 my $self = shift;
58  
59 ...
60  
61 $self->plugin( 'OAuth2::Server' => $oauth2_auth_code_grant_config );
62 }
63  
64 Then in your controller:
65  
66 sub my_route_name {
67 my ( $c ) = @_;
68
69 if ( my $oauth_details = $c->oauth( qw/required scopes/ ) ) {
70 ... # do something, user_id, client_id, etc, available in $oauth_details
71 } else {
72 return $c->render( status => 401, text => 'Unauthorized' );
73 }
74  
75 ...
76 }
77  
78 =head1 DESCRIPTION
79  
80 This plugin implements the various OAuth2 grant types flow as described at
81 L<http://tools.ietf.org/html/rfc6749>. It is a complete implementation of
82 RFC6749, with the exception of the "Extension Grants" as the description of
83 that grant type is rather hand-wavy.
84  
85 The bulk of the functionality is implemented in the L<Net::OAuth2::AuthorizationServer>
86 distribution, you should see that for more comprehensive documentation and
87 examples of usage.
88  
89 The examples here use the "Authorization Code Grant" flow as that is considered
90 the most secure and most complete form of OAuth2.
91  
92 =cut
93  
94 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 22 ▶use base qw/ Mojolicious::Plugin /;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 22 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 22 ▶use Mojo::Parameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 22 ▶use Mojo::Util qw/ b64_decode url_unescape /;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 22 ▶use Net::OAuth2::AuthorizationServer;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 22 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 22 our $VERSION = '0.53';
105  
106 22 my ( $AuthCodeGrant,$PasswordGrant,$ImplicitGrant,$ClientCredentialsGrant,$Grant,$JWTCallback );
107  
108 =head1 METHODS
109  
110 =head2 register
111  
112 Registers the plugin with your app - note that you must pass callbacks for
113 certain functions that the plugin expects to call if you are not using the
114 plugin in its simplest form.
115  
116 $self->register($app, \%config);
117  
118 Registering the plugin will call the L<Net::OAuth2::AuthorizationServer>
119 and create a C<auth_code_grant> that can be accessed using the defined
120 C<authorize_route> and C<access_token_route>. The arguments passed to the
121 plugin are passed straight through to the C<auth_code_grant> method in
122 the L<Net::OAuth2::AuthorizationServer> module.
123  
124 =head2 oauth
125  
126 Checks if there is a valid Authorization: Bearer header with a valid access
127 token and if the access token has the requisite scopes. The scopes are optional:
128  
129 unless ( my $oauth_details = $c->oauth( @scopes ) ) {
130 return $c->render( status => 401, text => 'Unauthorized' );
131 }
132  
133 This calls the L<Net::OAuth2::AuthorizationServer::AuthorizationCodeGrant>
134 module (C<verify_token_and_scope> method) to validate the access/refresh token.
135  
136 =head2 oauth2_auth_request
137  
138 This is a helper to allow you get get the redirect URI instead of directing
139 a user to the authorize_route - it requires the details of the client:
140  
141 my $redirect_uri = $c->oauth2_auth_request({
142 client_id => $client_id,
143 redirect_uri => 'https://foo',
144 response_type => 'token',
145 scope => 'list,of,scopes',
146 state => 'foo=bar&baz=boz',
147 });
148  
149 if ( $redirect_uri ) {
150 # do something with $redirect_uri
151 } else {
152 # something didn't work, e.g. bad client, scopes, etc
153 }
154  
155 You can use this helper instead of directing a user to the authorize_route if
156 you need to do something more involved with the redirect_uri rather than
157 having the plugin direct to the user to the resulting redirect uri
158  
159 =cut
160  
161 22 my $warned_dep = 0;
162  
163 22 ▶sub register {
Subroutine register: called CC=9 SCAR=22.0
Pod documented
164 22 my ( $self,$app,$config ) = @_;
165  
166 22 ▶ my $auth_route = $config->{authorize_route} // '/oauth/authorize';
Condition100%2/2
$config->{"authorize_route"} // "/oauth/authorize"
l!l
517
MC/DC100%1/1
$config->{"authorize_route"} // "/oauth/authorize"
$config->{"authorize_route"}
Truth table100%2/2
A
A= $config->{"authorize_route"}
Aresult
11
00
167 22 ▶ my $atoken_route = $config->{access_token_route} // '/oauth/access_token';
Condition100%2/2
$config->{"access_token_route"} // "/oauth/access_token"
l!l
517
MC/DC100%1/1
$config->{"access_token_route"} // "/oauth/access_token"
$config->{"access_token_route"}
Truth table100%2/2
A
A= $config->{"access_token_route"}
Aresult
11
00
168  
169 22 ▶ if ( $config->{users} && ! $config->{jwt_secret} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"users"} and not $config->{"jwt_secret"}) 0 22 22
Condition66%2/3
$config->{"users"} and not $config->{"jwt_secret"}
!ll&&!rl&&r
2020
MC/DC0%0/2
$config->{"users"} and not $config->{"jwt_secret"}
$config->{"users"} not $config->{"jwt_secret"}
Truth table66%2/3
A and not(B)
A= $config->{"users"}B= not $config->{"jwt_secret"}
ABresult
0X0
100
111
170 0 croak "You MUST provide a jwt_secret to use the password grant (users supplied)";
171 }
172  
173 22 my $Server = Net::OAuth2::AuthorizationServer->new;
174  
175 # note that access_tokens and refresh_tokens will not be shared between
176 # the various grant type objects, so if you need to support
177 # both then you *must* either supply a jwt_secret or supply callbacks
178 $AuthCodeGrant = $Server->auth_code_grant(
179 154 ▶ ( map { +"${_}_cb" => ( $config->{$_} // undef ) } qw/
Condition100%2/2
$config->{$_} // undef
l!l
28126
MC/DC100%1/1
$config->{$_} // undef
$config->{$_}
Truth table100%2/2
A
A= $config->{$_}
Aresult
11
00
180 verify_client store_auth_code verify_auth_code
181 store_access_token verify_access_token
182 login_resource_owner confirm_by_resource_owner
183 / ),
184 22 %{ $config },
185 );
186  
187 $PasswordGrant = $Server->password_grant(
188 126 ▶ ( map { +"${_}_cb" => ( $config->{$_} // undef ) } qw/
Condition100%2/2
$config->{$_} // undef
l!l
24102
MC/DC100%1/1
$config->{$_} // undef
$config->{$_}
Truth table100%2/2
A
A= $config->{$_}
Aresult
11
00
189 verify_client verify_user_password
190 store_access_token verify_access_token
191 login_resource_owner confirm_by_resource_owner
192 / ),
193 21 %{ $config },
194 );
195  
196 $ImplicitGrant = $Server->implicit_grant(
197 105 ▶ ( map { +"${_}_cb" => ( $config->{$_} // undef ) } qw/
Condition100%2/2
$config->{$_} // undef
l!l
2382
MC/DC100%1/1
$config->{$_} // undef
$config->{$_}
Truth table100%2/2
A
A= $config->{$_}
Aresult
11
00
198 verify_client store_access_token verify_access_token
199 login_resource_owner confirm_by_resource_owner
200 / ),
201 21 %{ $config },
202 );
203  
204 $ClientCredentialsGrant = $Server->client_credentials_grant(
205 63 ▶ ( map { +"${_}_cb" => ( $config->{$_} // undef ) } qw/
Condition100%2/2
$config->{$_} // undef
l!l
2043
MC/DC100%1/1
$config->{$_} // undef
$config->{$_}
Truth table100%2/2
A
A= $config->{$_}
Aresult
11
00
206 verify_client store_access_token verify_access_token
207 / ),
208 21 %{ $config },
209 );
210  
211 21 $JWTCallback = $config->{jwt_claims};
212  
213 $app->routes->get(
214 47 ▶ $auth_route => sub { _authorization_request( @_ ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
215 21 );
216  
217 1 ▶ $app->helper( oauth2_auth_request => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
218 1 my ( $c,$args ) = @_;
219 1 _authorization_request( $c,$args,1 );
220 21 } );
221  
222 $app->routes->post(
223 72 ▶ $atoken_route => sub { _access_token_request( @_ ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
224 21 );
225  
226 $app->helper(
227 105 ▶ oauth => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
228 105 my $c = shift;
229 105 my @scopes = @_;
230 105 ▶ $Grant ||= $AuthCodeGrant;
Condition66%2/3
$Grant ||= $AuthCodeGrant
l!l&&r!l&&!r
10410
MC/DC0%0/2
$Grant ||= $AuthCodeGrant
$Grant $AuthCodeGrant
Truth table66%2/3
A ||= B
A= $GrantB= $AuthCodeGrant
ABresult
1X1
011
000
231 105 my @res = $Grant->verify_token_and_scope(
232 scopes => [ @scopes ],
233 auth_header => $c->req->headers->header( 'Authorization' ),
234 mojo_controller => $c,
235 );
236  
237 105 my $oauth_details = $res[0];
238  
239 105 ▶ if ( ref( $oauth_details ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $oauth_details) 34 71 105
240 34 ▶ $oauth_details->{client_id} ||= $oauth_details->{client};
Condition66%2/3
$oauth_details->{"client_id"} ||= $oauth_details->{"client"}
l!l&&r!l&&!r
20140
MC/DC0%0/2
$oauth_details->{"client_id"} ||= $oauth_details->{"client"}
$oauth_details->{"client_id"} $oauth_details->{"client"}
Truth table66%2/3
A ||= B
A= $oauth_details->{"client_id"}B= $oauth_details->{"client"}
ABresult
1X1
011
000
241 }
242  
243 105 return $oauth_details;
244 },
245 21 );
246 }
247  
248 48 ▶sub _authorization_request {
Subroutine _authorization_request: called CC=24 SCAR=31.9
249 48 my ( $self,$args,$is_helper ) = @_;
250  
251 48 ▶ $args //= {};
Condition100%2/2
$args //= {}
l!l
147
MC/DC100%1/1
$args //= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
252  
253 48 ▶ my $client_id = $args->{client_id} // $self->param( 'client_id' );
Condition100%3/3
$args->{"client_id"} // $self->param("client_id")
l!l&&r!l&&!r
1416
MC/DC100%2/2
$args->{"client_id"} // $self->param("client_id")
$args->{"client_id"} $self->param("client_id")
Truth table100%3/3
A // B
A= $args->{"client_id"}B= $self->param("client_id")
ABresult
1X1
011
000
254 48 ▶ my $uri = $args->{redirect_uri} // $self->param( 'redirect_uri' );
Condition100%3/3
$args->{"redirect_uri"} // $self->param("redirect_uri")
l!l&&r!l&&!r
13512
MC/DC100%2/2
$args->{"redirect_uri"} // $self->param("redirect_uri")
$args->{"redirect_uri"} $self->param("redirect_uri")
Truth table100%3/3
A // B
A= $args->{"redirect_uri"}B= $self->param("redirect_uri")
ABresult
1X1
011
000
255 48 ▶ my $type = $args->{response_type} // $self->param( 'response_type' );
Condition100%3/3
$args->{"response_type"} // $self->param("response_type")
l!l&&r!l&&!r
1416
MC/DC100%2/2
$args->{"response_type"} // $self->param("response_type")
$args->{"response_type"} $self->param("response_type")
Truth table100%3/3
A // B
A= $args->{"response_type"}B= $self->param("response_type")
ABresult
1X1
011
000
256 48 ▶ my $scope = $args->{scope} // $self->param( 'scope' );
Condition66%2/3
$args->{"scope"} // $self->param("scope")
l!l&&r!l&&!r
02622
MC/DC50%1/2
$args->{"scope"} // $self->param("scope")
$args->{"scope"} $self->param("scope")
Truth table66%2/3
A // B
A= $args->{"scope"}B= $self->param("scope")
ABresult
1X1
011
000
257 48 ▶ my $state = $args->{state} // $self->param( 'state' );
Condition66%2/3
$args->{"state"} // $self->param("state")
l!l&&r!l&&!r
03018
MC/DC50%1/2
$args->{"state"} // $self->param("state")
$args->{"state"} $self->param("state")
Truth table66%2/3
A // B
A= $args->{"state"}B= $self->param("state")
ABresult
1X1
011
000
258 48 ▶ my $user_id = $args->{user_id} // undef;
Condition100%2/2
$args->{"user_id"} // undef
l!l
147
MC/DC100%1/1
$args->{"user_id"} // undef
$args->{"user_id"}
Truth table100%2/2
A
A= $args->{"user_id"}
Aresult
11
00
259  
260 48 ▶ my @scopes = $scope ? split( / /,$scope ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$scope ? : 26 22 48
261  
262 48 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $client_id and defined $type and $type =~ /^(code|token)$/) 12 36 48
Condition83%5/6
defined $client_id and defined $type and $type =~ /^(code|token)$/
!ll&&!rl&&r
12036
defined $client_id and defined $type
!ll&&!rl&&r
6636
MC/DC66%2/3
defined $client_id and defined $type and $type =~ /^(code|token)$/
defined $client_id defined $type $type =~ /^(code|token)$/
Truth table75%3/4
A and B and C
A= defined $client_idB= defined $typeC= $type =~ /^(code|token)$/
ABCresult
0XX0
10X0
1100
1111
263 ! defined( $client_id )
264 or ! defined( $type )
265 or $type !~ /^(code|token)$/
266 ) {
267 12 $self->render(
268 status => 400,
269 json => {
270 error => 'invalid_request',
271 error_description => 'the request was missing one of: client_id, '
272 . 'response_type;'
273 . 'or response_type did not equal "code" or "token"',
274 error_uri => '',
275 }
276 );
277 12 return;
278 }
279  
280 36 ▶ $Grant = $type eq 'token' ? $ImplicitGrant : $AuthCodeGrant;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'token' ? : 11 25 36
281  
282 36 my $mojo_url = Mojo::URL->new( $uri );
283 36 my ( $res,$error,$error_description ) = $Grant->verify_client(
284 client_id => $client_id,
285 redirect_uri => $uri,
286 scopes => [ @scopes ],
287 mojo_controller => $self,
288 response_type => $type,
289 );
290  
291 36 ▶ if ( $res ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 19 17 36
292  
293 19 ▶ if ( ! $Grant->login_resource_owner(
Branch: 1 branch
BranchTrueFalseTotal
if (not $Grant->login_resource_owner('mojo_controller', $self, 'client_id', $client_id)) { } 2 17 19
294 mojo_controller => $self,
295 client_id => $client_id,
296 ) ) {
297 2 $self->app->log->debug( "OAuth2::Server: Resource owner not logged in" );
298 # call to $resource_owner_logged_in method should have called redirect_to
299 2 return;
300 } else {
301 17 $self->app->log->debug( "OAuth2::Server: Resource owner is logged in" );
302 17 $res = $Grant->confirm_by_resource_owner(
303 client_id => $client_id,
304 scopes => [ @scopes ],
305 mojo_controller => $self,
306 );
307 17 ▶ if ( ! defined $res ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $res) { } 1 16 17
elsif ($res == 0) { } 1 15 16
308 1 $self->app->log->debug( "OAuth2::Server: Resource owner to confirm scopes" );
309 # call to $resource_owner_confirms method should have called redirect_to
310 1 return;
311 }
312 elsif ( $res == 0 ) {
313 1 $self->app->log->debug( "OAuth2::Server: Resource owner denied scopes" );
314 1 $error = 'access_denied';
315 1 ▶ $error_description //= 'resource owner denied access';
Condition50%1/2
$error_description //= "resource owner denied access"
l!l
01
MC/DC0%0/1
$error_description //= "resource owner denied access"
$error_description
Truth table50%1/2
A
A= $error_description
Aresult
11
00
316 }
317 }
318 }
319  
320 33 ▶ if ( $res ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res) { } 15 18 33
elsif ($error) { } 18 0 18
321  
322 15 ▶ return _maybe_generate_access_token(
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "token" 3 12 15
323 $self,$mojo_url,$client_id,[ @scopes ],$state,$is_helper,$user_id
324 ) if $type eq 'token'; # implicit grant
325  
326 12 $self->app->log->debug( "OAuth2::Server: Generating auth code for $client_id" );
327 12 my $auth_code = $Grant->token(
328 client_id => $client_id,
329 scopes => [ @scopes ],
330 type => 'auth',
331 redirect_uri => $uri,
332 jwt_claims_cb => $JWTCallback,
333 user_id => $user_id,
334 );
335  
336 12 $Grant->store_auth_code(
337 auth_code => $auth_code,
338 client_id => $client_id,
339 expires_in => $Grant->auth_code_ttl,
340 redirect_uri => $uri,
341 scopes => [ @scopes ],
342 mojo_controller => $self,
343 );
344  
345 12 $mojo_url->query->append( code => $auth_code );
346  
347 } elsif ( $error ) {
348 18 $mojo_url->query->append( error => $error );
349 18 ▶ $mojo_url->query->append( error_description => $error_description ) if $error_description;
Branch: 1 branch
BranchTrueFalseTotal
if $error_description 2 16 18
350 } else {
351 # callback has not returned anything, assume server error
352 0 $mojo_url->query->append(
353 error => 'server_error',
354 error_description => 'call to verify_client returned unexpected value',
355 );
356 }
357  
358 30 ▶ $mojo_url->query->append( state => $state ) if defined( $state );
Branch: 1 branch
BranchTrueFalseTotal
if defined $state 28 2 30
359  
360 30 ▶ return $is_helper ? $mojo_url : $self->redirect_to( $mojo_url );
Branch: 1 branch
BranchTrueFalseTotal
$is_helper ? : 0 30 30
361 }
362  
363 3 ▶sub _maybe_generate_access_token {
Subroutine _maybe_generate_access_token: called CC=4 SCAR=13.9
364 3 my ( $self,$mojo_url,$client,$scope,$state,$is_helper,$user_id ) = @_;
365  
366 3 ▶ my $access_token_ttl = $Grant->can('get_access_token_ttl')
Branch: 1 branch
BranchTrueFalseTotal
$Grant->can('get_access_token_ttl') ? : 3 0 3
367 ? $Grant->get_access_token_ttl(
368 scopes => $scope,
369 client_id => $client,
370 )
371 : $Grant->access_token_ttl;
372  
373 3 my $access_token = $Grant->token(
374 client_id => $client,
375 scopes => $scope,
376 type => 'access',
377 jwt_claims_cb => $JWTCallback,
378 user_id => $user_id,
379 );
380  
381 3 $Grant->store_access_token(
382 client_id => $client,
383 access_token => $access_token,
384 expires_in => $access_token_ttl,
385 scopes => $scope,
386 mojo_controller => $self,
387 );
388  
389 # http://example.com/cb#access_token=2YotnFZFEjr1zCsicMWpAA
390 # &state=xyz&token_type=example&expires_in=3600
391 3 ▶ my $params = Mojo::Parameters->new(
Branch: 1 branch
BranchTrueFalseTotal
$state ? : 2 1 3
392 access_token => $access_token,
393 token_type => 'bearer',
394 expires_in => $access_token_ttl,
395 ( $state
396 ? ( state => $state )
397 : (),
398 )
399 );
400  
401 3 $mojo_url->fragment( url_unescape( $params->to_string ) );
402 3 ▶ return $is_helper ? $mojo_url : $self->redirect_to( $mojo_url );
Branch: 1 branch
BranchTrueFalseTotal
$is_helper ? : 1 2 3
403 }
404  
405 72 ▶sub _access_token_request {
Subroutine _access_token_request: called CC=19 SCAR=30.1
406 72 my ( $self ) = @_;
407  
408 my (
409 $client_id,$client_secret,$grant_type,$auth_code,$uri,
410 $refresh_token,$username,$password
411 72 ▶ ) = map { $self->param( $_ ) // undef } qw/
Condition100%2/2
$self->param($_) // undef
l!l
235341
MC/DC100%1/1
$self->param($_) // undef
$self->param($_)
Truth table100%2/2
A
A= $self->param($_)
Aresult
11
00
412 client_id client_secret grant_type code redirect_uri
413 refresh_token username password
414 /;
415  
416 72 ▶ $grant_type //='';
Condition100%2/2
$grant_type //= ""
l!l
5220
MC/DC100%1/1
$grant_type //= ""
$grant_type
Truth table100%2/2
A
A= $grant_type
Aresult
11
00
417  
418 72 ▶ _access_token_request_check_params(
Branch: 1 branch
BranchTrueFalseTotal
_access_token_request_check_params($self, $grant_type, $username, $password, $auth_code, $uri) or return 51 21 72
419 $self,$grant_type,$username,$password,$auth_code,$uri
420 ) || return;
421  
422 51 my $json_response = {};
423 51 my $status = 400;
424  
425 51 ▶ $Grant = $grant_type eq 'password'
Branch: 2 branches
BranchTrueFalseTotal
$grant_type eq 'password' ? : 7 44 51
$grant_type eq 'client_credentials' ? : 12 32 44
426 ? $PasswordGrant : $grant_type eq 'client_credentials'
427 ? $ClientCredentialsGrant : $AuthCodeGrant;
428  
429 51 my ( $client,$error,$scope,$user_id,$old_refresh_token,$error_description ) = _verify_credentials(
430 $self,$Grant,$grant_type,$refresh_token,$client_id,$client_secret,
431 $auth_code,$username,$password,$uri
432 );
433  
434 51 ▶ if ( $client ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($client) { } 23 28 51
elsif ($error) { } 28 0 28
435  
436 23 ▶ $self->app->log->debug( "OAuth2::Server: Generating access token for @{[ ref $client ? $client->{client} : $client ]}" );
Branch: 1 branch
BranchTrueFalseTotal
ref $client ? : 0 23 23
437  
438 23 ▶ my $access_token_ttl = $Grant->can('get_access_token_ttl')
Branch: 1 branch
BranchTrueFalseTotal
$Grant->can('get_access_token_ttl') ? : 23 0 23
439 ? $Grant->get_access_token_ttl(
440 scopes => $scope,
441 client_id => $client_id,
442 )
443 : $Grant->access_token_ttl;
444  
445 23 my $access_token = $Grant->token(
446 client_id => $client,
447 scopes => $scope,
448 type => 'access',
449 user_id => $user_id,
450 jwt_claims_cb => $JWTCallback,
451 );
452  
453 23 my $refresh_token = $Grant->token(
454 client_id => $client,
455 scopes => $scope,
456 type => 'refresh',
457 user_id => $user_id,
458 jwt_claims_cb => $JWTCallback,
459 );
460  
461 23 ▶ $Grant->store_access_token(
Branch: 2 branches
BranchTrueFalseTotal
$grant_type ne 'password' ? : 20 3 23
$grant_type eq 'client_credentials' ? : 3 20 23
462 client_id => $client,
463 ( $grant_type ne 'password' ? ( auth_code => $auth_code ) : () ),
464 access_token => $access_token,
465 expires_in => $access_token_ttl,
466 scopes => $scope,
467 ( $grant_type eq 'client_credentials'
468 ? ()
469 : (
470 refresh_token => $refresh_token,
471 old_refresh_token => $old_refresh_token,
472 )
473 ),
474 mojo_controller => $self,
475 );
476  
477 23 $status = 200;
478 $json_response = {
479 # RFC6749 section 5.1 says that Access Token Response should include
480 # the authorized scope when it does not match the requested scopes:
481 #
482 # OPTIONAL, if identical to the scope requested by the client;
483 # otherwise, REQUIRED. The scope of the access token as
484 # described by Section 3.3.
485 ( ! $old_refresh_token && $scope
486 ? ( scopes => ref( $scope ) eq 'HASH'
487 23 ▶ ? [ map { $_ } grep { $scope->{$_} } keys %{ $scope } ]
Branch: 3 branches
BranchTrueFalseTotal
!$old_refresh_token && $scope ? : 15 8 23
ref $scope eq 'HASH' ? : 2 13 15
$grant_type eq 'client_credentials' ? : 3 20 23
Condition66%2/3
not $old_refresh_token and $scope
!ll&&!rl&&r
8015
MC/DC50%1/2
not $old_refresh_token and $scope
not $old_refresh_token $scope
Truth table66%2/3
not(A) and B
A= not $old_refresh_tokenB= $scope
ABresult
0X0
100
111
488 : $scope )
489 : ()),
490  
491 access_token => $access_token,
492 token_type => 'Bearer',
493 expires_in => $access_token_ttl,
494 ( $grant_type eq 'client_credentials'
495 ? ()
496 : ( refresh_token => $refresh_token ),
497 )
498 };
499  
500 } elsif ( $error ) {
501 28 $json_response->{error} = $error;
502 28 ▶ $json_response->{error_description} = $error_description if $error_description;
Branch: 1 branch
BranchTrueFalseTotal
if $error_description 1 27 28
503 } else {
504 # callback has not returned anything, assume server error
505 0 ▶ my $method = $grant_type eq 'password'
Branch: 2 branches
BranchTrueFalseTotal
$grant_type eq 'password' ? : 0 0 0
$grant_type eq 'client_credentials' ? : 0 0 0
506 ? 'verify_user_password' : $grant_type eq 'client_credentials'
507 ? 'verify_client' : 'verify_auth_code';
508  
509 0 $json_response = {
510 error => 'server_error',
511 error_description => "call to $method returned unexpected value",
512 };
513 }
514  
515 51 $self->res->headers->header( 'Cache-Control' => 'no-store' );
516 51 $self->res->headers->header( 'Pragma' => 'no-cache' );
517  
518 51 $self->render(
519 status => $status,
520 json => $json_response,
521 );
522 }
523  
524 72 ▶sub _access_token_request_check_params {
Subroutine _access_token_request_check_params: called CC=13 SCAR=27.2
525 72 my ( $self,$grant_type,$username,$password,$auth_code,$uri ) = @_;
526  
527 72 ▶ if (
Branch: 3 branches
BranchTrueFalseTotal
if ($grant_type eq 'password') { } 7 65 72
elsif ($grant_type eq 'client_credentials') { } 13 52 65
elsif ($grant_type ne 'authorization_code' and $grant_type ne 'refresh_token' or $grant_type eq 'authorization_code' and not defined $auth_code or $grant_type eq 'authorization_code' and not defined $uri) { } 20 41 61
528 $grant_type eq 'password'
529 ) {
530 7 ▶ if ( ! $username && ! $password ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($username or $password) 0 7 7
Condition33%1/3
$username or $password
l!l&&r!l&&!r
700
MC/DC0%0/2
$username or $password
$username $password
Truth table33%1/3
A or B
A= $usernameB= $password
ABresult
1X1
011
000
531 0 $self->render(
532 status => 400,
533 json => {
534 error => 'invalid_request',
535 error_description => 'the request was missing one of: '
536 . 'client_id, client_secret, username, password',
537 error_uri => '',
538 }
539 );
540 0 return 0;
541 }
542 } elsif (
543 $grant_type eq 'client_credentials'
544 ) {
545 13 my ( $client_id,$client_secret ) = _client_credentials_from_header( $self );
546  
547 13 ▶ if ( ! $client_id || ! $client_secret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($client_id and $client_secret) 1 12 13
Condition66%2/3
$client_id and $client_secret
!ll&&!rl&&r
1012
MC/DC50%1/2
$client_id and $client_secret
$client_id $client_secret
Truth table66%2/3
A and B
A= $client_idB= $client_secret
ABresult
0X0
100
111
548 1 $self->render(
549 status => 400,
550 json => {
551 error => 'invalid_request',
552 error_description => 'the request was missing an Authorization: Basic'
553 . ' header or it was missing the encoded client_id:client_secret data',
554 error_uri => '',
555 }
556 );
557 1 return 0;
558 }
559 ▶ } elsif (
Condition73%11/15
$grant_type ne "authorization_code" and $grant_type ne "refresh_token" or $grant_type eq "authorization_code" and not defined $auth_code or $grant_type eq "authorization_code" and not defined $uri
l!l&&r!l&&!r
20032
$grant_type ne "authorization_code" and $grant_type ne "refresh_token" or $grant_type eq "authorization_code" and not defined $auth_code
l!l&&r!l&&!r
20032
$grant_type ne "authorization_code" and $grant_type ne "refresh_token"
!ll&&!rl&&r
23920
$grant_type eq "authorization_code" and not defined $auth_code
!ll&&!rl&&r
9230
$grant_type eq "authorization_code" and not defined $uri
!ll&&!rl&&r
9230
MC/DC33%2/6
$grant_type ne "authorization_code" and $grant_type ne "refresh_token" or $grant_type eq "authorization_code" and not defined $auth_code or $grant_type eq "authorization_code" and not defined $uri
$grant_type ne "authorization_code" $grant_type ne "refresh_token" $grant_type eq "authorization_code" not defined $auth_code $grant_type eq "authorization_code" not defined $uri
Truth table20%3/15
A and B or C and not(D) or E and not(F)
A= $grant_type ne "authorization_code"B= $grant_type ne "refresh_token"C= $grant_type eq "authorization_code"D= not defined $auth_codeE= $grant_type eq "authorization_code"F= not defined $uri
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
560 ( $grant_type ne 'authorization_code' and $grant_type ne 'refresh_token' )
561 or ( $grant_type eq 'authorization_code' and ! defined( $auth_code ) )
562 or ( $grant_type eq 'authorization_code' and ! defined( $uri ) )
563 ) {
564 20 $self->render(
565 status => 400,
566 json => {
567 error => 'invalid_request',
568 error_description => 'the request was missing one of: grant_type, '
569 . 'client_id, client_secret, code, redirect_uri;'
570 . 'or grant_type did not equal "authorization_code" '
571 . 'or "refresh_token"',
572 error_uri => '',
573 }
574 );
575 20 return 0;
576 }
577  
578 51 return 1;
579 }
580  
581 25 ▶sub _client_credentials_from_header {
Subroutine _client_credentials_from_header: called CC=7 SCAR=20.2
582 25 my ( $self ) = @_;
583  
584 25 my ( $client_id,$client_secret );
585  
586 # params in the header
587 25 ▶ if ( my $auth_header = $self->req->headers->header( 'Authorization' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $auth_header = $self->req->headers->header('Authorization')) { } 7 18 25
elsif ($client_id = $self->req->param('client_id')) { } 18 0 18
588 7 ▶ if ( my ( $encoded_details ) = ( split( 'Basic ',$auth_header ) )[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($encoded_details) = (split(/Basic /, $auth_header, 0))[1]) 7 0 7
589 7 ▶ my $decoded_details = b64_decode( $encoded_details // '' );
Condition100%2/2
$encoded_details // ""
l!l
61
MC/DC100%1/1
$encoded_details // ""
$encoded_details
Truth table100%2/2
A
A= $encoded_details
Aresult
11
00
590 7 ( $client_id,$client_secret ) = split( ':',$decoded_details, 2);
591 7 return ( $client_id,$client_secret );
592 }
593  
594 # params in the body
595 } elsif ( $client_id = $self->req->param('client_id') ) {
596 18 $client_secret = $self->req->param('client_secret');
597 }
598  
599 18 ▶ if ( $client_id && $client_secret ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client_id and $client_secret) 18 0 18
Condition33%1/3
$client_id and $client_secret
!ll&&!rl&&r
0018
MC/DC0%0/2
$client_id and $client_secret
$client_id $client_secret
Truth table33%1/3
A and B
A= $client_idB= $client_secret
ABresult
0X0
100
111
600 18 return ( $client_id,$client_secret );
601 }
602 }
603  
604 51 ▶sub _verify_credentials {
Subroutine _verify_credentials: called CC=7 SCAR=19.5
605 my (
606 51 $self,$Grant,$grant_type,$refresh_token,$client_id,$client_secret,
607 $auth_code,$username,$password,$uri
608 ) = @_;
609  
610 51 my ( $client,$error,$scope,$user_id,$old_refresh_token,$error_description );
611  
612 51 ▶ if ( $grant_type eq 'refresh_token' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($grant_type eq 'refresh_token') { } 9 42 51
elsif ($grant_type eq 'password') { } 7 35 42
elsif ($grant_type eq 'client_credentials') { } 12 23 35
613 9 ( $client,$error,$scope,$user_id,$error_description ) = $Grant->verify_token_and_scope(
614 refresh_token => $refresh_token,
615 auth_header => $self->req->headers->header( 'Authorization' ),
616 mojo_controller => $self,
617 );
618 9 $old_refresh_token = $refresh_token;
619  
620 } elsif ( $grant_type eq 'password' ) {
621 7 $scope = $self->every_param( 'scope' );
622  
623 7 ( $client,$error,$scope,$user_id,$error_description ) = $Grant->verify_user_password(
624 client_id => $client_id,
625 client_secret => $client_secret,
626 username => $username,
627 password => $password,
628 mojo_controller => $self,
629 scopes => $scope,
630 );
631 } elsif ( $grant_type eq 'client_credentials' ) {
632  
633 12 my $client_secret;
634  
635 12 ( $client,$client_secret ) = _client_credentials_from_header( $self );
636  
637 12 $scope = $self->every_param( 'scope' );
638 12 my $res;
639  
640 12 ( $res,$error,$error_description ) = $Grant->verify_client(
641 client_id => $client,
642 client_secret => $client_secret,
643 mojo_controller => $self,
644 scopes => $scope,
645 );
646  
647 12 ▶ undef( $client ) if ! $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 9 3 12
648  
649 } else {
650 23 ( $client,$error,$scope,$user_id,$error_description ) = $Grant->verify_auth_code(
651 client_id => $client_id,
652 client_secret => $client_secret,
653 auth_code => $auth_code,
654 redirect_uri => $uri,
655 mojo_controller => $self,
656 );
657 }
658  
659 $client = ! ref $client
660 ? $client
661 51 ▶ : ( $client->{client_id} || $client->{client} );
Branch: 1 branch
BranchTrueFalseTotal
!ref($client) ? : 44 7 51
Condition66%2/3
$client->{"client_id"} or $client->{"client"}
l!l&&r!l&&!r
430
MC/DC0%0/2
$client->{"client_id"} or $client->{"client"}
$client->{"client_id"} $client->{"client"}
Truth table66%2/3
A or B
A= $client->{"client_id"}B= $client->{"client"}
ABresult
1X1
011
000
662  
663 51 return ( $client,$error,$scope,$user_id,$old_refresh_token,$error_description );
664 }
665  
666 =head1 SEE ALSO
667  
668 L<Net::OAuth2::AuthorizationServer> - The dist that handles the bulk of the
669 functionality used by this plugin
670  
671 =head1 AUTHOR & CONTRIBUTORS
672  
673 Lee Johnson - C<leejo@cpan.org>
674  
675 With contributions from:
676  
677 Nick Logan C<nlogan@gmail.com>
678  
679 Pierre VIGIER C<pierre.vigier@gmail.com>
680  
681 Renee C<reb@perl-services.de>
682  
683 =head1 LICENSE
684  
685 This library is free software; you can redistribute it and/or modify it under
686 the same terms as Perl itself. If you would like to contribute documentation
687 or file a bug report then please raise an issue / pull request:
688  
689 https://github.com/Humanstate/mojolicious-plugin-oauth2-server
690  
691 =cut
692  
693 22 1;
694  
695 # vim: ts=2:sw=2:et
↑ Summary