| 1 | package WWW::LetsEncrypt::Message::Authorization; | ||||||||||||||
| 2 | 3 | $WWW::LetsEncrypt::Message::Authorization::VERSION = '0.001'; | |||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 3 | ▶ | use Carp qw(confess); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 3 | ▶ | use Digest::SHA; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 3 | ▶ | use HTTP::Request; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 3 | ▶ | use HTTP::Status qw(RC_CREATED RC_FORBIDDEN); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 3 | ▶ | use JSON; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 3 | ▶ | use MIME::Base64 qw(encode_base64url); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 3 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 3 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 3 | extends qw(WWW::LetsEncrypt::Message); | |||||||||||||
| 17 | |||||||||||||||
| 18 | =pod | ||||||||||||||
| 19 | |||||||||||||||
| 20 | =head1 NAME | ||||||||||||||
| 21 | |||||||||||||||
| 22 | WWW::LetsEncrypt::Message::Authorization | ||||||||||||||
| 23 | |||||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||||
| 25 | |||||||||||||||
| 26 | use WWW::LetsEncrypt::Message::Authorization; | ||||||||||||||
| 27 | |||||||||||||||
| 28 | my $JWK = ...; | ||||||||||||||
| 29 | my $AuthMsg = WWW::LetsEncrypt::Message::Authorization->new({ | ||||||||||||||
| 30 | challenge => 'http-01', # Optional, | ||||||||||||||
| 31 | domain => 'example.tld', # Required, | ||||||||||||||
| 32 | }); | ||||||||||||||
| 33 | |||||||||||||||
| 34 | # Step 1: Request Auth Token | ||||||||||||||
| 35 | my $result = $AuthMsg->do_request(); | ||||||||||||||
| 36 | die 'failed' if !$result->{successful}; | ||||||||||||||
| 37 | my $challenge_token_name = $result->{token}; | ||||||||||||||
| 38 | my $challenge_token_data = $result->{content}; | ||||||||||||||
| 39 | |||||||||||||||
| 40 | # Do a thing to make token/content consumable by Let's Encrypt | ||||||||||||||
| 41 | # (eg: populate .well-known/acme/$challenge_token_name with $challenge_token_data | ||||||||||||||
| 42 | |||||||||||||||
| 43 | # Step 2: Submit Auth Challenge | ||||||||||||||
| 44 | $result = $AuthMsg->do_request() | ||||||||||||||
| 45 | die 'failed' if !$result->{successful}; | ||||||||||||||
| 46 | |||||||||||||||
| 47 | # Step 3...: Polling | ||||||||||||||
| 48 | $result = $AuthMsg->do_request(); | ||||||||||||||
| 49 | ... | ||||||||||||||
| 50 | #sleep maybe? | ||||||||||||||
| 51 | goto 3 or not | ||||||||||||||
| 52 | |||||||||||||||
| 53 | |||||||||||||||
| 54 | =head1 DESCRIPTION | ||||||||||||||
| 55 | |||||||||||||||
| 56 | This class provides an method for authorizing a domain, currently by http-01. | ||||||||||||||
| 57 | |||||||||||||||
| 58 | =head2 Attributes | ||||||||||||||
| 59 | |||||||||||||||
| 60 | (Includes all attributes inherited from WWW::LetsEncrypt::Message) | ||||||||||||||
| 61 | |||||||||||||||
| 62 | =over 4 | ||||||||||||||
| 63 | |||||||||||||||
| 64 | =item challenge | ||||||||||||||
| 65 | |||||||||||||||
| 66 | an attribute that holds a string of the challenge method. | ||||||||||||||
| 67 | Current implemented methods include: | ||||||||||||||
| 68 | |||||||||||||||
| 69 | =over 4 | ||||||||||||||
| 70 | |||||||||||||||
| 71 | =item http-01 (the renamed simpleHttp) | ||||||||||||||
| 72 | |||||||||||||||
| 73 | =item dns-01 | ||||||||||||||
| 74 | |||||||||||||||
| 75 | =back | ||||||||||||||
| 76 | |||||||||||||||
| 77 | =item domain | ||||||||||||||
| 78 | |||||||||||||||
| 79 | a string attribute that holds the domain you are trying to authorize. | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =back | ||||||||||||||
| 82 | |||||||||||||||
| 83 | =cut | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 3 | has 'challenge' => ( | |||||||||||||
| 86 | is => 'rw', | ||||||||||||||
| 87 | isa => enum(['http-01', 'dns-01']), | ||||||||||||||
| 88 | required => 1, | ||||||||||||||
| 89 | default => 'http-01', | ||||||||||||||
| 90 | ); | ||||||||||||||
| 91 | |||||||||||||||
| 92 | 3 | has 'domain' => ( | |||||||||||||
| 93 | is => 'ro', | ||||||||||||||
| 94 | isa => 'Str', | ||||||||||||||
| 95 | required => 1, | ||||||||||||||
| 96 | ); | ||||||||||||||
| 97 | |||||||||||||||
| 98 | # '_key_auth_token' a private string attribute that holds the challenge value. | ||||||||||||||
| 99 | 3 | has '_key_auth_token' => ( | |||||||||||||
| 100 | is => 'rw', | ||||||||||||||
| 101 | isa => 'Str', | ||||||||||||||
| 102 | ); | ||||||||||||||
| 103 | |||||||||||||||
| 104 | # '_token' a private string attribute that holds the name of the challenge value. | ||||||||||||||
| 105 | 3 | has '_token' => ( | |||||||||||||
| 106 | is => 'rw', | ||||||||||||||
| 107 | isa => 'Str', | ||||||||||||||
| 108 | ); | ||||||||||||||
| 109 | |||||||||||||||
| 110 | sub _process_response { | ||||||||||||||
| 111 | 3 | ▶ | my ($self, $Response) = @_; | ||||||||||||
|
Subroutine
_process_response: called CC=1 SCAR=0.0
| |||||||||||||||
| 112 | 3 | my $process_step = "_" . $self->_step . "_step"; | |||||||||||||
| 113 | 3 | return $self->$process_step($Response); | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | # $Obj->_create_key_auth_token() | ||||||||||||||
| 117 | # | ||||||||||||||
| 118 | #Internal object function that generates the challenge response data. | ||||||||||||||
| 119 | # | ||||||||||||||
| 120 | #Output | ||||||||||||||
| 121 | # An array where the first value should be used for provisioning proof, | ||||||||||||||
| 122 | # and the second value should be submitted back via JWT to Boulder. | ||||||||||||||
| 123 | |||||||||||||||
| 124 | sub _create_key_auth_token { | ||||||||||||||
| 125 | 3 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_create_key_auth_token: called CC=2 SCAR=6.9
| |||||||||||||||
| 126 | 3 | my $token = $self->_token; | |||||||||||||
| 127 | 3 | my $challenge = $self->challenge; | |||||||||||||
| 128 | 3 | my $thumbprint = $self->JWK->thumbprint(); | |||||||||||||
| 129 | 3 | my $auth_value = "$token.$thumbprint"; | |||||||||||||
| 130 | |||||||||||||||
| 131 | # Boulder's API looks for the "$token.$thumb" value when we confirm | ||||||||||||||
| 132 | # that it should go look for the proof. However, the proof value | ||||||||||||||
| 133 | # differs between the various challenges so we need to calculate that. | ||||||||||||||
| 134 | 3 | my $proof_value; | |||||||||||||
| 135 | 3 | ▶ | if ($challenge eq 'dns-01') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 136 | 1 | $proof_value = encode_base64url(Digest::SHA::sha256($auth_value)); | |||||||||||||
| 137 | } else { | ||||||||||||||
| 138 | # such is the case with http-01. | ||||||||||||||
| 139 | 2 | $proof_value = $auth_value; | |||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | 3 | return ($proof_value, $auth_value); | |||||||||||||
| 143 | } | ||||||||||||||
| 144 | |||||||||||||||
| 145 | sub _prep_request_step { | ||||||||||||||
| 146 | 2 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_prep_request_step: called CC=1 SCAR=0.0
| |||||||||||||||
| 147 | |||||||||||||||
| 148 | # Setup HTTP Request | ||||||||||||||
| 149 | 2 | my $uri = $self->acme_base_url() . '/acme/new-authz'; | |||||||||||||
| 150 | 2 | my $Request = HTTP::Request->new(POST => $uri); | |||||||||||||
| 151 | 2 | $self->_Request($Request); | |||||||||||||
| 152 | |||||||||||||||
| 153 | # Generate payload | ||||||||||||||
| 154 | 2 | my $payload = { | |||||||||||||
| 155 | resource => 'new-authz', | ||||||||||||||
| 156 | identifier => { | ||||||||||||||
| 157 | type => 'dns', | ||||||||||||||
| 158 | value => $self->domain, | ||||||||||||||
| 159 | }, | ||||||||||||||
| 160 | }; | ||||||||||||||
| 161 | 2 | $self->_payload($payload); | |||||||||||||
| 162 | |||||||||||||||
| 163 | 2 | return 1; | |||||||||||||
| 164 | } | ||||||||||||||
| 165 | |||||||||||||||
| 166 | # $Obj->_request_step($Response) | ||||||||||||||
| 167 | # | ||||||||||||||
| 168 | #Step 1 of the process, returns necessary data that should be pushed to a machine which will be used for authorization. | ||||||||||||||
| 169 | # | ||||||||||||||
| 170 | #Input | ||||||||||||||
| 171 | # $Response - HTTP::Response object referece | ||||||||||||||
| 172 | # | ||||||||||||||
| 173 | #Output | ||||||||||||||
| 174 | # \%hash_ref = { | ||||||||||||||
| 175 | # successful => bool if actions was successful, | ||||||||||||||
| 176 | # finished => bool if all steps are completed, | ||||||||||||||
| 177 | # token => name of the challenge response resource | ||||||||||||||
| 178 | # content => data to be returned for the challenge response | ||||||||||||||
| 179 | # no_tos => bool if the Terms of Service have not been agreed to (error) | ||||||||||||||
| 180 | # } | ||||||||||||||
| 181 | |||||||||||||||
| 182 | sub _request_step { | ||||||||||||||
| 183 | 2 | ▶ | my ($self, $Response) = @_; | ||||||||||||
|
Subroutine
_request_step: called CC=7 SCAR=22.5
| |||||||||||||||
| 184 | 2 | my $output_ref; | |||||||||||||
| 185 | 2 | ▶ | if ($Response->code == RC_CREATED) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 186 | 1 | my $response_ref = decode_json($Response->content); | |||||||||||||
| 187 | |||||||||||||||
| 188 | 1 | ▶ | if ($response_ref->{status} eq 'pending') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 189 | 1 | my ($challenge_ref) = grep { $_->{type} eq $self->challenge } @{$response_ref->{challenges}}; | |||||||||||||
| 190 | 1 | ▶ | confess "Challenge '" . $self->challenge ."' could not be selected!" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 191 | if (!$challenge_ref); | ||||||||||||||
| 192 | |||||||||||||||
| 193 | 1 | $self->_url($challenge_ref->{uri}); | |||||||||||||
| 194 | 1 | $self->_token($challenge_ref->{token}); | |||||||||||||
| 195 | |||||||||||||||
| 196 | 1 | my ($proof_val, $submit_val) = $self->_create_key_auth_token(); | |||||||||||||
| 197 | 1 | $self->_key_auth_token($submit_val); | |||||||||||||
| 198 | |||||||||||||||
| 199 | 1 | $output_ref = { | |||||||||||||
| 200 | token => $self->_token, | ||||||||||||||
| 201 | content => $proof_val, | ||||||||||||||
| 202 | successful => 1, | ||||||||||||||
| 203 | finished => 0, | ||||||||||||||
| 204 | }; | ||||||||||||||
| 205 | 1 | $self->_step('submit_challenge'); | |||||||||||||
| 206 | } else { | ||||||||||||||
| 207 | 0 | return {error => 1}; | |||||||||||||
| 208 | } | ||||||||||||||
| 209 | } elsif ($Response->code == RC_FORBIDDEN) { | ||||||||||||||
| 210 | 0 | my $response_ref = decode_json($Response->content); | |||||||||||||
| 211 | 0 | ▶ | if ($response_ref->{type} eq 'urn:acme:error:unauthorized') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 212 | 0 | ▶ | if ($response_ref->{detail} =~ m/^Must agree to/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 213 | return { | ||||||||||||||
| 214 | 0 | finished => 1, | |||||||||||||
| 215 | successful => 0, | ||||||||||||||
| 216 | no_tos => 1, | ||||||||||||||
| 217 | }; | ||||||||||||||
| 218 | } else { | ||||||||||||||
| 219 | 0 | return {error => 1}; | |||||||||||||
| 220 | } | ||||||||||||||
| 221 | } | ||||||||||||||
| 222 | } else { | ||||||||||||||
| 223 | 1 | return {error => 1}; | |||||||||||||
| 224 | } | ||||||||||||||
| 225 | 1 | return $output_ref; | |||||||||||||
| 226 | } | ||||||||||||||
| 227 | |||||||||||||||
| 228 | sub _prep_submit_challenge_step { | ||||||||||||||
| 229 | 2 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_prep_submit_challenge_step: called CC=1 SCAR=0.0
| |||||||||||||||
| 230 | |||||||||||||||
| 231 | # Setup HTTP Request | ||||||||||||||
| 232 | 2 | my $uri = $self->_url; | |||||||||||||
| 233 | 2 | my $Request = HTTP::Request->new(POST => $uri); | |||||||||||||
| 234 | 2 | $self->_Request($Request); | |||||||||||||
| 235 | |||||||||||||||
| 236 | 2 | $self->_payload({ | |||||||||||||
| 237 | resource => 'challenge', | ||||||||||||||
| 238 | KeyAuthorization => $self->_key_auth_token, | ||||||||||||||
| 239 | }); | ||||||||||||||
| 240 | |||||||||||||||
| 241 | 2 | return 1; | |||||||||||||
| 242 | } | ||||||||||||||
| 243 | |||||||||||||||
| 244 | # $Obj->_submit_challenge_step($Response) | ||||||||||||||
| 245 | # | ||||||||||||||
| 246 | #Step 2 in the process of authorization. Note is is possible for early | ||||||||||||||
| 247 | #finishing if the server is able to check after processing the request but | ||||||||||||||
| 248 | #before returning a response. | ||||||||||||||
| 249 | # | ||||||||||||||
| 250 | #Input | ||||||||||||||
| 251 | # $Response - HTTP::Response object reference | ||||||||||||||
| 252 | # | ||||||||||||||
| 253 | #Output | ||||||||||||||
| 254 | # \%hash_ref = { | ||||||||||||||
| 255 | # sucessful => bool if step was successful | ||||||||||||||
| 256 | # finished => false | ||||||||||||||
| 257 | # } | ||||||||||||||
| 258 | |||||||||||||||
| 259 | sub _submit_challenge_step { | ||||||||||||||
| 260 | 2 | ▶ | my ($self, $Response) = @_; | ||||||||||||
|
Subroutine
_submit_challenge_step: called CC=3 SCAR=11.1
| |||||||||||||||
| 261 | |||||||||||||||
| 262 | 2 | my $response_ref = decode_json($Response->content()); | |||||||||||||
| 263 | 2 | ▶ | if ($Response->code == 202) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 264 | 1 | $self->_step('polling'); | |||||||||||||
| 265 | 1 | $self->_url($response_ref->{uri}); | |||||||||||||
| 266 | 1 | $response_ref->{successful} = 1; | |||||||||||||
| 267 | 1 | $response_ref->{finished} = 0; | |||||||||||||
| 268 | 1 | return $response_ref; | |||||||||||||
| 269 | } elsif ($Response->code == 200) { | ||||||||||||||
| 270 | return { | ||||||||||||||
| 271 | 0 | successful => 1, | |||||||||||||
| 272 | finished => 1, | ||||||||||||||
| 273 | } | ||||||||||||||
| 274 | } else { | ||||||||||||||
| 275 | 1 | return {error => 1}; | |||||||||||||
| 276 | } | ||||||||||||||
| 277 | } | ||||||||||||||
| 278 | |||||||||||||||
| 279 | sub _prep_polling_step { | ||||||||||||||
| 280 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_prep_polling_step: not called CC=1 SCAR=6.9
| |||||||||||||||
| 281 | |||||||||||||||
| 282 | 0 | my $Request = HTTP::Request->new(GET => $self->_url); | |||||||||||||
| 283 | 0 | $self->_Request($Request); | |||||||||||||
| 284 | |||||||||||||||
| 285 | 0 | return 1; | |||||||||||||
| 286 | } | ||||||||||||||
| 287 | |||||||||||||||
| 288 | # $Obj->_polling_step($Response) | ||||||||||||||
| 289 | # | ||||||||||||||
| 290 | #Last step, polling to see if the authorization was successful | ||||||||||||||
| 291 | #Input | ||||||||||||||
| 292 | # $Response - HTTP::Response object ref | ||||||||||||||
| 293 | #Output | ||||||||||||||
| 294 | # \%hash_ref = { | ||||||||||||||
| 295 | # successful => bool if authorization has been accepted | ||||||||||||||
| 296 | # finished => bool if all steps have been completed (re-polling will set this false) | ||||||||||||||
| 297 | # retry => bool if polling should be retried (also check retry_time attribute) | ||||||||||||||
| 298 | # fault => \%hashref that contains information about the fault that occurred | ||||||||||||||
| 299 | # } | ||||||||||||||
| 300 | |||||||||||||||
| 301 | sub _polling_step { | ||||||||||||||
| 302 | 0 | ▶ | my ($self, $Response) = @_; | ||||||||||||
|
Subroutine
_polling_step: not called CC=2 SCAR=17.9
| |||||||||||||||
| 303 | 0 | ▶ | if ($Response->code == 202) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 304 | 0 | my $response_ref = decode_json($Response->content()); | |||||||||||||
| 305 | 0 | my $status_func = $self->can("_polling_" . $response_ref->{status}); | |||||||||||||
| 306 | 0 | return $status_func->($self, $response_ref, $Response); | |||||||||||||
| 307 | } | ||||||||||||||
| 308 | 0 | return {error => 1}; | |||||||||||||
| 309 | } | ||||||||||||||
| 310 | |||||||||||||||
| 311 | sub _polling_pending { | ||||||||||||||
| 312 | 0 | ▶ | my ($self, $response_ref, $Response) = @_; | ||||||||||||
|
Subroutine
_polling_pending: not called CC=2 SCAR=17.9
| |||||||||||||||
| 313 | |||||||||||||||
| 314 | # If the retry_after header isn't present, wait 1 second. | ||||||||||||||
| 315 | 0 | ▶ | my $retry_time = $Response->header('retry_after') || 1; | ||||||||||||
|
Condition0%0/2
$Response->header('retry_after') || 1
MC/DC0%0/1
$Response->header('retry_after') || 1
$Response->header('retry_after')
Truth table0%0/2
A
A= $Response->header('retry_after')
| |||||||||||||||
| 316 | 0 | $self->retry_time($retry_time); | |||||||||||||
| 317 | return { | ||||||||||||||
| 318 | 0 | successful => 1, | |||||||||||||
| 319 | finished => 0, | ||||||||||||||
| 320 | retry => 1, | ||||||||||||||
| 321 | }; | ||||||||||||||
| 322 | } | ||||||||||||||
| 323 | |||||||||||||||
| 324 | sub _polling_valid { | ||||||||||||||
| 325 | 0 | ▶ | my ($self, $response_ref) = @_; | ||||||||||||
|
Subroutine
_polling_valid: not called CC=1 SCAR=6.9
| |||||||||||||||
| 326 | 0 | my $expires = $response_ref->{expires}; | |||||||||||||
| 327 | return { | ||||||||||||||
| 328 | successful => 1, | ||||||||||||||
| 329 | finished => 1, | ||||||||||||||
| 330 | uri => $response_ref->{uri}, | ||||||||||||||
| 331 | 0 | expires => $expires, | |||||||||||||
| 332 | } | ||||||||||||||
| 333 | } | ||||||||||||||
| 334 | |||||||||||||||
| 335 | sub _polling_invalid { | ||||||||||||||
| 336 | 0 | ▶ | my ($self, $response_ref) = @_; | ||||||||||||
|
Subroutine
_polling_invalid: not called CC=1 SCAR=6.9
| |||||||||||||||
| 337 | return { | ||||||||||||||
| 338 | successful => 0, | ||||||||||||||
| 339 | finished => 1, | ||||||||||||||
| 340 | fault => $response_ref->{error}, | ||||||||||||||
| 341 | 0 | }; | |||||||||||||
| 342 | } | ||||||||||||||
| 343 | |||||||||||||||
| 344 | sub _prep_step { | ||||||||||||||
| 345 | 4 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_prep_step: called CC=2 SCAR=7.5
| |||||||||||||||
| 346 | 4 | my $step = $self->_step; | |||||||||||||
| 347 | 4 | my $step_function; | |||||||||||||
| 348 | 4 | ▶ | if ($step) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 349 | 4 | $step_function = "_prep_${step}_step"; | |||||||||||||
| 350 | } else { | ||||||||||||||
| 351 | 0 | $step_function = "_prep_request_step"; | |||||||||||||
| 352 | 0 | $self->_step('request'); | |||||||||||||
| 353 | } | ||||||||||||||
| 354 | 4 | return $self->$step_function; | |||||||||||||
| 355 | } | ||||||||||||||
| 356 | |||||||||||||||
| 357 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 358 | |||||||||||||||