blib/lib/Google/Auth/IDTokens/Verifier.pm

filter: Statementstmt 94.4% 135 / 143 Branchbran 73.6% 56 / 76 Conditioncond 51.7% 29 / 56 MC/DCmcdc 14.7% 5 / 34 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 74.2% 242 / 326 CC 62 SCAR 38.5
CC
62
Cov
80%
verify
72.8
__ANON__
16.9
_is_finite_number
4.1
CRAP 46.9
« blib/lib/Google/Auth/ExternalAccountCredentials/Pluggable.pm ↑ Summary blib/lib/Google/Auth/ImpersonatedServiceAccountCredentials.pm »
1 package Google::Auth::IDTokens::Verifier;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 my %ALLOWED_JWT_ALGS = (
12 RS256 => 1,
13 ES256 => 1,
14 );
15  
16 3 my %ALG_TO_KTY = (
17 RS256 => 'RSA',
18 ES256 => 'EC',
19 );
20  
21 17 ▶sub _is_finite_number {
Subroutine _is_finite_number: called CC=4 SCAR=14.2
22 17 my ($val) = @_;
23 17 ▶ return 0 if !defined $val || ref($val);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $val or ref $val 1 16 17
Condition66%2/3
not defined $val or ref $val
l!l&&r!l&&!r
1016
MC/DC50%1/2
not defined $val or ref $val
not defined $val ref $val
Truth table66%2/3
not(A) or B
A= not defined $valB= ref $val
ABresult
1X1
011
000
24 16 ▶ return $val =~ /\A-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\z/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /\A-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\z/ ? : 16 0 16
25 }
26 3 ▶use Google::Auth;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use Google::Auth::IDTokens::KeySources;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 3 has key_source => (
30 is => 'ro',
31 required => 0,
32 );
33  
34 3 has aud => (
35 is => 'ro',
36 required => 0,
37 );
38  
39 3 has azp => (
40 is => 'ro',
41 required => 0,
42 );
43  
44 3 has iss => (
45 is => 'ro',
46 required => 0,
47 );
48  
49 38 ▶sub _decode_base64url {
Subroutine _decode_base64url: called CC=2 SCAR=6.9
50 38 my ($s) = @_;
51 38 $s =~ tr{-_}{+/};
52 38 my $padding = length($s) % 4;
53 38 ▶ if ($padding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($padding) 30 8 38
54 30 $s .= '=' x (4 - $padding);
55 }
56 38 return MIME::Base64::decode_base64($s);
57 }
58  
59 13 ▶sub verify {
Subroutine verify: called CC=45 SCAR=42.9
Pod documented
60 13 my ($self, $token, %options) = @_;
61  
62 13 ▶ $self = ref $self ? $self : $self->new();
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 13 0 13
63  
64 13 ▶ my $key_source = $options{key_source} // $self->key_source;
Condition66%2/3
$options{"key_source"} // $self->key_source
l!l&&r!l&&!r
1120
MC/DC0%0/2
$options{"key_source"} // $self->key_source
$options{"key_source"} $self->key_source
Truth table66%2/3
A // B
A= $options{"key_source"}B= $self->key_source
ABresult
1X1
011
000
65 13 ▶ my $aud = $options{aud} // $self->aud;
Condition66%2/3
$options{"aud"} // $self->aud
l!l&&r!l&&!r
850
MC/DC0%0/2
$options{"aud"} // $self->aud
$options{"aud"} $self->aud
Truth table66%2/3
A // B
A= $options{"aud"}B= $self->aud
ABresult
1X1
011
000
66 13 ▶ my $azp = $options{azp} // $self->azp;
Condition100%3/3
$options{"azp"} // $self->azp
l!l&&r!l&&!r
265
MC/DC100%2/2
$options{"azp"} // $self->azp
$options{"azp"} $self->azp
Truth table100%3/3
A // B
A= $options{"azp"}B= $self->azp
ABresult
1X1
011
000
67 13 ▶ my $iss = $options{iss} // $self->iss;
Condition66%2/3
$options{"iss"} // $self->iss
l!l&&r!l&&!r
1120
MC/DC0%0/2
$options{"iss"} // $self->iss
$options{"iss"} $self->iss
Truth table66%2/3
A // B
A= $options{"iss"}B= $self->iss
ABresult
1X1
011
000
68 13 ▶ my $clock_skew = $options{clock_skew} // 300;
Condition50%1/2
$options{"clock_skew"} // 300
l!l
013
MC/DC0%0/1
$options{"clock_skew"} // 300
$options{"clock_skew"}
Truth table50%1/2
A
A= $options{"clock_skew"}
Aresult
11
00
69  
70 13 ▶ die 'KeySourceError: No key source provided' unless defined $key_source;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key_source 0 13 13
71  
72 13 my @parts = split /\./, $token;
73 13 ▶ if (@parts != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts != 3) 0 13 13
74 0 die 'VerificationError: Invalid JWT format';
75 }
76 13 my ($header_b64, $payload_b64, $signature_b64) = @parts;
77  
78 13 my $coder = JSON::MaybeXS->new->utf8->allow_nonref;
79 13 my $header = eval { $coder->decode(_decode_base64url($header_b64)) };
80 13 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 13 13
81 0 die 'VerificationError: Failed to parse JWT header: ' . $@;
82 }
83 13 my $payload = eval { $coder->decode(_decode_base64url($payload_b64)) };
84 13 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 13 13
85 0 die 'VerificationError: Failed to parse JWT payload: ' . $@;
86 }
87  
88 13 my $alg = $header->{alg};
89 13 ▶ if (!defined $alg || !$ALLOWED_JWT_ALGS{$alg}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $alg and $ALLOWED_JWT_ALGS{$alg}) 1 12 13
Condition66%2/3
defined $alg and $ALLOWED_JWT_ALGS{$alg}
!ll&&!rl&&r
0112
MC/DC50%1/2
defined $alg and $ALLOWED_JWT_ALGS{$alg}
defined $alg $ALLOWED_JWT_ALGS{$alg}
Truth table66%2/3
A and B
A= defined $algB= $ALLOWED_JWT_ALGS{$alg}
ABresult
0X0
100
111
90 1 die 'VerificationError: Unsupported or missing alg claim in JWT header';
91 }
92  
93 12 my $kid = $header->{kid};
94 12 ▶ die 'VerificationError: Missing kid claim in JWT header' unless defined $kid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kid 0 12 12
95  
96 12 my $signature = _decode_base64url($signature_b64);
97 12 my $message = $header_b64 . '.' . $payload_b64;
98  
99 12 my $payload_verified;
100  
101 17 ▶ my $verify_with_keys = sub {
Subroutine __ANON__: called CC=12 SCAR=28.3
102 17 my ($keys_ref) = @_;
103 17 foreach my $key (@$keys_ref) {
104 32 ▶ if ($key->id eq $kid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key->id eq $kid) 14 18 32
105 14 ▶ if ( defined $key->algorithm
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key->algorithm and length $key->algorithm and $key->algorithm ne $alg) 0 14 14
Condition33%2/6
defined $key->algorithm and length $key->algorithm and $key->algorithm ne $alg
!ll&&!rl&&r
0140
defined $key->algorithm and length $key->algorithm
!ll&&!rl&&r
0014
MC/DC0%0/3
defined $key->algorithm and length $key->algorithm and $key->algorithm ne $alg
defined $key->algorithm length $key->algorithm $key->algorithm ne $alg
Truth table25%1/4
A and B and C
A= defined $key->algorithmB= length $key->algorithmC= $key->algorithm ne $alg
ABCresult
0XX0
10X0
1100
1111
106 && length $key->algorithm
107 && $key->algorithm ne $alg)
108 {
109 0 next;
110 }
111 14 ▶ if ( $key->can('key_type')
Branch: 1 branch
BranchTrueFalseTotal
if ($key->can("key_type") and defined $key->key_type and length $key->key_type) 13 1 14
Condition66%4/6
$key->can("key_type") and defined $key->key_type and length $key->key_type
!ll&&!rl&&r
1013
$key->can("key_type") and defined $key->key_type
!ll&&!rl&&r
0113
MC/DC33%1/3
$key->can("key_type") and defined $key->key_type and length $key->key_type
$key->can("key_type") defined $key->key_type length $key->key_type
Truth table50%2/4
A and B and C
A= $key->can("key_type")B= defined $key->key_typeC= length $key->key_type
ABCresult
0XX0
10X0
1100
1111
112 && defined $key->key_type
113 && length $key->key_type)
114 {
115 13 my $expected_kty = $ALG_TO_KTY{$alg};
116 13 ▶ if (defined $expected_kty && $key->key_type ne $expected_kty) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expected_kty and $key->key_type ne $expected_kty) 0 13 13
Condition33%1/3
defined $expected_kty and $key->key_type ne $expected_kty
!ll&&!rl&&r
0130
MC/DC0%0/2
defined $expected_kty and $key->key_type ne $expected_kty
defined $expected_kty $key->key_type ne $expected_kty
Truth table33%1/3
A and B
A= defined $expected_ktyB= $key->key_type ne $expected_kty
ABresult
0X0
100
111
117 0 next;
118 }
119 }
120 14 ▶ if (Google::Auth::verify_signature($key->key, $message, $signature)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Google::Auth::verify_signature(scalar $key->key, $message, $signature)) 10 4 14
121 10 return 1;
122 }
123 }
124 }
125 7 return 0;
126 12 };
127  
128 12 my $keys_ref = $key_source->current_keys;
129 12 ▶ if ($verify_with_keys->($keys_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$verify_with_keys($keys_ref)) { } 7 5 12
130 7 $payload_verified = 1;
131 } else {
132 5 my $refreshed_keys_ref = $key_source->refresh_keys;
133 5 ▶ if ($verify_with_keys->($refreshed_keys_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$verify_with_keys($refreshed_keys_ref)) 3 2 5
134 3 $payload_verified = 1;
135 }
136 }
137  
138 12 ▶ unless ($payload_verified) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($payload_verified) 2 10 12
139 2 die 'SignatureError: Token signature verification failed';
140 }
141  
142 10 ▶ my $now = $options{time_now} // time();
Condition50%1/2
$options{"time_now"} // time
l!l
100
MC/DC0%0/1
$options{"time_now"} // time
$options{"time_now"}
Truth table50%1/2
A
A= $options{"time_now"}
Aresult
11
00
143 10 ▶ if (!_is_finite_number($payload->{exp})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_is_finite_number($payload->{"exp"})) 1 9 10
144 1 die 'VerificationError: Missing or invalid exp claim';
145 }
146 9 ▶ if ($payload->{exp} < $now - $clock_skew) {
Branch: 1 branch
BranchTrueFalseTotal
if ($payload->{"exp"} < $now - $clock_skew) 2 7 9
147 2 die 'ExpiredTokenError: Token signature is expired';
148 }
149 7 ▶ if (exists $payload->{nbf}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $payload->{"nbf"}) 0 7 7
150 0 ▶ if (!_is_finite_number($payload->{nbf})
Branch: 1 branch
BranchTrueFalseTotal
if (not _is_finite_number($payload->{"nbf"}) or $payload->{"nbf"} > $now + $clock_skew) 0 0 0
Condition0%0/3
not _is_finite_number($payload->{"nbf"}) or $payload->{"nbf"} > $now + $clock_skew
l!l&&r!l&&!r
000
MC/DC0%0/2
not _is_finite_number($payload->{"nbf"}) or $payload->{"nbf"} > $now + $clock_skew
not _is_finite_number($payload->{"nbf"}) $payload->{"nbf"} > $now + $clock_skew
Truth table0%0/3
not(A) or B
A= not _is_finite_number($payload->{"nbf"})B= $payload->{"nbf"} > $now + $clock_skew
ABresult
1X1
011
000
151 || $payload->{nbf} > $now + $clock_skew)
152 {
153 0 die 'VerificationError: Token not yet valid';
154 }
155 }
156 7 ▶ if (exists $payload->{iat}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $payload->{"iat"}) 7 0 7
157 7 ▶ if (!_is_finite_number($payload->{iat})
Branch: 1 branch
BranchTrueFalseTotal
if (not _is_finite_number($payload->{"iat"}) or $payload->{"iat"} > $now + $clock_skew) 0 7 7
Condition33%1/3
not _is_finite_number($payload->{"iat"}) or $payload->{"iat"} > $now + $clock_skew
l!l&&r!l&&!r
007
MC/DC0%0/2
not _is_finite_number($payload->{"iat"}) or $payload->{"iat"} > $now + $clock_skew
not _is_finite_number($payload->{"iat"}) $payload->{"iat"} > $now + $clock_skew
Truth table33%1/3
not(A) or B
A= not _is_finite_number($payload->{"iat"})B= $payload->{"iat"} > $now + $clock_skew
ABresult
1X1
011
000
158 || $payload->{iat} > $now + $clock_skew)
159 {
160 0 die 'VerificationError: Token issued in the future';
161 }
162 }
163  
164 7 ▶ $payload->{azp} ||= $payload->{cid} if exists $payload->{cid};
Branch: 1 branch
BranchTrueFalseTotal
if exists $payload->{"cid"} 0 7 7
Condition0%0/3
$payload->{"azp"} ||= $payload->{"cid"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$payload->{"azp"} ||= $payload->{"cid"}
$payload->{"azp"} $payload->{"cid"}
Truth table0%0/3
A ||= B
A= $payload->{"azp"}B= $payload->{"cid"}
ABresult
1X1
011
000
165  
166 7 ▶ if (defined $aud) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $aud) 7 0 7
167 7 ▶ my @expected_auds = ref $aud eq 'ARRAY' ? @$aud : ($aud);
Branch: 1 branch
BranchTrueFalseTotal
ref $aud eq 'ARRAY' ? : 1 6 7
168 7 my $token_aud = $payload->{aud};
169 7 ▶ my @token_auds = ref $token_aud eq 'ARRAY' ? @$token_aud : ($token_aud);
Branch: 1 branch
BranchTrueFalseTotal
ref $token_aud eq 'ARRAY' ? : 0 7 7
170  
171 7 my %expected_map = map { $_ => 1 } @expected_auds;
172 7 my $aud_match = 0;
173 7 foreach my $t_aud (@token_auds) {
174 7 ▶ if (exists $expected_map{$t_aud}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $expected_map{$t_aud}) 6 1 7
175 6 $aud_match = 1;
176 6 last;
177 }
178 }
179 7 ▶ unless ($aud_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aud_match) 1 6 7
180 1 die 'AudienceMismatchError: Token aud mismatch: ' .
181 join(', ', @token_auds);
182 }
183 }
184  
185 6 ▶ if (defined $azp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $azp) 5 1 6
186 5 ▶ my @expected_azps = ref $azp eq 'ARRAY' ? @$azp : ($azp);
Branch: 1 branch
BranchTrueFalseTotal
ref $azp eq 'ARRAY' ? : 0 5 5
187 5 my $token_azp = $payload->{azp};
188 5 ▶ my @token_azps = ref $token_azp eq 'ARRAY' ? @$token_azp : ($token_azp);
Branch: 1 branch
BranchTrueFalseTotal
ref $token_azp eq 'ARRAY' ? : 0 5 5
189  
190 5 my %expected_map = map { $_ => 1 } @expected_azps;
191 5 my $azp_match = 0;
192 5 foreach my $t_azp (@token_azps) {
193 5 ▶ if (exists $expected_map{$t_azp}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $expected_map{$t_azp}) 4 1 5
194 4 $azp_match = 1;
195 4 last;
196 }
197 }
198 5 ▶ unless ($azp_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($azp_match) 1 4 5
199 1 ▶ die 'AuthorizedPartyMismatchError: Token azp mismatch: ' .
Condition50%1/2
$token_azp // ""
l!l
10
MC/DC0%0/1
$token_azp // ""
$token_azp
Truth table50%1/2
A
A= $token_azp
Aresult
11
00
200 ($token_azp // '');
201 }
202 }
203  
204 5 ▶ if (defined $iss) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $iss) 5 0 5
205 5 ▶ my @expected_isss = ref $iss eq 'ARRAY' ? @$iss : ($iss);
Branch: 1 branch
BranchTrueFalseTotal
ref $iss eq 'ARRAY' ? : 1 4 5
206 5 my $token_iss = $payload->{iss};
207  
208 5 my %expected_map = map { $_ => 1 } @expected_isss;
209 5 ▶ unless (exists $expected_map{$token_iss}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $expected_map{$token_iss}) 1 4 5
210 1 ▶ die 'IssuerMismatchError: Token iss mismatch: ' . ($token_iss // '');
Condition50%1/2
$token_iss // ""
l!l
10
MC/DC0%0/1
$token_iss // ""
$token_iss
Truth table50%1/2
A
A= $token_iss
Aresult
11
00
211 }
212 }
213  
214 4 return $payload;
215 }
216  
217 3 my $_oidc_verifier;
218  
219 3 ▶sub verify_oidc {
Subroutine verify_oidc: called CC=2 SCAR=7.0
Pod documented
220 3 my ($class, $token, %options) = @_;
221  
222 3 ▶ $_oidc_verifier //= Google::Auth::IDTokens::Verifier->new(
Condition66%2/3
$_oidc_verifier //= "Google::Auth::IDTokens::Verifier"->new("key_source", "Google::Auth::IDTokens::JwkHttpKeySource"->new({"uri", "https://www.googleapis.com/oauth2/v3/certs"}), "iss", ["accounts.google.com", "https://accounts.google.com"])
l!l&&r!l&&!r
210
MC/DC0%0/2
$_oidc_verifier //= "Google::Auth::IDTokens::Verifier"->new("key_source", "Google::Auth::IDTokens::JwkHttpKeySource"->new({"uri", "https://www.googleapis.com/oauth2/v3/certs"}), "iss", ["accounts.google.com", "https://accounts.google.com"])
$_oidc_verifier "Google::Auth::IDTokens::Verifier"->new("key_source", "Google::Auth::IDTokens::JwkHttpKeySource"->new({"uri", "https://www.googleapis.com/oauth2/v3/certs"}), "iss", ["accounts.google.com", "https://accounts.google.com"])
Truth table66%2/3
A //= B
A= $_oidc_verifierB= "Google::Auth::IDTokens::Verifier"->new("key_source", "Google::Auth::IDTokens::JwkHttpKeySource"->new({"uri", "https://www.googleapis.com/oauth2/v3/certs"}), "iss", ["accounts.google.com", "https://accounts.google.com"])
ABresult
1X1
011
000
223 key_source => Google::Auth::IDTokens::JwkHttpKeySource->new({
224 uri => 'https://www.googleapis.com/oauth2/v3/certs'
225 }
226 ),
227 iss => ['accounts.google.com', 'https://accounts.google.com']);
228  
229 3 return $_oidc_verifier->verify($token, %options);
230 }
231  
232 my $_iap_verifier;
233  
234 3 ▶sub verify_iap {
Subroutine verify_iap: called CC=2 SCAR=7.0
Pod documented
235 3 my ($class, $token, %options) = @_;
236  
237 3 ▶ $_iap_verifier //= Google::Auth::IDTokens::Verifier->new(
Condition66%2/3
$_iap_verifier //= "Google::Auth::IDTokens::Verifier"->new("key_source", "Google::Auth::IDTokens::JwkHttpKeySource"->new({"uri", "https://www.googleapis.com/iap/verify/public_key-jwk"}), "iss", "https://cloud.google.com/iap")
l!l&&r!l&&!r
210
MC/DC0%0/2
$_iap_verifier //= "Google::Auth::IDTokens::Verifier"->new("key_source", "Google::Auth::IDTokens::JwkHttpKeySource"->new({"uri", "https://www.googleapis.com/iap/verify/public_key-jwk"}), "iss", "https://cloud.google.com/iap")
$_iap_verifier "Google::Auth::IDTokens::Verifier"->new("key_source", "Google::Auth::IDTokens::JwkHttpKeySource"->new({"uri", "https://www.googleapis.com/iap/verify/public_key-jwk"}), "iss", "https://cloud.google.com/iap")
Truth table66%2/3
A //= B
A= $_iap_verifierB= "Google::Auth::IDTokens::Verifier"->new("key_source", "Google::Auth::IDTokens::JwkHttpKeySource"->new({"uri", "https://www.googleapis.com/iap/verify/public_key-jwk"}), "iss", "https://cloud.google.com/iap")
ABresult
1X1
011
000
238 key_source => Google::Auth::IDTokens::JwkHttpKeySource->new({
239 uri => 'https://www.googleapis.com/iap/verify/public_key-jwk'
240 }
241 ),
242 iss => 'https://cloud.google.com/iap'
243 );
244  
245 3 return $_iap_verifier->verify($token, %options);
246 }
247  
248 3 1;
249  
250 __END__
« blib/lib/Google/Auth/ExternalAccountCredentials/Pluggable.pm ↑ Summary blib/lib/Google/Auth/ImpersonatedServiceAccountCredentials.pm »