chiark / gitweb /
wip found before multi obj
[cgi-auth-flexible.git] / cgi-auth-hybrid.pm
index 9536ba726e8dc3507ec76d8b34a349db2addfe17..bf2bdd65bb73f9f3edd82d0e8ffdba00e0824298 100644 (file)
@@ -1,5 +1,21 @@
 # -*- perl -*-
 
+# This is part of CGI::Auth::Hybrid, a perl CGI authentication module.
+# Copyright (C) 2012 Ian Jackson.
+# 
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+# 
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
 BEGIN {
     use Exporter   ();
     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
@@ -13,65 +29,160 @@ BEGIN {
 }
 our @EXPORT_OK;
 
-our %_s = (
-    assocdb_path => 'cah-assocs.db';
-    assocdb_dsn => undef,
-    assocdb_user => '',
-    assocdb_password => '',
-    assocdb_table => 'assocs',
-    random_source => '/dev/urandom',
-    associdlen => 128, # bits
-    );
-
 use DBI;
+use CGI;
 
-our $dbh;
-
-sub setup {
+sub new {
+    my $class = shift;
+    my $s = {
+       S => {
+           assocdb_path => 'cah-assocs.db';
+           assocdb_dsn => undef,
+           assocdb_user => '',
+           assocdb_password => '',
+           assocdb_table => 'assocs',
+           random_source => '/dev/urandom',
+           associdlen => 128, # bits
+           login_timeout => 86400, # seconds
+           param_name => 'cah_associd',
+           cookie_name => 'cah_associd', # make undef to disable cookie
+           cgi => undef,
+           get_param => sub { $s->_c()->param($s->{S}{param_name}) },
+           get_cookie => sub { $s->{S}{cookie_name}
+                               ? $s->_c()->cookie($s->{S}{cookie_name})
+                               : '' },
+           get_method => sub { $s->_c()->request_method() },
+       },
+       D => undef,
+    };
     my ($k,$v);
     while (($k,$v,@_) = @_) {
-       die "unknown setting $k" unless %_s{$k};
-       $_s{$k} = $v;
+       die "unknown setting $k" unless exists $s->{S}{$k};
+       $s->{S}{$k} = $v;
     }
+    bless $s, $class;
+    return $s;
 }
 
-sub _dbopen () {
-    return if $dbh;
+sub _c ($) {
+    my ($s) = @_;
+    return $s->{S}{cgi};
+}
+
+sub _dbopen ($) {
+    my ($s) = @_;
+    my $dbh = $s->{D};
+    return $dbh if $dbh; 
 
-    $_s{assocdb_dsn}="dbi:SQLite:dbname=$s_{assocdb_path}" 
-       if !$_s{assocdb_dsn};
+    $s->{S}{assocdb_dsn} ||= "dbi:SQLite:dbname=$s->{S}{assocdb_path}";
 
     my $u = umask 077;
-    $dbh = DBI->open($_s{assocdb_dsn}, $s_{assocdb_user}, 
-                    $s_{assocdb_password}, { 
+    $dbh = DBI->open($s->{S}{assocdb_dsn}, $s->{S}{assocdb_user}, 
+                    $s->{S}{assocdb_password}, { 
                         AutoCommit => 0, RaiseError => 1,
                     });
     die "${assocdb_dsn} $! ?" unless $dbh;
+    $s->{D} = $dbh;
 
     $dbh->do("BEGIN");
 
     eval {
-       $dbh->do("CREATE TABLE $_s{assocdb_table} (".
+       $dbh->do("CREATE TABLE $s->{S}{assocdb_table} (".
                 " associd VARCHAR PRIMARY KEY,".
-                 " username VARCHAR".
+                 " username VARCHAR,".
+                " last INTEGER"
                 ")");
     };
+    return $dbh;
 }
 
-sub record_login ($) {
-    my ($nusername) = @_;
-    my $rs = new IO::File $_s{random_source}, '<'
-       or die "$_s{random_source} $!";
-    my $bytes = ($_s{associdlen} + 7) >> 3;
+sub record_login ($$) {
+    my ($s,$nusername) = @_;
+    my $rsp = $s->{S}{random_source};
+    my $rsf = new IO::File $rsp, '<' or die "$rsp $!";
+    my $bytes = ($s->{S}{associdlen} + 7) >> 3;
     my $nassocbin;
     $!=0;
-    read($rs,$nassocbin,$bytes) == $bytes or die "$s_{random_source} $!";
+    read($rsf,$nassocbin,$bytes) == $bytes or die "$rsp $!";
+    close $rsf;
     my $nassoc = unpack "H*", $nassocbin;
-    _dbopen();
-    $dbh->do("INSERT INTO $_s{assocdb_table}".
-            " (associd, username) VALUES (?,?)", {},
-            $nassoc, $nusername);
+    my $dbh = $s->_dbopen();
+    $dbh->do("INSERT INTO $s->{S}{assocdb_table}".
+            " (associd, username, last) VALUES (?,?,?)", {},
+            $nassoc, $nusername, time);
+    $dbh->do("COMMIT");
+    $s->{U} = $nusername;
+    $s->{A} = $nassoc;
+}
+
+sub _check ($) {
+    my ($s) = @_;
+    my $qassoc = $s->{S}{param_get}();
+    if (!defined $qassoc) {
+       $qassoc = $s->{S}{cookie_get}();
+       return undef unless defined $qassoc;
+       return undef unless $s->{S}{get_method}() eq 'GET';
+    }
+    my $dbh = $s->_dbopen();
+    my ($nusername, $nlast) =
+       $dbh->selectrow_array("SELECT username, last".
+                             " FROM $s->{S}{assocdb_table}".
+                             " WHERE associd = ?", {}, $qassoc);
+    return undef unless defined $nusername;
+    my $timeout = $s->{S}{login_timeout};
+    return undef unless !defined $timeout || time <= $nlast + $timeout;
+
+    return ($nusername, $qassoc);
+}
+
+sub check ($) {
+    my ($s) = @_;
+
+    my ($nusername, $qassoc) = $s->_check() or return undef;
+
+    # hooray
+    $dbh->do("UPDATE $s->{S}{assocdb_table}".
+            " SET last = ?".
+            " WHERE associd = ?", {}, time, $qassoc);
     $dbh->do("COMMIT");
-    $username = $nusername;
-    $assoc =    $nassoc;
+
+    $s->{U} = $nusername;
+    $s->{A} = $qassoc;
+    return $username;
+}
+
+sub logout ($) {
+    my ($s) = @_;
+
+    if (my ($nusername, $qassoc) = $s->_check()) {
+       $dbh->do("DELETE FROM $s->{S}{assocdb_table}".
+                " WHERE associd = ?", {}, $qassoc);
+       $dbh->do("COMMIT");
+    }
+}
+
+sub username ($) {
+    my ($s) = @_;
+    return $s->{U};
+
+sub hiddenv ($) {
+    my ($s) = @_;
+    return defined $s->{A} ? $s->{A} : '';
+}
+
+sub hiddena ($) {
+    my ($s) = @_;
+    return (-name => $s->{param_name},
+           -default => $s->hiddenv());
 }
+
+sub hiddenh ($) {
+    my ($s) = @_;
+    return hidden($s->hiddena());
+}
+
+sub cookieac ($) {
+    my ($s) = @_;
+    return (-name => $s->{cookie_name},
+           -value => hiddenv());
+