- if ($self->{challenges}{$_}{status} eq 'valid') {
- next;
- } elsif ($challenge->{status} eq 'valid') {
- $self->{challenges}{$_}{status} = $challenge->{status};
- next;
- } elsif ($challenge->{status} eq 'pending') {
- # Handle check
- if (
- ($challenge->{type} =~ /^http-01$/ and $self->_httpCheck($_, $challenge->{token})) or
- ($challenge->{type} =~ /^dns-01$/ and $self->_dnsCheck($_, $challenge->{token}))
- ) {
- # Post challenge request
- my $res = $self->_post($challenge->{uri}, {resource => 'challenge', keyAuthorization => $challenge->{token}.'.'.$self->{account}{thumbprint}});
-
- # Handle error
- unless ($res->is_success) {
- confess 'POST '.$challenge->{uri}.' failed: '.$res->status_line;
- }
-
- # Extract content
- my $content = from_json($res->content);
-
- # Save if valid
- if ($content->{status} eq 'valid') {
- $self->{challenges}{$_}{status} = $content->{status};
- # Check is still polling
- } elsif ($content->{status} eq 'pending') {
- # Add to poll list for later use
- push(@{$self->{challenges}{$_}{polls}}, {
- type => (split(/-/, $challenge->{type}))[0],
- status => $content->{status},
- poll => $content->{uri}
- });
+ unless($self->{req}{challenges}{$identifier}{status} eq 'valid') {
+ # One challenge validated
+ if ($self->{req}{challenges}{$identifier}{challenges}{$_}{status} eq 'valid') {
+ $self->{req}{challenges}{$identifier}{status} = $self->{req}{challenges}{$identifier}{challenges}{$_}{status};
+ # This challenge is to be validated
+ } elsif ($self->{req}{challenges}{$identifier}{challenges}{$_}{status} eq 'pending') {
+ #TODO: implement tls-alpn-01 challenge someday if possible
+ if (
+ ($_ eq 'http-01' and $self->_httpCheck($identifier, $self->{req}{challenges}{$identifier}{challenges}{$_}{token})) or
+ ($_ eq 'dns-01' and $self->_dnsCheck($identifier, $self->{req}{challenges}{$identifier}{challenges}{$_}{token}))
+ ) {
+ # Init file
+ #XXX: tmpdir.'/'.<orderuri>.'/'.<authuri>
+ my $file = $self->{req}{pending}.'/'.encode_base64url($self->{req}{challenges}{$identifier}{challenges}{$_}{url});
+
+ # Reset content
+ $content = undef;
+
+ # Load auth request content or post a new one
+ #TODO: add more check on cache file ???
+ if (
+ #XXX: use eval to workaround a fatal in from_json
+ ! defined eval {
+ # Check that file exists
+ -f $file &&
+ # Read it
+ ($content = read_file($file)) &&
+ # Decode it
+ ($content = from_json($content))
+ #TODO: Check file modification time ? There is no expires field in json answer
+ }# || (str2time($content->{expires}) <= time()+3600)
+ ) {
+ # Post challenge request
+ my $res = $self->_post(
+ $self->{req}{challenges}{$identifier}{challenges}{$_}{url},
+ {keyAuthorization => $self->{req}{challenges}{$identifier}{challenges}{$_}{token}.'.'.$self->{account}{thumbprint}}
+ );
+
+ # Handle error
+ unless ($res->is_success) {
+ confess('POST '.$self->{req}{challenges}{$identifier}{challenges}{$_}{url}.' failed: '.$res->status_line);
+ }
+
+ # Extract content
+ $content = from_json($res->content);
+
+ # Write to file
+ write_file($file, to_json($content));
+ }
+
+ # Save if valid
+ if ($content->{status} eq 'valid') {
+ $self->{req}{challenges}{$identifier}{status} = $content->{status};
+ # Check is still polling
+ } elsif ($content->{status} eq 'pending') {
+ # Add to poll list for later use
+ $self->{req}{challenges}{$identifier}{polls}{$content->{type}} = 1;
+ }