Keycloak/OIDC.pm

filter: Statementstmt 98.5% 132 / 134 Branchbran 92.8% 26 / 28 Conditioncond 92.3% 24 / 26 MC/DCmcdc 85.7% 12 / 14 Subroutinesub 97.3% 37 / 38 Podpod 100% 19 / 19 totaltotal 96.5% 250 / 259 CC 25 SCAR 15.0
CC
25
Cov
97%
verify_token_f
14.0
_token_call_f
4.0
_shared
3.0
CRAP 4.5
« Keycloak/Auth.pm ↑ Summary Keycloak/Admin.pm »
1 package Net::Async::Keycloak::OIDC;
2  
3 # ABSTRACT: OpenID Connect against one Keycloak realm, asynchronously
4  
5 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 with 'Net::Async::Keycloak::Role::HTTP';
7 7 with 'WWW::Keycloak::Role::HTTP';
8 7 ▶use Crypt::JWT qw( decode_jwt );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Types::Standard qw( ArrayRef CodeRef Int Object Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 our $VERSION = '0.001';
15  
16  
17 7 has issuer => ( is => 'ro', isa => Str, required => 1 );
18 7 has http => ( is => 'ro', isa => Object, required => 1 );
19  
20  
21 has algorithms => (
22 is => 'ro',
23 isa => ArrayRef[Str],
24 9 ▶ default => sub { [qw( RS256 RS384 RS512 PS256 PS384 PS512 ES256 ES384 ES512 )] }
Subroutine __ANON__: called CC=1 SCAR=0.0
25 7 );
26 7 has jwks_min_age => ( is => 'ro', isa => Int, default => 60 );
27 8 ▶has now => ( is => 'ro', isa => CodeRef, default => sub { sub { time } } );
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
28  
29  
30 7 has _discovery => ( is => 'rw' );
31 7 has _jwks => ( is => 'rw' );
32 7 has _jwks_fetched => ( is => 'rw' );
33 9 ▶has _in_flight => ( is => 'ro', init_arg => undef, default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
34  
35 # One fetch for everybody who asks while it is under way, each with a view of
36 # its own so that cancelling one does not cancel the fetch.
37 52 ▶sub _shared {
Subroutine _shared: called CC=3 SCAR=11.0
38 52 my ( $self, $key, $start ) = @_;
39 52 my $in_flight = $self->_in_flight;
40 52 ▶ unless ( $in_flight->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_flight->{$key}) 14 38 52
41 14 ▶ my $future = $start->()->on_ready( sub { delete $in_flight->{$key} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
42 14 ▶ return $future if $future->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $future->is_ready 10 4 14
43 4 $in_flight->{$key} = $future;
44 }
45 42 return $in_flight->{$key}->without_cancel;
46 }
47  
48 26 ▶sub discovery_f {
Subroutine discovery_f: called CC=2 SCAR=6.9
Pod documented
49 26 my ( $self ) = @_;
50 26 ▶ return Future->done( $self->_discovery ) if $self->_discovery;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_discovery 18 8 26
51 8 ▶ return $self->_shared( discovery => sub { $self->_fetch_discovery_f } );
Subroutine __ANON__: called CC=1 SCAR=0.0
52 }
53  
54 8 ▶async sub _fetch_discovery_f {
Subroutine _fetch_discovery_f: called CC=2 SCAR=7.0
55 8 my ( $self ) = @_;
56 8 my $data = ( await $self->send_request_f( GET => $self->issuer.'/.well-known/openid-configuration' ) )->{data};
57 6 ▶ $self->validation_error_class->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 6 6
58 6 return $self->_discovery($data);
59 7 }
60  
61  
62 24 ▶async sub endpoint_f {
Subroutine endpoint_f: called CC=2 SCAR=6.9
Pod documented
63 24 my ( $self, $name ) = @_;
64 24 my $url = ( await $self->discovery_f )->{$name};
65 24 ▶ $self->validation_error_class->throw( message => 'the discovery document of '.$self->issuer.' has no '.$name ) unless defined $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url 1 23 24
66 23 return $url;
67 7 }
68  
69  
70 7 ▶sub token_endpoint_f { $_[0]->endpoint_f('token_endpoint') }
Subroutine token_endpoint_f: called CC=1 SCAR=0.0
Pod documented
71 3 ▶sub userinfo_endpoint_f { $_[0]->endpoint_f('userinfo_endpoint') }
Subroutine userinfo_endpoint_f: called CC=1 SCAR=0.0
Pod documented
72 1 ▶sub introspection_endpoint_f { $_[0]->endpoint_f('introspection_endpoint') }
Subroutine introspection_endpoint_f: called CC=1 SCAR=0.0
Pod documented
73 1 ▶sub end_session_endpoint_f { $_[0]->endpoint_f('end_session_endpoint') }
Subroutine end_session_endpoint_f: called CC=1 SCAR=0.0
Pod documented
74 2 ▶sub device_endpoint_f { $_[0]->endpoint_f('device_authorization_endpoint') }
Subroutine device_endpoint_f: called CC=1 SCAR=0.0
Pod documented
75 7 ▶sub jwks_uri_f { $_[0]->endpoint_f('jwks_uri') }
Subroutine jwks_uri_f: called CC=1 SCAR=0.0
Pod documented
76  
77 61 ▶sub jwks_f {
Subroutine jwks_f: called CC=3 SCAR=11.0
Pod documented
78 61 my ( $self, %opt ) = @_;
79 61 ▶ return Future->done( $self->_jwks ) if $self->_jwks && !$opt{force_refresh};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_jwks and not $opt{"force_refresh"} 17 44 61
Condition100%3/3
$self->_jwks and not $opt{"force_refresh"}
!ll&&!rl&&r
232117
MC/DC100%2/2
$self->_jwks and not $opt{"force_refresh"}
$self->_jwks not $opt{"force_refresh"}
Truth table100%3/3
A and not(B)
A= $self->_jwksB= not $opt{"force_refresh"}
ABresult
0X0
100
111
80 6 ▶ return $self->_shared( jwks => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
81 # counted from the start, so that verifications already under way see it
82 6 $self->_jwks_fetched( $self->now->() );
83 6 $self->_fetch_jwks_f;
84 44 } );
85 }
86  
87 6 ▶async sub _fetch_jwks_f {
Subroutine _fetch_jwks_f: called CC=1 SCAR=0.0
88 6 my ( $self ) = @_;
89 6 my $uri = await $self->jwks_uri_f;
90 6 return $self->_jwks( ( await $self->send_request_f( GET => $uri ) )->{data} );
91 7 }
92  
93  
94 40 ▶async sub verify_token_f {
Subroutine verify_token_f: called CC=14 SCAR=26.4
Pod documented
95 40 my ( $self, $token, %opt ) = @_;
96 40 ▶ $self->validation_error_class->throw( message => 'verify_token needs a token' ) unless defined $token && length $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and length $token 1 39 40
Condition66%2/3
defined $token and length $token
!ll&&!rl&&r
0139
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
97 my %check = (
98 token => $token,
99 verify_iss => $self->issuer,
100 verify_exp => 1,
101 accepted_alg => $self->algorithms,
102 decode_payload => 1,
103 39 ▶ defined $opt{audience} ? ( verify_aud => $opt{audience} ) : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'audience'} ? : 7 32 39
104 );
105 39 my $keys = await $self->jwks_f;
106 39 my $claims = eval { decode_jwt( %check, kid_keys => $keys ) };
107 39 my $error = $@;
108 39 ▶ my $again = $self->_in_flight->{jwks} || $self->now->() - ( $self->_jwks_fetched // 0 ) >= $self->jwks_min_age;
Condition80%4/5
$self->_in_flight->{'jwks'} || $self->now->() - ($self->_jwks_fetched // 0) >= $self->jwks_min_age
l!l&&r!l&&!r
19218
$self->_jwks_fetched // 0
l!l
200
MC/DC100%2/2
$self->_in_flight->{'jwks'} || $self->now->() - ($self->_jwks_fetched // 0) >= $self->jwks_min_age
$self->_in_flight->{'jwks'} $self->now->() - ($self->_jwks_fetched // 0) >= $self->jwks_min_age
MC/DC0%0/1
$self->_jwks_fetched // 0
$self->_jwks_fetched
Truth table100%3/3
A || B
A= $self->_in_flight->{'jwks'}B= $self->now->() - ($self->_jwks_fetched // 0) >= $self->jwks_min_age
ABresult
1X1
011
000
Truth table50%1/2
A
A= $self->_jwks_fetched
Aresult
11
00
109 39 ▶ if ( !$claims && $error =~ /kid_keys lookup failed/ && $again ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $claims and $error =~ /kid_keys lookup failed/ and $again) 21 18 39
Condition100%6/6
not $claims and $error =~ /kid_keys lookup failed/ and $again
!ll&&!rl&&r
16221
not $claims and $error =~ /kid_keys lookup failed/
!ll&&!rl&&r
61023
MC/DC100%3/3
not $claims and $error =~ /kid_keys lookup failed/ and $again
not $claims $error =~ /kid_keys lookup failed/ $again
Truth table100%4/4
not(A) and B and C
A= not $claimsB= $error =~ /kid_keys lookup failed/C= $again
ABCresult
0XX0
10X0
1100
1111
110 21 $keys = await $self->jwks_f( force_refresh => 1 );
111 21 $claims = eval { decode_jwt( %check, kid_keys => $keys ) };
112 21 $error = $@;
113 }
114 39 ▶ $self->_reject( $error =~ s/ at \S+ line \d+.*//sr ) unless $claims;
Branch: 1 branch
BranchTrueFalseTotal
unless $claims 32 7 39
115 $self->_reject( 'typ is '.( $claims->{typ} // 'missing' ).', expected '.$opt{type} )
116 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%7/7
defined $opt{"type"} and ($claims->{"typ"} // "") ne $opt{"type"}
!ll&&!rl&&r
412
$claims->{"typ"} // ""
l!l
21
$claims->{"typ"} // "missing"
l!l
11
MC/DC100%2/2
defined $opt{"type"} and ($claims->{"typ"} // "") ne $opt{"type"}
defined $opt{"type"} $claims->{"typ"}
MC/DC100%1/1
$claims->{"typ"} // "missing"
$claims->{"typ"}
Truth table100%3/3
A and B
A= defined $opt{"type"}B= $claims->{"typ"}
ABresult
0X0
100
111
Truth table100%2/2
A
A= $claims->{"typ"}
Aresult
11
00
117 5 return $claims;
118 7 }
119  
120 34 ▶sub _reject {
Subroutine _reject: called CC=1 SCAR=0.0
121 34 my ( $self, $why ) = @_;
122 34 $self->validation_error_class->throw( message => 'token rejected: '.$why );
123 }
124  
125  
126 2 ▶async sub userinfo_f {
Subroutine userinfo_f: called CC=1 SCAR=0.0
Pod documented
127 2 my ( $self, $access_token ) = @_;
128 2 return ( await $self->send_request_f( GET => await( $self->userinfo_endpoint_f ), bearer => $access_token ) )->{data};
129 7 }
130  
131 1 ▶async sub introspect_f {
Subroutine introspect_f: called CC=1 SCAR=0.0
Pod documented
132 1 my ( $self, $token, %client ) = @_;
133 1 return await $self->_token_call_f( await( $self->endpoint_f('introspection_endpoint') ), { token => $token }, %client );
134 7 }
135  
136 3 ▶sub password_token_f { my ( $self, %arg ) = @_; $self->_grant_f( password => [qw( username password totp scope )], %arg ) }
Subroutine password_token_f: called CC=1 SCAR=0.0
Pod documented
137 1 ▶sub client_credentials_token_f { my ( $self, %arg ) = @_; $self->_grant_f( client_credentials => ['scope'], %arg ) }
Subroutine client_credentials_token_f: called CC=1 SCAR=0.0
Pod documented
138 1 ▶sub refresh_token_f { my ( $self, $refresh, %arg ) = @_; $self->_grant_f( refresh_token => [qw( refresh_token scope )], %arg, refresh_token => $refresh ) }
Subroutine refresh_token_f: called CC=1 SCAR=0.0
Pod documented
139 0 ▶sub exchange_authorization_code_f { my ( $self, %arg ) = @_; $self->_grant_f( authorization_code => [qw( code redirect_uri code_verifier )], %arg ) }
Subroutine exchange_authorization_code_f: not called CC=1 SCAR=6.9
Pod documented
140 1 ▶sub device_token_f { my ( $self, %arg ) = @_; $self->_grant_f( 'urn:ietf:params:oauth:grant-type:device_code' => ['device_code'], %arg ) }
Subroutine device_token_f: called CC=1 SCAR=0.0
Pod documented
141  
142 1 ▶async sub device_authorization_f {
Subroutine device_authorization_f: called CC=2 SCAR=7.1
Pod documented
143 1 my ( $self, %arg ) = @_;
144 1 ▶ return await $self->_token_call_f( await( $self->device_endpoint_f ), { defined $arg{scope} ? ( scope => $arg{scope} ) : () }, %arg );
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'scope'} ? : 1 0 1
145 7 }
146  
147 1 ▶async sub logout_f {
Subroutine logout_f: called CC=1 SCAR=0.0
Pod documented
148 1 my ( $self, %arg ) = @_;
149 1 await $self->_token_call_f( await( $self->endpoint_f('end_session_endpoint') ), { refresh_token => $arg{refresh_token} }, %arg );
150 1 return 1;
151 7 }
152  
153  
154 6 ▶async sub _grant_f {
Subroutine _grant_f: called CC=1 SCAR=0.0
155 6 my ( $self, $type, $fields, %arg ) = @_;
156 return await $self->_token_call_f( await( $self->token_endpoint_f ),
157 6 { grant_type => $type, map { $_ => $arg{$_} } grep { defined $arg{$_} } @$fields }, %arg );
158 7 }
159  
160 9 ▶async sub _token_call_f {
Subroutine _token_call_f: called CC=4 SCAR=13.9
161 9 my ( $self, $url, $form, %arg ) = @_;
162 9 ▶ $self->validation_error_class->throw( message => 'a client_id is needed' ) unless defined $arg{client_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg{"client_id"} 1 8 9
163 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
164 8 ▶ return ( await $self->send_request_f( POST => $url, form => \%form ) )->{data} // {};
Condition100%2/2
<custom op await>->{"data"} // {}
l!l
51
MC/DC100%1/1
<custom op await>->{"data"} // {}
<custom op await>->{"data"}
Truth table100%2/2
A
A= <custom op await>->{"data"}
Aresult
11
00
165 7 }
166  
167 7 1;
168  
169 __END__
« Keycloak/Auth.pm ↑ Summary Keycloak/Admin.pm »