]>
Commit | Line | Data |
---|---|---|
7dc32473 MG |
1 | #!/usr/bin/perl -w |
2 | use v5.14; | |
3 | ||
c85bf4a6 | 4 | use Apache2::Authen::Passphrase qw/pwcheck/; |
5a879d9a MG |
5 | use Apache2::AuthzCaps qw/hascaps/; |
6 | use Gruntmaster::Data; | |
7dc32473 | 7 | use Plack::App::Gruntmaster; |
c85bf4a6 MG |
8 | use Plack::Builder; |
9 | use Plack::Request; | |
37bc8c44 | 10 | use Digest::SHA qw/sha256/; |
68d5c3ff MG |
11 | use Log::Log4perl; |
12 | ||
13 | use constant ACCESSLOG_FORMAT => '%{X-Forwarded-For}i|%h %u "%r" %>s %b "%{Referer}i" "%{User-agent}i"'; | |
1d2cbe7d | 14 | use constant CONTENT_SECURITY_POLICY => q,default-src 'none'; script-src 'self' www.google-analytics.com; style-src 'self'; img-src 'self'; connect-src 'self',; |
c85bf4a6 | 15 | |
5a879d9a MG |
16 | $Apache2::AuthzCaps::rootdir = $Apache2::Authen::Passphrase::rootdir; |
17 | my $word = qr,(\w+),a; | |
18 | ||
19 | sub debug { | |
20 | local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; | |
21 | $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]}) | |
22 | } | |
23 | ||
c85bf4a6 MG |
24 | sub some_auth_required { |
25 | my $r = Plack::Request->new($_[0]); | |
5a879d9a | 26 | return 1 if $_[0]->{'gruntmaster.reqadmin'} || $r->path eq '/action/passwd' || $r->path =~ m,/pb/$word/submit$,; |
acef92e8 | 27 | return 1 if $r->path =~ m,^/ct/$word/pb/$word, && time < contest_end $1; |
5a879d9a MG |
28 | 0 |
29 | } | |
30 | ||
31 | sub admin_required { | |
32 | local $_ = $_[0]; | |
33 | return 1 if m,^/pb/$word, && problem_private $1; | |
34 | return 1 if m,^/log/(?:job|src)/$word, && job_private $1; | |
35 | return 1 if m,^/ct/$word/(?:pb|log), && time < contest_start $1; | |
36 | return 1 if m,^/ct/$word/log/src, && time < contest_end $1; | |
c85bf4a6 MG |
37 | 0 |
38 | } | |
7dc32473 | 39 | |
5a879d9a MG |
40 | sub require_admin { |
41 | my $app = $_[0]; | |
42 | sub { | |
acef92e8 | 43 | local *__ANON__ = "require_admin_middleware"; |
5a879d9a MG |
44 | my $env = $_[0]; |
45 | my $r = Plack::Request->new($env); | |
46 | $env->{'gruntmaster.reqadmin'} = 1 if admin_required $r->path; | |
47 | $app->($env) | |
48 | } | |
49 | } | |
50 | ||
37bc8c44 MG |
51 | my %authen_cache; |
52 | ||
5a879d9a MG |
53 | sub authenticate { |
54 | my ($user, $pass, $env) = @_; | |
37bc8c44 MG |
55 | my $cache_key = sha256 "$user:$pass"; |
56 | my $time = $authen_cache{$cache_key} // 0; | |
57 | if ($time >= time - 300) { | |
58 | return 1; | |
59 | } else { | |
60 | delete $authen_cache{$cache_key}; | |
61 | } | |
62 | ||
5a879d9a MG |
63 | return unless eval { |
64 | pwcheck $user, $pass; | |
65 | 1 | |
66 | }; | |
37bc8c44 | 67 | $authen_cache{$cache_key} = time; |
5a879d9a MG |
68 | |
69 | return if $env->{'gruntmaster.reqadmin'} && !hascaps $user, 'gmadm'; | |
70 | 1 | |
71 | } | |
72 | ||
68d5c3ff MG |
73 | Log::Log4perl->init('log.conf'); |
74 | my $access_logger = Log::Log4perl->get_logger('access'); | |
75 | ||
7dc32473 | 76 | builder { |
819b82bb | 77 | enable_if { $_[0]->{PATH_INFO} eq '/ok' } sub { sub{ [200, [], []] }}; |
68d5c3ff | 78 | enable 'AccessLog', format => ACCESSLOG_FORMAT, logger => sub { $access_logger->info(@_) }; |
287424cb | 79 | enable 'ContentLength'; |
1d2cbe7d | 80 | enable Header => set => ['Content-Security-Policy', CONTENT_SECURITY_POLICY]; |
7e3d8247 MG |
81 | enable_if { $_[0]->{PATH_INFO} =~ qr,^/static/,} Header => set => ['Cache-Control', 'public, max-age=604800']; |
82 | enable 'Static', path => qr,^/static/,; | |
68d5c3ff | 83 | enable 'Log4perl', category => 'plack'; |
5a879d9a MG |
84 | enable \&require_admin; |
85 | enable_if \&some_auth_required, 'Auth::Basic', authenticator => \&authenticate, realm => 'Gruntmaster 6000'; | |
7dc32473 MG |
86 | Plack::App::Gruntmaster->to_app |
87 | } |