LetsEncrypt/Message.pm

filter: Statementstmt 95.4% 84 / 88 Branchbran 60.0% 12 / 20 Conditioncond 63.6% 7 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 90.0% 18 / 20 Podpod 100% 1 / 1 totaltotal 84.3% 124 / 147 CC 16 SCAR 19.6
CC
16
Cov
87%
do_request
15.7
_get_nonce
3.0
_prep_step
2.0
CRAP 7.1
« LetsEncrypt/Message/Registration.pm ↑ Summary LetsEncrypt/Message/Certificate.pm »
1 package WWW::LetsEncrypt::Message;
2 8 $WWW::LetsEncrypt::Message::VERSION = '0.001';
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use constant {RC_TOO_MANY_REQUESTS => 429};
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use WWW::LetsEncrypt::JWS;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 WWW::LetsEncrypt::Message - Base class for all ACME messages.
20  
21 =head1 SYNPOSIS
22  
23 package Some::Acme::Message;
24  
25 use Moose;
26 extends 'WWW::LetsEncrypt::Message';
27  
28 # implement functions required functions
29 ...
30  
31 =head1 DESCRIPTION
32  
33 This is an abstract class that provides the basis for ACME messages being sent
34 to the Let's Encrypt CA server.
35  
36 =head2 Attributes
37  
38 =over 4
39  
40 =item _acme_base_url
41  
42 private attribute that holds the ACME API URL. Defaults to the
43 Let's Encrypt API URL, cannot be changed once the object has been created.
44  
45 =item JWK
46  
47 JSON Web Key object that implements both JWK and JWA (will be passed to
48 a JWS object). This is required.
49  
50 =item nonce
51  
52 scalar attribute that holds the nonce. It will be used when sending
53 messages, and will be automatically refreshed upon receiving a message from
54 the Let's Encrypt CA server. This attribute is not required but messages w/o
55 a valid nonce may fail!
56  
57 =item retry_time
58  
59 scalar integer attribute that holds how much time the client
60 should wait before trying to send another API request. This value being true
61 usually means an HTTP 429 was encountered and that the client should back off.
62  
63 =item WebAgent
64  
65 object attribute that holds an instance of LWP::UserAgent (or
66 anything that happens to implement the same functions). If not provided during
67 initialization, a new LWP::UserAgent object will be created with the following
68 settings:
69  
70 =over 4
71  
72 =item protocols allows: https
73  
74 =item agent: LE-Bot (libwww-perl)
75  
76 =item ssl_option: verify_hostname => true
77  
78 =back
79  
80 =back
81  
82 =cut
83  
84 8 has 'acme_base_url' => (
85 is => 'ro',
86 isa => 'Str',
87 default => 'https://acme-v01.api.letsencrypt.org',
88 );
89  
90 8 has 'JWK' => (
91 is => 'ro',
92 isa => 'Object',
93 required => 1
94 );
95  
96 8 has 'nonce' => (
97 is => 'rw',
98 isa => 'Str',
99 );
100  
101 8 has 'retry_time' => (
102 isa => 'Int',
103 is => 'rw',
104 init_arg => undef,
105 );
106  
107 has 'WebAgent' => (
108 is => 'rw',
109 isa => duck_type("LWUA_Compatible" => [qw(request)]),
110 required => 1,
111 default => sub {
112 19 ▶ return LWP::UserAgent->new(
Subroutine __ANON__: called CC=1 SCAR=0.0
113 ssl_opts => { verify_hostname => 1 },
114 protocols_allowed => ['https'],
115 agent => 'LE-Bot (libwww-perl)',
116 );
117 },
118 8 );
119  
120 # '_payload' private attribute that holds the payload hashref, which will be put
121 # into the JWS and sent to the server.
122  
123 has '_payload' => (
124 is => 'rw',
125 isa => 'HashRef',
126 19 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
127 8 init_arg => undef,
128 );
129  
130 # '_Request' private attribute that holds an HTTP::Request object. Derived
131 # classes must set this prior to do_request() being called, as this object will
132 # be passed to WebAgent to consume.
133  
134 8 has '_Request' => (
135 is => 'rw',
136 isa => 'Object',
137 init_arg => undef,
138 );
139  
140 # '_step' private attribute that should be used to keep track of the what step
141 # the Message object is currently performing.
142  
143 8 has '_step' => (
144 is => 'rw',
145 isa => 'Str',
146 );
147  
148 # '_uri' private attribute that potentially holds the URL for the next step,
149 # which is necessary for some messages that contain a URL that is dynamically
150 # generated and must be followed.
151  
152 8 has '_url' => (
153 is => 'rw',
154 isa => 'Str',
155 );
156  
157 =head2 Functions
158  
159 =over 4
160  
161 =item $Obj->do_request()
162  
163 This method is the primary method that a client should use to perform message
164 calls out to an ACME server. It performs some error handling, but passes all
165 major processing off to a derived class's _process_request function.
166  
167 Output
168  
169 \%hash_ref = {
170 successful => boolean value that expresses if a step performed successfully,
171 finished => boolean value that expresses if all steps have been performed,
172 error => boolean value that expresses a FATAL error has occurred,
173 ...
174 (other values depending on step)
175 }
176  
177 =cut
178  
179 sub do_request {
180 2 ▶ my ($self) = @_;
Subroutine do_request: called CC=14 SCAR=27.6
Pod documented
181 2 $self->_prep_step();
182 2 my %payload = %{$self->_payload};
183 2 ▶ confess 'nonce was not set, but is required.'
Branch: 1 branch
BranchTrueFalseTotal
if $self->_need_nonce and not $self->nonce 0 2 2
Condition66%2/3
$self->_need_nonce and not $self->nonce
!ll&&!rl&&r
110
MC/DC0%0/2
$self->_need_nonce and not $self->nonce
$self->_need_nonce not $self->nonce
Truth table66%2/3
A and not(B)
A= $self->_need_nonceB= not $self->nonce
ABresult
0X0
100
111
184 if $self->_need_nonce && !$self->nonce;
185  
186 2 my $Request = $self->_Request;
187  
188 2 my $JWS;
189  
190 # Technically speaking the RFC for JWA allows for a 'none' algorithm.
191 # But Let's Encrypt doesn't make use of it, and so we aren't going to
192 # try to support that.
193 2 ▶ if ($self->_need_jwk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_need_jwk) 2 0 2
194 2 $JWS = WWW::LetsEncrypt::JWS->new({
195 jwk => $self->JWK(),
196 payload => \%payload,
197 protected_headers => {
198 nonce => $self->nonce,
199 },
200 });
201 2 ▶ $Request->content($JWS->serialize()) if $Request->method eq 'POST';
Branch: 1 branch
BranchTrueFalseTotal
if $Request->method eq "POST" 1 1 2
202 }
203  
204 2 my $Response;
205 # Attempt to retry the connection at most 3 times if a client-warning
206 # is being thrown. This is typically due to either: resolution failure
207 # or connection to the host timed out.
208 2 for my $iteration (1..3) {
209 2 $Response = $self->WebAgent->request($Request);
210 2 ▶ last if !$Response->header('client-warning');
Branch: 1 branch
BranchTrueFalseTotal
unless $Response->header("client-warning") 2 0 2
211 }
212  
213 # Grab and store the nonce from the message.
214 2 my $got_nonce = $self->_get_nonce($Response);
215 2 ▶ confess 'Unable to get a new nonce. This is problematic.' if !$got_nonce;
Branch: 1 branch
BranchTrueFalseTotal
unless $got_nonce 1 1 2
216  
217 # Reset the retry_time after each request
218 1 $self->retry_time(0);
219  
220 1 my $rate_limited = 0;
221  
222 1 ▶ if (my $retry_after = $Response->header('Retry-After')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $retry_after = $Response->header("Retry-After")) 0 1 1
223 0 $self->retry_time($retry_after);
224 }
225 1 ▶ $rate_limited = 1 if $Response->code == RC_TOO_MANY_REQUESTS;
Branch: 1 branch
BranchTrueFalseTotal
if $Response->code == 429 1 0 1
226  
227 # TODO: Error processing.
228  
229 # Hand over the derived classes for content processing
230 1 my $processed_response;
231 1 ▶ $processed_response = $self->_process_response($Response) if !$rate_limited;
Branch: 1 branch
BranchTrueFalseTotal
unless $rate_limited 0 1 1
232  
233 1 ▶ if ($processed_response->{error} || $rate_limited) {
Branch: 1 branch
BranchTrueFalseTotal
if ($processed_response->{'error'} or $rate_limited) { } 1 0 1
Condition33%1/3
$processed_response->{"error"} or $rate_limited
l!l&&r!l&&!r
010
MC/DC0%0/2
$processed_response->{"error"} or $rate_limited
$processed_response->{"error"} $rate_limited
Truth table33%1/3
A or B
A= $processed_response->{"error"}B= $rate_limited
ABresult
1X1
011
000
234 1 my $err_ref;
235 try {
236 1 ▶ $err_ref = decode_json($Response->content());
Subroutine __ANON__: called CC=1 SCAR=0.0
237 } catch {
238 1 ▶ $err_ref->{message} = $Response->content();
Subroutine __ANON__: called CC=1 SCAR=0.0
239 1 $err_ref->{failed_parsing} = 1;
240 1 $err_ref->{error} = 1;
241 1 };
242 1 $err_ref->{rc} = $Response->code;
243 1 $err_ref->{errmsg} = $Response->message;
244 1 ▶ $err_ref->{rate_limited} = 1 if $rate_limited;
Branch: 1 branch
BranchTrueFalseTotal
if $rate_limited 1 0 1
245 1 $err_ref->{successful} = 0;
246 1 $err_ref->{finished} = 1;
247 1 return $err_ref;
248 } else {
249 0 return $processed_response;
250 }
251 }
252  
253 =back
254  
255 =cut
256  
257 # $Obj->_get_nonce($Response)
258 #
259 #Internal object function that grabs the nonce value from the header and tries to
260 #update the nonce attribute.
261 #
262 #Input
263 # $Response - HTTP::Response object reference
264 #
265 #Output
266 # $scalar boolean that denotes if the action was successful
267  
268 sub _get_nonce {
269 4 ▶ my ($self, $Response) = @_;
Subroutine _get_nonce: called CC=3 SCAR=11.0
270 4 ▶ my $header_nonce = $Response->header('replay_nonce') || '';
Condition100%2/2
$Response->header('replay_nonce') || ''
l!l
22
MC/DC100%1/1
$Response->header('replay_nonce') || ''
$Response->header('replay_nonce')
Truth table100%2/2
A
A= $Response->header('replay_nonce')
Aresult
11
00
271 # Presumably all valid responses will contain a nonce that we will use for the next message out.
272 4 $self->nonce($header_nonce);
273 4 ▶ return ($header_nonce and $header_nonce eq $self->nonce);
Condition66%2/3
$header_nonce && $header_nonce eq $self->nonce
!ll&&!rl&&r
202
MC/DC50%1/2
$header_nonce && $header_nonce eq $self->nonce
$header_nonce $header_nonce eq $self->nonce
Truth table66%2/3
A && B
A= $header_nonceB= $header_nonce eq $self->nonce
ABresult
0X0
100
111
274 }
275  
276 # $Obj->_prep_step()
277 #
278 #Internal object function that must be implemented by a derived class. This
279 #function is called during do_request on the Message object, and it should
280 #cause the derive class to prepare for handling the next (or first) step.
281 #
282 #
283 #Output
284 # discarded. If failure occurs, it should be fatal.
285  
286 sub _prep_step {
287 0 ▶ confess 'Not Implemented';
Subroutine _prep_step: not called CC=1 SCAR=6.9
288 }
289  
290 # $Obj->_process_response($Response)
291 #
292 #Internal object function that must be implemented by the derived class. This
293 #function should implement any processing necessary for the derived Message
294 #class or call other methods to handle the processing.
295 #
296 ##Input
297 # $Response - HTTP::Response object reference
298 #
299 #Output
300 # \%hash_ref = {
301 # successful => boolean value that expresses if a step performed successfully,
302 # finished => boolean value that expresses if all steps have been performed,
303 # error => boolean value that expresses a FATAL error has occurred,
304 # ...
305 # (other values depending on step)
306 # }
307  
308 sub _process_response {
309 0 ▶ confess 'Not Implemented.';
Subroutine _process_response: not called CC=1 SCAR=6.9
310 }
311  
312 # $Obj->_need_nonce()
313 #
314 #Internal function that should be used to specify if the do_request call will
315 #require a nonce. Implementing classes should override this function if they do
316 #not require a nonce. Currently, presenting a nonce when none is needed does
317 #not cause an error.
318 #
319 #Output
320 # $scalar boolean if a nonce is required (defaults to true)
321  
322 sub _need_nonce {
323 # By default, most messages require a nonce.
324 1 ▶ return 1;
Subroutine _need_nonce: called CC=1 SCAR=0.0
325 }
326  
327 # $Obj->_need_jwk
328 #
329 # Internal object method used for checking if the JWK will be needed.
330 # Almost all messages need access to the JWK due to signing requests, however,
331 # there may be exceptions to this rule that must be accounted for.
332 #
333 # Output
334 # $scalar boolean if a JWK object is requred (defaults to true)
335  
336 sub _need_jwk {
337 2 ▶ return 1;
Subroutine _need_jwk: called CC=1 SCAR=0.0
338 }
339  
340 8 __PACKAGE__->meta->make_immutable;
« LetsEncrypt/Message/Registration.pm ↑ Summary LetsEncrypt/Message/Certificate.pm »