chiark / gitweb /
need_add_hidden: support use with the class rather than an object
[cgi-auth-flexible.git] / cgi-auth-flexible.pm
index 987170edabe2ab0cf00a418b6185c228cc1fc437..1b51142bfeab55c2e6883b111e0dbab1f4735209 100644 (file)
@@ -179,8 +179,9 @@ sub gen_plain_login_link ($$) {
 sub gen_srcdump_link_html ($$$$) {
     my ($c,$r,$anchor,$specval) = @_;
     my %params = ($r->{S}{srcdump_param_name} => [ $specval ]);
-    return '<a href="'.escapeHTML($r->url_with_query_params(\%params)).'">'.
-       $anchor."</a>";
+    return '<a href="'.
+       escapeHTML($r->url_with_query_params(\%params,'SRCDUMP')).
+       '">'.$anchor."</a>";
 }
 sub gen_plain_licence_link_html ($$) {
     my ($c,$r) = @_;
@@ -383,6 +384,7 @@ sub srcdump_dirscan_prepare ($$) {
        $dirsdone{$item}++;
     }
     close $reportfh or die $!;
+    srcdump_install($c,$v, $dumpdir, 'licence', 'text/plain');
     $!=0;
     my @cmd = (qw(tar -zvvcf), "$dumpdir/source.tmp",
               "-C", $dumpdir, qw(  --), @srcfiles);
@@ -392,7 +394,6 @@ sub srcdump_dirscan_prepare ($$) {
        die "tar failed";
     }
     die "licence file not found" unless defined $needlicence;
-    srcdump_install($c,$v, $dumpdir, 'licence', 'text/plain');
     srcdump_install($c,$v, $dumpdir, 'source', 'application/octet-stream');
     close $lockf or die $!;
 }
@@ -498,7 +499,9 @@ sub new_verifier {
     };
     my ($k,$v);
     while (($k,$v,@_) = @_) {
-       die "unknown setting $k" unless exists $verifier->{S}{$k};
+       die "unknown setting $k" unless
+           $k =~ m/^promise_/ or
+           exists $verifier->{S}{$k};
        $verifier->{S}{$k} = $v;
     }
     bless $verifier, $class;
@@ -664,9 +667,9 @@ my @ca = (-name => $r->{S}{cookie_name},
 
 # pages/param-sets are
 #   n normal non-mutating page
-#   r retrieval of information for JS, non-mutating
+#   r retrieval of information for JS etc., non-mutating
 #   m mutating page
-#   u update of information by JS, mutating
+#   u update of information by JS etc., mutating
 #   i login
 #   o logout
 #   O "you have just logged out" page load
@@ -903,6 +906,7 @@ sub _check_divert_core ($) {
         die unless $parmt eq 'y';
         die unless $cookh eq $parmh;
     }
+    $r->{ParmT} = $parmt;
     $r->{AssocSecret} = $cooks;
     $r->{UserOK} = $cooku;
 #print STDERR "C-D-C OK\n";
@@ -1032,12 +1036,16 @@ sub get_username ($) {
     return $r->{UserOK};
 }
 
-sub url_with_query_params ($$) {
-    my ($r, $params) = @_;
+sub url_with_query_params ($$;$) {
+    my ($r, $params, $nonpagetype) = @_;
 #print STDERR "PARAMS ",Dumper($params);
     my $uri = URI->new($r->_ch('get_url'));
     $uri->path($uri->path() . $params->{''}[0]) if $params->{''};
-    $uri->query_form(flatten_params($params));
+    my @flatparams = flatten_params($params);
+    if (defined $nonpagetype && $r->need_add_hidden('GET',$nonpagetype)) {
+       push @flatparams, $r->{S}{assoc_param_name}, $r->secret_hidden_val();
+    }
+    $uri->query_form(@flatparams);
     return $uri->as_string();
 }
 
@@ -1069,7 +1077,7 @@ sub check_ok ($) {
     }
 
     if ($kind =~ m/^REDIRECT-/) {
-       # for redirects, we honour stored NextParams and SetCookie,
+       # for redirects, we honour stored Params and Cookie,
        # as we would for non-divert
        if ($kind eq 'REDIRECT-LOGGEDOUT') {
            $params->{$r->{S}{loggedout_param_names}[0]} = [ 1 ];
@@ -1277,11 +1285,39 @@ sub check_mutate ($) {
     $r->_must_be_post();
 }
 
-sub mutate_ok ($) {
-    my ($r) = @_;
+our %_resource_get_needs_secret_hidden =
+    (map { $_ => 0 } qw(PAGE FRAME IFRAME SRCDUMP STYLESHEET FAVICON ROBOTS),
+     map { $_ => 1 } qw(IMAGE SCRIPT AJAX-XML AJAX-JSON AJAX-OTHER));
+
+sub update_get_need_add_hidden ($$;$) {
+    my ($r, $reqtype, $value, $force) = @_;
+    my $hash = ref $r
+       ? ($r->{GetNeedsSecretHidden} ||= { })
+       : \%_resource_get_needs_secret_hidden;
+    return if !$force &&
+       (exists $_resource_get_needs_secret_hidden{$reqtype} ||
+        exists $hash->{$reqtype});
+    $hash->{$reqtype} = $value;
+}
+
+sub need_add_hidden ($$) {
+    my ($r, $method, $reqtype) = @_;
+    return 1 if $method ne 'GET';
+    if (ref $r) {
+       my $ent = $r->{GetNeedsSecretHidden}{$reqtype};
+       return $ent if defined $ent;
+    }
+    my $ent = $_resource_get_needs_secret_hidden{$reqtype};
+    return $ent if defined $ent;
+    die "unsupported nonpage GET type $reqtype";
+}
+
+sub check_nonpage ($$) {
+    my ($r, $reqtype) = @_;
     $r->_assert_checked();
-    die if $r->{Divert};
-    return $r->_is_post();
+    return unless $r->resource_get_needs_secret_hidden($nonpagetype);
+    return if $r->{ParmT};
+    die "missing hidden secret parameter on nonpage request $nonpagetype";
 }
 
 #---------- output ----------
@@ -1315,33 +1351,3 @@ sub secret_cookie ($) {
 }
 
 1;
-
-__END__
-
-=head1 NAME
-
-CGI::Auth::Flexible - web authentication optionally using cookies
-
-=head1 SYNOPSYS - STARTUP
-
- my $verifier = CGI::Auth::Flexible->new_verifier(setting => value,...);
- my $authreq = $verifier->new_request($cgi_request_object);
-
-=head1 SYNOPSYS - SIMPLE APPLICATIONS
-
- $authreq->check_ok() or return;
- ...
- $authreq->check_mutate();
- ...
-
-=head1 SYNOPSIS - SOPHISTICATED APPLICATIONS
-
- my $divert_kind = $authreq->check_divert();
- if ($divert_kind) { ... print diversion page and quit ... }
- ...
- $authreq->check_mutate();
- ...
-
-=head1 DESCRIPTION
-
-CGI::Auth::Flexible is a