File Coverage

blib/lib/Net/ACME/Authorization/Pending.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Net::ACME::Authorization::Pending;
2              
3             =encoding utf-8
4              
5             =head1 NAME
6              
7             Net::ACME::Authorization::Pending - pending ACME authorization
8              
9             =head1 SYNOPSIS
10              
11             use Net::ACME::Authorization::Pending ();
12              
13             my $authz_p = Net::ACME::Authorization::Pending->new(
14             uri => 'http://url/to/poll/for/authz',
15             challenges => \@challenge_objects,
16             combinations => [
17             [ 2 ],
18             [ 1 ],
19             [ 0 ],
20             ],
21             );
22              
23             for my $cmb ( $authz_p->combinations() ) {
24              
25             #An example of only doing “http-01”:
26             next if @$cmb > 1;
27             next if $cmb->[0]->type() ne 'http-01';
28              
29             #Prepare for the challenge …
30              
31             #Assume we instantiated Net::ACME above …
32             $acme->do_challenge($cmb->[0]);
33              
34             while (1) {
35             if ($authz_p->is_time_to_poll()) {
36             my $poll = $authz_p->poll();
37              
38             last if $poll->status() eq 'valid';
39              
40             if ($poll->status() eq 'invalid') {
41             my $completed_challenge = ($poll->challenges())[0];
42             print $completed_challenge->error()->detail() . $/;
43             die "Failed authorization!$/";
44             }
45              
46             }
47              
48             sleep 1;
49             }
50              
51             last;
52             }
53              
54             =cut
55              
56             #NOTE: For now, this assumes a domain name.
57             #Might be useful moving forward to separate out other types
58             #of authz as per ACME spec expansion. (As of April 2016 only
59             #domain names are handled.)
60              
61 2     2   6 use strict;
  2         2  
  2         42  
62 2     2   5 use warnings;
  2         3  
  2         40  
63              
64 2     2   392 use parent qw( Net::ACME::RetryAfter );
  2         196  
  2         15  
65              
66             use Call::Context ();
67              
68             use Net::ACME::Authorization ();
69             use Net::ACME::Challenge ();
70             use Net::ACME::Error ();
71             use Net::ACME::X ();
72              
73             my $PENDING_CLASS = 'Net::ACME::Challenge::Pending';
74              
75             sub new {
76             my ( $class, %opts ) = @_;
77              
78             my $self = {
79             _uri => $opts{'uri'} || die('Need “uri”!'),
80             _challenges => $opts{'challenges'},
81             _combinations => $opts{'combinations'},
82             };
83              
84             if ( !@{ $opts{'challenges'} } ) {
85             die Net::ACME::X::create( 'Empty', { name => 'challenges' } );
86             }
87              
88             for my $c ( 0 .. $#{ $opts{'challenges'} } ) {
89             my $challenge = $opts{'challenges'}[$c];
90              
91             if ( !$challenge->isa($PENDING_CLASS) ) {
92             die "Challenge $c ($challenge) is not an instance of “$PENDING_CLASS”!";
93             }
94             }
95              
96             bless $self, $class;
97              
98             return $self;
99             }
100              
101             sub combinations {
102             my ($self) = @_;
103              
104             Call::Context::must_be_list();
105              
106             return map {
107             [ map { $self->{'_challenges'}[$_] } @$_ ]
108             } @{ $self->{'_combinations'} };
109             }
110              
111             sub _handle_non_202_poll {
112             my ( $self, $resp ) = @_;
113              
114             $resp->die_because_unexpected() if $resp->status() != 200;
115              
116             my $payload = $resp->content_struct();
117              
118             my @challenge_objs;
119              
120             for my $c ( @{ $payload->{'challenges'} } ) {
121              
122             #We only care here about challenges that have been resolved.
123             next if $c->{'status'} eq 'pending';
124              
125             my $err = $c->{'error'};
126             $err &&= Net::ACME::Error->new(%$err);
127              
128             push @challenge_objs, Net::ACME::Challenge->new(
129             status => $c->{'status'},
130             error => $err,
131             );
132             }
133              
134             return Net::ACME::Authorization->new(
135             status => $payload->{'status'},
136             challenges => \@challenge_objs,
137             );
138             }
139              
140             1;