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