Make Gruntmaster::Page::Base also act as strict/warnings/feature/etc
[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"';
1d2cbe7d 14use 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;
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 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
31sub admin_required {
32 local $_ = $_[0];
ce37ae0c
MG
33 return problem_owner $1 if m,^/pb/$word, && problem_private $1;
34 return job_owner $1 if m,^/log/(?:job|src)/$word, && job_private $1;
35 return contest_owner $1 if m,^/ct/$word/(?:pb|log), && time < contest_start $1;
36 if (m,^/ct/$word/log/(?:job|src)/$word, && time < contest_end $1){
37 local $Gruntmaster::Data::contest = $1;
38 return job_owner $2;
39 }
c85bf4a6
MG
40 0
41}
7dc32473 42
5a879d9a
MG
43sub require_admin {
44 my $app = $_[0];
45 sub {
acef92e8 46 local *__ANON__ = "require_admin_middleware";
5a879d9a
MG
47 my $env = $_[0];
48 my $r = Plack::Request->new($env);
ce37ae0c 49 $env->{'gruntmaster.reqadmin'} = admin_required $r->path;
5a879d9a
MG
50 $app->($env)
51 }
52}
53
37bc8c44
MG
54my %authen_cache;
55
5a879d9a
MG
56sub authenticate {
57 my ($user, $pass, $env) = @_;
37bc8c44
MG
58 my $cache_key = sha256 "$user:$pass";
59 my $time = $authen_cache{$cache_key} // 0;
60 if ($time >= time - 300) {
61 return 1;
62 } else {
63 delete $authen_cache{$cache_key};
64 }
65
5a879d9a
MG
66 return unless eval {
67 pwcheck $user, $pass;
68 1
69 };
37bc8c44 70 $authen_cache{$cache_key} = time;
5a879d9a 71
ce37ae0c 72 return if $env->{'gruntmaster.reqadmin'} && $env->{'gruntmaster.reqadmin'} ne $user && !hascaps $user, 'gmadm';
5a879d9a
MG
73 1
74}
75
68d5c3ff
MG
76Log::Log4perl->init('log.conf');
77my $access_logger = Log::Log4perl->get_logger('access');
78
7dc32473 79builder {
819b82bb 80 enable_if { $_[0]->{PATH_INFO} eq '/ok' } sub { sub{ [200, [], []] }};
68d5c3ff 81 enable 'AccessLog', format => ACCESSLOG_FORMAT, logger => sub { $access_logger->info(@_) };
287424cb 82 enable 'ContentLength';
1d2cbe7d 83 enable Header => set => ['Content-Security-Policy', CONTENT_SECURITY_POLICY];
7e3d8247
MG
84 enable_if { $_[0]->{PATH_INFO} =~ qr,^/static/,} Header => set => ['Cache-Control', 'public, max-age=604800'];
85 enable 'Static', path => qr,^/static/,;
68d5c3ff 86 enable 'Log4perl', category => 'plack';
5a879d9a
MG
87 enable \&require_admin;
88 enable_if \&some_auth_required, 'Auth::Basic', authenticator => \&authenticate, realm => 'Gruntmaster 6000';
7dc32473
MG
89 Plack::App::Gruntmaster->to_app
90}
This page took 0.016429 seconds and 4 git commands to generate.