]>
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; | |
10 | ||
5a879d9a MG |
11 | $Apache2::AuthzCaps::rootdir = $Apache2::Authen::Passphrase::rootdir; |
12 | my $word = qr,(\w+),a; | |
13 | ||
14 | sub debug { | |
15 | local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; | |
16 | $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]}) | |
17 | } | |
18 | ||
c85bf4a6 MG |
19 | sub some_auth_required { |
20 | my $r = Plack::Request->new($_[0]); | |
5a879d9a MG |
21 | return 1 if $_[0]->{'gruntmaster.reqadmin'} || $r->path eq '/action/passwd' || $r->path =~ m,/pb/$word/submit$,; |
22 | 0 | |
23 | } | |
24 | ||
25 | sub admin_required { | |
26 | local $_ = $_[0]; | |
27 | return 1 if m,^/pb/$word, && problem_private $1; | |
28 | return 1 if m,^/log/(?:job|src)/$word, && job_private $1; | |
29 | return 1 if m,^/ct/$word/(?:pb|log), && time < contest_start $1; | |
30 | return 1 if m,^/ct/$word/log/src, && time < contest_end $1; | |
c85bf4a6 MG |
31 | 0 |
32 | } | |
7dc32473 | 33 | |
5a879d9a MG |
34 | sub require_admin { |
35 | my $app = $_[0]; | |
36 | sub { | |
37 | *__ANON__ = "require_admin_middleware"; | |
38 | my $env = $_[0]; | |
39 | my $r = Plack::Request->new($env); | |
40 | $env->{'gruntmaster.reqadmin'} = 1 if admin_required $r->path; | |
41 | $app->($env) | |
42 | } | |
43 | } | |
44 | ||
45 | sub authenticate { | |
46 | my ($user, $pass, $env) = @_; | |
47 | return unless eval { | |
48 | pwcheck $user, $pass; | |
49 | 1 | |
50 | }; | |
51 | ||
52 | return if $env->{'gruntmaster.reqadmin'} && !hascaps $user, 'gmadm'; | |
53 | 1 | |
54 | } | |
55 | ||
7dc32473 | 56 | builder { |
287424cb | 57 | enable 'ContentLength'; |
7e3d8247 MG |
58 | enable_if { $_[0]->{PATH_INFO} =~ qr,^/static/,} Header => set => ['Cache-Control', 'public, max-age=604800']; |
59 | enable 'Static', path => qr,^/static/,; | |
7dc32473 | 60 | enable 'Log4perl', category => 'plack', conf => 'log.conf'; |
5a879d9a MG |
61 | enable \&require_admin; |
62 | enable_if \&some_auth_required, 'Auth::Basic', authenticator => \&authenticate, realm => 'Gruntmaster 6000'; | |
7dc32473 MG |
63 | Plack::App::Gruntmaster->to_app |
64 | } |