blib/lib/HTTP/Message/Signature.pm

filter: Statementstmt 77.4% 179 / 231 Branchbran 45.3% 39 / 86 Conditioncond 18.3% 9 / 49 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 70.5% 36 / 51 Podpod 66.6% 2 / 3 totaltotal 58.7% 265 / 451 CC 68 SCAR 27.6
CC
68
Cov
62%
_build_public_keys_json
72.0
_build_signature_agent
42.0
verify
32.8
CRAP 15.9
↑ Summary
1 package HTTP::Message::Signature;
2  
3 # ABSTRACT: Verify HTTP Message Signatures (RFC 9421)
4  
5 4 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Crypt::PK::Ed25519;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Crypt::PK::RSA;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Hash::Ordered;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use HTTP::Message 6.41;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use HTTP::StructuredField v0.0.5;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use JSON qw( decode_json ); # used by CryptX
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use List::Util 1.29 qw( first max pairmap );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use MIME::Base64 qw( decode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use PerlX::Maybe qw( maybe );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Ref::Util qw( is_blessed_ref is_plain_hashref );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use String::Util qw( trim unquote );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Syntax::Keyword::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Types::Common qw( Bool HashRef InstanceOf NonEmptyStr PositiveInt );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Types::URI qw( Uri );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # RECOMMEND PREREQ: Ref::Util::XS
28 # RECOMMEND PREREQ: Type::Tiny::XS
29  
30 4 ▶use experimental qw( lexical_subs signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 4 our $VERSION = 'v0.1.2';
35  
36  
37  
38 4 has message => (
39 is => 'ro',
40 isa => InstanceOf [qw/ HTTP::Message Plack::Request /],
41 required => 1,
42 handles => [qw/ as_string headers /],
43 );
44  
45  
46 has is_request => (
47 is => 'lazy',
48 isa => Bool,
49 0 ▶ builder => sub($self) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Subroutine _build_is_request: not called CC=2 SCAR=17.9
50 0 my $message = $self->message;
51 0 ▶ return $message->isa("HTTP::Request") || $message->isa("Plack::Request");
Condition0%0/6
$message->isa('HTTP::Request') || $message->isa('Plack::Request')
l!l&&r!l&&!r
000
$message->isa('HTTP::Request') || $message->isa('Plack::Request')
l!l&&r!l&&!r
000
MC/DC0%0/2
$message->isa('HTTP::Request') || $message->isa('Plack::Request')
$message->isa('HTTP::Request') $message->isa('Plack::Request')
MC/DC0%0/2
$message->isa('HTTP::Request') || $message->isa('Plack::Request')
$message->isa('HTTP::Request') $message->isa('Plack::Request')
Truth table0%0/3
A || B
A= $message->isa('HTTP::Request')B= $message->isa('Plack::Request')
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $message->isa('HTTP::Request')B= $message->isa('Plack::Request')
ABresult
1X1
011
000
52 },
53 4 init_arg => undef,
54 );
55  
56  
57 has is_response => (
58 is => 'lazy',
59 isa => Bool,
60 0 ▶ builder => sub($self) { $self->message->isa("HTTP::Response") },
Subroutine _build_is_response: not called CC=1 SCAR=1.2
Subroutine __ANON__: called CC=1 SCAR=1.2
61 4 init_arg => undef,
62 );
63  
64  
65 4 has request => (
66 is => 'ro',
67 isa => InstanceOf [qw/ HTTP::Request Plack::Request /],
68 predicate => 1,
69 );
70  
71  
72 has user_agent => (
73 is => 'lazy',
74 isa => InstanceOf ['LWP::UserAgent'],
75 0 ▶ builder => sub($self) {
Subroutine _build_user_agent: not called CC=1 SCAR=6.9
76 0 return LWP::UserAgent->new( max_size => 8192, timeout => 10 );
77 },
78 4 );
79  
80  
81 has signature_agent => (
82 is => 'lazy',
83 isa => NonEmptyStr,
84 0 ▶ coerce => sub($uri) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
85 0 ▶ if (is_blessed_ref($uri) && $uri->isa("URI")) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_blessed_ref($uri) and $uri->isa("URI")) 0 0 0
Condition0%0/3
Ref::Util::XS::is_blessed_ref($uri) and $uri->isa("URI")
!ll&&!rl&&r
000
MC/DC0%0/2
Ref::Util::XS::is_blessed_ref($uri) and $uri->isa("URI")
Ref::Util::XS::is_blessed_ref($uri) $uri->isa("URI")
Truth table0%0/3
A and B
A= Ref::Util::XS::is_blessed_ref($uri)B= $uri->isa("URI")
ABresult
0X0
100
111
86 0 return $uri->scheme . "://" . $uri->authority;
87 }
88 0 return "$uri";
89 },
90 0 ▶ builder => sub($self) {
Subroutine _build_signature_agent: not called CC=6 SCAR=37.4
91 0 ▶ my $agent = join(",", $self->headers->header("signature-agent") // "" );
Condition0%0/2
$self->headers->header("signature-agent") // ""
l!l
00
MC/DC0%0/1
$self->headers->header("signature-agent") // ""
$self->headers->header("signature-agent")
Truth table0%0/2
A
A= $self->headers->header("signature-agent")
Aresult
11
00
92 0 ▶ if ( !$agent && $self->is_response ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $agent and $self->is_response) 0 0 0
Condition0%0/3
not $agent and $self->is_response
!ll&&!rl&&r
000
MC/DC0%0/2
not $agent and $self->is_response
not $agent $self->is_response
Truth table0%0/3
not(A) and B
A= not $agentB= $self->is_response
ABresult
0X0
100
111
93 0 return $self->request->uri;
94 }
95 0 ▶ if ( $agent =~ /\A [a-z\*][a-z0-1_\-\.\*]* = /ax ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($agent =~ /\A [a-z\*][a-z0-1_\-\.\*]* = /ax) 0 0 0
96 0 ▶ my ($key) = $self->headers->header("signature-agent-key") || $self->signature_labels;
Condition0%0/3
$self->headers->header('signature-agent-key') || $self->signature_labels
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->headers->header('signature-agent-key') || $self->signature_labels
$self->headers->header('signature-agent-key') $self->signature_labels
Truth table0%0/3
A || B
A= $self->headers->header('signature-agent-key')B= $self->signature_labels
ABresult
1X1
011
000
97 0 my $agents = HTTP::StructuredField->parse_dictionary( $agent );
98 0 $agent = $agents->get( unquote($key) )->value;
99 }
100 0 return $agent;
101 }
102 4 );
103  
104  
105 has signature_agent_uri => (
106 is => 'lazy',
107 isa => Uri,
108 coerce => 1,
109 0 ▶ builder => sub($self) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
110 0 my $uri = URI->new( unquote( $self->signature_agent ) );
111 0 ▶ carp "signature-agent header contains a full URL" if $uri->path && $uri->path ne "/";
Branch: 2 branches
BranchTrueFalseTotal
if $uri->path and $uri->path ne "/" 0 0 0
if $uri->path and $uri->path ne "/" 0 0 0
Condition0%0/3
$uri->path and $uri->path ne "/"
!ll&&!rl&&r
000
MC/DC0%0/2
$uri->path and $uri->path ne "/"
$uri->path $uri->path ne "/"
Truth table0%0/3
A and B
A= $uri->pathB= $uri->path ne "/"
ABresult
0X0
100
111
112 0 $uri->path("/.well-known/http-message-signatures-directory");
113 0 return $uri;
114 },
115 4 );
116  
117  
118 has signature_input => (
119 is => 'lazy',
120 isa => InstanceOf ['Hash::Ordered'],
121 11 ▶ builder => sub($self) {
Subroutine __ANON__: called CC=1 SCAR=0.0
122 11 my @input = $self->headers->header("signature-input");
123 11 return HTTP::StructuredField->parse_dictionary( join( ',', @input ) );
124 },
125 4 handles => {
126 signature_labels => 'keys',
127 },
128 );
129  
130  
131 has signature_header => (
132 is => 'lazy',
133 isa => InstanceOf ['Hash::Ordered'],
134 8 ▶ builder => sub($self) {
Subroutine _build_signature_header: called CC=1 SCAR=0.0
135 8 my @input = $self->headers->header("signature");
136 8 return HTTP::StructuredField->parse_dictionary( join( ',', @input ) );
137 },
138 4 );
139  
140  
141 has public_keys_json => (
142 is => 'lazy',
143 isa => NonEmptyStr,
144 0 ▶ builder => sub($self) {
Subroutine _build_public_keys_json: not called CC=8 SCAR=42.8
145  
146 0 state $types = { map { $_ => 1 } qw[ application/http-message-signatures-directory+json application/json ] };
147  
148 0 my $uri = $self->signature_agent_uri;
149 0 ▶ croak "agent_uri should be an https URL" unless $uri->scheme eq "https";
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->scheme eq "https" 0 0 0
150  
151 0 my $req = HTTP::Request->new(
152 GET => $uri,
153 [
154 Accept => join( ",", keys $types->%* ),
155 'User-Agent' => sprintf( 'Perl-%s-%s', __PACKAGE__, $VERSION ),
156 ]
157 );
158  
159 0 my $res = $self->user_agent->request($req);
160  
161 0 ▶ if ( $res->is_success && $types->{ $res->content_type } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success and $$types{$res->content_type}) 0 0 0
Condition0%0/3
$res->is_success and $$types{$res->content_type}
!ll&&!rl&&r
000
MC/DC0%0/2
$res->is_success and $$types{$res->content_type}
$res->is_success $$types{$res->content_type}
Truth table0%0/3
A and B
A= $res->is_successB= $$types{$res->content_type}
ABresult
0X0
100
111
162  
163 0 ▶ if ( $self->verify_public_keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verify_public_keys) 0 0 0
164  
165 0 ▶ if ( $res->header("Signature-Input") && $res->header("Signature") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->header('Signature-Input') and $res->header('Signature')) { } 0 0 0
Condition0%0/3
$res->header("Signature-Input") and $res->header("Signature")
!ll&&!rl&&r
000
MC/DC0%0/2
$res->header("Signature-Input") and $res->header("Signature")
$res->header("Signature-Input") $res->header("Signature")
Truth table0%0/3
A and B
A= $res->header("Signature-Input")B= $res->header("Signature")
ABresult
0X0
100
111
166  
167 0 my $check = __PACKAGE__->new(
168 request => $req,
169 response => $res,
170 require_tag => "http-message-signatures-directory",
171 public_keys_json => $res->decoded_content,
172 );
173  
174 0 ▶ $check->verify or croak "signatures-directory was not signed";
Branch: 1 branch
BranchTrueFalseTotal
$check->verify or croak("signatures-directory was not signed") 0 0 0
175  
176 }
177 else {
178 0 carp "signatures-directory response was unsigned";
179 }
180  
181 }
182  
183 0 $res->max_body_size( $self->max_public_keys_json_size );
184  
185 0 return $res->decoded_content;
186 }
187  
188 0 croak "error retrieving ${uri}";
189 }
190 4 );
191  
192  
193 4 has max_public_keys_json_size => (
194 is => 'ro',
195 isa => PositiveInt,
196 default => 16384,
197 );
198  
199  
200 4 has verify_public_keys => (
201 is => 'ro',
202 isa => Bool,
203 default => 0,
204 );
205  
206  
207 has public_keys => (
208 is => 'lazy',
209 isa => HashRef, # TODO make strict validation optional
210 2 ▶ builder => sub($self) {
Subroutine _build_public_keys: called CC=2 SCAR=6.9
211 2 my $pks = decode_json( $self->public_keys_json );
212 2 my $max = $self->max_keys;
213 2 ▶ if ( defined $pks->{keys}[$max] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pks->{"keys"}[$max]) 1 1 2
214 1 splice( $pks->{keys}->@*, $max );
215 1 carp "public_keys truncated to the first ${max} keys";
216 }
217 2 return $pks;
218 },
219 4 );
220  
221  
222  
223 4 has max_keys => (
224 is => 'ro',
225 isa => PositiveInt,
226 default => 100,
227 );
228  
229  
230 4 has require_tag => (
231 is => 'ro',
232 isa => NonEmptyStr,
233 predicate => 1,
234 );
235  
236  
237 12 ▶sub is_expired( $self, $label = undef ) {
Subroutine is_expired: called CC=4 SCAR=15.2
Pod documented
238 12 ▶ my $input = $self->_get_input_from_label( $label // $self->_get_default_label );
Condition33%1/3
$label // $self->_get_default_label
l!l&&r!l&&!r
0120
MC/DC0%0/2
$label // $self->_get_default_label
$label $self->_get_default_label
Truth table33%1/3
A // B
A= $labelB= $self->_get_default_label
ABresult
1X1
011
000
239 12 ▶ my $expires = $input->params->get("expires") or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $expires = $input->params->get("expires") or return undef 12 0 12
240 12 return time > $expires->value;
241 }
242  
243  
244 8 ▶sub verify( $self, $options = undef ) {
Subroutine verify: called CC=22 SCAR=34.9
Pod documented
245  
246 8 ▶ $options //= {};
Condition50%1/2
$options //= {}
l!l
80
MC/DC0%0/1
$options //= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
247 8 ▶ $options = { label => $options } unless is_plain_hashref($options);
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_plain_hashref($options) 0 8 8
248  
249 8 ▶ my $label = $options->{label} //= $self->_get_default_label;
Condition33%1/3
$options->{"label"} //= $self->_get_default_label
l!l&&r!l&&!r
080
MC/DC0%0/2
$options->{"label"} //= $self->_get_default_label
$options->{"label"} $self->_get_default_label
Truth table33%1/3
A //= B
A= $options->{"label"}B= $self->_get_default_label
ABresult
1X1
011
000
250 8 my $input = $self->_get_input_from_label($label);
251  
252 8 ▶ if ( $options->{check_expires} //= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"check_expires"} //= 1) 4 4 8
Condition50%1/2
$options->{"check_expires"} //= 1
l!l
80
MC/DC0%0/1
$options->{"check_expires"} //= 1
$options->{"check_expires"}
Truth table50%1/2
A
A= $options->{"check_expires"}
Aresult
11
00
253 4 ▶ croak "signature is expired" if $self->is_expired;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_expired 0 4 4
254 }
255  
256 8 ▶ if ( $options->{check_created} //= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"check_created"} //= 1) 8 0 8
Condition50%1/2
$options->{"check_created"} //= 1
l!l
08
MC/DC0%0/1
$options->{"check_created"} //= 1
$options->{"check_created"}
Truth table50%1/2
A
A= $options->{"check_created"}
Aresult
11
00
257 8 my $created = $input->params->get("created");
258 8 ▶ croak "signature is postdated" if $created && $created->value > time;
Branch: 1 branch
BranchTrueFalseTotal
if $created and $created->value > time 0 8 8
Condition33%1/3
$created and $created->value > time
!ll&&!rl&&r
080
MC/DC0%0/2
$created and $created->value > time
$created $created->value > time
Truth table33%1/3
A and B
A= $createdB= $created->value > time
ABresult
0X0
100
111
259 }
260  
261 8 ▶ if ( $self->has_require_tag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_require_tag) 8 0 8
262 8 my $check = $input->params->get("tag");
263 8 my $tag = $self->require_tag;
264 8 ▶ croak "tag is not '${tag}'" unless $check && $check->value eq $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless $check and $check->value eq $tag 0 8 8
Condition33%1/3
$check and $check->value eq $tag
!ll&&!rl&&r
008
MC/DC0%0/2
$check and $check->value eq $tag
$check $check->value eq $tag
Truth table33%1/3
A and B
A= $checkB= $check->value eq $tag
ABresult
0X0
100
111
265 }
266  
267 8 ▶ my $keyid = $input->params->get("keyid") or croak "no keyid was found in the signature-input";
Branch: 1 branch
BranchTrueFalseTotal
my $keyid = $input->params->get("keyid") or croak("no keyid was found in the signature-input") 8 0 8
268 8 my $kid = unquote( $keyid->value );
269  
270 8 state $algs = {
271 "ed25519" => { class => "Crypt::PK::Ed25519", key => { kty => "OKP", crv => "Ed25519" } },
272 "rsa-pss-sha512" => { class => "Crypt::PK::RSA", key => { kty => "RSA", alg => "PS512" }, hash => [qw/ SHA512 pss 64 /] },
273 };
274  
275 8 ▶ my $alg = eval { $input->params->get("alg")->value } or croak "missing alg parameter";
Branch: 1 branch
BranchTrueFalseTotal
my $alg = eval { do { $input->params->get("alg")->value } } or croak("missing alg parameter") 8 0 8
276 8 ▶ my $meta = $algs->{ $alg } or croak "unsupported algorithm";
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $algs->{$alg} or croak("unsupported algorithm") 8 0 8
277  
278 8 ▶ my $key = first { $_->{kid} eq $kid } $self->public_keys->{keys}->@*
Subroutine __ANON__: called CC=1 SCAR=0.0
279 8 ▶ or croak "keyid '${kid}' was not found in the public keys";
Branch: 1 branch
BranchTrueFalseTotal
my $key = &first(sub { $_->{"kid"} eq $kid; } , @{$self->public_keys->{"keys"}}) or croak("keyid '${kid}' was not found in the public keys") 8 0 8
280  
281 # TODO: add support for looking up keys from a cache based on agent URL and key id, and alg
282  
283 8 for my $field ( keys $meta->{key}->%* ) {
284 16 ▶ $key->{$field} eq $meta->{key}{$field} or croak "keyid ${kid} is incompatible with ${alg}";
Branch: 1 branch
BranchTrueFalseTotal
$key->{$field} eq $meta->{"key"}{$field} or croak("keyid $kid is incompatible with $alg") 16 0 16
285 }
286  
287 8 my $class = $meta->{class};
288  
289 8 ▶ my @params = $meta->{hash} ? $meta->{hash}->@* : ();
Branch: 1 branch
BranchTrueFalseTotal
$meta->{'hash'} ? : 4 4 8
290  
291 8 my $pk = $class->new($key);
292  
293 8 my $sig = $self->signature_header->get($label)->value;
294  
295 8 my $base = $self->_generate_signature_base($options);
296  
297 8 return $pk->verify_message( $sig, $base, @params );
298 }
299  
300 20 ▶sub _get_default_label($self) {
Subroutine _get_default_label: called CC=2 SCAR=7.1
301 20 my ($label) = $self->signature_labels;
302 20 ▶ croak "no labels found" unless defined $label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label 0 20 20
303 20 return $label;
304 }
305  
306 31 ▶sub _get_input_from_label( $self, $label ) {
Subroutine _get_input_from_label: called CC=2 SCAR=7.2
307 31 ▶ my $input = $self->signature_input->get($label) or croak "signature-input label ${label} was not found";
Branch: 1 branch
BranchTrueFalseTotal
my $input = $self->signature_input->get($label) or croak("signature-input label $label was not found") 31 0 31
308 31 return $input;
309 }
310  
311 11 ▶sub _generate_signature_base( $self, $options ) {
Subroutine _generate_signature_base: called CC=6 SCAR=18.0
312  
313 11 my $label = $options->{label};
314  
315 11 my @buffer;
316  
317 11 my $seen = Hash::Ordered->new;
318  
319 28 ▶ my sub _process_header_field( $field, $value ) {
Subroutine _process_header_field: called CC=3 SCAR=11.4
320  
321 # TODO serialize line as per "component-identifier ABNF rule"
322 # TODO canonicalize the field value
323 28 ▶ my $line = sprintf( '%s: %s', lc($field), $value // "" );
Condition50%1/2
$value // ""
l!l
280
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
324 28 ▶ croak "component has already been added to the sginature base" if $seen->exists( lc $field );
Branch: 1 branch
BranchTrueFalseTotal
if $seen->exists(lc $field) 0 28 28
325 28 $seen->set( lc $field => 1 );
326 28 push @buffer, $line;
327 }
328  
329 11 my $input = $self->_get_input_from_label($label);
330 11 ▶ croak "label ${label} is not a list" unless $input->is_inner_list;
Branch: 1 branch
BranchTrueFalseTotal
unless $input->is_inner_list 0 11 11
331  
332 11 for my $field ( $input->value->@* ) {
333  
334 17 my $name = unquote($field->value, braces => 1 );
335 17 ▶ if ( substr($name, 0, 1) eq '@' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($name, 0, 1) eq '@') { } 11 6 17
336 11 _process_header_field( $self->_get_derived_field( $field, $options ) );
337 }
338 else {
339  
340 6 my $header = $self->headers->header($name);
341 6 ▶ if ($field->has_params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->has_params) { } 2 4 6
342  
343 2 my ($key) = $field->params->get("key")->value;
344 2 my $dict = HTTP::StructuredField->parse_dictionary($header);
345 2 my $param = $dict->get($key);
346  
347 # TODO: improve quoting
348 2 _process_header_field( "\"${name}\";key=\"${key}\"" => sprintf('"%s"', $param->value ) );
349  
350 }
351 else {
352 4 _process_header_field( "\"${name}\"" => $header );
353 }
354 }
355  
356 }
357  
358 11 my $header = $self->headers->header("signature-input");
359 11 ▶ croak "unexpected signature-input" unless substr( $header, 0, length($label) + 1 ) eq "${label}=";
Branch: 1 branch
BranchTrueFalseTotal
unless substr($header, 0, length($label) + 1) eq "$label=" 0 11 11
360 11 my $params = substr( $header, length($label) + 1 );
361 11 _process_header_field( '"@signature-params"' => $params );
362  
363 11 return join( "\n", @buffer );
364 }
365  
366 11 ▶sub _get_derived_field( $self, $field, $options ) {
Subroutine _get_derived_field: called CC=5 SCAR=22.1
367  
368 11 my $name = $field->value;
369  
370 # Note: HTTP::Message has uri_canonical but Plack::Request does not.
371  
372 state $handlers = {
373 0 ▶ '@status' => sub( $message, $, $ ) { $message->code },
Subroutine __ANON__: not called CC=1 SCAR=6.9
374 0 ▶ '@method' => sub( $message, $, $ ) { $message->method },
Subroutine __ANON__: not called CC=1 SCAR=6.9
375 0 ▶ '@target-uri' => sub( $message, $, $ ) { $message->uri },
Subroutine __ANON__: not called CC=1 SCAR=6.9
376 11 ▶ '@authority' => sub( $message, $, $ ) { $message->uri->authority },
Subroutine __ANON__: called CC=1 SCAR=0.0
377 0 ▶ '@scheme' => sub( $message, $, $ ) { $message->uri->scheme },
Subroutine __ANON__: not called CC=1 SCAR=6.9
378 0 ▶ '@path' => sub( $message, $, $ ) { $message->uri->path },
Subroutine __ANON__: not called CC=1 SCAR=6.9
379 0 ▶ '@query' => sub( $message, $, $ ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
380 0 my $query = $message->uri->query;
381 0 ▶ if ( defined $query ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query) 0 0 0
382 0 return '?' . $query;
383 }
384 0 return undef;
385 },
386 0 ▶ '@query-param' => sub( $message, $, $params ) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
387 0 ▶ if ( my ($key) = $params->keys ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($key) = $params->keys) 0 0 0
388 # Note: RFC 9421 Sec 2.2.8 says named parameters with multiple values must not be included
389 0 my @values = $message->uri->query_param($key);
390 0 ▶ croak "multipe query-param values for '${key}'" if @values > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @values > 1 0 0 0
391 0 return $values[0];
392 }
393 }
394 11 };
395  
396 11 ▶ croak "unsupported field ${name}" unless exists $handlers->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $handlers->{$name} 0 11 11
397  
398 11 my $quoted = "\"${name}\"";
399 11 my $message = $self->message;
400  
401 11 my ($param, @extra) = $field->params->keys;
402 11 ▶ if ( defined $param ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param) 1 10 11
403 1 ▶ croak "extra parameters in field ${name}" if @extra;
Branch: 1 branch
BranchTrueFalseTotal
if @extra 0 1 1
404 1 $quoted .= ";" . unquote($param);
405 1 ▶ if ( $param eq "req" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "req") 1 0 1
406 1 $message = $self->request;
407 }
408 }
409  
410 11 return ( maybe $quoted => $handlers->{$name}->( $message, $options, $field->params ) );
411 }
412  
413  
414 13 ▶sub BUILDARGS( $, @args ) {
Subroutine BUILDARGS: called CC=6 SCAR=18.1
Pod undocumented
415  
416 13 ▶ my %args = @args == 1 && is_plain_hashref( $args[0] ) ? $args[0]->%* : @args;
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 && Ref::Util::XS::is_plain_hashref($args[0]) ? : 0 13 13
Condition33%1/3
@args == 1 and Ref::Util::XS::is_plain_hashref($args[0])
!ll&&!rl&&r
1300
MC/DC0%0/2
@args == 1 and Ref::Util::XS::is_plain_hashref($args[0])
@args == 1 Ref::Util::XS::is_plain_hashref($args[0])
Truth table33%1/3
A and B
A= @args == 1B= Ref::Util::XS::is_plain_hashref($args[0])
ABresult
0X0
100
111
417  
418 13 ▶ if (my $res = $args{response}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $res = $args{'response'}) { } 1 12 13
elsif (my $req = $args{'request'}) { } 8 4 12
419 1 $args{message} = $res;
420 }
421 elsif (my $req = $args{request}) {
422 8 $args{message} = $req;
423 }
424  
425 13 my $message = $args{message};
426 13 ▶ if ( is_plain_hashref($message) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_hashref($message)) 1 12 13
427 1 require Plack::Request;
428 1 $args{message} = Plack::Request->new($message);
429  
430 }
431  
432 13 return \%args;
433 }
434  
435  
436 4 1;
437  
438 __END__
↑ Summary