ACME/HTTP.pm

filter: Statementstmt 98.7% 78 / 79 Branchbran 66.6% 12 / 18 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 3 totaltotal 87.3% 110 / 126 CC 12 SCAR 11.2
CC
12
Cov
90%
_request
7.1
post
3.3
new
2.0
CRAP 3.1
« ACME/Authorization/Pending.pm ↑ Summary ACME/Certificate.pm »
1 package Net::ACME::HTTP;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Net::ACME::HTTP - transport logic for C<Net::ACME>.
8  
9 =head1 SYNOPSIS
10  
11 my $resp = Net::ACME::HTTP->new()->get('https://url/to/endpoint');
12  
13 my $http_authn = Net::ACME::HTTP->new( key => $account_key );
14  
15 my $post_resp = $http_authn->post(
16 'https://url/to/endpoint',
17 { foo => 4 }, #i.e., the payload to send
18 %opts, #cf. HTTP::Tiny::request
19 );
20  
21 =head1 DESCRIPTION
22  
23 This module handles communication with an ACME server at the HTTP level.
24 It handles the wrapping of POSTs in JWSes (JSON Wed Signatures).
25  
26 Failure responses prompt exceptions. This includes cases like HTTP 409
27 from “new-reg”, which maybe isn’t an B<error> case so much as just
28 something to accommodate.
29  
30 =cut
31  
32 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 7 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 7 ▶use Net::ACME::Crypt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
38 7 ▶use Net::ACME::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 7 ▶use Net::ACME::HTTP_Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 7 ▶use Net::ACME::HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 7 ▶use Net::ACME::Utils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 7 ▶use Net::ACME::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 #accessed from tests
45 7 our $_NONCE_HEADER = 'replay-nonce';
46  
47 #Used in testing
48 7 our $verify_SSL = 1;
49  
50 #NB: “key” isn’t needed if we’re just doing GETs.
51 12 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
52 12 my ( $class, %opts ) = @_;
53  
54 12 my $ua = Net::ACME::HTTP_Tiny->new( verify_SSL => $verify_SSL );
55  
56 my $self = bless {
57 _ua => $ua,
58 12 ▶ _acme_key => $opts{'key'} && Net::ACME::Crypt::parse_key($opts{'key'}),
Condition66%2/3
$opts{'key'} && Net::ACME::Crypt::parse_key($opts{'key'})
!ll&&!rl&&r
606
MC/DC50%1/2
$opts{'key'} && Net::ACME::Crypt::parse_key($opts{'key'})
$opts{'key'} Net::ACME::Crypt::parse_key($opts{'key'})
Truth table66%2/3
A && B
A= $opts{'key'}B= Net::ACME::Crypt::parse_key($opts{'key'})
ABresult
0X0
100
111
59 }, $class;
60  
61 12 return bless $self, $class;
62 }
63  
64 #GETs submit no data and thus are not signed.
65 8 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
66 8 my ( $self, $url ) = @_;
67  
68 8 return $self->_request_and_set_last_nonce( 'GET', $url );
69 }
70  
71 #POSTs are signed.
72 9 ▶sub post {
Subroutine post: called CC=3 SCAR=12.0
Pod undocumented
73 9 my ( $self, $url, $data, $opts_hr ) = @_;
74  
75 9 ▶ die "Constructor needed “key” to do POST! ($url)" if !$self->{'_acme_key'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_acme_key"} 0 9 9
76  
77 9 my $jws = $self->_create_jws( JSON->new()->allow_nonref()->encode($data) );
78  
79 8 ▶ return $self->_request_and_set_last_nonce(
Condition33%1/3
$opts_hr || ()
l!l&&r!l&&!r
080
MC/DC0%0/2
$opts_hr || ()
$opts_hr ()
Truth table33%1/3
A || B
A= $opts_hrB= ()
ABresult
1X1
011
000
80 'POST',
81 $url,
82 { content => $jws },
83 $opts_hr || (),
84 );
85 }
86  
87 #----------------------------------------------------------------------
88  
89 13 ▶sub _ua_request {
Subroutine _ua_request: called CC=1 SCAR=0.0
90 13 my ( $self, $type, @args ) = @_;
91  
92 13 return $self->{'_ua'}->request( $type, @args );
93 }
94  
95 #overridden in tests
96 16 ▶sub _request {
Subroutine _request: called CC=7 SCAR=19.6
97 16 my ( $self, $type, @args ) = @_;
98  
99 16 my $resp;
100  
101 #cf. eval_bug.readme
102 16 my $eval_err = $@;
103  
104 16 eval { $resp = $self->_ua_request( $type, @args ); };
105  
106 16 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 3 13 16
107 3 my $exc = $@;
108  
109 3 ▶ if ( eval { $exc->isa('Net::ACME::X::HTTP::Protocol') } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $exc->isa("Net::ACME::X::HTTP::Protocol") } }) 2 1 3
110 2 my $_nonce_header_lc = $_NONCE_HEADER;
111 2 $_nonce_header_lc =~ tr<A-Z><a-z>;
112  
113 2 my $nonce = $exc->get('headers')->{$_nonce_header_lc};
114 2 ▶ $self->{'_last_nonce'} = $nonce if $nonce;
Branch: 1 branch
BranchTrueFalseTotal
if $nonce 2 0 2
115  
116 #If the exception is able to be made into a Net::ACME::Error,
117 #then do so to get a nicer error message.
118 2 my $acme_error = eval {
119 Net::ACME::Error->new(
120 2 %{ JSON::decode_json( $exc->get('content') ) },
121 );
122 };
123  
124 2 my $detail;
125 2 ▶ if ($acme_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acme_error) { } 2 0 2
126 2 $detail = $acme_error->detail();
127 2 my $desc = $acme_error->description();
128 2 ▶ if ($desc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc) 2 0 2
129 2 $detail = sprintf "%s (%s)", $detail, $desc;
130 }
131 }
132 else {
133 0 $detail = $exc->get('content');
134 }
135  
136 die Net::ACME::X::create(
137 'Protocol',
138 {
139 2 ▶ ( map { $_ => $exc->get($_) } qw( url status reason headers ) ),
Branch: 1 branch
BranchTrueFalseTotal
$acme_error ? : 2 0 2
140 type => $acme_error ? $acme_error->type() : '(unknown type)',
141 detail => $detail,
142 }
143 );
144 }
145  
146 1 $@ = $exc;
147 1 die;
148 }
149  
150 13 $@ = $eval_err;
151  
152 13 return Net::ACME::HTTP::Response->new($resp);
153 }
154  
155 16 ▶sub _request_and_set_last_nonce {
Subroutine _request_and_set_last_nonce: called CC=2 SCAR=7.0
156 16 my ( $self, $type, @args ) = @_;
157  
158 16 my $resp = $self->_request( $type, @args );
159  
160 #NB: ACME’s replay protection works thus:
161 # - each server response includes a nonce
162 # - each request must include ONE of the nonces that have been sent
163 # - once used, a nonce can’t be reused
164 #
165 #This is subtly different from what was originally in mind (i.e., that
166 #each request must use the most recently sent nonce). It implies that GETs
167 #do not need to send nonces, though each GET will *receive* a nonce that
168 #may be used.
169 13 ▶ if ( my $nonce = $resp->header($_NONCE_HEADER) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $nonce = $resp->header($Net::ACME::HTTP::_NONCE_HEADER)) 13 0 13
170 13 $self->{'_last_nonce'} = $nonce;
171 }
172  
173 13 return $resp;
174 }
175  
176 9 ▶sub _create_jws {
Subroutine _create_jws: called CC=2 SCAR=6.9
177 9 my ( $self, $msg ) = @_;
178  
179 9 ▶ die "Need a nonce before JWS can be created!" if !$self->{'_last_nonce'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_last_nonce"} 1 8 9
180  
181 return Net::ACME::Crypt::create_jwt(
182 key => $self->{'_acme_key'},
183 extra_headers => {
184 nonce => $self->{'_last_nonce'},
185 8 jwk => $self->{'_acme_key'}->get_struct_for_public_jwk(),
186 },
187 payload => $msg,
188 );
189 }
190  
191 7 1;
« ACME/Authorization/Pending.pm ↑ Summary ACME/Certificate.pm »