chiark / gitweb /
wip fixes, debugging
[cgi-auth-flexible.git] / cgi-auth-hybrid.pm
1 # -*- perl -*-
2
3 # This is part of CGI::Auth::Hybrid, a perl CGI authentication module.
4 # Copyright (C) 2012 Ian Jackson.
5 # Copyright (C) 2012 Citrix.
6
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.
11
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.
16
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/>.
19
20 use strict;
21 use warnings;
22
23 package CGI::Auth::Hybrid;
24 require Exporter;
25
26 BEGIN {
27     use Exporter   ();
28     our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
29
30     $VERSION     = 1.00;
31     @ISA         = qw(Exporter);
32     @EXPORT      = qw();
33     %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
34
35     @EXPORT_OK   = qw(setup);
36 }
37 our @EXPORT_OK;
38
39 use DBI;
40 use CGI qw/escapeHTML/;
41 use Locale::gettext;
42 use URI;
43 use Data::Dumper;
44
45 #---------- public utilities ----------
46
47 sub flatten_params ($) {
48     my ($p) = @_;
49     my @p;
50     foreach my $k (keys %$p) {
51         foreach my $v (@{ $p->{$k} }) {
52             push @p, $k, $v;
53         }
54     }
55     return @p;
56 }
57
58 #---------- default callbacks ----------
59
60 sub has_a_param ($$) {
61     my ($r,$cn) = @_;
62     foreach my $pn (@{ $r->{S}{$cn} }) {
63         return 1 if $r->_ch('get_param',$pn);
64     }
65     return 0;
66 }
67
68 sub get_params ($) {
69     my ($r) = @_;
70     my %p;
71     my $c = $r->{Cgi};
72     foreach my $name ($c->param()) {
73         $p{$name} = [ $c->param($name) ];
74     }
75     return \%p;
76 }
77
78 sub get_cookie_domain ($$$) {
79     my ($c,$r) = @_;
80     my $uri = new URI $r->_ch('get_url');
81     return $uri->host();
82 }
83
84 sub login_ok_password ($$) {
85     my ($c, $r) = @_;
86     my $username_params = $r->{S}{username_param_names};
87     my $username = $r->_ch('get_param',$username_params->[0]);
88     my $password = $r->_rp('password_param_name');
89     return $r->_ch('username_password_ok', $username, $password);
90 }
91
92 sub do_redirect_cgi ($$$$) {
93     my ($c, $r, $new_url, $cookie) = @_;
94     my @ha = ('text/html',
95               -status => '303 See other',
96               -location => $new_url);
97     push @ha, (-cookie => $cookie) if defined $cookie;
98     $r->_print($c->header(@ha),
99                $r->_ch('gen_start_html',$r->_gt('Redirection')),
100                '<a href="'.escapeHTML($new_url).'">',
101                $r->_gt("If you aren't redirected, click to continue."),
102                "</a>",
103                $c->_ch('gen_end_html'));
104 }
105
106 sub gen_plain_login_form ($$) {
107     my ($c,$r, $params) = @_;
108     my @form;
109     push @form, ('<form method="POST" action="'.
110                  escapeHTML($r->_ch('get_url')).'>'.
111                  '<table>');
112     my $sz = 'size="'.$r->{S}{form_entry_size}.'"';
113     foreach my $up (@{ $r->{S}{username_param_names}}) {
114         push @form, ('<tr><td>',$r->_gt(ucfirst $up),'</td>',
115                      '<td><input type="text" ',$sz.
116                      ' name=',$up,'></td></tr>');
117     }
118     push @form, ('<tr><td>'.$r->_gt('Password'),'</td>',
119                  '<td><input type="password" '.$sz.
120                  ' name="'.$r->{S}{password_param_name}.'"></td></tr>');
121     push @form, ('<tr><td colspan="2">',
122                  '<input type="submit"'.
123                  ' name="'.$r->{S}{login_submit_name}.'"'.
124                  ' value="'.$r->_gt('Login').'"></td></tr>',
125                  '</table>');
126     foreach my $n (keys %$params) {
127         push @form, ('<input type="hidden"'.
128                      ' name="'.$n.'"'.
129                      ' value="'.$params->{$n}.'">');
130     }
131     push @form, ('</form>');
132     return join "\n", @form;
133 }
134
135 sub gen_login_link ($$) {
136     my ($c,$r, $params) = @_;
137     my $url = $r->url_with_query_params($params);
138     return ('<a href="'.escapeHTML($url).'">'.
139             $r->_gt('Log in again to continue.').
140             '</a>');
141 }
142
143 #---------- verifier object methods ----------
144
145 sub new_verifier {
146     my $class = shift;
147     my $verifier = {
148         S => {
149             assocdb_path => 'cah-assocs.db',
150             assocdb_dsn => undef,
151             assocdb_user => '',
152             assocdb_password => '',
153             assocdb_table => 'assocs',
154             random_source => '/dev/urandom',
155             associdlen => 128, # bits
156             login_timeout => 86400, # seconds
157             assoc_param_name => 'cah_associd',
158             password_param_name => 'password',
159             username_param_names => [qw(username)],
160             form_entry_size => 60,
161             logout_param_names => [qw(cah_logout)],
162             login_submit_name => [qw(cah_login)],
163             loggedout_param_names => [qw(cah_loggedout)],
164             promise_check_mutate => 0,
165             get_param => sub { $_[0]->param($_[2]) },
166             get_params => sub { $_[1]->get_params() },
167             get_cookie => sub { $_[0]->cookie($_[1]->{S}{cookie_name}) },
168             get_method => sub { $_[0]->request_method() },
169             get_url => sub { $_[0]->url(); },
170             is_login => sub { defined $_[1]->_rp('password_param_name') },
171             login_ok => \&login_ok_password,
172             username_password_ok => sub { die },
173             is_logout => sub { $_[1]->has_a_param('logout_param_names') },
174             is_loggedout => sub { $_[1]->has_a_param('loggedout_param_names') },
175             is_page => sub { return 1 },
176             handle_divert => sub { return 0 },
177             do_redirect => \&do_redirect_cgi, # this hook is allowed to throw
178             cookie_path => "/",
179             get_cookie_domain => \&get_cookie_domain,
180             encrypted_only => 0,
181             gen_start_html => sub { $_[0]->start_html($_[2]); },
182             gen_end_html => sub { $_[0]->end_html(); },
183             gen_login_form => \&gen_plain_login_form,
184             gen_login_link => \&gen_plain_login_link,
185             gettext => sub { gettext($_[2]); },
186         },
187         Dbh => undef,
188     };
189     my ($k,$v);
190     while (($k,$v,@_) = @_) {
191         die "unknown setting $k" unless exists $verifier->{S}{$k};
192         $verifier->{S}{$k} = $v;
193     }
194     bless $verifier, $class;
195     $verifier->_dbopen();
196     return $verifier;
197 }
198
199 sub _dbopen ($) {
200     my ($v) = @_;
201     my $dbh = $v->{Dbh};
202     return $dbh if $dbh; 
203
204     $v->{S}{assocdb_dsn} ||= "dbi:SQLite:dbname=$v->{S}{assocdb_path}";
205     my $dsn = $v->{S}{assocdb_dsn};
206
207     my $u = umask 077;
208     $dbh = DBI->connect($dsn, $v->{S}{assocdb_user}, 
209                         $v->{S}{assocdb_password}, { 
210                             AutoCommit => 0,
211                             RaiseError => 1,
212                             ShowErrorStatement => 1,
213                         });
214     die "$dsn $! ?" unless $dbh;
215     $v->{Dbh} = $dbh;
216
217     eval {
218         $v->_db_transaction(sub {
219             local ($dbh->{PrintError}) = 0;
220             $dbh->do("CREATE TABLE $v->{S}{assocdb_table} (".
221                      " associd VARCHAR PRIMARY KEY,".
222                      " username VARCHAR,".
223                      " last INTEGER NOT NULL".
224                      ")");
225         });
226     };
227     return $dbh;
228 }
229
230 sub disconnect ($) {
231     my ($v) = @_;
232     my $dbh = $v->{Dbh};
233     return unless $dbh;
234     $dbh->disconnect();
235 }
236
237 sub _db_transaction ($$) {
238     my ($v, $fn) = @_;
239     my $retries = 10;
240     my $rv;
241     my $dbh = $v->{Dbh};
242 print STDERR "DT entry\n";
243     for (;;) {
244 print STDERR "DT loop\n";
245         if (!eval {
246             $rv = $fn->();
247 print STDERR "DT fn ok\n";
248             1;
249         }) {
250 print STDERR "DT fn error\n";
251             { local ($@); $dbh->rollback(); }
252 print STDERR "DT fn throwing\n";
253             die $@;
254         }
255 print STDERR "DT fn eval ok\n";
256         if (eval {
257             $dbh->commit();
258 print STDERR "DT commit ok\n";
259             1;
260         }) {
261 print STDERR "DT commit eval ok $rv\n";
262             return $rv;
263         }
264 print STDERR "DT commit throw?\n";
265         die $@ if !--$retries;
266 print STDERR "DT loop again\n";
267     }
268 }
269
270 #---------- request object methods ----------
271
272 sub new_request {
273     my ($classbase, $cgi, @extra) = @_;
274     if (!ref $classbase) {
275         $classbase = $classbase->new_verifier(@extra);
276     } else {
277         die if @extra;
278     }
279     my $r = {
280         V => $classbase,
281         S => $classbase->{S},
282         Dbh => $classbase->{Dbh},
283         Cgi => $cgi,
284     };
285     bless $r, ref $classbase;
286 }
287
288 sub _ch ($$@) { # calls an application hook
289     my ($r,$methname, @args) = @_;
290     my $methfunc = $r->{S}{$methname};
291     die "$methname ?" unless $methfunc;
292     return $methfunc->($r->{Cgi}, $r, @args);
293 }
294
295 sub _rp ($$@) {
296     my ($r,$pnvb) = @_;
297     my $pn = $r->{S}{$pnvb};
298     my $p = scalar $r->_ch('get_param',$pn)
299 }
300
301 sub _gt ($$) { my ($r, $t) = @_; return $r->_ch('gettext',$t); }
302 sub _print ($$) { my ($r, @t) = @_; return $r->_ch('print', join '', @t); }
303
304 sub construct_cookie ($$$) {
305     my ($r, $cookv) = @_;
306     return undef unless $cookv;
307     my $c = $r->{Cgi};
308     my $cookt = $c->cookie(-name => $r->{S}{cookie_name},
309                              -value => $cookv,
310                              -path => $r->{S}{cookie_path},
311                              -domain => $r->_ch('get_cookie_domain'),
312                              -expires => '+'.$r->{S}{login_timeout}.'s',
313                              -secure => $r->{S}{encrypted_only});
314 print STDERR "CC $r $c $cookv $cookt\n";
315     return $cookt;
316 }
317
318 # pages/param-sets are
319 #   n normal non-mutating page
320 #   r retrieval of information for JS, non-mutating
321 #   m mutating page
322 #   u update of information by JS, mutating
323 #   i login
324 #   o logout
325 #   O "you have just logged out" page load
326
327 # in cook and par,
328 #    a, aN     anything including -
329 #    t, tN     temporary value (in our db, no logged in user yet)
330 #    y, yN     value corresponds to logged-in user
331 #    n, nN     value not in our db
332 #    x, xN     t or y
333 #    -         no value supplied
334 # if N differs the case applies only when the two values differ
335 # (eg,   a1 y2   does not apply when the logged-in value is supplied twice)
336
337 # "stale session" means request originates from a page from a login
338 # session which has been revoked (eg by logout); "cleared session"
339 # means request originates from a browser which has a different (or
340 # no) cookie.
341
342     # Case analysis, cookie mode, app promises re mutate:
343     # cook parm meth form
344     #                      
345     #  any -   POST  nrmuoi   bug or attack, fail
346     #  any -   GET    rmuoi   bug or attack, fail
347     #  any any GET     muoi   bug or attack, fail
348     #  any t   any   nrmu     bug or attack, fail
349     #
350     #  -   -   GET         O  "just logged out" page
351     #  (any other)         O  bug or attack, fail
352     #
353     #  a1  a2  POST      o    logout
354     #                           if a1 is valid, revoke it
355     #                           if a2 is valid, revoke it
356     #                           delete cookie
357     #                           redirect to "just logged out" page
358     #                             (which contains link to login form)
359     #
360     #  -   t   POST       i   complain about cookies being disabled
361     #                           (with link to login form)
362     #
363     #  any n   POST       i   complain about stale login form
364     #                           show new login form
365     #
366     #  x1  t2  POST       i   login (or switch user)
367     #                           if bad
368     #                             show new login form
369     #                           if good
370     #                             revoke x1 if it was valid and !=t2
371     #                             upgrade t2 to y2 in our db (setting username)
372     #                             set cookie to t2
373     #                             redirect to GET of remaining params
374     #
375     #  t1  a2  ANY   nrmu     treat as  - a2 ANY
376     #
377     #  y   -   GET   n        cross-site link
378     #                           show data
379     #
380     #  y   y   GET   nr       fine, show page or send data
381     #  y   y   POST  nrmu     mutation is OK, do operation
382     #
383     #  y1  y2  GET   nr       request from stale page
384     #                           do not revoke y2 as not RESTful
385     #                           treat as   y1 n GET
386     #
387     #  y1  y2  POST  nrmu     request from stale page
388     #                           revoke y2
389     #                           treat as   y1 n POST
390     #
391     #  y   n   GET   n        intra-site link from stale page,
392     #                           treat as cross-site link, show data
393     #
394     #  y   n   POST  n m      intra-site form submission from stale page
395     #                           show "session interrupted"
396     #                           with link to main data page
397     #
398     #  y   n   GET    r       intra-site request from stale page
399     #                           fail
400     #
401     #  y   n   POST   r u     intra-site request from stale page
402     #                           fail
403     #
404     #  -/n y2  GET   nr       intra-site link from cleared session
405     #                           do not revoke y2 as not RESTful
406     #                           treat as   -/n n GET
407     #
408     #  -/n y2  POST  nrmu     request from cleared session
409     #                           revoke y2
410     #                           treat as   -/n n POST
411     #
412     #  -/n n   GET   n        cross-site link but user not logged in
413     #                           show login form with redirect to orig params
414     #
415     #  -/n n   GET    rmu     user not logged in
416     #                           fail
417     #
418     #  -/n n   POST  n m      user not logged in
419     #                           show login form
420     #
421     #  -/n n   POST   r u     user not logged in
422     #                           fail
423
424 sub _check_divert_core ($) {
425     my ($r) = @_;
426
427     my $meth = $r->_ch('get_method');
428     my $cookv = $r->_ch('get_cookie');
429     my $parmv = $r->_rp('assoc_param_name');
430
431     my ($cookt,$cooku) = $r->_db_lookup($cookv);
432     my $parmt = $r->_db_lookup($parmv);
433
434     if ($r->_ch('is_logout')) {
435         $r->_must_be_post();
436         die unless $parmt;
437         $r->_db_revoke($cookv);
438         $r->_db_revoke($parmv);
439         return ({ Kind => 'REDIRECT-LOGGEDOUT',
440                   Message => "Logging out...",
441                   CookieVal => '',
442                   Params => { } });
443     }
444     if ($r->_ch('is_loggedout')) {
445         die unless $meth eq 'GET';
446         die unless $cookt;
447         die unless $parmt;
448         return ({ Kind => 'SMALLPAGE-LOGGEDOUT',
449                   Message => "You have been logged out.",
450                   CookieVal => '',
451                   Params => { } });
452     }
453     if ($r->_ch('is_login')) {
454         $r->_must_be_post();
455         return ({ Kind => 'LOGIN-STALE',
456                   Message => "Stale session; you need to log in again.",
457                   CookieVal => $r->_fresh_cookie(),
458                   Params => { } })
459             if $parmt eq 'n';
460         die unless $parmt eq 't' || $parmt eq 'y';
461         return ({ Kind => 'SMALLPAGE-NOCOOKIE',
462                   Message => "You do not seem to have cookies enabled.  ".
463                       "You must enable cookies as we use them for login.",
464                   CookieVal => $r->_fresh_cookie(),
465                   Params => $r->_chain_params() })
466             if !$cookt && $parmt eq 't';
467         my $username = $r->_ch('login_ok');
468         return ({ Kind => 'LOGIN-BAD',
469                   Message => "Incorrect username/password.",
470                   CookieVal => $cookv,
471                   Params => $r->_chain_params() })
472             unless defined $username && length $username;
473         $r->_db_revoke($cookv) 
474             if defined $cookv && !(defined $parmv && $cookv eq $parmv);
475         $r->_db_record_login_ok($parmv,$username);
476         return ({ Kind => 'REDIRECT-LOGGEDIN',
477                   Message => "Logging in...",
478                   CookieVal => $parmv,
479                   Params => $r->_chain_params() });
480     }
481     if ($cookt eq 't') {
482         $cookt = '';
483     }
484     die if $parmt eq 't';
485
486     if ($cookt eq 'y' && $parmt eq 'y' && $cookv ne $parmv) {
487         $r->_db_revoke($parmv) if $meth eq 'POST';
488         $parmt = 'n';
489     }
490
491     if ($cookt ne 'y') {
492         die unless !$cookt || $cookt eq 'n';
493         die unless !$parmt || $parmt eq 'n' || $parmt eq 'y';
494         if ($meth eq 'GET') {
495             return ({ Kind => 'LOGIN-INCOMINGLINK',
496                       Message => "You need to log in again.",
497                       CookieVal => $parmv,
498                       Params => $r->_chain_params() });
499         } else {
500             return ({ Kind => 'LOGIN-FRESH',
501                       Message => "You need to log in again.",
502                       CookieVal => $parmv,
503                       Params => { } });
504         }
505     }
506
507     if (!$r->{S}{promise_check_mutate}) {
508         if ($meth ne 'POST') {
509             return ({ Kind => 'MAINPAGEONLY',
510                       Message => 'Entering via cross-site link.',
511                       CookieVal => $cookv,
512                       Params => { } });
513             # NB caller must then ignore params & path!
514             # if this is too hard they can spit out a small form
515             # with a "click to continue"
516         }
517     }
518
519     die unless $cookt eq 'y';
520     die unless $parmt eq 'y';
521     die unless $cookv eq $parmv;
522     $r->{Assoc} = $cookv;
523     $r->{UserOK} = $cooku;
524     print STDERR "C-D-C OK\n";
525     return undef;
526 }
527
528 sub _chain_params ($) {
529     my ($r) = @_;
530     my %p = %{ $r->_ch('get_params') };
531     foreach my $pncn (keys %{ $r->{S} }) {
532         my $names;
533         if ($pncn =~ m/_param_name$/) {
534             my $name = $r->{S}{$pncn};
535             die "$pncn ?" if ref $name;
536             $names = [ $name ];
537         } elsif ($pncn =~ m/_param_names$/) {
538             $names = $r->{S}{$pncn};
539         } else {
540             next;
541         }
542         foreach my $name (@$names) {
543             delete $p{$name};
544         }
545     }
546     return \%p;
547 }
548
549 sub _db_lookup ($$) {
550     my ($r,$v) = @_;
551     # returns ($t,$username)
552     # where $t is one of "t" "y" "n", or "" (for -)
553
554     my $dbh = $r->{Dbh};
555
556     my $row = $dbh->selectrow_arrayref("SELECT username, last".
557                               " FROM $r->{S}{assocdb_table}".
558                               " WHERE associd = ?", {}, $v);
559     return ('') unless defined $row;
560
561     my ($nusername, $nlast) = @$row;
562
563     my $timeout = $r->{S}{login_timeout};
564     return ('n') unless !defined $timeout || time <= $nlast + $timeout;
565
566     return ('t') unless defined $nusername;
567
568     # hooray
569     return ('y', $nusername);
570 }
571
572 sub _db_revoke ($$) {
573     # revokes $v if it's valid; no-op if it's not
574     my ($r,$v) = @_;
575
576     my $dbh = $r->{Dbh};
577
578     $dbh->do("DELETE FROM $r->{S}{assocdb_table}".
579              " WHERE associd = ?", {}, $v);
580 }
581
582 sub _db_record_login_ok ($$$) {
583     my ($r,$v,$user) = @_;
584     $r->_db_revoke($v);
585     my $dbh = $r->{Dbh};
586     $dbh->do("INSERT INTO $r->{S}{assocdb_table}".
587              " (associd, username, last) VALUES (?,?,?)", {},
588              $v, $user, time);
589 }
590
591 sub check_divert ($) {
592     my ($r) = @_;
593     if (exists $r->{Divert}) {
594         return $r->{Divert};
595     }
596     my $dbh = $r->{Dbh};
597     $r->{Divert} = $r->_db_transaction(sub { $r->_check_divert_core(); });
598     $dbh->commit();
599     print STDERR Dumper($r->{Divert});
600     return $r->{Divert};
601 }
602
603 sub get_divert ($) {
604     my ($r) = @_;
605     die "unchecked" unless exists $r->{Divert};
606     return $r->{Divert};
607 }
608
609 sub get_username ($) {
610     my ($r) = @_;
611     my $divert = $r->get_divert();
612     return undef if $divert;
613     return $r->{UserOK};
614 }
615
616 sub url_with_query_params ($$) {
617     my ($r, $params) = @_;
618     my $uri = URI->new($r->_ch('get_url'));
619     $uri->query_form(flatten_params($params));
620     return $uri->as_string();
621 }
622
623 sub check_ok ($) {
624     my ($r) = @_;
625
626     my ($divert) = $r->check_divert();
627     return 1 if !$divert;
628
629     my $handled = $r->_ch('handle_divert',$divert);
630     return 0 if $handled;
631
632     my $kind = $divert->{Kind};
633     my $cookieval = $divert->{CookieVal};
634     my $params = $divert->{Params};
635
636     if ($kind =~ m/^REDIRECT-/) {
637         # for redirects, we honour stored NextParams and SetCookie,
638         # as we would for non-divert
639         if ($kind eq 'REDIRECT-LOGGEDOUT') {
640             $params->{$r->{S}{loggedout_param_names}[0]} = 1;
641         } elsif ($kind eq 'REDIRECT-LOGOUT') {
642             $params->{$r->{S}{logout_param_names}[0]} = 1;
643         } elsif ($kind eq 'REDIRECT-LOGGEDIN') {
644         } else {
645             die;
646         }
647         my $new_url = $r->url_with_query_params($params);
648         my $cookie = $r->construct_cookie($r, $cookieval);
649         $r->_ch('do_redirect',$new_url, $cookie);
650         return 0;
651     }
652
653     my ($title, @body);
654     if ($kind =~ m/^LOGIN-/) {
655         $title = $r->_gt('Login');
656         push @body, $r->_gt($divert->{Message});
657         push @body, $r->_ch('gen_login_form', $params);
658     } elsif ($kind =~ m/^SMALLPAGE-/) {
659         $title = $r->_gt('Not logged in');
660         push @body, $r->_gt($divert->{Message});
661         push @body, $r->_ch('gen_login_link');
662     } else {
663         die $kind;
664     }
665
666     $r->_print($r->_ch('gen_start_html',$title),
667                @body,
668                $r->_ch('gen_end_html'));
669     return 0;
670 }
671
672 sub _random ($$) {
673     my ($r, $bytes) = @_;
674     my $v = $r->{V};
675     my $rsf = $v->{RandomHandle};
676     my $rsp = $r->{S}{random_source};
677     if (!$rsf) {
678         $v->{RandomHandle} = $rsf = new IO::File $rsp, '<' or die "$rsp $!";
679     }
680     my $bin;
681     $!=0;
682     read($rsf,$bin,$bytes) == $bytes or die "$rsp $!";
683     close $rsf;
684     return unpack "H*", $bin;
685 }
686
687 sub _fresh_cookie ($) {
688     my ($r) = @_;
689     my $bytes = ($r->{S}{associdlen} + 7) >> 3;
690     return $r->_random($bytes);
691 }
692
693 sub _assert_checked ($) {
694     my ($r) = @_;
695     die "unchecked" unless exists $r->{Divert};
696 }
697
698 sub check_mutate ($) {
699     my ($r) = @_;
700     $r->_assert_checked();
701     die if $r->{Divert};
702     my $meth = $r->_ch('get_method');
703     die "mutating non-POST" if $meth ne 'POST';
704 }
705
706 #---------- output ----------
707
708 sub secret_val ($) {
709     my ($r) = @_;
710     $r->_assert_checked();
711     return defined $r->{Assoc} ? $r->{Assoc} : '';
712 }
713
714 sub secret_hidden_html ($) {
715     my ($r) = @_;
716     return $r->{Cgi}->hidden(-name => $r->{S}{assoc_param_name},
717                              -default => $r->secret_val());
718 }
719
720 sub secret_cookie ($) {
721     my ($r) = @_;
722 #print STDERR "SC\n";
723     my $cookv = $r->construct_cookie($r->secret_val()); 
724 #print STDERR "SC=$cookv\n";
725     return $cookv;
726 }
727
728 __END__
729
730 =head1 NAME
731
732 CGI::Auth::Hybrid - web authentication optionally using cookies
733
734 =head1 SYNOPSYS
735
736  my $verifier = CGI::Auth::Hybrid->new_verifier(setting => value,...);
737  my $authreq = $verifier->new_request($cgi_request_object);
738
739  my $authreq = CGI::Auth::Hybrid->new_request($cgi_request_object,
740                                               setting => value,...);
741
742 =head1 USAGE PATTERN FOR SIMPLE APPLICATIONS
743
744  $authreq->check_ok() or return;
745
746  blah blah blah
747  $authreq->check_mutate();
748  blah blah blah
749
750 =head1 USAGE PATTERN FOR FANCY APPLICATIONS
751
752  my $divert_kind = $authreq->check_divert();
753  if ($divert_kind) {
754      if ($divert_kind eq 'LOGGEDOUT') {
755          print "goodbye you are now logged out" and quit
756      } elsif ($divert_kind eq 'NOCOOKIES') {
757          print "you need cookies" and quit
758      ... etc.
759      }
760  }
761
762  blah blah blah
763  $authreq->check_mutate();
764  blah blah blah