fix logic error
[ikiwiki.git] / IkiWiki / Plugin / httpauth.pm
1 #!/usr/bin/perl
2 # HTTP basic auth plugin.
3 package IkiWiki::Plugin::httpauth;
4
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
8
9 sub import {
10         hook(type => "getsetup", id => "httpauth", call => \&getsetup);
11         hook(type => "auth", id => "httpauth", call => \&auth);
12         hook(type => "formbuilder_setup", id => "httpauth",
13                 call => \&formbuilder_setup);
14         hook(type => "canedit", id => "httpauth", call => \&canedit);
15         hook(type => "pagetemplate", id => "httpauth", call => \&pagetemplate);
16 }
17
18 sub getsetup () {
19         return
20                 plugin => {
21                         safe => 1,
22                         rebuild => 0,
23                 },
24                 cgiauthurl => {
25                         type => "string",
26                         example => "http://example.com/wiki/auth/ikiwiki.cgi",
27                         description => "url to redirect to when authentication is needed",
28                         safe => 1,
29                         rebuild => 0,
30                 },
31                 httpauth_pagespec => {
32                         type => "pagespec",
33                         example => "!*/Discussion",
34                         description => "PageSpec of pages where only httpauth will be used for authentication",
35                         safe => 0,
36                         rebuild => 0,
37                 },
38 }
39                         
40 sub redir_cgiauthurl ($;@) {
41         my $cgi=shift;
42
43         IkiWiki::redirect($cgi, 
44                 IkiWiki::cgiurl(cgiurl => $config{cgiauthurl}, @_));
45         exit;
46 }
47
48 sub auth ($$) {
49         my $cgi=shift;
50         my $session=shift;
51
52         if (defined $cgi->remote_user()) {
53                 $session->param("name", $cgi->remote_user());
54         }
55 }
56
57 sub formbuilder_setup (@) {
58         my %params=@_;
59
60         my $form=$params{form};
61         my $session=$params{session};
62         my $cgi=$params{cgi};
63         my $buttons=$params{buttons};
64
65         if ($form->title eq "signin" &&
66             ! defined $cgi->remote_user() && defined $config{cgiauthurl}) {
67                 my $button_text="Login with HTTP auth";
68                 push @$buttons, $button_text;
69
70                 if ($form->submitted && $form->submitted eq $button_text) {
71                         # bounce thru cgiauthurl and then back to
72                         # the stored postsignin action
73                         redir_cgiauthurl($cgi, do => "postsignin");
74                 }
75         }
76 }
77
78 sub need_httpauth_pagespec () {
79         return defined $config{httpauth_pagespec} &&
80                length $config{httpauth_pagespec} &&
81                defined $config{cgiauthurl};
82 }
83
84 sub test_httpauth_pagespec ($) {
85         my $page=shift;
86
87         pagespec_match($page, $config{httpauth_pagespec});
88 }
89
90 sub canedit ($$$) {
91         my $page=shift;
92         my $cgi=shift;
93         my $session=shift;
94
95         if (! defined $cgi->remote_user() &&
96             need_httpauth_pagespec() &&
97             ! test_httpauth_pagespec($page)) {
98                 return sub {
99                         IkiWiki::redirect($cgi, 
100                                 $config{cgiauthurl}.'?'.$cgi->query_string());
101                         exit;
102                 };
103         }
104         else {
105                 return undef;
106         }
107 }
108
109 sub pagetemplate (@_) {
110         my %params=@_;
111         my $template=$params{template};
112
113         if ($template->param("editurl") &&
114             need_httpauth_pagespec() &&
115             test_httpauth_pagespec($params{page})) {
116                 # go directly to cgiauthurl when editing a page matching
117                 # the pagespec
118                 $template->param(editurl => IkiWiki::cgiurl(
119                         cgiurl => $config{cgiauthurl},
120                         do => "edit", page => $params{page}));
121         }
122 }
123
124 1