chiark / gitweb /
wip
[cgi-auth-flexible.git] / cgi-auth-hybrid.pm
index 2993bc239c54ddb0a4cb78731a726bc52e9702cc..f7b48aa1ef4ccbd96ff156a66036202196349221 100644 (file)
@@ -136,7 +136,7 @@ sub record_login ($$) {
     $r->{A} = $nassoc;
 }
 
-sub _check ($) {
+sub _check_core ($) {
     my ($r) = @_;
     my $qassoc = $r->_cm('get_param');
     my ($nassoc,$nmutate);
@@ -160,6 +160,82 @@ sub _check ($) {
            $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".
@@ -173,24 +249,18 @@ sub _check ($) {
     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 ($) {
@@ -204,15 +274,31 @@ 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 hidden_val ($) {
     my ($r) = @_;
+    $r->check();
     return defined $r->{Assoc} ? $r->{Assoc} : '';
 }
 
+#---------- simple wrappers ----------
+
 sub hidden_hargs ($) {
     my ($r) = @_;
     return (-name => $r->{S}{param_name},
@@ -229,3 +315,58 @@ sub cookiea_cargs ($) {
     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