File Coverage

blib/lib/Mojo/ACME/ChallengeServer.pm
Criterion Covered Total %
statement 15 34 44.1
branch 0 8 0.0
condition n/a
subroutine 5 8 62.5
pod 0 1 0.0
total 20 51 39.2


line stmt bran cond sub pod time code
1             package Mojo::ACME::ChallengeServer;
2              
3 1     1   7 use Mojo::Base -base;
  1         2  
  1         7  
4              
5 1     1   173 use Mojo::Server::Daemon;
  1         2  
  1         10  
6 1     1   30 use Mojo::Util qw/hmac_sha1_sum secure_compare/;
  1         2  
  1         68  
7 1     1   6 use Mojolicious;
  1         2  
  1         7  
8 1     1   37 use Scalar::Util;
  1         2  
  1         460  
9              
10             has acme => sub { die 'Mojo::ACME instance is required' };
11             has server => sub { shift->_start };
12              
13 0     0 0   sub start { shift->tap('server') }
14              
15             sub _start {
16 0     0     my $self = shift;
17 0           my $secret = $self->acme->secret;
18 0           my $app = Mojolicious->new(
19             secrets => [$secret],
20             );
21 0           $app->log->unsubscribe('message');
22 0           my $server = Mojo::Server::Daemon->new(
23             app => $app,
24             listen => [$self->acme->server_url],
25             silent => 1,
26             );
27 0           Scalar::Util::weaken $self;
28             $app->routes->get('/:token' => sub {
29 0 0   0     return unless $self;
30 0           my $c = shift;
31 0           my $token = $c->stash('token');
32 0           my $hmac = $c->req->headers->header('X-HMAC');
33              
34             return $c->reply->not_found
35 0 0         unless my $challenge = delete $self->acme->challenges->{$token};
36              
37             return $c->rendered(410)
38 0 0         unless $challenge->{status} eq 'pending';
39              
40 0 0         return $c->render(text => 'Unauthorized', status => 401)
41             unless secure_compare $hmac, hmac_sha1_sum($token, $secret);
42              
43 0           my $auth = $self->acme->keyauth($token);
44 0           $c->res->headers->header('X-HMAC' => hmac_sha1_sum($auth, $secret));
45 0           $c->render(text => $auth);
46 0           });
47 0           return $server->start;
48             }
49              
50             1;
51