Server.pm

filter: Statementstmt 90.0% 118 / 131 Branchbran 80.0% 32 / 40 Conditioncond 73.9% 34 / 46 MC/DCmcdc 42.8% 9 / 21 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 1 totaltotal 82.0% 210 / 256 CC 39 SCAR 24.2
CC
39
Cov
85%
_authorization_request
21.3
_access_token_request
13.8
_verify_access_token_and_scope
4.1
CRAP 11.2
« Server/Simple.pm ↑ Summary Server/Role.pm »
1 package Dancer2::Plugin::OAuth2::Server;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 our $VERSION = '0.11';
7 5 ▶use Dancer2::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Class::Load qw(try_load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 31 ▶sub get_server_class {
Subroutine get_server_class: called CC=3 SCAR=11.8
Pod undocumented
14 31 my ($settings) = @_;
15 31 ▶ my $server_class = $settings->{server_class}//"Dancer2::Plugin::OAuth2::Server::Simple";
Condition50%1/2
$settings->{"server_class"} // "Dancer2::Plugin::OAuth2::Server::Simple"
l!l
031
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 31 my ($ok, $error) = try_load_class($server_class);
17 31 ▶ if (! $ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 31 31
18 0 confess "Cannot load server class $server_class: $error";
19 }
20  
21 31 return $server_class->new();
22 }
23  
24 4 ▶on_plugin_import {
Subroutine __ANON__: called CC=3 SCAR=11.0
25 4 my $dsl = shift;
26 4 my $settings = plugin_setting;
27 4 ▶ my $authorization_route = $settings->{authorize_route}//'/oauth/authorize';
Condition100%2/2
$settings->{"authorize_route"} // "/oauth/authorize"
l!l
13
MC/DC100%1/1
$settings->{"authorize_route"} // "/oauth/authorize"
$settings->{"authorize_route"}
Truth table100%2/2
A
A= $settings->{"authorize_route"}
Aresult
11
00
28 4 ▶ my $access_token_route = $settings->{access_token_route}//'/oauth/access_token';
Condition100%2/2
$settings->{"access_token_route"} // "/oauth/access_token"
l!l
13
MC/DC100%1/1
$settings->{"access_token_route"} // "/oauth/access_token"
$settings->{"access_token_route"}
Truth table100%2/2
A
A= $settings->{"access_token_route"}
Aresult
11
00
29  
30 $dsl->app->add_route(
31 method => 'get',
32 regexp => $authorization_route,
33 15 ▶ code => sub { _authorization_request( $dsl, $settings ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
34 4 );
35 $dsl->app->add_route(
36 method => 'post',
37 regexp => $access_token_route,
38 8 ▶ code => sub { _access_token_request( $dsl, $settings ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
39 4 );
40 5 };
41  
42 2 ▶register 'oauth_scopes' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
43 2 my ($dsl, $scopes, $code_ref) = @_;
44  
45 2 my $settings = plugin_setting;
46  
47 2 ▶ $scopes = [$scopes] unless ref $scopes eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $scopes eq "ARRAY" 2 0 2
48  
49 8 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
50 8 my $server = get_server_class( $settings );
51 8 my @res = _verify_access_token_and_scope( $dsl, $settings, $server,0, @$scopes );
52 8 ▶ if( not $res[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $res[0]) { } 3 5 8
53 3 $dsl->status( 400 );
54 3 return $dsl->send_as( JSON => { error => $res[1] } );
55 } else {
56 5 $dsl->app->request->var( oauth_access_token => $res[0] );
57 5 goto $code_ref;
58 }
59 }
60 2 };
61  
62 15 ▶sub _authorization_request {
Subroutine _authorization_request: called CC=18 SCAR=30.6
63 15 my ($dsl, $settings) = @_;
64 my ( $c_id,$url,$type,$scope,$state )
65 15 ▶ = map { $dsl->param( $_ ) // undef }
Condition100%2/2
$dsl->param($_) // undef
l!l
4926
MC/DC100%1/1
$dsl->param($_) // undef
$dsl->param($_)
Truth table100%2/2
A
A= $dsl->param($_)
Aresult
11
00
66 qw/ client_id redirect_uri response_type scope state /;
67  
68 15 my $server = get_server_class( $settings );
69  
70 15 ▶ my @scopes = $scope ? split( / /,$scope ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$scope ? : 11 4 15
71  
72 15 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $c_id && defined $type or $type ne "code") 4 11 15
Condition66%4/6
not defined $c_id && defined $type or $type ne "code"
l!l&&r!l&&!r
4011
defined $c_id && defined $type
!ll&&!rl&&r
4011
MC/DC33%1/3
not defined $c_id && defined $type or $type ne "code"
defined $c_id defined $type $type ne "code"
Truth table50%2/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 4 $dsl->status( 400 );
78 4 return $dsl->send_as(
79 JSON => {
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 11 ▶ my $state_required = $settings->{state_required} // 0;
Condition100%2/2
$settings->{"state_required"} // 0
l!l
29
MC/DC100%1/1
$settings->{"state_required"} // 0
$settings->{"state_required"}
Truth table100%2/2
A
A= $settings->{"state_required"}
Aresult
11
00
90 11 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($state_required and not defined $state and not length $state) 1 10 11
Condition83%5/6
$state_required and not defined $state and not length $state
!ll&&!rl&&r
1001
$state_required and not defined $state
!ll&&!rl&&r
911
MC/DC66%2/3
$state_required and not defined $state and not length $state
$state_required not defined $state not length $state
Truth table75%3/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 1 $dsl->status( 400 );
96 1 return $dsl->send_as(
97 JSON => {
98 error => 'invalid_request',
99 error_description => 'the request was missing : state ',
100 error_uri => '',
101 }
102 );
103 }
104  
105 10 my $uri = URI->new( $url );
106 10 my ( $res,$error ) = $server->verify_client($dsl, $settings, $c_id, \@scopes, $url );
107  
108 10 ▶ if ( $res ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 6 4 10
109 6 ▶ if ( ! $server->login_resource_owner( $dsl, $settings ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $server->login_resource_owner($dsl, $settings)) { } 0 6 6
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 6 $dsl->debug( "OAuth2::Server: Resource owner is logged in" );
115 6 $res = $server->confirm_by_resource_owner($dsl, $settings, $c_id, \@scopes );
116 6 ▶ if ( ! defined $res ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $res) { } 0 6 6
elsif ($res == 0) { } 0 6 6
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 10 ▶ if ( $res ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($res) { } 6 4 10
elsif ($error) { } 4 0 4
129 6 $dsl->debug( "OAuth2::Server: Generating auth code for $c_id" );
130 6 ▶ my $expires_in = $settings->{auth_code_ttl} // 600;
Condition50%1/2
$settings->{"auth_code_ttl"} // 600
l!l
06
MC/DC0%0/1
$settings->{"auth_code_ttl"} // 600
$settings->{"auth_code_ttl"}
Truth table50%1/2
A
A= $settings->{"auth_code_ttl"}
Aresult
11
00
131  
132 6 my $auth_code = $server->generate_token($dsl, $settings, $expires_in, $c_id, \@scopes, 'auth', $url );
133  
134 6 $server->store_auth_code($dsl, $settings, $auth_code,$c_id,$expires_in,$url,@scopes );
135  
136 6 $uri->query_param_append( code => $auth_code );
137  
138 } elsif ( $error ) {
139 4 $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 10 ▶ $uri->query_param_append( state => $state ) if defined( $state );
Branch: 1 branch
BranchTrueFalseTotal
if defined $state 5 5 10
147  
148 10 $dsl->redirect( $uri );
149 }
150  
151 8 ▶sub _access_token_request {
Subroutine _access_token_request: called CC=13 SCAR=26.3
152 8 my ($dsl, $settings) = @_;
153 my ( $client_id,$client_secret,$grant_type,$auth_code,$url,$refresh_token )
154 8 ▶ = map { $dsl->param( $_ ) // undef }
Condition100%2/2
$dsl->param($_) // undef
l!l
2919
MC/DC100%1/1
$dsl->param($_) // undef
$dsl->param($_)
Truth table100%2/2
A
A= $dsl->param($_)
Aresult
11
00
155 qw/ client_id client_secret grant_type code redirect_uri refresh_token /;
156  
157 8 my $server = get_server_class( $settings );
158  
159 8 ▶ 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) 2 6 8
Condition66%12/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
206
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
206
not defined $grant_type or $grant_type ne "authorization_code" and $grant_type ne "refresh_token"
l!l&&r!l&&!r
206
$grant_type ne "authorization_code" and $grant_type ne "refresh_token"
!ll&&!rl&&r
510
$grant_type eq "authorization_code" and not defined $auth_code
!ll&&!rl&&r
150
$grant_type eq "authorization_code" and not defined $url
!ll&&!rl&&r
150
MC/DC14%1/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 table18%3/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 2 $dsl->status( 400 );
166 2 return $dsl->send_as(
167 JSON => {
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 6 my $json_response = {};
180 6 my $status = 400;
181 6 my ( $client,$error,$scope,$old_refresh_token,$user_id );
182  
183 6 ▶ if ( $grant_type eq 'refresh_token' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($grant_type eq 'refresh_token') { } 1 5 6
184 1 ( $client,$error,$scope,$user_id ) = _verify_access_token_and_scope(
185 $dsl, $settings, $server, $refresh_token
186 );
187 1 $old_refresh_token = $refresh_token;
188 } else {
189 5 ( $client,$error,$scope,$user_id ) = $server->verify_auth_code(
190 $dsl, $settings, $client_id,$client_secret,$auth_code,$url
191 );
192 }
193  
194 6 ▶ if ( $client ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($client) { } 5 1 6
elsif ($error) { } 1 0 1
195  
196 5 $dsl->debug( "OAuth2::Server: Generating access token for $client" );
197  
198 5 ▶ my $expires_in = $settings->{access_token_ttl} // 3600;
Condition50%1/2
$settings->{"access_token_ttl"} // 3600
l!l
05
MC/DC0%0/1
$settings->{"access_token_ttl"} // 3600
$settings->{"access_token_ttl"}
Truth table50%1/2
A
A= $settings->{"access_token_ttl"}
Aresult
11
00
199 5 my $access_token = $server->generate_token($dsl, $settings, $expires_in,$client,$scope,'access',undef,$user_id );
200 5 my $refresh_token = $server->generate_token($dsl, $settings, undef,$client,$scope,'refresh',undef,$user_id );
201  
202 5 $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 5 $status = 200;
209 5 $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 1 $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 6 $dsl->response_header( 'Cache-Control' => 'no-store' );
227 6 $dsl->response_header( 'Pragma' => 'no-cache' );
228  
229 6 $dsl->status( $status );
230 6 return $dsl->send_as( JSON => $json_response );
231 }
232  
233 9 ▶sub _verify_access_token_and_scope {
Subroutine _verify_access_token_and_scope: called CC=4 SCAR=14.0
234 9 my ($dsl, $settings, $server, $refresh_token, @scopes) = @_;
235  
236 9 my $access_token;
237  
238 9 ▶ if ( ! $refresh_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $refresh_token) { } 8 1 9
239 8 ▶ if ( my $auth_header = $dsl->app->request->header( 'Authorization' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $auth_header = $dsl->app->request->header('Authorization')) { } 7 1 8
240 7 my ( $auth_type,$auth_access_token ) = split( / /,$auth_header );
241  
242 7 ▶ if ( $auth_type ne 'Bearer' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth_type ne 'Bearer') { } 0 7 7
243 0 $dsl->debug( "OAuth2::Server: Auth type is not 'Bearer'" );
244 0 return ( 0,'invalid_request' );
245 } else {
246 7 $access_token = $auth_access_token;
247 }
248 } else {
249 1 $dsl->debug( "OAuth2::Server: Authorization header missing" );
250 1 return ( 0,'invalid_request' );
251 }
252 } else {
253 1 $access_token = $refresh_token;
254 }
255  
256 8 return $server->verify_access_token($dsl, $settings, $access_token,\@scopes,$refresh_token );
257 }
258  
259 5 register_plugin;
260  
261 5 1;
262 __END__
« Server/Simple.pm ↑ Summary Server/Role.pm »