ACME2/HTTP.pm

filter: Statementstmt 79.1% 76 / 96 Branchbran 37.5% 9 / 24 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 95.0% 19 / 20 Podpod 0.0% 0 / 6 totaltotal 67.0% 108 / 161 SCAR 31.6
CC
16
Cov
69%
_request
18.2
_create_jwt
6.9
_post
3.2
CRAP 23.6
« ACME2.pm ↑ Summary ACME2/Error.pm »
1 package Net::ACME2::HTTP;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Net::ACME2::HTTP - transport logic for C<Net::ACME2>.
8  
9 =head1 DESCRIPTION
10  
11 This module handles communication with an ACME server at the HTTP level.
12 It wraps POSTs in JWSes (JSON Web Signatures) as needed.
13  
14 There should be no reason to interact with this class in production.
15  
16 =cut
17  
18 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use Net::ACME2::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Net::ACME2::HTTP_Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Net::ACME2::HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Net::ACME2::X ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 ▶use constant _CONTENT_TYPE => 'application/jose+json';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 #accessed from tests
31 2 our $_NONCE_HEADER = 'replay-nonce';
32  
33 #Used in testing
34 2 our $verify_SSL = 1;
35  
36 #NB: “key” isn’t needed if we’re just doing GETs.
37 sub new {
38 2 ▶ my ( $class, %opts ) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
39  
40 2 ▶ die Net::ACME2::X->create('Generic', 'need “key”!') if !$opts{'key'};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"key"} 0 2 2
41  
42 2 my $ua = Net::ACME2::HTTP_Tiny->new( verify_SSL => $verify_SSL );
43  
44 my $self = bless {
45 _ua => $ua,
46 _acme_key => $opts{'key'},
47 _key_id => $opts{'key_id'},
48 2 _jws_format => $opts{'jws_format'},
49 }, $class;
50  
51 2 return bless $self, $class;
52 }
53  
54 sub set_key_id {
55 2 ▶ my ($self, $key_id) = @_;
Subroutine set_key_id: called CC=1 SCAR=0.0
Pod undocumented
56  
57 2 $self->{'_key_id'} = $key_id;
58  
59 2 return $self;
60 }
61  
62 sub set_new_nonce_url {
63 5 ▶ my ($self, $url) = @_;
Subroutine set_new_nonce_url: called CC=1 SCAR=0.0
Pod undocumented
64  
65 5 $self->{'_nonce_url'} = $url;
66  
67 5 return $self;
68 }
69  
70 #GETs submit no data and thus are not signed.
71 sub get {
72 1 ▶ my ( $self, $url ) = @_;
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
73  
74 1 return $self->_request( 'GET', $url );
75 }
76  
77 # ACME spec 6.2: for all requests not signed using an existing account,
78 # e.g., newAccount
79 sub post_full_jwt {
80 2 ▶ my $self = shift;
Subroutine post_full_jwt: called CC=1 SCAR=0.0
Pod undocumented
81  
82 2 return $self->_post( 'create_full_jws', @_ );
83 }
84  
85 # ACME spec 6.2: for all requests signed using an existing account
86 sub post_key_id {
87 0 ▶ my $self = shift;
Subroutine post_key_id: not called CC=1 SCAR=6.9
Pod undocumented
88  
89 0 return $self->_post(
90 'create_key_id_jws',
91 @_,
92 );
93 }
94  
95 #----------------------------------------------------------------------
96  
97 #POSTs are signed.
98 sub _post {
99 2 ▶ my ( $self, $jwt_method, $url, $data, $opts_hr ) = @_;
Subroutine _post: called CC=3 SCAR=11.8
100  
101 # Shouldn’t be needed because the constructor requires “key”,
102 # but just in case.
103 2 ▶ die "Constructor needed “key” to do POST! ($url)" if !$self->{'_acme_key'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_acme_key"} 0 2 2
104  
105 2 my $jws = $self->_create_jwt( $jwt_method, $url, $data );
106  
107 2 local $opts_hr->{'headers'}{'Content-Type'} = 'application/jose+json';
108  
109 2 ▶ return $self->_request_and_set_last_nonce(
Condition33%1/3
$opts_hr || ()
l!l&&r!l&&!r
200
MC/DC0%0/2
$opts_hr || ()
$opts_hr ()
Truth table33%1/3
A || B
A= $opts_hrB= ()
ABresult
1X1
011
000
110 'POST',
111 $url,
112 {
113 content => $jws,
114 headers => {
115 'content-type' => _CONTENT_TYPE,
116 },
117 },
118 $opts_hr || (),
119 );
120 }
121  
122 sub _ua_request {
123 4 ▶ my ( $self, $type, @args ) = @_;
Subroutine _ua_request: called CC=1 SCAR=0.0
124  
125 4 return $self->{'_ua'}->request( $type, @args );
126 }
127  
128 #overridden in tests
129 sub _request {
130 4 ▶ my ( $self, $type, @args ) = @_;
Subroutine _request: called CC=6 SCAR=29.0
131  
132 4 my $resp;
133  
134 #cf. eval_bug.readme
135 4 my $eval_err = $@;
136  
137 4 eval { $resp = $self->_ua_request( $type, @args ); };
138  
139 # Check ref() first to avoid potentially running overload.pm’s
140 # stringification.
141 4 ▶ if (ref($@) || $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $@ or $@) 0 4 4
Condition33%1/3
ref $@ or $@
l!l&&r!l&&!r
004
MC/DC0%0/2
ref $@ or $@
ref $@ $@
Truth table33%1/3
A or B
A= ref $@B= $@
ABresult
1X1
011
000
142 0 my $exc = $@;
143  
144 0 ▶ if ( eval { $exc->isa('Net::ACME2::X::HTTP::Protocol') } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $exc->isa("Net::ACME2::X::HTTP::Protocol") } }) 0 0 0
145 0 my $_nonce_header_lc = $_NONCE_HEADER;
146 0 $_nonce_header_lc =~ tr<A-Z><a-z>;
147  
148 0 my $nonce = $exc->get('headers')->{$_nonce_header_lc};
149 0 ▶ $self->{'_last_nonce'} = $nonce if $nonce;
Branch: 1 branch
BranchTrueFalseTotal
if $nonce 0 0 0
150  
151 #If the exception is able to be made into a Net::ACME2::Error,
152 #then do so to get a nicer error message.
153 0 my $acme_error = eval {
154 Net::ACME2::Error->new(
155 0 %{ JSON::decode_json( $exc->get('content') ) },
156 );
157 };
158  
159 0 ▶ if ($acme_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acme_error) 0 0 0
160 0 die Net::ACME2::X->create(
161 'ACME',
162 {
163 http => $exc,
164 acme => $acme_error,
165 },
166 );
167 }
168 }
169  
170 0 $@ = $exc;
171 0 die;
172 }
173  
174 4 $@ = $eval_err;
175  
176 4 return Net::ACME2::HTTP::Response->new($resp);
177 }
178  
179 sub _request_and_set_last_nonce {
180 3 ▶ my ( $self, $type, $url, @args ) = @_;
Subroutine _request_and_set_last_nonce: called CC=2 SCAR=7.4
181  
182 3 my $resp = $self->_request( $type, $url, @args );
183  
184 #NB: ACME’s replay protection works thus:
185 # - each server response includes a nonce
186 # - each request must include ONE of the nonces that have been sent
187 # - once used, a nonce can’t be reused
188 #
189 #This is subtly different from what was originally in mind (i.e., that
190 #each request must use the most recently sent nonce). It implies that GETs
191 #do not need to send nonces, though each GET will *receive* a nonce that
192 #may be used.
193 3 ▶ $self->{'_last_nonce'} = $resp->header($_NONCE_HEADER) or do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_last_nonce"} = $resp->header($Net::ACME2::HTTP::_NONCE_HEADER) or do { die "Net::ACME2::X"->create("Generic", "Received no $Net::ACME2::HTTP::_NONCE_HEADER from $url!") } 0 3 3
194 0 die Net::ACME2::X->create('Generic', "Received no $_NONCE_HEADER from $url!");
195 };
196  
197 3 return $resp;
198 }
199  
200 sub _get_first_nonce {
201 1 ▶ my ($self) = @_;
Subroutine _get_first_nonce: called CC=2 SCAR=7.4
202  
203 1 ▶ my $url = $self->{'_nonce_url'} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $url = $self->{"_nonce_url"} or do { die "Net::ACME2::X"->create("Set newNonce URL first!") } 0 1 1
204  
205 # Shouldn’t happen unless there’s an errant refactor.
206 0 die Net::ACME2::X->create('Set newNonce URL first!');
207 };
208  
209 1 $self->_request_and_set_last_nonce( 'HEAD', $url );
210  
211 1 return;
212 }
213  
214 sub _create_jwt {
215 2 ▶ my ( $self, $jwt_method, $url, $data ) = @_;
Subroutine _create_jwt: called CC=6 SCAR=19.3
216  
217 2 ▶ $self->_get_first_nonce() if !$self->{'_last_nonce'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_last_nonce"} 1 1 2
218  
219 2 ▶ $self->{'_jwt_maker'} ||= do {
Condition66%2/3
$self->{"_jwt_maker"} ||= do { my $class; if ($self->{"_acme_key"}->isa("Crypt::Perl::RSA::PrivateKey")) { $class = "Net::ACME2::JWTMaker::RSA"; } elsif ($self->{"_acme_key"}->isa("Crypt::Perl::ECDSA::PrivateKey")) { $class = "Net::ACME2::JWTMaker::ECC"; } else { die "Net::ACME2::X"->create("Generic", qq[Unrecognized key type: $self->{"_acme_key"}]); } ; unless ($class->can("new")) { require Module::Load; &Module::Load::load($class); } ; $class->new("key", $self->{"_acme_key"}, "format", $self->{"_jws_format"}) }
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->{"_jwt_maker"} ||= do { my $class; if ($self->{"_acme_key"}->isa("Crypt::Perl::RSA::PrivateKey")) { $class = "Net::ACME2::JWTMaker::RSA"; } elsif ($self->{"_acme_key"}->isa("Crypt::Perl::ECDSA::PrivateKey")) { $class = "Net::ACME2::JWTMaker::ECC"; } else { die "Net::ACME2::X"->create("Generic", qq[Unrecognized key type: $self->{"_acme_key"}]); } ; unless ($class->can("new")) { require Module::Load; &Module::Load::load($class); } ; $class->new("key", $self->{"_acme_key"}, "format", $self->{"_jws_format"}) }
$self->{"_jwt_maker"} do { my $class; if ($self->{"_acme_key"}->isa("Crypt::Perl::RSA::PrivateKey")) { $class = "Net::ACME2::JWTMaker::RSA"; } elsif ($self->{"_acme_key"}->isa("Crypt::Perl::ECDSA::PrivateKey")) { $class = "Net::ACME2::JWTMaker::ECC"; } else { die "Net::ACME2::X"->create("Generic", qq[Unrecognized key type: $self->{"_acme_key"}]); } ; unless ($class->can("new")) { require Module::Load; &Module::Load::load($class); } ; $class->new("key", $self->{"_acme_key"}, "format", $self->{"_jws_format"}) }
Truth table66%2/3
A ||= B
A= $self->{"_jwt_maker"}B= do { my $class; if ($self->{"_acme_key"}->isa("Crypt::Perl::RSA::PrivateKey")) { $class = "Net::ACME2::JWTMaker::RSA"; } elsif ($self->{"_acme_key"}->isa("Crypt::Perl::ECDSA::PrivateKey")) { $class = "Net::ACME2::JWTMaker::ECC"; } else { die "Net::ACME2::X"->create("Generic", qq[Unrecognized key type: $self->{"_acme_key"}]); } ; unless ($class->can("new")) { require Module::Load; &Module::Load::load($class); } ; $class->new("key", $self->{"_acme_key"}, "format", $self->{"_jws_format"}) }
ABresult
1X1
011
000
220 1 my $class;
221  
222 1 ▶ if ($self->{'_acme_key'}->isa('Crypt::Perl::RSA::PrivateKey')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_acme_key'}->isa('Crypt::Perl::RSA::PrivateKey')) { } 1 0 1
elsif ($self->{'_acme_key'}->isa('Crypt::Perl::ECDSA::PrivateKey')) { } 0 0 0
223 1 $class = 'Net::ACME2::JWTMaker::RSA';
224 }
225 elsif ($self->{'_acme_key'}->isa('Crypt::Perl::ECDSA::PrivateKey')) {
226 0 $class = 'Net::ACME2::JWTMaker::ECC';
227 }
228 else {
229  
230 # As of this writing, Crypt::Perl only does RSA and ECDSA keys.
231 # If we get here, it’s possible that Crypt::Perl now supports
232 # an additional key type that this library doesn’t recognize.
233 0 die Net::ACME2::X->create('Generic', "Unrecognized key type: $self->{'_acme_key'}");
234 }
235  
236 1 ▶ if (!$class->can('new')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("new")) 1 0 1
237 1 require Module::Load;
238 1 Module::Load::load($class);
239 }
240  
241 $class->new(
242 key => $self->{'_acme_key'},
243 1 format => $self->{'_jws_format'},
244 );
245 };
246  
247 return $self->{'_jwt_maker'}->$jwt_method(
248 key_id => $self->{'_key_id'},
249 payload => $data,
250 extra_headers => {
251 2 nonce => $self->{'_last_nonce'},
252 url => $url,
253 },
254 );
255 }
256  
257 2 1;
« ACME2.pm ↑ Summary ACME2/Error.pm »