| 1 | package Toadfarm::Plugin::Letsencrypt; | ||
| 2 | use Mojo::Base 'Mojolicious::Plugin'; | ||
| 3 | |||
| 4 | our $VERSION = '0.01'; | ||
| 5 | |||
| 6 | use Mojo::Loader 'data_section'; | ||
| 7 | use Mojo::Path; | ||
| 8 | |||
| 9 | use File::Spec::Functions qw(abs2rel catdir catfile splitdir); | ||
| 10 | |||
| 11 | sub register { | ||
| 12 | my ($self, $app, $config) = @_; | ||
| 13 | |||
| 14 | $config->{challenges} ||= '/tmp/letsencrypt/public_html'; | ||
| 15 | $config->{route} ||= '/.well-known/acme-challenge'; | ||
| 16 | |||
| 17 | $app->plugin('HeaderCondition'); | ||
| 18 | $app->routes->get($config->{route}.'/:challenge')->over(agent => qr{letsencrypt})->to(cb => sub { | ||
| 19 | my $c = shift; | ||
| 20 | my $challenge = $c->param('challenge'); | ||
| 21 | $c->app->log->debug("Let's Encrypt Verification: $challenge"); | ||
| 22 | if ( my $challenges = data_section('main', 'challenges') ) { | ||
| 23 | if ( my $found = ((grep { /^$challenge\t/ } split /\n+/, $challenges)[0]) ) { | ||
| 24 | if ( my $response = ((split /\s+/, $found)[1]) ) { | ||
| 25 | return $c->render(text => $response); | ||
| 26 | } | ||
| 27 | } | ||
| 28 | } | ||
| 29 | my $le = catfile $config->{challenges}, $config->{route}, $challenge; | ||
| 30 | if ( -e $le ) { | ||
| 31 | $c->res->headers->content_type('text/plain'); | ||
| 32 | return $c->reply->asset(Mojo::Asset::File->new(path => $le)); | ||
| 33 | } | ||
| 34 | $c->reply->not_found; | ||
| 35 | }); | ||
| 36 | |||
| 37 | }; | ||
| 38 | |||
| 39 | 1; | ||
| 40 | __END__ |