3 # This is part of CGI::Auth::Flexible, a perl CGI authentication module.
4 # Copyright (C) 2012 Ian Jackson.
5 # Copyright (C) 2012 Citrix.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 package CGI::Auth::Flexible;
28 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
33 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
35 @EXPORT_OK = qw(setup);
40 use CGI qw/escapeHTML/;
51 #---------- public utilities ----------
53 sub flatten_params ($) {
56 foreach my $k (keys %$p) {
57 foreach my $v (@{ $p->{$k} }) {
64 #---------- default callbacks ----------
66 sub has_a_param ($$) {
68 foreach my $pn (@{ $r->{S}{$cn} }) {
69 return 1 if $r->_ch('get_param',$pn);
78 foreach my $name ($c->param()) {
79 $p{$name} = [ $c->param($name) ];
84 sub get_cookie_domain ($$$) {
86 my $uri = new URI $r->_ch('get_url');
90 sub login_ok_password ($$) {
92 my $username_params = $r->{S}{username_param_names};
93 my $username = $r->_ch('get_param',$username_params->[0]);
94 my $password = $r->_rp('password_param_name');
95 return $r->_ch('username_password_ok', $username, $password);
98 sub do_redirect_cgi ($$$$) {
99 my ($c, $r, $new_url, $cookie) = @_;
100 $r->_print($c->header($r->_cgi_header_args($cookie,
101 -status => '303 See other',
102 -location => $new_url)),
103 $r->_ch('gen_start_html',$r->_gt('Redirection')),
104 '<a href="'.escapeHTML($new_url).'">',
105 $r->_gt("If you aren't redirected, click to continue."),
107 $c->_ch('gen_end_html'));
110 sub gen_plain_login_form ($$) {
111 my ($c,$r, $params) = @_;
113 push @form, ('<form method="POST" action="'.
114 escapeHTML($r->_ch('get_url')).'">'.
116 my $sz = 'size="'.$r->{S}{form_entry_size}.'"';
117 foreach my $up (@{ $r->{S}{username_param_names}}) {
118 push @form, ('<tr><td>',$r->_gt(ucfirst $up),'</td>',
119 '<td><input type="text" '.$sz.
120 ' name='.$up.'></td></tr>');
122 push @form, ('<tr><td>'.$r->_gt('Password').'</td>',
123 '<td><input type="password" '.$sz.
124 ' name="'.$r->{S}{password_param_name}.'"></td></tr>');
125 push @form, ('<tr><td colspan="2">',
126 '<input type="submit"'.
127 ' name="'.$r->{S}{login_submit_name}[0].'"'.
128 ' value="'.$r->_gt('Login').'"></td></tr>',
130 foreach my $n (keys %$params) {
131 push @form, ('<input type="hidden"'.
133 ' value="'.$params->{$n}.'">');
135 push @form, ('</form>');
136 return join "\n", @form;
139 sub gen_login_link ($$) {
140 my ($c,$r, $params) = @_;
141 my $url = $r->url_with_query_params($params);
142 return ('<a href="'.escapeHTML($url).'">'.
143 $r->_gt('Log in again to continue.').
147 #---------- verifier object methods ----------
154 assocdb_path => 'caf-assocs.db',
155 keys_path => 'caf-keys',
156 assocdb_dsn => undef,
158 assocdb_password => '',
159 assocdb_table => 'caf_assocs',
160 random_source => '/dev/urandom',
161 secretbits => 128, # bits
162 hash_algorithm => "SHA-256",
163 login_timeout => 86400, # seconds
164 login_form_timeout => 3600, # seconds
165 key_rollover => 86400, # seconds
166 assoc_param_name => 'caf_assochash',
167 cookie_name => "caf_assocsecret",
168 password_param_name => 'password',
169 username_param_names => [qw(username)],
170 form_entry_size => 60,
171 logout_param_names => [qw(caf_logout)],
172 login_submit_name => [qw(caf_login)],
173 loggedout_param_names => [qw(caf_loggedout)],
174 promise_check_mutate => 0,
175 get_param => sub { $_[0]->param($_[2]) },
176 get_params => sub { $_[1]->get_params() },
177 get_cookie => sub { $_[0]->cookie($_[1]->{S}{cookie_name}) },
178 get_method => sub { $_[0]->request_method() },
179 get_url => sub { $_[0]->url(); },
180 is_login => sub { defined $_[1]->_rp('password_param_name') },
181 login_ok => \&login_ok_password,
182 username_password_ok => sub { die },
183 is_logout => sub { $_[1]->has_a_param('logout_param_names') },
184 is_loggedout => sub { $_[1]->has_a_param('loggedout_param_names') },
185 is_page => sub { return 1 },
186 handle_divert => sub { return 0 },
187 do_redirect => \&do_redirect_cgi, # this hook is allowed to throw
189 get_cookie_domain => \&get_cookie_domain,
191 gen_start_html => sub { $_[0]->start_html($_[2]); },
192 gen_end_html => sub { $_[0]->end_html(); },
193 gen_login_form => \&gen_plain_login_form,
194 gen_login_link => \&gen_plain_login_link,
195 gettext => sub { gettext($_[2]); },
196 print => sub { print $_[2] or die $!; },
201 while (($k,$v,@_) = @_) {
202 die "unknown setting $k" unless exists $verifier->{S}{$k};
203 $verifier->{S}{$k} = $v;
205 bless $verifier, $class;
206 $verifier->_dbopen();
210 sub _db_setup_do ($$) {
214 $v->_db_transaction(sub {
215 local ($dbh->{PrintError}) = 0;
226 $v->{S}{assocdb_dsn} ||= "dbi:SQLite:dbname=".$v->_get_path('assocdb');
227 my $dsn = $v->{S}{assocdb_dsn};
230 $dbh = DBI->connect($dsn, $v->{S}{assocdb_user},
231 $v->{S}{assocdb_password}, {
234 ShowErrorStatement => 1,
236 die "$dsn $! ?" unless $dbh;
239 $v->_db_setup_do("CREATE TABLE $v->{S}{assocdb_table} (".
240 " assochash VARCHAR PRIMARY KEY,".
241 " username VARCHAR NOT NULL,".
242 " last INTEGER NOT NULL".
244 $v->_db_setup_do("CREATE INDEX $v->{S}{assocdb_table}_timeout_index".
245 " ON $v->{S}{assocdb_table}".
257 sub _db_transaction ($$) {
262 print STDERR "DT entry\n";
264 print STDERR "DT loop\n";
267 print STDERR "DT fn ok\n";
270 print STDERR "DT fn error\n";
271 { local ($@); $dbh->rollback(); }
272 print STDERR "DT fn throwing\n";
275 print STDERR "DT fn eval ok\n";
278 print STDERR "DT commit ok\n";
281 print STDERR "DT commit eval ok $rv\n";
284 print STDERR "DT commit throw?\n";
285 die $@ if !--$retries;
286 print STDERR "DT loop again\n";
290 #---------- request object methods ----------
293 my ($classbase, $cgi, @extra) = @_;
294 if (!ref $classbase) {
295 $classbase = $classbase->new_verifier(@extra);
301 S => $classbase->{S},
302 Dbh => $classbase->{Dbh},
305 bless $r, ref $classbase;
308 sub _ch ($$@) { # calls an application hook
309 my ($r,$methname, @args) = @_;
310 my $methfunc = $r->{S}{$methname};
311 die "$methname ?" unless $methfunc;
312 return $methfunc->($r->{Cgi}, $r, @args);
317 my $pn = $r->{S}{$pnvb};
318 my $p = scalar $r->_ch('get_param',$pn)
322 my ($v,$keybase) = @_;
323 my $leaf = $v->{S}{"${keybase}_path"};
324 my $dir = $v->{S}{dir};
325 return $leaf if $leaf =~ m,^/,;
326 die "relying on cwd by default ?! set dir" unless defined $dir;
330 sub _gt ($$) { my ($r, $t) = @_; return $r->_ch('gettext',$t); }
331 sub _print ($$) { my ($r, @t) = @_; return $r->_ch('print', join '', @t); }
333 sub construct_cookie ($$$) {
334 my ($r, $cooks) = @_;
335 return undef unless $cooks;
337 my @ca = (-name => $r->{S}{cookie_name},
339 -path => $r->{S}{cookie_path},
340 -domain => $r->_ch('get_cookie_domain'),
341 -expires => '+'.$r->{S}{login_timeout}.'s',
342 -secure => $r->{S}{encrypted_only});
343 my $cookie = $c->cookie(@ca);
344 print STDERR "CC $r $c $cooks $cookie (@ca).\n";
348 # pages/param-sets are
349 # n normal non-mutating page
350 # r retrieval of information for JS, non-mutating
352 # u update of information by JS, mutating
355 # O "you have just logged out" page load
358 # - no value supplied (represented in code as $cookt='')
359 # n, nN value not in our db
360 # t, tN temporary value (in our db, no logged in user yet)
361 # y, yN value corresponds to logged-in user
362 # and, aggregated conditions:
363 # a, aN anything including -
365 # if N differs the case applies only when the two values differ
366 # (eg, a1 y2 does not apply when the logged-in value is supplied twice)
368 # "stale session" means request originates from a page from a login
369 # session which has been revoked (eg by logout); "cleared session"
370 # means request originates from a browser which has a different (or
373 # Case analysis, cookie mode, app promises re mutate:
374 # cook parm meth form
376 # any - POST nrmuoi bug or attack, fail
377 # any - GET rmuoi bug or attack, fail
378 # any any GET muoi bug or attack, fail
379 # any t any nrmu bug or attack, fail
381 # - - GET O "just logged out" page
382 # (any other) O bug or attack, fail
384 # a1 a2 POST o logout
385 # if a1 is valid, revoke it
386 # if a2 is valid, revoke it
388 # redirect to "just logged out" page
389 # (which contains link to login form)
391 # - t POST i complain about cookies being disabled
392 # (with link to login form)
394 # t1 t1 POST i login (or switch user)
396 # show new login form
398 # upgrade t1 to y1 in our db (setting username)
399 # redirect to GET of remaining params
401 # y1 a2 POST i complain about stale login form
403 # show new login form
405 # (other) POST i complain about stale login form
406 # show new login form
408 # t1 a2 ANY nrmu treat as - a2 ANY
410 # y - GET n cross-site link
413 # y y GET nr fine, show page or send data
414 # y y POST nrmu mutation is OK, do operation
416 # y1 y2 GET nr request from stale page
417 # do not revoke y2 as not RESTful
420 # y1 y2 POST nrmu request from stale page
424 # y n GET n intra-site link from stale page,
425 # treat as cross-site link, show data
427 # y n POST n m intra-site form submission from stale page
428 # show "session interrupted"
429 # with link to main data page
431 # y n GET r intra-site request from stale page
434 # y n POST r u intra-site request from stale page
437 # -/n y2 GET nr intra-site link from cleared session
438 # do not revoke y2 as not RESTful
441 # -/n y2 POST nrmu request from cleared session
443 # treat as -/n n POST
445 # -/n -/n GET n cross-site link but user not logged in
446 # show login form with redirect to orig params
447 # generate fresh cookie
449 # -/n n GET rmu user not logged in
452 # -/n n POST n m user not logged in
455 # -/n n POST r u user not logged in
458 sub _check_divert_core ($) {
461 my $meth = $r->_ch('get_method');
462 my $cooks = $r->_ch('get_cookie');
463 my $parmh = $r->_rp('assoc_param_name');
464 my $cookh = defined $cooks ? $r->hash($cooks) : undef;
466 my ($cookt,$cooku) = $r->_identify($cookh, $cooks);
467 my $parmt = $r->_identify($parmh, undef);
469 print STDERR "_c_d_c cookt=$cookt parmt=$parmt\n";
471 if ($r->_ch('is_logout')) {
474 $r->_db_revoke($cookh);
475 $r->_db_revoke($parmh);
476 return ({ Kind => 'REDIRECT-LOGGEDOUT',
477 Message => "Logging out...",
481 if ($r->_ch('is_loggedout')) {
482 die unless $meth eq 'GET';
485 return ({ Kind => 'SMALLPAGE-LOGGEDOUT',
486 Message => "You have been logged out.",
490 if ($r->_ch('is_login')) {
493 if (!$cookt && $parmt eq 't') {
494 return ({ Kind => 'SMALLPAGE-NOCOOKIE',
495 Message => "You do not seem to have cookies enabled. ".
496 "You must enable cookies as we use them for login.",
497 CookieSecret => $r->_fresh_secret(),
498 Params => $r->_chain_params() })
500 if (!$cookt || $cookt eq 'n' || $cookh ne $parmh) {
501 $r->_db_revoke($cookh);
502 return ({ Kind => 'LOGIN-STALE',
503 Message => "Stale session; you need to log in again.",
504 CookieSecret => $r->_fresh_secret(),
507 die unless $parmt eq 't' || $parmt eq 'y';
508 my $username = $r->_ch('login_ok');
509 unless (defined $username && length $username) {
510 return ({ Kind => 'LOGIN-BAD',
511 Message => "Incorrect username/password.",
512 CookieSecret => $cooks,
513 Params => $r->_chain_params() })
515 $r->_db_record_login_ok($parmh,$username);
516 return ({ Kind => 'REDIRECT-LOGGEDIN',
517 Message => "Logging in...",
518 CookieSecret => $cooks,
519 Params => $r->_chain_params() });
524 die if $parmt eq 't';
526 if ($cookt eq 'y' && $parmt eq 'y' && $cookh ne $parmh) {
527 $r->_db_revoke($parmh) if $meth eq 'POST';
532 die unless !$cookt || $cookt eq 'n';
533 die unless !$parmt || $parmt eq 'n' || $parmt eq 'y';
534 my $news = $r->_fresh_secret();
535 if ($meth eq 'GET') {
536 return ({ Kind => 'LOGIN-INCOMINGLINK',
537 Message => "You need to log in again.",
538 CookieSecret => $news,
539 Params => $r->_chain_params() });
541 $r->_db_revoke($parmh);
542 return ({ Kind => 'LOGIN-FRESH',
543 Message => "You need to log in again.",
544 CookieSecret => $news,
549 if (!$r->{S}{promise_check_mutate}) {
550 if ($meth ne 'POST') {
551 return ({ Kind => 'MAINPAGEONLY',
552 Message => 'Entering via cross-site link.',
553 CookieSecret => $cooks,
555 # NB caller must then ignore params & path!
556 # if this is too hard they can spit out a small form
557 # with a "click to continue"
561 die unless $cookt eq 'y';
562 die unless $parmt eq 'y';
563 die unless $cookh eq $parmh;
564 $r->{AssocSecret} = $cooks;
565 $r->{UserOK} = $cooku;
566 print STDERR "C-D-C OK\n";
570 sub _chain_params ($) {
572 my %p = %{ $r->_ch('get_params') };
573 foreach my $pncn (keys %{ $r->{S} }) {
575 if ($pncn =~ m/_param_name$/) {
576 my $name = $r->{S}{$pncn};
577 die "$pncn ?" if ref $name;
579 } elsif ($pncn =~ m/_param_names$/) {
580 $names = $r->{S}{$pncn};
584 foreach my $name (@$names) {
593 # returns ($t,$username)
594 # where $t is one of "t" "y" "n", or "" (for -)
595 # either $s must be undef, or $h eq $r->hash($s)
597 return '' unless defined $h && length $h;
601 $dbh->do("DELETE FROM $r->{S}{assocdb_table}".
602 " WHERE last < ?", {},
603 time - $r->{S}{login_timeout});
605 my $row = $dbh->selectrow_arrayref("SELECT username, last".
606 " FROM $r->{S}{assocdb_table}".
607 " WHERE assochash = ?", {}, $h);
609 my ($nusername, $nlast) = @$row;
610 return ('y', $nusername);
613 # Well, it's not in the database. But maybe it's a hash of a
616 return 'n' unless defined $s;
618 my ($keyt, $signature, $message, $noncet, $nonce) =
619 $s =~ m/^(\d+)\.(\w+)\.((\d+)\.(\w+))$/ or die;
621 return 'n' if time > $noncet + $r->{S}{form_timeout};
623 my $keys = $r->_open_keys();
624 while (my ($rkeyt, $rkey, $line) = $r->_read_key($keys)) {
625 last if $rkeyt < $keyt; # too far down in the file
626 my $trysignature = $r->_hmac($rkey, $message);
627 return 't' if $trysignature eq $signature;
631 $keys->error and die $!;
635 sub _db_revoke ($$) {
636 # revokes $h if it's valid; no-op if it's not
641 $dbh->do("DELETE FROM $r->{S}{assocdb_table}".
642 " WHERE assochash = ?", {}, $h);
645 sub _db_record_login_ok ($$$) {
646 my ($r,$h,$user) = @_;
649 $dbh->do("INSERT INTO $r->{S}{assocdb_table}".
650 " (associd, username, last) VALUES (?,?,?)", {},
654 sub check_divert ($) {
656 if (exists $r->{Divert}) {
660 $r->{Divert} = $r->_db_transaction(sub { $r->_check_divert_core(); });
662 print STDERR Dumper($r->{Divert});
668 die "unchecked" unless exists $r->{Divert};
672 sub get_username ($) {
674 my $divert = $r->get_divert();
675 return undef if $divert;
679 sub url_with_query_params ($$) {
680 my ($r, $params) = @_;
681 my $uri = URI->new($r->_ch('get_url'));
682 $uri->query_form(flatten_params($params));
683 return $uri->as_string();
686 sub _cgi_header_args ($$@) {
687 my ($r, $cookie, @ha) = @_;
688 unshift @ha, qw(-type text/html);
689 push @ha, (-cookie => $cookie) if defined $cookie;
690 print STDERR "_cgi_header_args ",join('|',@ha),".\n";
697 my ($divert) = $r->check_divert();
698 return 1 if !$divert;
700 my $handled = $r->_ch('handle_divert',$divert);
701 return 0 if $handled;
703 my $kind = $divert->{Kind};
704 my $cookiesecret = $divert->{CookieSecret};
705 my $params = $divert->{Params};
706 my $cookie = $r->construct_cookie($cookiesecret);
708 if ($kind =~ m/^REDIRECT-/) {
709 # for redirects, we honour stored NextParams and SetCookie,
710 # as we would for non-divert
711 if ($kind eq 'REDIRECT-LOGGEDOUT') {
712 $params->{$r->{S}{loggedout_param_names}[0]} = 1;
713 } elsif ($kind eq 'REDIRECT-LOGOUT') {
714 $params->{$r->{S}{logout_param_names}[0]} = 1;
715 } elsif ($kind eq 'REDIRECT-LOGGEDIN') {
719 my $new_url = $r->url_with_query_params($params);
720 $r->_ch('do_redirect',$new_url, $cookie);
725 if ($kind =~ m/^LOGIN-/) {
726 $title = $r->_gt('Login');
727 push @body, $r->_gt($divert->{Message});
728 push @body, $r->_ch('gen_login_form', $params);
729 } elsif ($kind =~ m/^SMALLPAGE-/) {
730 $title = $r->_gt('Not logged in');
731 push @body, $r->_gt($divert->{Message});
732 push @body, $r->_ch('gen_login_link');
737 $r->_print($r->{Cgi}->header($r->_cgi_header_args($cookie)),
738 $r->_ch('gen_start_html',$title),
740 $r->_ch('gen_end_html'));
745 my ($r, $bytes) = @_;
747 my $rsf = $v->{RandomHandle};
748 my $rsp = $r->{S}{random_source};
750 $v->{RandomHandle} = $rsf = new IO::File $rsp, '<' or die "$rsp $!";
751 print STDERR "RH $rsf\n";
755 read($rsf,$bin,$bytes) == $bytes or die "$rsp $!";
756 my $out = unpack "H*", $bin;
757 print STDERR "_random out $out\n";
761 sub _random_key ($) {
763 print STDERR "_random_key\n";
764 my $bytes = ($r->{S}{secretbits} + 7) >> 3;
765 return $r->_random($bytes);
770 # returns $gen_time_t, $key_value_in_hex, $complete_line
772 my ($gen, $k) = m/^(\d+) (\S+)$/ or die "$_ ?";
773 my $age = time - $gen;
774 next if $age > $r->{S}{key_rollover} &&
775 $age > $r->{S}{login_form_timeout}*2;
776 return ($gen, $k, $_);
783 my $spath = $r->_get_path('keys');
785 print STDERR "_open_keys\n";
786 my $keys = new IO::File $spath, 'r+';
788 print STDERR "_open_keys open\n";
789 stat $keys or die $!; # NB must not disturb stat _
790 my $size = (stat _)[7];
791 my $age = time - (stat _)[9];
792 print STDERR "_open_keys open size=$size age=$age\n";
794 if $size && $age <= $r->{S}{key_rollover} / 2;
795 print STDERR "_open_keys open bad\n";
797 # file doesn't exist, or is empty or too old
799 print STDERR "_open_keys closed\n";
800 die "$spath $!" unless $!==&ENOENT;
801 # doesn't exist, so create it just so we can lock it
802 $keys = new IO::File $spath, 'a+';
803 die "$keys $!" unless $keys;
804 stat $keys or die $!; # NB must not disturb stat _
805 my $size = (stat _)[7];
806 print STDERR "_open_keys created size=$size\n";
807 next if $size; # oh someone else has done it, reopen and read it
809 # file now exists is empty or too old, we must try to replace it
810 my $our_inum = (stat _)[1]; # last use of that stat _
811 flock $keys, LOCK_EX or die "$spath $!";
812 stat $spath or die "$spath $!";
813 my $path_inum = (stat _)[1];
814 print STDERR "_open_keys locked our=$our_inum path=$path_inum\n";
815 next if $our_inum != $path_inum; # someone else has done it
816 # We now hold the lock!
817 print STDERR "_open_keys creating\n";
818 my $newkeys = new IO::Handle;
819 sysopen $newkeys, "$spath.new", O_CREAT|O_TRUNC|O_WRONLY, 0600
820 or die "$spath.new $!";
821 # we add the new key to the front which means it's always sorted
822 print $newkeys time, ' ', $r->_random_key(), "\n" or die $!;
823 while (my ($gen,$key,$line) = $r->_read_key($keys)) {
824 print STDERR "_open_keys copy1\n";
825 print $newkeys, $line or die $!;
827 $keys->error and die $!;
828 close $newkeys or die "$spath.new $!";
829 rename "$spath.new", "$spath" or die "$spath: $!";
830 print STDERR "_open_keys installed\n";
831 # that rename effective unlocks, since it makes the name refer
832 # to the new file which we haven't locked
833 # we go round again opening the file at the beginning
834 # so that our caller gets a fresh handle onto the existing key file
838 sub _fresh_secret ($) {
840 print STDERR "_fresh_secret\n";
842 my $keys = $r->_open_keys();
843 my ($keyt, $key) = $r->_read_key($keys);
844 die unless defined $keyt;
846 my $nonce = $r->_random_key();
848 my $message = "$noncet.$nonce";
850 my $signature = $r->_hmac($key, $message);
851 my $secret = "$keyt.$signature.$message";
852 print STDERR "FRESH $secret\n";
857 my ($r, $keyhex, $message) = @_;
858 my $keybin = pack "H*", $keyhex;
859 my $alg = $r->{S}{hash_algorithm};
860 print STDERR "hmac $alg\n";
861 my $base = new Digest $alg;
862 print STDERR "hmac $alg $base\n";
863 my $digest = new Digest::HMAC $keybin, $base;
864 print STDERR "hmac $alg $base $digest\n";
865 $digest->add($message);
866 return $digest->hexdigest();
870 my ($r, $message) = @_;
871 my $alg = $r->{S}{hash_algorithm};
872 print STDERR "hash $alg";
873 my $digest = new Digest $alg;
874 $digest->add($message);
875 return $digest->hexdigest();
878 sub _assert_checked ($) {
880 die "unchecked" unless exists $r->{Divert};
883 sub check_mutate ($) {
885 $r->_assert_checked();
887 my $meth = $r->_ch('get_method');
888 die "mutating non-POST" if $meth ne 'POST';
891 #---------- output ----------
893 sub secret_cookie_val ($) {
895 $r->_assert_checked();
896 return defined $r->{AssocSecret} ? $r->{AssocSecret} : '';
899 sub secret_hidden_val ($) {
901 $r->_assert_checked();
902 return defined $r->{AssocSecret} ? r->hash($r->{AssocSecret}) : '';
905 sub secret_hidden_html ($) {
907 return $r->{Cgi}->hidden(-name => $r->{S}{assoc_param_name},
908 -default => $r->secret_hidden_val());
911 sub secret_cookie ($) {
913 my $secret = $r->secret_cookie_val();
914 return undef if !defined $secret;
915 #print STDERR "SC\n";
916 my $cookv = $r->construct_cookie($secret);
917 #print STDERR "SC=$cookv\n";
925 CGI::Auth::Flexible - web authentication optionally using cookies
929 my $verifier = CGI::Auth::Flexible->new_verifier(setting => value,...);
930 my $authreq = $verifier->new_request($cgi_request_object);
932 my $authreq = CGI::Auth::Flexible->new_request($cgi_request_object,
933 setting => value,...);
935 =head1 USAGE PATTERN FOR SIMPLE APPLICATIONS
937 $authreq->check_ok() or return;
940 $authreq->check_mutate();
943 =head1 USAGE PATTERN FOR FANCY APPLICATIONS
945 my $divert_kind = $authreq->check_divert();
947 if ($divert_kind eq 'LOGGEDOUT') {
948 print "goodbye you are now logged out" and quit
949 } elsif ($divert_kind eq 'NOCOOKIES') {
950 print "you need cookies" and quit
956 $authreq->check_mutate();