$r->{A} = $nassoc;
}
-sub _check ($) {
+sub _check_core ($) {
my ($r) = @_;
my $qassoc = $r->_cm('get_param');
my ($nassoc,$nmutate);
$nmutate = 0;
}
}
+
+# pages/param-sets are
+# n normal non-mutating page
+# r retrieval of information for JS, non-mutating
+# m mutating page
+# u update of information by JS, mutating
+# i login
+# o logout
+
+ # Case analysis, cookie mode, app promises re mutate:
+ # cook par meth form
+ #
+ # y y GET nr fine, show page or send data
+ #
+ # y - GET n cross-site link
+ # show data
+ # y1 y2 GET n intra-site link
+ # from session no longer known to browser
+ # do not revoke y2 as not a POST so not RESTful
+ # treat as cross-site link, show data
+ # y n GET n intra-site link from stale session,
+ # treat as cross-site link, show data
+ #
+ # y - GET r bug or attack, fail
+ # y1 y2 GET r intra-site data request
+ # from session no longer known to browser
+ # do not revoke y2 as not a POST so not RESTful
+ # fail
+ # y n GET r intra-site data request from stale session
+ # fail
+ #
+ # - y GET n CLEAR COOKIES TO LOGOUT OPTION
+ #
+ # -/n any GET n cross-site link but user not logged in
+ # show login form
+ #
+ # -/n any GET r data request from stale session
+ # fail
+ #
+ # any any GET muoi bug or attack, fail
+ #
+ # y y POST nrmu mutation is OK, do operation
+ # y y POST o logout
+ # y y POST i login, redirect to GET of remaining params
+ #
+ # any - POST bug or xsrf attack, fail
+ #
+ # n/y1 y2 POST r intra-site form submission
+ # from session no longer known to browser
+ # revoke y2
+ # show "session interrupted"
+ # n/y1 y2 POST m intra-site js operation
+ # from session no longer known to browser
+ # revoke y2
+ # fail
+ # y n POST r intra-site form submission from stale session
+ # show "session interrupted"
+ # y n POST m intra-site form submission from stale session
+ # fail
+ #
+ # - y2 GET intra-site link or data request
+ # from session no longer known to browser
+ # fail
+
+ # - y2 any req from old session y2, del y2, show login
+ # y1 y2 any req from old session y2, del y2, show y1 main
+
+ # y1 any GET non-mut cross-site link
+ # y1 any GET non-mut no mutation, show page providing new par
+ #
+ # A B any page from old session or cross-site request
+ # any y2 POST logout do logout y2
+
+
+ # any any GET mutate bug or attack, forbidden, call die
+
my $dbh = $r->{Dbh};
my ($nusername, $nlast) =
$dbh->selectrow_array("SELECT username, last".
return ($nusername, $nassoc, $nmutate);
}
-sub check ($) {
+sub _check ($) {
my ($r) = @_;
- my ($nusername, $nassoc, $nmutate) = $r->_check() or return undef;
+ return if exists $r->{Username};
+ ($r->{Username}, $r->{Assoc}, $r->{Mutate}) = $r->_check();
- $dbh->do("UPDATE $r->{S}{assocdb_table}".
- " SET last = ?".
- " WHERE associd = ?", {}, time, $nassoc);
- $dbh->do("COMMIT");
-
- $r->{Username} = $nusername;
- $r->{Assoc} = $nassoc;
- $r->{Mutate} = $nmutate;
- return $nusername;
-}
-
-sub check_mutate ($) {
- my ($r) = @_;
+ if (defined $r->{Assoc}) {
+ $dbh->do("UPDATE $r->{S}{assocdb_table}".
+ " SET last = ?".
+ " WHERE associd = ?", {}, time, $nassoc);
+ $dbh->do("COMMIT");
+ }
}
sub logout ($) {
return $nusername;
}
+sub check ($) {
+ my ($r) = @_;
+ $r->_check();
+ return !!defined $r->{Username};
+}
+
+sub check_mutate ($) {
+ my ($r) = @_;
+ $r->check();
+ return $r->{Mutate};
+}
+
sub username ($) {
my ($r) = @_;
+ $r->check();
return $r->{Username};
-sub hiddenv ($) {
+sub hidden_val ($) {
my ($r) = @_;
- return defined $r->{A} ? $r->{A} : '';
+ $r->check();
+ return defined $r->{Assoc} ? $r->{Assoc} : '';
}
-sub hiddena ($) {
+#---------- simple wrappers ----------
+
+sub hidden_hargs ($) {
my ($r) = @_;
- return (-name => $r->{param_name},
- -default => $r->hiddenv());
+ return (-name => $r->{S}{param_name},
+ -default => $r->hidden_val());
}
-sub hiddenh ($) {
+sub hidden_html ($) {
my ($r) = @_;
- return hidden($r->hiddena());
+ return hidden($r->hidden_hargs());
}
-sub cookieac ($) {
+sub cookiea_cargs ($) {
my ($r) = @_;
- return (-name => $r->{cookie_name},
- -value => hiddenv());
-
+ return (-name => $r->{S}{cookie_name},
+ -value => hidden_val());
+}
+
+__END__
+
+=head1 NAME
+
+CGI::Auth::Hybrid - web authentication optionally using cookies
+
+=head1 SYNOPSYS
+
+ my $verifier = CGI::Auth::Hybrid->new_verifier(setting => value,...);
+ my $authreq = $verifier->new_request($cgi_request_object);
+
+ my $authreq = CGI::Auth::Hybrid->new_request($cgi_request_object,
+ setting => value,...);
+
+=head1 USAGE PATTERN FOR SIMPLE APPLICATIONS
+
+ if ( form submission is login request ) {
+ check login details, if wrong print error and quit
+ $authreq->record_login(...username...);
+ }
+ if ( form submission is logout request ) {
+ my $logged_out_user = $authreq->logout();
+ if (!defined $logged_out_user) {
+ print "you are not logged in" error and quit
+ } else {
+ print "goodbye $username you are now logged out" and quit
+ }
+ }
+ if ( !$authreq->check() ) {
+ display login form, quit
+
+
+=head1 USAGE PATTERN FOR FANCY APPLICATIONS
+
+ if ( form submission is login request ) {
+ check login details, if wrong print error and quit
+ $authreq->record_login(...username...);
+ }
+ if ( !$authreq->check() ) {
+ display login form, quit
+ if ( form submission is logout request ) {
+ die unless $authreq->mutate();
+ my $logged_out_user = $authreq->logout();
+ if (!defined $logged_out_user) {
+ print "you are not logged in" error and quit
+ } else {
+ print "goodbye $username you are now logged out" and quit
+ }
+ }
+
+
+advantages of cookie
+ - user can sort of log out by clearing cookies
+ - sophisticated applications can have get-requests