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