Keycloak/OIDC.pm

filter: Statementstmt 97.8% 92 / 94 Branchbran 90.9% 20 / 22 Conditioncond 91.3% 21 / 23 MC/DCmcdc 75.0% 9 / 12 Subroutinesub 96.7% 30 / 31 Podpod 66.6% 12 / 18 totaltotal 92.0% 184 / 200 CC 21 SCAR 15.2
CC
21
Cov
96%
verify_token
13.0
_token_call
4.0
jwks
3.0
CRAP 4.6
« Keycloak/Auth.pm ↑ Summary Keycloak/Admin.pm »
1 package WWW::Keycloak::OIDC;
2  
3 # ABSTRACT: OpenID Connect against one Keycloak realm
4  
5 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 with 'WWW::Keycloak::Role::HTTP';
7 6 ▶use Crypt::JWT qw( decode_jwt );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Types::Standard qw( ArrayRef CodeRef InstanceOf Int Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use WWW::Keycloak::Error::Validation;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 our $VERSION = '0.001';
14  
15  
16 6 has issuer => (
17 is => 'ro',
18 isa => Str,
19 required => 1
20 );
21  
22  
23 6 has ua => (
24 is => 'ro',
25 isa => InstanceOf['LWP::UserAgent'],
26 required => 1
27 );
28  
29  
30 has algorithms => (
31 is => 'ro',
32 isa => ArrayRef[Str],
33 6 ▶ default => sub { [qw( RS256 RS384 RS512 PS256 PS384 PS512 ES256 ES384 ES512 )] }
Subroutine __ANON__: called CC=1 SCAR=0.0
34 6 );
35  
36  
37 6 has jwks_min_age => (
38 is => 'ro',
39 isa => Int,
40 default => 60
41 );
42  
43  
44 has now => (
45 is => 'ro',
46 isa => CodeRef,
47 5 ▶ default => sub { sub { time } }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
48 6 );
49  
50  
51 6 has _jwks_fetched => (
52 is => 'rw',
53 init_arg => undef
54 );
55  
56 6 has discovery => (
57 is => 'lazy',
58 init_arg => undef
59 );
60  
61 5 ▶sub _build_discovery {
Subroutine _build_discovery: called CC=2 SCAR=7.0
62 5 my ( $self ) = @_;
63 5 my $data = $self->send_request( GET => $self->issuer.'/.well-known/openid-configuration' )->{data};
64 4 ▶ WWW::Keycloak::Error::Validation->throw( message => 'discovery for '.$self->issuer.' returned no JSON object' ) unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 4 4
65 4 return $data;
66 }
67  
68  
69 6 has _jwks => (
70 is => 'rw',
71 init_arg => undef
72 );
73  
74 19 ▶sub endpoint {
Subroutine endpoint: called CC=2 SCAR=6.9
Pod documented
75 19 my ( $self, $name ) = @_;
76 19 my $url = $self->discovery->{$name};
77 19 ▶ WWW::Keycloak::Error::Validation->throw( message => 'the discovery document of '.$self->issuer.' has no '.$name ) unless defined $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url 1 18 19
78 18 return $url;
79 }
80  
81  
82 7 ▶sub token_endpoint { $_[0]->endpoint('token_endpoint') }
Subroutine token_endpoint: called CC=1 SCAR=0.0
Pod undocumented
83 3 ▶sub userinfo_endpoint { $_[0]->endpoint('userinfo_endpoint') }
Subroutine userinfo_endpoint: called CC=1 SCAR=0.0
Pod undocumented
84 1 ▶sub introspection_endpoint { $_[0]->endpoint('introspection_endpoint') }
Subroutine introspection_endpoint: called CC=1 SCAR=0.0
Pod undocumented
85 1 ▶sub end_session_endpoint { $_[0]->endpoint('end_session_endpoint') }
Subroutine end_session_endpoint: called CC=1 SCAR=0.0
Pod undocumented
86 2 ▶sub device_endpoint { $_[0]->endpoint('device_authorization_endpoint') }
Subroutine device_endpoint: called CC=1 SCAR=0.0
Pod undocumented
87 4 ▶sub jwks_uri { $_[0]->endpoint('jwks_uri') }
Subroutine jwks_uri: called CC=1 SCAR=0.0
Pod undocumented
88  
89 21 ▶sub jwks {
Subroutine jwks: called CC=3 SCAR=11.0
Pod documented
90 21 my ( $self, %opt ) = @_;
91 21 ▶ if ( $opt{force_refresh} || !$self->_jwks ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"force_refresh"} or not $self->_jwks) 4 17 21
Condition100%3/3
$opt{"force_refresh"} or not $self->_jwks
l!l&&r!l&&!r
1317
MC/DC100%2/2
$opt{"force_refresh"} or not $self->_jwks
$opt{"force_refresh"} not $self->_jwks
Truth table100%3/3
A or not(B)
A= $opt{"force_refresh"}B= not $self->_jwks
ABresult
1X1
011
000
92 4 $self->_jwks( $self->send_request( GET => $self->jwks_uri )->{data} );
93 4 $self->_jwks_fetched( $self->now->() );
94 }
95 21 return $self->_jwks;
96 }
97  
98  
99 20 ▶sub verify_token {
Subroutine verify_token: called CC=13 SCAR=25.7
Pod documented
100 20 my ( $self, $token, %opt ) = @_;
101 20 ▶ WWW::Keycloak::Error::Validation->throw( message => 'verify_token needs a token' ) unless defined $token && length $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and length $token 1 19 20
Condition66%2/3
defined $token and length $token
!ll&&!rl&&r
0119
MC/DC50%1/2
defined $token and length $token
defined $token length $token
Truth table66%2/3
A and B
A= defined $tokenB= length $token
ABresult
0X0
100
111
102 my %check = (
103 token => $token,
104 verify_iss => $self->issuer,
105 verify_exp => 1,
106 accepted_alg => $self->algorithms,
107 decode_payload => 1,
108 19 ▶ defined $opt{audience} ? ( verify_aud => $opt{audience} ) : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'audience'} ? : 7 12 19
109 );
110 19 my $claims = eval { decode_jwt( %check, kid_keys => $self->jwks ) };
111 19 my $error = $@;
112 # A key Keycloak rotated in since the keys were fetched: fetch them again,
113 # but only for that reason and not more often than jwks_min_age allows.
114 19 ▶ if ( !$claims && $error =~ /kid_keys lookup failed/ && $self->now->() - ( $self->_jwks_fetched // 0 ) >= $self->jwks_min_age ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $claims and $error =~ /kid_keys lookup failed/ and $self->now->() - ($self->_jwks_fetched // 0) >= $self->jwks_min_age) 1 18 19
Condition87%7/8
not $claims and $error =~ /kid_keys lookup failed/ and $self->now->() - ($self->_jwks_fetched // 0) >= $self->jwks_min_age
!ll&&!rl&&r
1621
not $claims and $error =~ /kid_keys lookup failed/
!ll&&!rl&&r
6103
$self->_jwks_fetched // 0
l!l
30
MC/DC100%3/3
not $claims and $error =~ /kid_keys lookup failed/ and $self->now->() - ($self->_jwks_fetched // 0) >= $self->jwks_min_age
not $claims $error =~ /kid_keys lookup failed/ $self->now->() - ($self->_jwks_fetched // 0) >= $self->jwks_min_age
MC/DC0%0/1
$self->_jwks_fetched // 0
$self->_jwks_fetched
Truth table100%4/4
not(A) and B and C
A= not $claimsB= $error =~ /kid_keys lookup failed/C= $self->now->() - ($self->_jwks_fetched // 0) >= $self->jwks_min_age
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $self->_jwks_fetched
Aresult
11
00
115 1 $claims = eval { decode_jwt( %check, kid_keys => $self->jwks( force_refresh => 1 ) ) };
116 1 $error = $@;
117 }
118 19 ▶ $self->_reject( $error =~ s/ at \S+ line \d+.*//sr ) unless $claims;
Branch: 1 branch
BranchTrueFalseTotal
unless $claims 12 7 19
119 7 ▶ if ( defined $opt{type} && ( $claims->{typ} // '' ) ne $opt{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"type"} and ($claims->{"typ"} // "") ne $opt{"type"}) 2 5 7
Condition100%5/5
defined $opt{"type"} and ($claims->{"typ"} // "") ne $opt{"type"}
!ll&&!rl&&r
412
$claims->{"typ"} // ""
l!l
21
MC/DC100%2/2
defined $opt{"type"} and ($claims->{"typ"} // "") ne $opt{"type"}
defined $opt{"type"} $claims->{"typ"}
Truth table100%3/3
A and B
A= defined $opt{"type"}B= $claims->{"typ"}
ABresult
0X0
100
111
120 2 ▶ $self->_reject( 'typ is '.( $claims->{typ} // 'missing' ).', expected '.$opt{type} );
Condition100%2/2
$claims->{"typ"} // "missing"
l!l
11
MC/DC100%1/1
$claims->{"typ"} // "missing"
$claims->{"typ"}
Truth table100%2/2
A
A= $claims->{"typ"}
Aresult
11
00
121 }
122 5 return $claims;
123 }
124  
125 14 ▶sub _reject {
Subroutine _reject: called CC=1 SCAR=0.0
126 14 my ( $self, $why ) = @_;
127 14 WWW::Keycloak::Error::Validation->throw( message => 'token rejected: '.$why );
128 }
129  
130  
131 2 ▶sub userinfo {
Subroutine userinfo: called CC=1 SCAR=0.0
Pod documented
132 2 my ( $self, $access_token ) = @_;
133 2 return $self->send_request( GET => $self->userinfo_endpoint, bearer => $access_token )->{data};
134 }
135  
136  
137 1 ▶sub introspect {
Subroutine introspect: called CC=1 SCAR=0.0
Pod documented
138 1 my ( $self, $token, %client ) = @_;
139 1 return $self->_token_call( $self->introspection_endpoint, { token => $token }, %client );
140 }
141  
142  
143 3 ▶sub password_token {
Subroutine password_token: called CC=1 SCAR=0.0
Pod documented
144 3 my ( $self, %arg ) = @_;
145 3 return $self->_grant( password => [qw( username password totp scope )], %arg );
146 }
147  
148 1 ▶sub client_credentials_token {
Subroutine client_credentials_token: called CC=1 SCAR=0.0
Pod documented
149 1 my ( $self, %arg ) = @_;
150 1 return $self->_grant( client_credentials => ['scope'], %arg );
151 }
152  
153 1 ▶sub refresh_token {
Subroutine refresh_token: called CC=1 SCAR=0.0
Pod documented
154 1 my ( $self, $refresh, %arg ) = @_;
155 1 return $self->_grant( refresh_token => [qw( refresh_token scope )], %arg, refresh_token => $refresh );
156 }
157  
158 0 ▶sub exchange_authorization_code {
Subroutine exchange_authorization_code: not called CC=1 SCAR=6.9
Pod documented
159 0 my ( $self, %arg ) = @_;
160 0 return $self->_grant( authorization_code => [qw( code redirect_uri code_verifier )], %arg );
161 }
162  
163 1 ▶sub device_token {
Subroutine device_token: called CC=1 SCAR=0.0
Pod documented
164 1 my ( $self, %arg ) = @_;
165 1 return $self->_grant( 'urn:ietf:params:oauth:grant-type:device_code' => ['device_code'], %arg );
166 }
167  
168  
169 1 ▶sub device_authorization {
Subroutine device_authorization: called CC=2 SCAR=7.2
Pod documented
170 1 my ( $self, %arg ) = @_;
171 1 ▶ return $self->_token_call( $self->device_endpoint, { defined $arg{scope} ? ( scope => $arg{scope} ) : () }, %arg );
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'scope'} ? : 1 0 1
172 }
173  
174  
175 1 ▶sub logout {
Subroutine logout: called CC=1 SCAR=0.0
Pod documented
176 1 my ( $self, %arg ) = @_;
177 1 $self->_token_call( $self->end_session_endpoint, { refresh_token => $arg{refresh_token} }, %arg );
178 1 return 1;
179 }
180  
181  
182 6 ▶sub _grant {
Subroutine _grant: called CC=1 SCAR=0.0
183 6 my ( $self, $type, $fields, %arg ) = @_;
184 6 return $self->_token_call( $self->token_endpoint, { grant_type => $type, map { $_ => $arg{$_} } grep { defined $arg{$_} } @$fields }, %arg );
185 }
186  
187 9 ▶sub _token_call {
Subroutine _token_call: called CC=4 SCAR=13.9
188 9 my ( $self, $url, $form, %arg ) = @_;
189 9 ▶ WWW::Keycloak::Error::Validation->throw( message => 'a client_id is needed' ) unless defined $arg{client_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg{"client_id"} 1 8 9
190 8 ▶ my %form = ( %$form, client_id => $arg{client_id}, defined $arg{client_secret} ? ( client_secret => $arg{client_secret} ) : () );
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'client_secret'} ? : 2 6 8
191 8 ▶ return $self->send_request( POST => $url, form => \%form )->{data} // {};
Condition100%2/2
$self->send_request("POST", $url, "form", \%form)->{"data"} // {}
l!l
51
MC/DC0%0/1
$self->send_request("POST", $url, "form", \%form)->{"data"} // {}
$self->send_request("POST", $url, "form", \%form)->{"data"}
Truth table50%1/2
A
A= $self->send_request("POST", $url, "form", \%form)->{"data"}
Aresult
11
00
192 }
193  
194 6 1;
195  
196 __END__
« Keycloak/Auth.pm ↑ Summary Keycloak/Admin.pm »