LetsEncrypt/Message/Registration.pm

filter: Statementstmt 70.6% 53 / 75 Branchbran 45.0% 9 / 20 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 10 / 12 Podpod 100% 1 / 1 totaltotal 64.6% 73 / 113 CC 12 SCAR 20.3
CC
12
Cov
63%
_process_response
13.8
_prep_update_step
12.0
_prep_step
2.2
CRAP 7.6
↑ Summary LetsEncrypt/Message.pm »
1 package WWW::LetsEncrypt::Message::Registration;
2 1 $WWW::LetsEncrypt::Message::Registration::VERSION = '0.001';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Status qw(RC_CREATED RC_ACCEPTED RC_FORBIDDEN RC_CONFLICT);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 extends 'WWW::LetsEncrypt::Message';
12  
13 =pod
14  
15 =head1 NAME
16  
17 WWW::LetsEncrypt::Message::Registration - ACME registration message
18  
19 =head1 SYNOPSIS
20  
21 use WWW::LetsEncrypt::Message::Registration;
22 my $JWK = ...;
23  
24 # Step 1 Register
25 my $RegMsg = WWW::LetsEncrypt::Message::Registration->new({
26 JWK => $JWK,
27 contact => ['mailto:noreply@example.tld', ...],
28 nonce => 'NONCE_VALUE',
29 });
30  
31 my $result = $RegMsg->do_request();
32  
33 #handle $result ...
34  
35 # Step 2 Agreement
36  
37 $RegMsg->agreement('URL_TO_CURRENT_AGREEMENT');
38 $result = $RegMsg->do_request();
39  
40 #handle result...
41  
42 =head1 DESCRIPTION
43  
44 This class implements the two-step process for registering a Let's Encrypt
45 account.
46  
47 =head2 Attributes
48  
49 =over 4
50  
51 =item contact
52  
53 an array reference that contains a list of valid contact information
54 strings.
55  
56 =item agreement
57  
58 a scalar string that holds the URI to the agreement, a truthy value
59 here denotes acceptance of said agreement!
60  
61 =item id
62  
63 a scalar integer that represents the ACME account ID, necessary when
64 updating any account detail. This value will be automatically set after a
65 registration occurs.
66  
67 =back
68  
69 =cut
70  
71 1 has 'contact' => (
72 is => 'rw',
73 isa => 'ArrayRef[Str]',
74 required => 1,
75 );
76  
77 1 has 'agreement' => (
78 is => 'rw',
79 isa => 'Str',
80 );
81  
82 1 has 'id' => (
83 is => 'rw',
84 isa => 'Int',
85 );
86  
87 =head2 Public Functions
88  
89 =over 4
90  
91 =item $Obj->update_only()
92  
93 Object function that marks the Registration message as being only used for
94 updates to the ACME account.
95  
96 Output
97  
98 nothing
99  
100 =cut
101  
102 sub update_only {
103 0 ▶ my ($self) = @_;
Subroutine update_only: not called CC=1 SCAR=6.9
Pod documented
104 0 $self->_prep_update_step;
105 0 return;
106 }
107  
108 =back
109  
110 =cut
111  
112 sub _process_response {
113 3 ▶ my ($self, $Response) = @_;
Subroutine _process_response: called CC=9 SCAR=26.2
114 3 ▶ confess 'Response was not passed!' if !$Response;
Branch: 1 branch
BranchTrueFalseTotal
unless $Response 0 3 3
115  
116 # System's reponse to new-reg or reg is the same, 201 CREATED,
117 # and we will follow the same processing path for both (mostly).
118  
119 3 my $status_code = $Response->code();
120 3 ▶ if ($status_code == RC_CREATED) {
Branch: 4 branches
BranchTrueFalseTotal
if ($status_code == 201) { } 1 2 3
elsif ($status_code == 202) { } 0 2 2
elsif ($status_code == 403) { } 0 2 2
elsif ($status_code == 409) { } 1 1 2
121 1 ▶ $self->_step('update') if $self->_step() eq 'new-reg';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_step eq "new-reg" 1 0 1
122 1 my $server_response = decode_json($Response->content);
123 1 my $resp_ref = {%$server_response};
124 1 $resp_ref->{successful} = 1;
125  
126 1 return $resp_ref;
127 } elsif ($status_code == RC_ACCEPTED) {
128 return {
129 0 successful => 1,
130 finished => 1,
131 };
132 } elsif ($status_code == RC_FORBIDDEN) {
133 0 my $err_ref = decode_json($Response->content);
134 0 ▶ if ($err_ref->{type} =~ m/unauthorized$/
Branch: 1 branch
BranchTrueFalseTotal
if ($err_ref->{'type'} =~ /unauthorized$/ and $err_ref->{'detail'} =~ /^No registration/) { } 0 0 0
Condition0%0/3
$err_ref->{"type"} =~ /unauthorized$/ and $err_ref->{"detail"} =~ /^No registration/
!ll&&!rl&&r
000
MC/DC0%0/2
$err_ref->{"type"} =~ /unauthorized$/ and $err_ref->{"detail"} =~ /^No registration/
$err_ref->{"type"} =~ /unauthorized$/ $err_ref->{"detail"} =~ /^No registration/
Truth table0%0/3
A and B
A= $err_ref->{"type"} =~ /unauthorized$/B= $err_ref->{"detail"} =~ /^No registration/
ABresult
0X0
100
111
135 && $err_ref->{detail} =~ m/^No registration/) {
136 return {
137 0 successful => 0,
138 finished => 1,
139 not_reg => 1,
140 };
141 } else {
142 0 return {error => 1};
143 }
144 } elsif ($status_code == RC_CONFLICT) {
145 # We appear to have tried to re-register this account_key pair, woops.
146 1 my $server_response = decode_json($Response->content);
147 1 my $resp_ref = {%$server_response};
148 1 $resp_ref->{successful} = 0;
149 1 $resp_ref->{already_registered} = 1,
150 return $resp_ref;
151 } else {
152 1 return {error => 1};
153 }
154 }
155  
156 sub _prep_step {
157 2 ▶ my ($self) = @_;
Subroutine _prep_step: called CC=2 SCAR=7.9
158 2 my $step = $self->_step;
159 2 ▶ if ($step) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step) 0 2 2
160 0 my $step_function = "_prep_${step}_step";
161 0 return $self->$step_function;
162 }
163 # By default, we assume that if a step is not specified, that we are updating.
164 2 return $self->_prep_new_reg_step();
165 }
166  
167 # $Obj->_prep_new_reg_step()
168 #
169 #Internal object function that prepares the Registration message for new
170 #registration.
171 #
172 #Output
173 # $scalar boolean if preparing was successful.
174  
175 sub _prep_new_reg_step {
176 2 ▶ my ($self) = @_;
Subroutine _prep_new_reg_step: called CC=1 SCAR=0.0
177 2 $self->_step('new-reg');
178  
179 # Setup the HTTP Request
180 2 my $uri = $self->acme_base_url() . '/acme/new-reg';
181 2 my $Request = HTTP::Request->new(POST => $uri);
182 2 $self->_Request($Request);
183  
184 # Setup payload
185 2 my $payload = $self->_standard_payload();
186 2 $payload->{resource} = 'new-reg';
187  
188 2 $self->_payload($payload);
189  
190 2 return 1;
191 }
192  
193 # $Obj->_prep_update_step()
194 #
195 #Internal object function that prepares the Registration message for updating
196 #an ACME account.
197 #
198 #Output
199 # $scalar boolean if preparing was successful.
200  
201 sub _prep_update_step {
202 0 ▶ my ($self) = @_;
Subroutine _prep_update_step: not called CC=3 SCAR=24.8
203 0 ▶ confess 'id field required if updating registration' if !$self->id;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->id 0 0 0
204 0 $self->_step('update');
205  
206 # Setup the HTTP Request
207 0 my $id = $self->id;
208 0 my $uri = $self->acme_base_url() . "/acme/reg/$id";
209 0 my $Request = HTTP::Request->new(POST => $uri);
210 0 $self->_Request($Request);
211  
212 # Setup payload
213 0 my $payload = $self->_standard_payload();
214 0 $payload->{resource} = 'reg';
215 0 ▶ $payload->{agreement} = $self->agreement if $self->agreement;
Branch: 1 branch
BranchTrueFalseTotal
if $self->agreement 0 0 0
216 0 $self->_payload($payload);
217  
218 0 return 1;
219 }
220  
221 sub _standard_payload {
222 2 ▶ my ($self) = @_;
Subroutine _standard_payload: called CC=1 SCAR=0.0
223 return {
224 2 contact => $self->contact,
225 key => $self->JWK->serialize_public_key(),
226 };
227 }
228  
229 1 __PACKAGE__->meta->make_immutable;
↑ Summary LetsEncrypt/Message.pm »