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

filter: Statementstmt 27.3% 29 / 106 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 44.4% 8 / 18 Podpod 0.0% 0 / 9 totaltotal 14.8% 37 / 249 SCAR 70.3
CC
42
Cov
15%
verify_auth_code
342.0
verify_access_token
240.0
verify_client
42.0
CRAP 1133.1
↑ Summary blib/lib/Dancer2/Plugin/OAuth2/Server.pm »
1 package Dancer2::Plugin::OAuth2::Server::Simple;
2 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Time::HiRes qw/ gettimeofday /;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MIME::Base64 qw/ encode_base64 decode_base64 /;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Crypt::PRNG qw/ random_string /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Dancer2::Plugin::OAuth2::Server::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 with 'Dancer2::Plugin::OAuth2::Server::Role';
9 1 ▶use feature qw/state/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub _get_clients {
12 0 ▶ my ($self, $dsl, $settings) = @_;
Subroutine _get_clients: not called CC=2 SCAR=17.9
13  
14 0 ▶ return $settings->{clients} // {};
Condition0%0/2
$settings->{"clients"} // {}
l!l
00
MC/DC0%0/1
$settings->{"clients"} // {}
$settings->{"clients"}
Truth table0%0/2
A
A= $settings->{"clients"}
Aresult
11
00
15 }
16  
17 sub login_resource_owner {
18 0 ▶ my ($self, $dsl, $settings) = @_;
Subroutine login_resource_owner: not called CC=1 SCAR=6.9
Pod undocumented
19  
20 0 return 1;
21 }
22  
23 sub confirm_by_resource_owner {
24 0 ▶ my ($self, $dsl, $settings, $client_id, $scopes) = @_;
Subroutine confirm_by_resource_owner: not called CC=1 SCAR=6.9
Pod undocumented
25  
26 0 return 1;
27 }
28  
29 sub verify_client {
30 0 ▶ my ($self, $dsl, $settings, $client_id, $scopes) = @_;
Subroutine verify_client: not called CC=6 SCAR=37.4
Pod undocumented
31  
32 0 ▶ if ( my $client = $self->_get_clients($dsl, $settings)->{$client_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $client = $self->_get_clients($dsl, $settings)->{$client_id}) 0 0 0
33  
34 0 ▶ foreach my $scope ( @{ $scopes // [] } ) {
Condition0%0/2
$scopes // []
l!l
00
MC/DC0%0/1
$scopes // []
$scopes
Truth table0%0/2
A
A= $scopes
Aresult
11
00
35  
36 0 ▶ if ( ! exists( $self->_get_clients($dsl, $settings)->{$client_id}{scopes}{$scope} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $self->_get_clients($dsl, $settings)->{$client_id}{'scopes'}{$scope}) { } 0 0 0
elsif (not $self->_get_clients($dsl, $settings)->{$client_id}{'scopes'}{$scope}) { } 0 0 0
37 0 $dsl->debug( "OAuth2::Server: Client lacks scope ($scope)" );
38 0 return ( 0,'invalid_scope' );
39 } elsif ( ! $self->_get_clients($dsl, $settings)->{$client_id}{scopes}{$scope} ) {
40 0 $dsl->debug( "OAuth2::Server: Client cannot scope ($scope)" );
41 0 return ( 0,'access_denied' );
42 }
43 }
44  
45 0 return ( 1 );
46 }
47  
48 0 $dsl->debug( "OAuth2::Server: Client ($client_id) does not exist" );
49 0 return ( 0,'unauthorized_client' );
50 }
51  
52 sub generate_token {
53 0 ▶ my ( $self, $dsl, $settings, $ttl,$client_id,$scopes,$type,$redirect_url,$user_id ) = @_;
Subroutine generate_token: not called CC=1 SCAR=6.9
Pod undocumented
54  
55 0 my $code;
56  
57 #if ( ! $JWT_SECRET ) {
58 0 my ( $sec,$usec ) = gettimeofday;
59 0 $code = encode_base64( join( '-',$sec,$usec,rand(),random_string(30) ),'' );
60 #} else {
61 #$code = Mojo::JWT->new(
62 #( $ttl ? ( expires => time + $ttl ) : () ),
63 #secret => $JWT_SECRET,
64 #set_iat => 1,
65 ## https://tools.ietf.org/html/rfc7519#section-4
66 #claims => {
67 ## Registered Claim Names
68 ## iss => undef, # us, the auth server / application (set using plugin config?)
69 ## sub => undef, # the logged in user, we could get this by returning it from the resource_owner_logged_in callback
70 #aud => $redirect_url, # the "audience"
71 #jti => random_string(32),
72  
73 ## Private Claim Names
74 #user_id => $user_id,
75 #client => $client_id,
76 #type => $type,
77 #scopes => $scopes,
78 #},
79 #)->encode;
80 #}
81  
82 0 return $code;
83 }
84  
85 1 state %AUTH_CODES;
86 sub store_auth_code {
87 0 ▶ my ( $self, $dsl, $settings, $auth_code,$client_id,$expires_in,$uri,@scopes ) = @_;
Subroutine store_auth_code: not called CC=1 SCAR=6.9
Pod undocumented
88 #return if $JWT_SECRET;
89  
90 $AUTH_CODES{$auth_code} = {
91 client_id => $client_id,
92 expires => time + $expires_in,
93 redirect_uri => $uri,
94 0 scope => { map { $_ => 1 } @scopes },
95 };
96  
97 0 return 1;
98 }
99  
100 1 state %REFRESH_TOKENS;
101 1 state %ACCESS_TOKENS;
102 sub verify_access_token {
103 0 ▶ my ( $self, $dsl, $settings, $access_token,$scopes_ref,$is_refresh_token ) = @_;
Subroutine verify_access_token: not called CC=15 SCAR=54.8
Pod undocumented
104  
105 #return _verify_access_token_jwt( @_ ) if $JWT_SECRET;
106  
107 0 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($is_refresh_token and exists $REFRESH_TOKENS{$access_token}) { } 0 0 0
elsif (exists $ACCESS_TOKENS{$access_token}) { } 0 0 0
Condition0%0/3
$is_refresh_token and exists $REFRESH_TOKENS{$access_token}
!ll&&!rl&&r
000
MC/DC0%0/2
$is_refresh_token and exists $REFRESH_TOKENS{$access_token}
$is_refresh_token exists $REFRESH_TOKENS{$access_token}
Truth table0%0/3
A and B
A= $is_refresh_tokenB= exists $REFRESH_TOKENS{$access_token}
ABresult
0X0
100
111
108 $is_refresh_token
109 && exists( $REFRESH_TOKENS{$access_token} )
110 ) {
111  
112 0 ▶ if ( $scopes_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scopes_ref) 0 0 0
113 0 ▶ foreach my $scope ( @{ $scopes_ref // [] } ) {
Condition0%0/2
$scopes_ref // []
l!l
00
MC/DC0%0/1
$scopes_ref // []
$scopes_ref
Truth table0%0/2
A
A= $scopes_ref
Aresult
11
00
114 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $REFRESH_TOKENS{$access_token}{"scope"}{$scope} and $REFRESH_TOKENS{$access_token}{"scope"}{$scope}) 0 0 0
Condition0%0/3
exists $REFRESH_TOKENS{$access_token}{"scope"}{$scope} and $REFRESH_TOKENS{$access_token}{"scope"}{$scope}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $REFRESH_TOKENS{$access_token}{"scope"}{$scope} and $REFRESH_TOKENS{$access_token}{"scope"}{$scope}
exists $REFRESH_TOKENS{$access_token}{"scope"}{$scope} $REFRESH_TOKENS{$access_token}{"scope"}{$scope}
Truth table0%0/3
A and B
A= exists $REFRESH_TOKENS{$access_token}{"scope"}{$scope}B= $REFRESH_TOKENS{$access_token}{"scope"}{$scope}
ABresult
0X0
100
111
115 ! exists( $REFRESH_TOKENS{$access_token}{scope}{$scope} )
116 or ! $REFRESH_TOKENS{$access_token}{scope}{$scope}
117 ) {
118 0 $dsl->debug( "OAuth2::Server: Refresh token does not have scope ($scope)" );
119 0 return ( 0,'invalid_grant' )
120 }
121 }
122 }
123  
124 0 return $REFRESH_TOKENS{$access_token}{client_id};
125 }
126 elsif ( exists( $ACCESS_TOKENS{$access_token} ) ) {
127  
128 0 ▶ if ( $ACCESS_TOKENS{$access_token}{expires} <= time ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ACCESS_TOKENS{$access_token}{'expires'} <= time) { } 0 0 0
elsif ($scopes_ref) { } 0 0 0
129 0 $dsl->debug( "OAuth2::Server: Access token has expired" );
130 0 $self->revoke_access_token( $dsl, $settings, $access_token );
131 0 return ( 0,'invalid_grant' )
132 } elsif ( $scopes_ref ) {
133  
134 0 ▶ foreach my $scope ( @{ $scopes_ref // [] } ) {
Condition0%0/2
$scopes_ref // []
l!l
00
MC/DC0%0/1
$scopes_ref // []
$scopes_ref
Truth table0%0/2
A
A= $scopes_ref
Aresult
11
00
135 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ACCESS_TOKENS{$access_token}{"scope"}{$scope} and $ACCESS_TOKENS{$access_token}{"scope"}{$scope}) 0 0 0
Condition0%0/3
exists $ACCESS_TOKENS{$access_token}{"scope"}{$scope} and $ACCESS_TOKENS{$access_token}{"scope"}{$scope}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ACCESS_TOKENS{$access_token}{"scope"}{$scope} and $ACCESS_TOKENS{$access_token}{"scope"}{$scope}
exists $ACCESS_TOKENS{$access_token}{"scope"}{$scope} $ACCESS_TOKENS{$access_token}{"scope"}{$scope}
Truth table0%0/3
A and B
A= exists $ACCESS_TOKENS{$access_token}{"scope"}{$scope}B= $ACCESS_TOKENS{$access_token}{"scope"}{$scope}
ABresult
0X0
100
111
136 ! exists( $ACCESS_TOKENS{$access_token}{scope}{$scope} )
137 or ! $ACCESS_TOKENS{$access_token}{scope}{$scope}
138 ) {
139 0 $dsl->debug( "OAuth2::Server: Access token does not have scope ($scope)" );
140 0 return ( 0,'invalid_grant' )
141 }
142 }
143  
144 }
145  
146 0 $dsl->debug( "OAuth2::Server: Access token is valid" );
147 0 return $ACCESS_TOKENS{$access_token}{client_id};
148 }
149  
150 0 $dsl->debug( "OAuth2::Server: Access token does not exist" );
151 0 return ( 0,'invalid_grant' )
152 }
153  
154 sub revoke_access_token {
155 0 ▶ my ( $self, $dsl, $settings, $access_token ) = @_;
Subroutine revoke_access_token: not called CC=1 SCAR=6.9
Pod undocumented
156 0 delete( $ACCESS_TOKENS{$access_token} );
157 }
158  
159 sub verify_auth_code {
160 0 ▶ my ($self, $dsl, $settings, $client_id,$client_secret,$auth_code,$uri ) = @_;
Subroutine verify_auth_code: not called CC=18 SCAR=58.3
Pod undocumented
161 #return _verify_auth_code_jwt( @_ ) if $JWT_SECRET;
162  
163 0 my ( $sec,$usec,$rand ) = split( '-',decode_base64( $auth_code ) );
164  
165 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $AUTH_CODES{$auth_code} && exists $self->_get_clients($dsl, $settings)->{$client_id} or $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{'client_secret'} or $AUTH_CODES{$auth_code}{'access_token'} or $uri and $AUTH_CODES{$auth_code}{'redirect_uri'} ne $uri or $AUTH_CODES{$auth_code}{'expires'} <= time) { } 0 0 0
Condition0%0/18
not exists $AUTH_CODES{$auth_code} && exists $self->_get_clients($dsl, $settings)->{$client_id} or $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{"client_secret"} or $AUTH_CODES{$auth_code}{"access_token"} or $uri and $AUTH_CODES{$auth_code}{"redirect_uri"} ne $uri or $AUTH_CODES{$auth_code}{"expires"} <= time
l!l&&r!l&&!r
000
not exists $AUTH_CODES{$auth_code} && exists $self->_get_clients($dsl, $settings)->{$client_id} or $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{"client_secret"} or $AUTH_CODES{$auth_code}{"access_token"} or $uri and $AUTH_CODES{$auth_code}{"redirect_uri"} ne $uri
l!l&&r!l&&!r
000
not exists $AUTH_CODES{$auth_code} && exists $self->_get_clients($dsl, $settings)->{$client_id} or $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{"client_secret"} or $AUTH_CODES{$auth_code}{"access_token"}
l!l&&r!l&&!r
000
not exists $AUTH_CODES{$auth_code} && exists $self->_get_clients($dsl, $settings)->{$client_id} or $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{"client_secret"}
l!l&&r!l&&!r
000
exists $AUTH_CODES{$auth_code} && exists $self->_get_clients($dsl, $settings)->{$client_id}
!ll&&!rl&&r
000
$uri and $AUTH_CODES{$auth_code}{"redirect_uri"} ne $uri
!ll&&!rl&&r
000
MC/DC0%0/7
not exists $AUTH_CODES{$auth_code} && exists $self->_get_clients($dsl, $settings)->{$client_id} or $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{"client_secret"} or $AUTH_CODES{$auth_code}{"access_token"} or $uri and $AUTH_CODES{$auth_code}{"redirect_uri"} ne $uri or $AUTH_CODES{$auth_code}{"expires"} <= time
exists $AUTH_CODES{$auth_code} exists $self->_get_clients($dsl, $settings)->{$client_id} $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{"client_secret"} $AUTH_CODES{$auth_code}{"access_token"} $uri $AUTH_CODES{$auth_code}{"redirect_uri"} ne $uri $AUTH_CODES{$auth_code}{"expires"} <= time
Truth table0%0/9
not(A && B) or C or D or E and F or G
A= exists $AUTH_CODES{$auth_code}B= exists $self->_get_clients($dsl, $settings)->{$client_id}C= $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{"client_secret"}D= $AUTH_CODES{$auth_code}{"access_token"}E= $uriF= $AUTH_CODES{$auth_code}{"redirect_uri"} ne $uriG= $AUTH_CODES{$auth_code}{"expires"} <= time
ABCDEFGresult
0XXXXXX1
10XXXXX1
111XXXX1
1101XXX1
110011X1
11000X11
11001011
11000X00
11001000
166 ! exists( $AUTH_CODES{$auth_code} )
167 or ! exists( $self->_get_clients($dsl, $settings)->{$client_id} )
168 or ( $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{client_secret} )
169 or $AUTH_CODES{$auth_code}{access_token}
170 or ( $uri && $AUTH_CODES{$auth_code}{redirect_uri} ne $uri )
171 or ( $AUTH_CODES{$auth_code}{expires} <= time )
172 ) {
173  
174 $dsl->debug( "OAuth2::Server: Auth code does not exist" )
175 0 ▶ if ! exists( $AUTH_CODES{$auth_code} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $AUTH_CODES{$auth_code} 0 0 0
176 $dsl->debug( "OAuth2::Server: Client ($client_id) does not exist" )
177 0 ▶ if ! exists( $self->_get_clients($dsl, $settings)->{$client_id} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->_get_clients($dsl, $settings)->{$client_id} 0 0 0
178 $dsl->debug( "OAuth2::Server: Client secret does not match" )
179 if (
180 ! $client_secret
181 or ! $self->_get_clients($dsl, $settings)->{$client_id}
182 or $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{client_secret}
183 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if not $client_secret && $self->_get_clients($dsl, $settings)->{$client_id} or $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{"client_secret"} 0 0 0
Condition0%0/6
not $client_secret && $self->_get_clients($dsl, $settings)->{$client_id} or $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{"client_secret"}
l!l&&r!l&&!r
000
$client_secret && $self->_get_clients($dsl, $settings)->{$client_id}
!ll&&!rl&&r
000
MC/DC0%0/3
not $client_secret && $self->_get_clients($dsl, $settings)->{$client_id} or $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{"client_secret"}
$client_secret $self->_get_clients($dsl, $settings)->{$client_id} $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{"client_secret"}
Truth table0%0/4
not(A && B) or C
A= $client_secretB= $self->_get_clients($dsl, $settings)->{$client_id}C= $client_secret ne $self->_get_clients($dsl, $settings)->{$client_id}{"client_secret"}
ABCresult
0XX1
10X1
1111
1100
184  
185 0 ▶ if ( $AUTH_CODES{$auth_code} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($AUTH_CODES{$auth_code}) 0 0 0
186 $dsl->debug( "OAuth2::Server: Redirect URI does not match" )
187 0 ▶ if ( $uri && $AUTH_CODES{$auth_code}{redirect_uri} ne $uri );
Branch: 1 branch
BranchTrueFalseTotal
if $uri and $AUTH_CODES{$auth_code}{"redirect_uri"} ne $uri 0 0 0
Condition0%0/3
$uri and $AUTH_CODES{$auth_code}{"redirect_uri"} ne $uri
!ll&&!rl&&r
000
MC/DC0%0/2
$uri and $AUTH_CODES{$auth_code}{"redirect_uri"} ne $uri
$uri $AUTH_CODES{$auth_code}{"redirect_uri"} ne $uri
Truth table0%0/3
A and B
A= $uriB= $AUTH_CODES{$auth_code}{"redirect_uri"} ne $uri
ABresult
0X0
100
111
188 $dsl->debug( "OAuth2::Server: Auth code expired" )
189 0 ▶ if ( $AUTH_CODES{$auth_code}{expires} <= time );
Branch: 1 branch
BranchTrueFalseTotal
if $AUTH_CODES{$auth_code}{"expires"} <= time 0 0 0
190 }
191  
192 0 ▶ if ( my $access_token = $AUTH_CODES{$auth_code}{access_token} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $access_token = $AUTH_CODES{$auth_code}{"access_token"}) 0 0 0
193 # this auth code has already been used to generate an access token
194 # so we need to revoke the access token that was previously generated
195 0 $dsl->debug(
196 "OAuth2::Server: Auth code already used to get access token"
197 );
198  
199 0 $self->revoke_access_token($dsl, $settings, $access_token );
200 }
201  
202 0 return ( 0,'invalid_grant' );
203 } else {
204 0 return ( 1,undef,$AUTH_CODES{$auth_code}{scope} );
205 }
206  
207 }
208  
209 sub store_access_token {
210 my (
211 0 ▶ $self, $dsl, $settings, $c_id,$auth_code,$access_token,$refresh_token,
Subroutine store_access_token: not called CC=5 SCAR=34.0
Pod undocumented
212 $expires_in,$scope,$old_refresh_token
213 ) = @_;
214 #return if $JWT_SECRET;
215  
216 0 ▶ if ( ! defined( $auth_code ) && $old_refresh_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $auth_code and $old_refresh_token) 0 0 0
Condition0%0/3
not defined $auth_code and $old_refresh_token
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $auth_code and $old_refresh_token
not defined $auth_code $old_refresh_token
Truth table0%0/3
not(A) and B
A= not defined $auth_codeB= $old_refresh_token
ABresult
0X0
100
111
217 # must have generated an access token via a refresh token so revoke the old
218 # access token and refresh token and update the AUTH_CODES hash to store the
219 # new one (also copy across scopes if missing)
220 0 $auth_code = $REFRESH_TOKENS{$old_refresh_token}{auth_code};
221  
222 0 my $prev_access_token = $REFRESH_TOKENS{$old_refresh_token}{access_token};
223  
224 # access tokens can be revoked, whilst refresh tokens can remain so we
225 # need to get the data from the refresh token as the access token may
226 # no longer exist at the point that the refresh token is used
227 0 ▶ $scope //= $REFRESH_TOKENS{$old_refresh_token}{scope};
Condition0%0/3
$scope //= $REFRESH_TOKENS{$old_refresh_token}{"scope"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$scope //= $REFRESH_TOKENS{$old_refresh_token}{"scope"}
$scope $REFRESH_TOKENS{$old_refresh_token}{"scope"}
Truth table0%0/3
A //= B
A= $scopeB= $REFRESH_TOKENS{$old_refresh_token}{"scope"}
ABresult
1X1
011
000
228  
229 0 $dsl->debug( "OAuth2::Server: Revoking old access token (refresh)" );
230 0 $self->revoke_access_token($dsl, $settings, $prev_access_token );
231 }
232  
233 0 ▶ delete( $REFRESH_TOKENS{$old_refresh_token} )
Branch: 1 branch
BranchTrueFalseTotal
if $old_refresh_token 0 0 0
234 if $old_refresh_token;
235  
236 0 $ACCESS_TOKENS{$access_token} = {
237 scope => $scope,
238 expires => time + $expires_in,
239 refresh_token => $refresh_token,
240 client_id => $c_id,
241 };
242  
243 0 $REFRESH_TOKENS{$refresh_token} = {
244 scope => $scope,
245 client_id => $c_id,
246 access_token => $access_token,
247 auth_code => $auth_code,
248 };
249  
250 0 $AUTH_CODES{$auth_code}{access_token} = $access_token;
251  
252 0 return $c_id;
253 }
254  
255  
256 1 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
257 1 1;
↑ Summary blib/lib/Dancer2/Plugin/OAuth2/Server.pm »