]> git.imager.perl.org - bse.git/blob - site/cgi-bin/modules/BSE/SessionSign.pm
the metadata fetcher
[bse.git] / site / cgi-bin / modules / BSE / SessionSign.pm
1 package BSE::SessionSign;
2 use strict;
3 use BSE::Cfg;
4
5 our $VERSION = "1.000";
6
7 sub _sign {
8   my ($self, $sessionid, $when) = @_;
9
10   require Digest::SHA;
11
12   my $secret = BSE::Cfg->single->entryErr("site", "secret");
13   my $sha = Digest::SHA::sha256_base64($secret, $sessionid, $when);
14
15   return $when . "." . $sha;
16 }
17
18 sub make {
19   my ($self, $sessionid) = @_;
20
21   my $now = time;
22
23   return $self->_sign($sessionid, $now);
24 }
25
26 sub check {
27   my ($self, $sessionid, $sig, $error) = @_;
28
29   my $now = time;
30   my ($then, $sha) = split /\./, $sig, 2;
31
32   my $good_sig = $self->_sign($sessionid, $then);
33
34   if ($good_sig ne $sig) {
35     require BSE::TB::AuditLog;
36     BSE::TB::AuditLog->log
37         (
38          component => "user::setcookie",
39          level => "warning",
40          actor => "S",
41          msg => "Bad signature setting session cookie",
42          dump => <<DUMP,
43 Received:
44 sessionid: $sessionid
45 sig: $sig
46 DUMP
47         );
48     $$error = "BADSIG";
49     return;
50   }
51
52   require BSE::TB::AuditLog;
53   unless ($then + 30 > $now) {
54     require BSE::TB::AuditLog;
55     BSE::TB::AuditLog->log
56         (
57          component => "user::setcookie",
58          level => "warning",
59          actor => "S",
60          msg => "Too old setting session cookie",
61          dump => <<DUMP,
62 Received:
63 then: $then
64 now: $now
65 DUMP
66         );
67     $$error = "OLDSIG";
68     return 0;
69   }
70
71   return 1;
72 }
73
74 1;