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

filter: Statementstmt 42.7% 56 / 131 Branchbran 7.5% 3 / 40 Conditioncond 21.7% 10 / 46 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 82.3% 14 / 17 Podpod 0.0% 0 / 1 totaltotal 32.4% 83 / 256 SCAR 62.6
CC
39
Cov
32%
_authorization_request
199.5
_access_token_request
104.0
_verify_access_token_and_scope
20.0
CRAP 521.5
« blib/lib/Dancer2/Plugin/OAuth2/Server/Simple.pm ↑ Summary Build »
1 package Dancer2::Plugin::OAuth2::Server;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 our $VERSION = '0.09';
7 2 ▶use Dancer2::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Class::Load qw(try_load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub get_server_class {
14 2 ▶ my ($settings) = @_;
Subroutine get_server_class: called CC=3 SCAR=11.8
Pod undocumented
15 2 ▶ my $server_class = $settings->{server_class}//"Dancer2::Plugin::OAuth2::Server::Simple";
Condition50%1/2
$settings->{"server_class"} // "Dancer2::Plugin::OAuth2::Server::Simple"
l!l
02
MC/DC0%0/1
$settings->{"server_class"} // "Dancer2::Plugin::OAuth2::Server::Simple"
$settings->{"server_class"}
Truth table50%1/2
A
A= $settings->{"server_class"}
Aresult
11
00
16 2 my ($ok, $error) = try_load_class($server_class);
17 2 ▶ if (! $ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 2 2
18 0 confess "Cannot load server class $server_class: $error";
19 }
20  
21 2 return $server_class->new();
22 }
23  
24 on_plugin_import {
25 1 ▶ my $dsl = shift;
Subroutine __ANON__: called CC=3 SCAR=11.1
26 1 my $settings = plugin_setting;
27 1 ▶ my $authorization_route = $settings->{authorize_route}//'/oauth/authorize';
Condition50%1/2
$settings->{"authorize_route"} // "/oauth/authorize"
l!l
01
MC/DC0%0/1
$settings->{"authorize_route"} // "/oauth/authorize"
$settings->{"authorize_route"}
Truth table50%1/2
A
A= $settings->{"authorize_route"}
Aresult
11
00
28 1 ▶ my $access_token_route = $settings->{access_token_route}//'/oauth/access_token';
Condition50%1/2
$settings->{"access_token_route"} // "/oauth/access_token"
l!l
01
MC/DC0%0/1
$settings->{"access_token_route"} // "/oauth/access_token"
$settings->{"access_token_route"}
Truth table50%1/2
A
A= $settings->{"access_token_route"}
Aresult
11
00
29  
30 $dsl->app->add_route(
31 method => 'get',
32 regexp => $authorization_route,
33 1 ▶ code => sub { _authorization_request( $dsl, $settings ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
34 1 );
35 $dsl->app->add_route(
36 method => 'post',
37 regexp => $access_token_route,
38 1 ▶ code => sub { _access_token_request( $dsl, $settings ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
39 1 );
40 2 };
41  
42 register 'oauth_scopes' => sub {
43 0 ▶ my ($dsl, $scopes, $code_ref) = @_;
Subroutine __ANON__: not called CC=2 SCAR=16.7
44  
45 0 my $settings = plugin_setting;
46  
47 0 ▶ $scopes = [$scopes] unless ref $scopes eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $scopes eq "ARRAY" 0 0 0
48  
49 return sub {
50 0 ▶ my $server = get_server_class( $settings );
Subroutine __ANON__: not called CC=2 SCAR=17.9
51 0 my @res = _verify_access_token_and_scope( $dsl, $settings, $server,0, @$scopes );
52 0 ▶ if( not $res[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $res[0]) { } 0 0 0
53 0 $dsl->status( 400 );
54 0 return $dsl->to_json( { error => $res[1] } );
55 } else {
56 0 $dsl->app->request->var( oauth_access_token => $res[0] );
57 0 goto $code_ref;
58 }
59 }
60 0 };
61  
62 sub _authorization_request {
63 1 ▶ my ($dsl, $settings) = @_;
Subroutine _authorization_request: called CC=18 SCAR=53.0
64 my ( $c_id,$url,$type,$scope,$state )
65 1 ▶ = map { $dsl->param( $_ ) // undef }
Condition50%1/2
$dsl->param($_) // undef
l!l
05
MC/DC0%0/1
$dsl->param($_) // undef
$dsl->param($_)
Truth table50%1/2
A
A= $dsl->param($_)
Aresult
11
00
66 qw/ client_id redirect_uri response_type scope state /;
67  
68 1 my $server = get_server_class( $settings );
69  
70 1 ▶ my @scopes = $scope ? split( / /,$scope ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$scope ? : 0 1 1
71  
72 1 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $c_id && defined $type or $type ne "code") 1 0 1
Condition33%2/6
not defined $c_id && defined $type or $type ne "code"
l!l&&r!l&&!r
100
defined $c_id && defined $type
!ll&&!rl&&r
100
MC/DC0%0/3
not defined $c_id && defined $type or $type ne "code"
defined $c_id defined $type $type ne "code"
Truth table25%1/4
not(A && B) or C
A= defined $c_idB= defined $typeC= $type ne "code"
ABCresult
0XX1
10X1
1111
1100
73 ! defined( $c_id )
74 or ! defined( $type )
75 or $type ne 'code'
76 ) {
77 1 $dsl->status( 400 );
78 1 return $dsl->to_json(
79 {
80 error => 'invalid_request',
81 error_description => 'the request was missing one of: client_id, '
82 . 'response_type;'
83 . 'or response_type did not equal "code"',
84 error_uri => '',
85 }
86 );
87 }
88  
89 0 ▶ my $state_required = $settings->{state_required} // 0;
Condition0%0/2
$settings->{"state_required"} // 0
l!l
00
MC/DC0%0/1
$settings->{"state_required"} // 0
$settings->{"state_required"}
Truth table0%0/2
A
A= $settings->{"state_required"}
Aresult
11
00
90 0 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($state_required and not defined $state and not length $state) 0 0 0
Condition0%0/6
$state_required and not defined $state and not length $state
!ll&&!rl&&r
000
$state_required and not defined $state
!ll&&!rl&&r
000
MC/DC0%0/3
$state_required and not defined $state and not length $state
$state_required not defined $state not length $state
Truth table0%0/4
A and not(B) and not(C)
A= $state_requiredB= not defined $stateC= not length $state
ABCresult
0XX0
10X0
1100
1111
91 $state_required
92 and ! defined $state
93 and ! length $state
94 ) {
95 0 $dsl->status( 400 );
96 0 return $dsl->to_json(
97 {
98 error => 'invalid_request',
99 error_description => 'the request was missing : state ',
100 error_uri => '',
101 }
102 );
103 }
104  
105 0 my $uri = URI->new( $url );
106 0 my ( $res,$error ) = $server->verify_client($dsl, $settings, $c_id, \@scopes );
107  
108 0 ▶ if ( $res ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
109 0 ▶ if ( ! $server->login_resource_owner( $dsl, $settings ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $server->login_resource_owner($dsl, $settings)) { } 0 0 0
110 0 $dsl->debug( "OAuth2::Server: Resource owner not logged in" );
111 # call to $resource_owner_logged_in method should have called redirect_to
112 0 return;
113 } else {
114 0 $dsl->debug( "OAuth2::Server: Resource owner is logged in" );
115 0 $res = $server->confirm_by_resource_owner($dsl, $settings, $c_id, \@scopes );
116 0 ▶ if ( ! defined $res ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $res) { } 0 0 0
elsif ($res == 0) { } 0 0 0
117 0 $dsl->debug( "OAuth2::Server: Resource owner to confirm scopes" );
118 # call to $resource_owner_confirms method should have called redirect_to
119 0 return;
120 }
121 elsif ( $res == 0 ) {
122 0 $dsl->debug( "OAuth2::Server: Resource owner denied scopes" );
123 0 $error = 'access_denied';
124 }
125 }
126 }
127  
128 0 ▶ if ( $res ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res) { } 0 0 0
elsif ($error) { } 0 0 0
129 0 $dsl->debug( "OAuth2::Server: Generating auth code for $c_id" );
130 0 ▶ my $expires_in = $settings->{auth_code_ttl} // 600;
Condition0%0/2
$settings->{"auth_code_ttl"} // 600
l!l
00
MC/DC0%0/1
$settings->{"auth_code_ttl"} // 600
$settings->{"auth_code_ttl"}
Truth table0%0/2
A
A= $settings->{"auth_code_ttl"}
Aresult
11
00
131  
132 0 my $auth_code = $server->generate_token($dsl, $settings, $expires_in, $c_id, \@scopes, 'auth', $url );
133  
134 0 $server->store_auth_code($dsl, $settings, $auth_code,$c_id,$expires_in,$url,@scopes );
135  
136 0 $uri->query_param_append( code => $auth_code );
137  
138 } elsif ( $error ) {
139 0 $uri->query_param_append( error => $error );
140 } else {
141 # callback has not returned anything, assume server error
142 0 $uri->query_param_append( error => 'server_error' );
143 0 $uri->query_param_append( error_description => 'call to verify_client returned unexpected value' );
144 }
145  
146 0 ▶ $uri->query_param_append( state => $state ) if defined( $state );
Branch: 1 branch
BranchTrueFalseTotal
if defined $state 0 0 0
147  
148 0 $dsl->redirect( $uri );
149 }
150  
151 sub _access_token_request {
152 1 ▶ my ($dsl, $settings) = @_;
Subroutine _access_token_request: called CC=13 SCAR=46.4
153 my ( $client_id,$client_secret,$grant_type,$auth_code,$url,$refresh_token )
154 1 ▶ = map { $dsl->param( $_ ) // undef }
Condition50%1/2
$dsl->param($_) // undef
l!l
06
MC/DC0%0/1
$dsl->param($_) // undef
$dsl->param($_)
Truth table50%1/2
A
A= $dsl->param($_)
Aresult
11
00
155 qw/ client_id client_secret grant_type code redirect_uri refresh_token /;
156  
157 1 my $server = get_server_class( $settings );
158  
159 1 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $grant_type or $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 $url) 0 0 0
Condition16%3/18
not defined $grant_type or $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 $url
l!l&&r!l&&!r
100
not defined $grant_type or $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
100
not defined $grant_type or $grant_type ne "authorization_code" and $grant_type ne "refresh_token"
l!l&&r!l&&!r
100
$grant_type ne "authorization_code" and $grant_type ne "refresh_token"
!ll&&!rl&&r
000
$grant_type eq "authorization_code" and not defined $auth_code
!ll&&!rl&&r
000
$grant_type eq "authorization_code" and not defined $url
!ll&&!rl&&r
000
MC/DC0%0/7
not defined $grant_type or $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 $url
not defined $grant_type $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 $url
Truth table6%1/16
not(A) or B and C or D and not(E) or F and not(G)
A= not defined $grant_typeB= $grant_type ne "authorization_code"C= $grant_type ne "refresh_token"D= $grant_type eq "authorization_code"E= not defined $auth_codeF= $grant_type eq "authorization_code"G= not defined $url
ABCDEFGresult
1XXXXXX1
011XXXX1
00X11XX1
01011XX1
00X0X111
00X10111
0100X111
01010111
00X0X0X0
00X0X100
00X100X0
00X10100
0100X0X0
0100X100
010100X0
01010100
160 ! defined( $grant_type )
161 or ( $grant_type ne 'authorization_code' and $grant_type ne 'refresh_token' )
162 or ( $grant_type eq 'authorization_code' and ! defined( $auth_code ) )
163 or ( $grant_type eq 'authorization_code' and ! defined( $url ) )
164 ) {
165 1 $dsl->status( 400 );
166 1 return $dsl->to_json(
167 {
168 error => 'invalid_request',
169 error_description => 'the request was missing one of: grant_type, '
170 . 'client_id, client_secret, code, redirect_uri;'
171 . 'or grant_type did not equal "authorization_code" '
172 . 'or "refresh_token"',
173 error_uri => '',
174 }
175 );
176 0 return;
177 }
178  
179 0 my $json_response = {};
180 0 my $status = 400;
181 0 my ( $client,$error,$scope,$old_refresh_token,$user_id );
182  
183 0 ▶ if ( $grant_type eq 'refresh_token' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($grant_type eq 'refresh_token') { } 0 0 0
184 0 ( $client,$error,$scope,$user_id ) = _verify_access_token_and_scope(
185 $dsl, $settings, $server, $refresh_token
186 );
187 0 $old_refresh_token = $refresh_token;
188 } else {
189 0 ( $client,$error,$scope,$user_id ) = $server->verify_auth_code(
190 $dsl, $settings, $client_id,$client_secret,$auth_code,$url
191 );
192 }
193  
194 0 ▶ if ( $client ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($client) { } 0 0 0
elsif ($error) { } 0 0 0
195  
196 0 $dsl->debug( "OAuth2::Server: Generating access token for $client" );
197  
198 0 ▶ my $expires_in = $settings->{access_token_ttl} // 3600;
Condition0%0/2
$settings->{"access_token_ttl"} // 3600
l!l
00
MC/DC0%0/1
$settings->{"access_token_ttl"} // 3600
$settings->{"access_token_ttl"}
Truth table0%0/2
A
A= $settings->{"access_token_ttl"}
Aresult
11
00
199 0 my $access_token = $server->generate_token($dsl, $settings, $expires_in,$client,$scope,'access',undef,$user_id );
200 0 my $refresh_token = $server->generate_token($dsl, $settings, undef,$client,$scope,'refresh',undef,$user_id );
201  
202 0 $server->store_access_token(
203 $dsl, $settings,
204 $client,$auth_code,$access_token,$refresh_token,
205 $expires_in,$scope,$old_refresh_token
206 );
207  
208 0 $status = 200;
209 0 $json_response = {
210 access_token => $access_token,
211 token_type => 'Bearer',
212 expires_in => $expires_in,
213 refresh_token => $refresh_token,
214 };
215  
216 } elsif ( $error ) {
217 0 $json_response->{error} = $error;
218 } else {
219 # callback has not returned anything, assume server error
220 0 $json_response = {
221 error => 'server_error',
222 error_description => 'call to verify_auth_code returned unexpected value',
223 };
224 }
225  
226 0 $dsl->header( 'Cache-Control' => 'no-store' );
227 0 $dsl->header( 'Pragma' => 'no-cache' );
228  
229 0 $dsl->status( $status );
230 0 return $dsl->to_json( $json_response );
231 }
232  
233 sub _verify_access_token_and_scope {
234 0 ▶ my ($dsl, $settings, $server, $refresh_token, @scopes) = @_;
Subroutine _verify_access_token_and_scope: not called CC=4 SCAR=30.0
235  
236 0 my $access_token;
237  
238 0 ▶ if ( ! $refresh_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $refresh_token) { } 0 0 0
239 0 ▶ if ( my $auth_header = $dsl->app->request->header( 'Authorization' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $auth_header = $dsl->app->request->header('Authorization')) { } 0 0 0
240 0 my ( $auth_type,$auth_access_token ) = split( / /,$auth_header );
241  
242 0 ▶ if ( $auth_type ne 'Bearer' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_type ne 'Bearer') { } 0 0 0
243 0 $dsl->debug( "OAuth2::Server: Auth type is not 'Bearer'" );
244 0 return ( 0,'invalid_request' );
245 } else {
246 0 $access_token = $auth_access_token;
247 }
248 } else {
249 0 $dsl->debug( "OAuth2::Server: Authorization header missing" );
250 0 return ( 0,'invalid_request' );
251 }
252 } else {
253 0 $access_token = $refresh_token;
254 }
255  
256 0 return $server->verify_access_token($dsl, $settings, $access_token,\@scopes,$refresh_token );
257 }
258  
259 2 register_plugin;
260  
261 2 1;
262 __END__
« blib/lib/Dancer2/Plugin/OAuth2/Server/Simple.pm ↑ Summary Build »