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

filter: Statementstmt 95.9% 117 / 122 Branchbran 75.8% 44 / 58 Conditioncond 52.6% 20 / 38 MC/DCmcdc 12.5% 3 / 24 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 3 totaltotal 76.2% 196 / 257 CC 47 SCAR 36.8
CC
47
Cov
83%
verify
55.4
__ANON__
4.0
verify_oidc
2.0
CRAP 39.8
« blib/lib/Google/Auth/IAMCredentials.pm ↑ Summary blib/lib/Google/Auth/UserRefreshCredentials.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 Google::Auth;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Google::Auth::IDTokens::KeySources;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 has key_source => (
13 is => 'ro',
14 required => 0,
15 );
16  
17 3 has aud => (
18 is => 'ro',
19 required => 0,
20 );
21  
22 3 has azp => (
23 is => 'ro',
24 required => 0,
25 );
26  
27 3 has iss => (
28 is => 'ro',
29 required => 0,
30 );
31  
32 33 ▶sub _decode_base64url {
Subroutine _decode_base64url: called CC=2 SCAR=6.9
33 33 my ($s) = @_;
34 33 $s =~ tr{-_}{+/};
35 33 my $padding = length($s) % 4;
36 33 ▶ if ($padding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($padding) 27 6 33
37 27 $s .= '=' x (4 - $padding);
38 }
39 33 return MIME::Base64::decode_base64($s);
40 }
41  
42 11 ▶sub verify {
Subroutine verify: called CC=41 SCAR=40.1
Pod undocumented
43 11 my ($self, $token, %options) = @_;
44  
45 11 ▶ $self = ref $self ? $self : $self->new();
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 11 0 11
46  
47 11 ▶ my $key_source = $options{key_source} // $self->key_source;
Condition33%1/3
$options{"key_source"} // $self->key_source
l!l&&r!l&&!r
0110
MC/DC0%0/2
$options{"key_source"} // $self->key_source
$options{"key_source"} $self->key_source
Truth table33%1/3
A // B
A= $options{"key_source"}B= $self->key_source
ABresult
1X1
011
000
48 11 ▶ my $aud = $options{aud} // $self->aud;
Condition66%2/3
$options{"aud"} // $self->aud
l!l&&r!l&&!r
830
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
49 11 ▶ my $azp = $options{azp} // $self->azp;
Condition100%3/3
$options{"azp"} // $self->azp
l!l&&r!l&&!r
245
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
50 11 ▶ my $iss = $options{iss} // $self->iss;
Condition66%2/3
$options{"iss"} // $self->iss
l!l&&r!l&&!r
1100
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
51 11 ▶ my $clock_skew = $options{clock_skew} // 300;
Condition50%1/2
$options{"clock_skew"} // 300
l!l
011
MC/DC0%0/1
$options{"clock_skew"} // 300
$options{"clock_skew"}
Truth table50%1/2
A
A= $options{"clock_skew"}
Aresult
11
00
52  
53 11 ▶ die 'KeySourceError: No key source provided' unless defined $key_source;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key_source 0 11 11
54  
55 11 my @parts = split /\./, $token;
56 11 ▶ if (@parts != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts != 3) 0 11 11
57 0 die 'VerificationError: Invalid JWT format';
58 }
59 11 my ($header_b64, $payload_b64, $signature_b64) = @parts;
60  
61 11 my $coder = JSON::MaybeXS->new->utf8->allow_nonref;
62 11 my $header = eval { $coder->decode(_decode_base64url($header_b64)) };
63 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 11 11
64 0 die 'VerificationError: Failed to parse JWT header: ' . $@;
65 }
66 11 my $payload = eval { $coder->decode(_decode_base64url($payload_b64)) };
67 11 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 11 11
68 0 die 'VerificationError: Failed to parse JWT payload: ' . $@;
69 }
70  
71 11 my $kid = $header->{kid};
72 11 ▶ die 'VerificationError: Missing kid claim in JWT header' unless defined $kid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kid 0 11 11
73  
74 11 my $signature = _decode_base64url($signature_b64);
75 11 my $message = $header_b64 . '.' . $payload_b64;
76  
77 11 my $payload_verified;
78  
79 16 ▶ my $verify_with_keys = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
80 16 my ($keys_ref) = @_;
81 16 foreach my $key (@$keys_ref) {
82 31 ▶ if ($key->id eq $kid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key->id eq $kid) 13 18 31
83 13 ▶ if (Google::Auth::verify_signature($key->key, $message, $signature)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Google::Auth::verify_signature(scalar $key->key, $message, $signature)) 9 4 13
84 9 return 1;
85 }
86 }
87 }
88 7 return 0;
89 11 };
90  
91 11 my $keys_ref = $key_source->current_keys;
92 11 ▶ if ($verify_with_keys->($keys_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$verify_with_keys($keys_ref)) { } 6 5 11
93 6 $payload_verified = 1;
94 } else {
95 5 my $refreshed_keys_ref = $key_source->refresh_keys;
96 5 ▶ if ($verify_with_keys->($refreshed_keys_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$verify_with_keys($refreshed_keys_ref)) 3 2 5
97 3 $payload_verified = 1;
98 }
99 }
100  
101 11 ▶ unless ($payload_verified) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($payload_verified) 2 9 11
102 2 die 'SignatureError: Token signature verification failed';
103 }
104  
105 9 ▶ my $now = $options{time_now} // time();
Condition50%1/2
$options{"time_now"} // time
l!l
90
MC/DC0%0/1
$options{"time_now"} // time
$options{"time_now"}
Truth table50%1/2
A
A= $options{"time_now"}
Aresult
11
00
106 9 ▶ if (exists $payload->{exp} && $payload->{exp} < $now - $clock_skew) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $payload->{"exp"} and $payload->{"exp"} < $now - $clock_skew) 2 7 9
Condition66%2/3
exists $payload->{"exp"} and $payload->{"exp"} < $now - $clock_skew
!ll&&!rl&&r
072
MC/DC50%1/2
exists $payload->{"exp"} and $payload->{"exp"} < $now - $clock_skew
exists $payload->{"exp"} $payload->{"exp"} < $now - $clock_skew
Truth table66%2/3
A and B
A= exists $payload->{"exp"}B= $payload->{"exp"} < $now - $clock_skew
ABresult
0X0
100
111
107 2 die 'ExpiredTokenError: Token signature is expired';
108 }
109 7 ▶ if (exists $payload->{nbf} && $payload->{nbf} > $now + $clock_skew) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $payload->{"nbf"} and $payload->{"nbf"} > $now + $clock_skew) 0 7 7
Condition33%1/3
exists $payload->{"nbf"} and $payload->{"nbf"} > $now + $clock_skew
!ll&&!rl&&r
700
MC/DC0%0/2
exists $payload->{"nbf"} and $payload->{"nbf"} > $now + $clock_skew
exists $payload->{"nbf"} $payload->{"nbf"} > $now + $clock_skew
Truth table33%1/3
A and B
A= exists $payload->{"nbf"}B= $payload->{"nbf"} > $now + $clock_skew
ABresult
0X0
100
111
110 0 die 'VerificationError: Token not yet valid';
111 }
112 7 ▶ if (exists $payload->{iat} && $payload->{iat} > $now + $clock_skew) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $payload->{"iat"} and $payload->{"iat"} > $now + $clock_skew) 0 7 7
Condition33%1/3
exists $payload->{"iat"} and $payload->{"iat"} > $now + $clock_skew
!ll&&!rl&&r
070
MC/DC0%0/2
exists $payload->{"iat"} and $payload->{"iat"} > $now + $clock_skew
exists $payload->{"iat"} $payload->{"iat"} > $now + $clock_skew
Truth table33%1/3
A and B
A= exists $payload->{"iat"}B= $payload->{"iat"} > $now + $clock_skew
ABresult
0X0
100
111
113 0 die 'VerificationError: Token issued in the future';
114 }
115  
116 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
117  
118 7 ▶ if (defined $aud) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $aud) 7 0 7
119 7 ▶ my @expected_auds = ref $aud eq 'ARRAY' ? @$aud : ($aud);
Branch: 1 branch
BranchTrueFalseTotal
ref $aud eq 'ARRAY' ? : 1 6 7
120 7 my $token_aud = $payload->{aud};
121 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
122  
123 7 my %expected_map = map { $_ => 1 } @expected_auds;
124 7 my $aud_match = 0;
125 7 foreach my $t_aud (@token_auds) {
126 7 ▶ if (exists $expected_map{$t_aud}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $expected_map{$t_aud}) 6 1 7
127 6 $aud_match = 1;
128 6 last;
129 }
130 }
131 7 ▶ unless ($aud_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($aud_match) 1 6 7
132 1 die 'AudienceMismatchError: Token aud mismatch: ' .
133 join(', ', @token_auds);
134 }
135 }
136  
137 6 ▶ if (defined $azp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $azp) 5 1 6
138 5 ▶ my @expected_azps = ref $azp eq 'ARRAY' ? @$azp : ($azp);
Branch: 1 branch
BranchTrueFalseTotal
ref $azp eq 'ARRAY' ? : 0 5 5
139 5 my $token_azp = $payload->{azp};
140 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
141  
142 5 my %expected_map = map { $_ => 1 } @expected_azps;
143 5 my $azp_match = 0;
144 5 foreach my $t_azp (@token_azps) {
145 5 ▶ if (exists $expected_map{$t_azp}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $expected_map{$t_azp}) 4 1 5
146 4 $azp_match = 1;
147 4 last;
148 }
149 }
150 5 ▶ unless ($azp_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($azp_match) 1 4 5
151 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
152 ($token_azp // '');
153 }
154 }
155  
156 5 ▶ if (defined $iss) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $iss) 5 0 5
157 5 ▶ my @expected_isss = ref $iss eq 'ARRAY' ? @$iss : ($iss);
Branch: 1 branch
BranchTrueFalseTotal
ref $iss eq 'ARRAY' ? : 1 4 5
158 5 my $token_iss = $payload->{iss};
159  
160 5 my %expected_map = map { $_ => 1 } @expected_isss;
161 5 ▶ unless (exists $expected_map{$token_iss}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $expected_map{$token_iss}) 1 4 5
162 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
163 }
164 }
165  
166 4 return $payload;
167 }
168  
169 3 my $_oidc_verifier;
170  
171 3 ▶sub verify_oidc {
Subroutine verify_oidc: called CC=2 SCAR=7.0
Pod undocumented
172 3 my ($class, $token, %options) = @_;
173  
174 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
175 key_source => Google::Auth::IDTokens::JwkHttpKeySource->new({
176 uri => 'https://www.googleapis.com/oauth2/v3/certs'
177 }
178 ),
179 iss => ['accounts.google.com', 'https://accounts.google.com']);
180  
181 3 return $_oidc_verifier->verify($token, %options);
182 }
183  
184 my $_iap_verifier;
185  
186 3 ▶sub verify_iap {
Subroutine verify_iap: called CC=2 SCAR=7.0
Pod undocumented
187 3 my ($class, $token, %options) = @_;
188  
189 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
190 key_source => Google::Auth::IDTokens::JwkHttpKeySource->new({
191 uri => 'https://www.googleapis.com/iap/verify/public_key-jwk'
192 }
193 ),
194 iss => 'https://cloud.google.com/iap'
195 );
196  
197 3 return $_iap_verifier->verify($token, %options);
198 }
199  
200 3 1;
« blib/lib/Google/Auth/IAMCredentials.pm ↑ Summary blib/lib/Google/Auth/UserRefreshCredentials.pm »