Authentik/OIDC.pm

filter: Statementstmt 99.1% 111 / 112 Branchbran 87.5% 42 / 48 Conditioncond 87.1% 34 / 39 MC/DCmcdc 77.2% 17 / 22 Subroutinesub 97.1% 34 / 35 Podpod 100% 22 / 22 totaltotal 93.5% 260 / 278 CC 39 SCAR 21.5
CC
39
Cov
94%
verify_token
23.0
_token_call
4.1
authorization_url
4.0
CRAP 8.6
« Authentik/Diff.pm ↑ Summary Authentik/API.pm »
1 package WWW::Authentik::OIDC;
2  
3 # ABSTRACT: OpenID Connect against one authentik application
4  
5 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 with 'WWW::Authentik::Role::HTTP';
7 7 ▶use Crypt::JWT qw( decode_jwt );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Types::Standard qw( ArrayRef CodeRef InstanceOf Int Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use WWW::Authentik::Error::Validation;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 our $VERSION = '0.001';
14  
15  
16 7 has application_url => (
17 is => 'ro',
18 isa => Str,
19 required => 1
20 );
21  
22  
23 7 has ua => (
24 is => 'ro',
25 isa => InstanceOf['LWP::UserAgent'],
26 required => 1
27 );
28  
29  
30 7 has client_id => (
31 is => 'ro',
32 isa => Str,
33 predicate => 'has_client_id'
34 );
35  
36  
37 has algorithms => (
38 is => 'ro',
39 isa => ArrayRef[Str],
40 14 ▶ default => sub { [qw( RS256 RS384 RS512 PS256 PS384 PS512 ES256 ES384 ES512 )] }
Subroutine __ANON__: called CC=1 SCAR=0.0
41 7 );
42  
43  
44 7 has jwks_min_age => (
45 is => 'ro',
46 isa => Int,
47 default => 60
48 );
49  
50  
51 has now => (
52 is => 'ro',
53 isa => CodeRef,
54 0 ▶ default => sub { sub { time } }
Subroutine __ANON__: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
55 7 );
56  
57  
58 7 has discovery => (
59 is => 'lazy',
60 init_arg => undef
61 );
62  
63 8 ▶sub _build_discovery {
Subroutine _build_discovery: called CC=2 SCAR=7.0
64 8 my ( $self ) = @_;
65 8 my $data = $self->send_request( GET => $self->application_url.'/.well-known/openid-configuration' )->{data};
66 6 ▶ WWW::Authentik::Error::Validation->throw( message => 'discovery for '.$self->application_url.' returned no JSON object' )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 6 6
67 unless ref $data eq 'HASH';
68 6 return $data;
69 }
70  
71  
72 7 has _jwks => ( is => 'rw', init_arg => undef );
73 7 has _jwks_fetched => ( is => 'rw', init_arg => undef );
74  
75 99 ▶sub endpoint {
Subroutine endpoint: called CC=2 SCAR=6.9
Pod documented
76 99 my ( $self, $name ) = @_;
77 99 my $url = $self->discovery->{$name};
78 99 ▶ WWW::Authentik::Error::Validation->throw( message => 'the discovery document of '.$self->application_url.' has no '.$name )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url 1 98 99
79 unless defined $url;
80 98 return $url;
81 }
82  
83  
84 53 ▶sub issuer { $_[0]->endpoint('issuer') }
Subroutine issuer: called CC=1 SCAR=0.0
Pod documented
85 3 ▶sub authorization_endpoint { $_[0]->endpoint('authorization_endpoint') }
Subroutine authorization_endpoint: called CC=1 SCAR=0.0
Pod documented
86 21 ▶sub token_endpoint { $_[0]->endpoint('token_endpoint') }
Subroutine token_endpoint: called CC=1 SCAR=0.0
Pod documented
87 5 ▶sub userinfo_endpoint { $_[0]->endpoint('userinfo_endpoint') }
Subroutine userinfo_endpoint: called CC=1 SCAR=0.0
Pod documented
88 4 ▶sub introspection_endpoint { $_[0]->endpoint('introspection_endpoint') }
Subroutine introspection_endpoint: called CC=1 SCAR=0.0
Pod documented
89 3 ▶sub revocation_endpoint { $_[0]->endpoint('revocation_endpoint') }
Subroutine revocation_endpoint: called CC=1 SCAR=0.0
Pod documented
90 1 ▶sub end_session_endpoint { $_[0]->endpoint('end_session_endpoint') }
Subroutine end_session_endpoint: called CC=1 SCAR=0.0
Pod documented
91 2 ▶sub device_endpoint { $_[0]->endpoint('device_authorization_endpoint') }
Subroutine device_endpoint: called CC=1 SCAR=0.0
Pod documented
92 6 ▶sub jwks_uri { $_[0]->endpoint('jwks_uri') }
Subroutine jwks_uri: called CC=1 SCAR=0.0
Pod documented
93  
94  
95 29 ▶sub jwks {
Subroutine jwks: called CC=3 SCAR=11.0
Pod documented
96 29 my ( $self, %opt ) = @_;
97 29 ▶ if ( $opt{force_refresh} || !$self->_jwks ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"force_refresh"} or not $self->_jwks) 5 24 29
Condition100%3/3
$opt{"force_refresh"} or not $self->_jwks
l!l&&r!l&&!r
2324
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
98 5 $self->_jwks( $self->send_request( GET => $self->jwks_uri )->{data} );
99 5 $self->_jwks_fetched( $self->now->() );
100 }
101 29 return $self->_jwks;
102 }
103  
104  
105 23 ▶sub issuer_names_the_application {
Subroutine issuer_names_the_application: called CC=2 SCAR=6.9
Pod documented
106 23 my ( $self ) = @_;
107 23 ▶ return $self->issuer eq $self->application_url.'/' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->issuer eq $self->application_url . '/' ? : 21 2 23
108 }
109  
110 31 ▶sub verify_token {
Subroutine verify_token: called CC=23 SCAR=31.4
Pod documented
111 31 my ( $self, $token, %opt ) = @_;
112 31 ▶ WWW::Authentik::Error::Validation->throw( message => 'verify_token needs a token' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and length $token 2 29 31
Condition100%3/3
defined $token and length $token
!ll&&!rl&&r
1129
MC/DC100%2/2
defined $token and length $token
defined $token length $token
Truth table100%3/3
A and B
A= defined $tokenB= length $token
ABresult
0X0
100
111
113 unless defined $token && length $token;
114 WWW::Authentik::Error::Validation->throw( message => "verify_token: type must be 'access' or 'id'" )
115 29 ▶ if defined $opt{type} && $opt{type} ne 'access' && $opt{type} ne 'id';
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"type"} and $opt{"type"} ne "access" and $opt{"type"} ne "id" 1 28 29
Condition100%6/6
defined $opt{"type"} and $opt{"type"} ne "access" and $opt{"type"} ne "id"
!ll&&!rl&&r
2621
defined $opt{"type"} and $opt{"type"} ne "access"
!ll&&!rl&&r
2423
MC/DC100%3/3
defined $opt{"type"} and $opt{"type"} ne "access" and $opt{"type"} ne "id"
defined $opt{"type"} $opt{"type"} ne "access" $opt{"type"} ne "id"
Truth table100%4/4
A and B and C
A= defined $opt{"type"}B= $opt{"type"} ne "access"C= $opt{"type"} ne "id"
ABCresult
0XX0
10X0
1100
1111
116 my $audience = exists $opt{audience} ? $opt{audience}
117 28 ▶ : $self->has_client_id ? $self->client_id
Branch: 2 branches
BranchTrueFalseTotal
exists $opt{'audience'} ? : 4 24 28
$self->has_client_id ? : 2 22 24
118 : undef;
119 # Every provider of one authentik signs with the same key, so the issuer is
120 # all that separates two applications - and with issuer_mode: global it is
121 # the bare instance URL, the same for all of them. Then only the audience
122 # tells them apart, and verifying without one would accept any token the
123 # instance ever issued.
124 WWW::Authentik::Error::Validation->throw( message => 'verify_token cannot tell this application apart: '
125 .'the provider issues tokens as "'.$self->issuer.'", which does not name the application, so a token '
126 .'of any other application of this authentik would pass. Give an audience, set client_id on the client, '
127 .'or say any_audience => 1 if you really mean to accept them all.' )
128 28 ▶ if !defined $audience && !$opt{any_audience} && !$self->issuer_names_the_application;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $audience or $opt{"any_audience"} or $self->issuer_names_the_application 1 27 28
Condition100%6/6
defined $audience or $opt{"any_audience"} or $self->issuer_names_the_application
l!l&&r!l&&!r
7201
defined $audience or $opt{"any_audience"}
l!l&&r!l&&!r
6121
MC/DC100%3/3
defined $audience or $opt{"any_audience"} or $self->issuer_names_the_application
defined $audience $opt{"any_audience"} $self->issuer_names_the_application
Truth table100%4/4
A or B or C
A= defined $audienceB= $opt{"any_audience"}C= $self->issuer_names_the_application
ABCresult
1XX1
01X1
0011
0000
129 27 ▶ my %check = (
Branch: 1 branch
BranchTrueFalseTotal
defined $audience ? : 6 21 27
130 token => $token,
131 verify_iss => $self->issuer,
132 verify_exp => 1,
133 accepted_alg => $self->algorithms,
134 decode_payload => 1,
135 defined $audience ? ( verify_aud => $audience ) : ()
136 );
137 27 my $claims = eval { decode_jwt( %check, kid_keys => $self->jwks ) };
138 27 my $error = $@;
139 # a key authentik rotated in since the keys were fetched: fetch them again,
140 # but only for that reason and not more often than jwks_min_age allows
141 27 ▶ 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) 2 25 27
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
1962
not $claims and $error =~ /kid_keys lookup failed/
!ll&&!rl&&r
1188
$self->_jwks_fetched // 0
l!l
80
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
142 2 $claims = eval { decode_jwt( %check, kid_keys => $self->jwks( force_refresh => 1 ) ) };
143 2 $error = $@;
144 }
145 27 ▶ $self->_reject( $error =~ s/ at \S+ line \d+.*//sr ) unless $claims;
Branch: 1 branch
BranchTrueFalseTotal
unless $claims 15 12 27
146 12 ▶ if ( defined $opt{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"type"}) 4 8 12
147 4 ▶ my $looks_like_access = exists $claims->{scope} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $claims->{'scope'} ? : 2 2 4
148 $self->_reject('not an access token: it carries no scope claim')
149 4 ▶ if $opt{type} eq 'access' && !$looks_like_access;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"type"} eq "access" and not $looks_like_access 1 3 4
Condition100%3/3
$opt{"type"} eq "access" and not $looks_like_access
!ll&&!rl&&r
211
MC/DC100%2/2
$opt{"type"} eq "access" and not $looks_like_access
$opt{"type"} eq "access" not $looks_like_access
Truth table100%3/3
A and not(B)
A= $opt{"type"} eq "access"B= not $looks_like_access
ABresult
0X0
100
111
150 $self->_reject('not an ID token: it carries a scope claim')
151 3 ▶ if $opt{type} eq 'id' && $looks_like_access;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"type"} eq "id" and $looks_like_access 1 2 3
Condition100%3/3
$opt{"type"} eq "id" and $looks_like_access
!ll&&!rl&&r
111
MC/DC100%2/2
$opt{"type"} eq "id" and $looks_like_access
$opt{"type"} eq "id" $looks_like_access
Truth table100%3/3
A and B
A= $opt{"type"} eq "id"B= $looks_like_access
ABresult
0X0
100
111
152 }
153 10 return $claims;
154 }
155  
156 17 ▶sub _reject {
Subroutine _reject: called CC=1 SCAR=0.0
157 17 my ( $self, $why ) = @_;
158 17 WWW::Authentik::Error::Validation->throw( message => 'token rejected: '.$why );
159 }
160  
161  
162 4 ▶sub userinfo {
Subroutine userinfo: called CC=3 SCAR=12.5
Pod documented
163 4 my ( $self, $access_token ) = @_;
164 4 ▶ WWW::Authentik::Error::Validation->throw( message => 'userinfo needs an access token' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $access_token and length $access_token 0 4 4
Condition33%1/3
defined $access_token and length $access_token
!ll&&!rl&&r
004
MC/DC0%0/2
defined $access_token and length $access_token
defined $access_token length $access_token
Truth table33%1/3
A and B
A= defined $access_tokenB= length $access_token
ABresult
0X0
100
111
165 unless defined $access_token && length $access_token;
166 4 return $self->send_request( GET => $self->userinfo_endpoint, bearer => $access_token )->{data};
167 }
168  
169  
170 3 ▶sub introspect {
Subroutine introspect: called CC=2 SCAR=7.2
Pod documented
171 3 my ( $self, $token, %client ) = @_;
172 return $self->_token_call( $self->introspection_endpoint,
173 3 ▶ { token => $token, defined $client{token_type_hint} ? ( token_type_hint => $client{token_type_hint} ) : () }, %client );
Branch: 1 branch
BranchTrueFalseTotal
defined $client{'token_type_hint'} ? : 0 3 3
174 }
175  
176  
177 2 ▶sub revoke {
Subroutine revoke: called CC=2 SCAR=7.1
Pod documented
178 2 my ( $self, $token, %client ) = @_;
179 $self->_token_call( $self->revocation_endpoint,
180 2 ▶ { token => $token, defined $client{token_type_hint} ? ( token_type_hint => $client{token_type_hint} ) : () }, %client );
Branch: 1 branch
BranchTrueFalseTotal
defined $client{'token_type_hint'} ? : 0 2 2
181 2 return 1;
182 }
183  
184  
185 9 ▶sub client_credentials_token {
Subroutine client_credentials_token: called CC=1 SCAR=0.0
Pod documented
186 9 my ( $self, %arg ) = @_;
187 9 return $self->_grant( client_credentials => [qw( scope username password )], %arg );
188 }
189  
190  
191 3 ▶sub refresh_token {
Subroutine refresh_token: called CC=1 SCAR=0.0
Pod documented
192 3 my ( $self, $refresh, %arg ) = @_;
193 3 return $self->_grant( refresh_token => [qw( refresh_token scope )], %arg, refresh_token => $refresh );
194 }
195  
196  
197 2 ▶sub exchange_authorization_code {
Subroutine exchange_authorization_code: called CC=1 SCAR=0.0
Pod documented
198 2 my ( $self, %arg ) = @_;
199 2 return $self->_grant( authorization_code => [qw( code redirect_uri code_verifier )], %arg );
200 }
201  
202  
203 1 ▶sub device_authorization {
Subroutine device_authorization: called CC=2 SCAR=7.2
Pod documented
204 1 my ( $self, %arg ) = @_;
205 return $self->_token_call( $self->device_endpoint,
206 1 ▶ { defined $arg{scope} ? ( scope => $arg{scope} ) : () }, %arg );
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'scope'} ? : 1 0 1
207 }
208  
209  
210 3 ▶sub device_token {
Subroutine device_token: called CC=1 SCAR=0.0
Pod documented
211 3 my ( $self, %arg ) = @_;
212 3 return $self->_grant( 'urn:ietf:params:oauth:grant-type:device_code' => ['device_code'], %arg );
213 }
214  
215  
216 4 ▶sub authorization_url {
Subroutine authorization_url: called CC=4 SCAR=13.9
Pod documented
217 4 my ( $self, %arg ) = @_;
218 4 ▶ WWW::Authentik::Error::Validation->throw( message => 'authorization_url needs a client_id' ) unless defined $arg{client_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg{"client_id"} 1 3 4
219 3 ▶ WWW::Authentik::Error::Validation->throw( message => 'authorization_url needs a redirect_uri' ) unless defined $arg{redirect_uri};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg{"redirect_uri"} 1 2 3
220 2 my $uri = URI->new( $self->authorization_endpoint );
221 $uri->query_form(
222 response_type => $arg{response_type} // 'code',
223 2 ▶ map { $_ => $arg{$_} } grep { defined $arg{$_} }
Condition50%1/2
$arg{"response_type"} // "code"
l!l
02
MC/DC0%0/1
$arg{"response_type"} // "code"
$arg{"response_type"}
Truth table50%1/2
A
A= $arg{"response_type"}
Aresult
11
00
224 qw( client_id redirect_uri scope state nonce prompt max_age acr_values
225 code_challenge code_challenge_method response_mode )
226 );
227 2 return $uri->as_string;
228 }
229  
230  
231 17 ▶sub _grant {
Subroutine _grant: called CC=1 SCAR=0.0
232 17 my ( $self, $type, $fields, %arg ) = @_;
233 return $self->_token_call( $self->token_endpoint,
234 17 { grant_type => $type, map { $_ => $arg{$_} } grep { defined $arg{$_} } @$fields }, %arg );
235 }
236  
237 23 ▶sub _token_call {
Subroutine _token_call: called CC=4 SCAR=14.2
238 23 my ( $self, $url, $form, %arg ) = @_;
239 23 ▶ WWW::Authentik::Error::Validation->throw( message => 'a client_id is needed' ) unless defined $arg{client_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg{"client_id"} 1 22 23
240 my %form = (
241 %$form,
242 client_id => $arg{client_id},
243 22 ▶ defined $arg{client_secret} ? ( client_secret => $arg{client_secret} ) : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $arg{'client_secret'} ? : 22 0 22
244 );
245 22 ▶ return $self->send_request( POST => $url, form => \%form )->{data} // {};
Condition50%1/2
$self->send_request("POST", $url, "form", \%form)->{"data"} // {}
l!l
130
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
246 }
247  
248 7 1;
249  
250 __END__
« Authentik/Diff.pm ↑ Summary Authentik/API.pm »