]>
Commit | Line | Data |
---|---|---|
7dc32473 MG |
1 | #!/usr/bin/perl -w |
2 | use v5.14; | |
491e82eb | 3 | no if $] >= 5.017011, warnings => 'experimental::smartmatch'; |
7dc32473 | 4 | |
5a879d9a | 5 | use Gruntmaster::Data; |
7dc32473 | 6 | use Plack::App::Gruntmaster; |
c85bf4a6 MG |
7 | use Plack::Builder; |
8 | use Plack::Request; | |
3b69df7a | 9 | use Plack::Util; |
37bc8c44 | 10 | use Digest::SHA qw/sha256/; |
68d5c3ff | 11 | use Log::Log4perl; |
594d53ba | 12 | use Tie::Hash::Expire; |
68d5c3ff | 13 | |
594d53ba | 14 | use constant AUTH_TIMEOUT => 5 * 60; |
5ab9964f | 15 | use constant ACCESSLOG_FORMAT => 'combined'; |
031ffaf5 MG |
16 | |
17 | sub CONTENT_SECURITY_POLICY () { | |
18 | my $csp = <<CSP; | |
19 | default-src 'none' | |
20 | connect-src 'self' | |
21 | form-action 'self' | |
22 | frame-ancestors 'none' | |
23 | img-src 'self' | |
24 | referrer origin-when-cross-origin | |
25 | script-src 'self' | |
26 | style-src 'self' | |
27 | CSP | |
28 | chomp $csp; | |
29 | $csp =~ s/\n/; /gr; | |
30 | } | |
c85bf4a6 | 31 | |
b5885a0b | 32 | our $db //= Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:'); |
5a879d9a | 33 | |
10c13642 | 34 | tie my %auth, 'Tie::Hash::Expire', {expire_seconds => AUTH_TIMEOUT}; |
37bc8c44 | 35 | |
5a879d9a MG |
36 | sub authenticate { |
37 | my ($user, $pass, $env) = @_; | |
594d53ba MG |
38 | my $key = sha256 "$user:$pass"; |
39 | $env->{'gruntmaster.user'} = $user; | |
40 | return 1 if exists $auth{$key}; | |
41 | return unless $db->user($user) && $db->user($user)->check_passphrase($pass); | |
42 | $auth{key} = 1; | |
5a879d9a MG |
43 | } |
44 | ||
3b69df7a MG |
45 | sub add_headers { |
46 | my $app = $_[0]; | |
47 | sub { | |
48 | my $resp = $app->($_[0]); | |
49 | my $hdrs = Plack::Util::headers($resp->[1]); | |
50 | $hdrs->set('Content-Security-Policy', CONTENT_SECURITY_POLICY); | |
51 | $hdrs->set('Cache-Control', 'public, max-age=604800') if $_[0]->{PATH_INFO} =~ qr,^/static/,; | |
52 | $resp->[1] = $hdrs->headers; | |
53 | $resp; | |
54 | } | |
55 | } | |
56 | ||
28b3c465 | 57 | Log::Log4perl->init_once('log.conf'); |
68d5c3ff | 58 | my $access_logger = Log::Log4perl->get_logger('access'); |
58a0ba09 | 59 | $ENV{DBIC_NULLABLE_KEY_NOWARN} = 1; |
68d5c3ff | 60 | |
7dc32473 | 61 | builder { |
28aeef34 | 62 | enable_if { $_[0]->{PATH_INFO} eq '/ok' } sub { sub{ [200, [], []] }}; |
68d5c3ff | 63 | enable 'AccessLog', format => ACCESSLOG_FORMAT, logger => sub { $access_logger->info(@_) }; |
287424cb | 64 | enable 'ContentLength'; |
3b69df7a | 65 | enable \&add_headers; |
7e3d8247 | 66 | enable 'Static', path => qr,^/static/,; |
68d5c3ff | 67 | enable 'Log4perl', category => 'plack'; |
3b69df7a | 68 | enable_if { shift->{HTTP_AUTHORIZATION} } 'Auth::Basic', authenticator => \&authenticate, realm => 'Gruntmaster 6000'; |
d3200993 | 69 | enable sub { my $app = $_[0]; sub { $_[0]->{'gruntmaster.dbic'} = $db; $app->($_[0]) } }; |
594d53ba | 70 | Plack::App::Gruntmaster->run_if_script |
7dc32473 | 71 | } |