5 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
10 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
12 @EXPORT_OK = qw(setup);
22 assocdb_path => 'cah-assocs.db';
25 assocdb_password => '',
26 assocdb_table => 'assocs',
27 random_source => '/dev/urandom',
28 associdlen => 128, # bits
29 login_timeout => 86400, # seconds
30 param_name => 'cah_associd',
31 cookie_name => 'cah_associd', # make undef to disable cookie
33 get_param => sub { $s->_c()->param($s->{S}{param_name}) },
34 get_cookie => sub { $s->{S}{cookie_name}
35 ? $s->_c()->cookie($s->{S}{cookie_name})
37 get_method => sub { $s->_c()->request_method() },
42 while (($k,$v,@_) = @_) {
43 die "unknown setting $k" unless exists $s->{S}{$k};
60 $s->{S}{assocdb_dsn} ||= "dbi:SQLite:dbname=$s->{S}{assocdb_path}";
63 $dbh = DBI->open($s->{S}{assocdb_dsn}, $s->{S}{assocdb_user},
64 $s->{S}{assocdb_password}, {
65 AutoCommit => 0, RaiseError => 1,
67 die "${assocdb_dsn} $! ?" unless $dbh;
73 $dbh->do("CREATE TABLE $s->{S}{assocdb_table} (".
74 " associd VARCHAR PRIMARY KEY,".
82 sub record_login ($$) {
83 my ($s,$nusername) = @_;
84 my $rsp = $s->{S}{random_source};
85 my $rsf = new IO::File $rsp, '<' or die "$rsp $!";
86 my $bytes = ($s->{S}{associdlen} + 7) >> 3;
89 read($rsf,$nassocbin,$bytes) == $bytes or die "$rsp $!";
91 my $nassoc = unpack "H*", $nassocbin;
92 my $dbh = $s->_dbopen();
93 $dbh->do("INSERT INTO $s->{S}{assocdb_table}".
94 " (associd, username, last) VALUES (?,?,?)", {},
95 $nassoc, $nusername, time);
97 $username = $nusername;
103 my $qassoc = $s->{S}{param_get}();
104 if (!defined $qassoc) {
105 $qassoc = $s->{S}{cookie_get}();
106 return undef unless defined $qassoc;
107 return undef unless $s->{S}{get_method}() eq 'GET';
109 my $dbh = $s->_dbopen();
110 my ($nusername, $nlast) =
111 $dbh->selectrow_array("SELECT username, last".
112 " FROM $s->{S}{assocdb_table}".
113 " WHERE associd = ?", {}, $qassoc);
114 return undef unless defined $nusername;
115 my $timeout = $s->{S}{login_timeout};
116 return undef unless !defined $timeout || time <= $nlast + $timeout;
118 return ($nusername, $qassoc);
124 my ($nusername, $qassoc) = $s->_check() or return undef;
127 $dbh->do("UPDATE $s->{S}{assocdb_table}".
129 " WHERE associd = ?", {}, time, $qassoc);
132 $username = $nusername;
140 if (my ($nusername, $qassoc) = $s->_check()) {
141 $dbh->do("DELETE FROM $s->{S}{assocdb_table}".
142 " WHERE associd = ?", {}, $qassoc);