4 use Apache2
::Authen
::Passphrase qw
/pwcheck/;
5 use Apache2
::AuthzCaps qw
/hascaps/;
7 use Plack
::App
::Gruntmaster
;
10 use Digest
::SHA qw
/sha256/;
13 use constant ACCESSLOG_FORMAT
=> '%{X-Forwarded-For}i|%h %u "%r" %>s %b "%{Referer}i" "%{User-agent}i"';
14 use constant CONTENT_SECURITY_POLICY
=> q
,default-src
'none'; script
-src
'self' www
.google
-analytics
.com
; style
-src
'self'; img
-src
'self' www
.google
-analytics
.com
; connect-src
'self',;
16 $Apache2::AuthzCaps
::rootdir
= $Apache2::Authen
::Passphrase
::rootdir
;
17 my $word = qr
,(\w
+),a
;
20 local $Log::Log4perl
::caller_depth
= $Log::Log4perl
::caller_depth
+ 1;
21 $_[0]->{'psgix.logger'}->({qw
/level debug message/ => $_[1]})
24 sub some_auth_required
{
25 my $r = Plack
::Request
->new($_[0]);
26 return 1 if $_[0]->{'gruntmaster.reqadmin'} || $r->path eq '/action/passwd' || $r->path =~ m
,/pb/$word/submit
$,;
27 return 1 if $r->path =~ m
,^/ct/$word/pb/$word, && time < contest_end
$1;
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;
43 local *__ANON__
= "require_admin_middleware";
45 my $r = Plack
::Request
->new($env);
46 $env->{'gruntmaster.reqadmin'} = 1 if admin_required
$r->path;
54 my ($user, $pass, $env) = @_;
55 my $cache_key = sha256
"$user:$pass";
56 my $time = $authen_cache{$cache_key} // 0;
57 if ($time >= time - 300) {
60 delete $authen_cache{$cache_key};
67 $authen_cache{$cache_key} = time;
69 return if $env->{'gruntmaster.reqadmin'} && !hascaps
$user, 'gmadm';
73 Log
::Log4perl
->init('log.conf');
74 my $access_logger = Log
::Log4perl
->get_logger('access');
77 enable_if
{ $_[0]->{PATH_INFO
} eq '/ok' } sub { sub{ [200, [], []] }};
78 enable
'AccessLog', format
=> ACCESSLOG_FORMAT
, logger
=> sub { $access_logger->info(@_) };
79 enable
'ContentLength';
80 enable Header
=> set
=> ['Content-Security-Policy', CONTENT_SECURITY_POLICY
];
81 enable_if
{ $_[0]->{PATH_INFO
} =~ qr
,^/static/,} Header
=> set
=> ['Cache-Control', 'public, max-age=604800'];
82 enable
'Static', path
=> qr
,^/static/,;
83 enable
'Log4perl', category
=> 'plack';
84 enable \
&require_admin
;
85 enable_if \
&some_auth_required
, 'Auth::Basic', authenticator
=> \
&authenticate
, realm
=> 'Gruntmaster 6000';
86 Plack
::App
::Gruntmaster
->to_app