2 # HTTP basic auth plugin.
3 package IkiWiki::Plugin::httpauth;
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);
26 example => "http://example.com/wiki/auth/ikiwiki.cgi",
27 description => "url to redirect to when authentication is needed",
31 httpauth_pagespec => {
33 example => "!*/Discussion",
34 description => "PageSpec of pages where only httpauth will be used for authentication",
40 sub redir_cgiauthurl ($;@) {
43 IkiWiki::redirect($cgi,
44 IkiWiki::cgiurl(cgiurl => $config{cgiauthurl}, @_));
52 if (defined $cgi->remote_user()) {
53 $session->param("name", $cgi->remote_user());
57 sub formbuilder_setup (@) {
60 my $form=$params{form};
61 my $session=$params{session};
63 my $buttons=$params{buttons};
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;
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");
78 sub need_httpauth_pagespec () {
79 return defined $config{httpauth_pagespec} &&
80 length $config{httpauth_pagespec} &&
81 defined $config{cgiauthurl};
84 sub test_httpauth_pagespec ($) {
87 pagespec_match($page, $config{httpauth_pagespec});
95 if (! defined $cgi->remote_user() &&
96 need_httpauth_pagespec() &&
97 ! test_httpauth_pagespec($page)) {
99 IkiWiki::redirect($cgi,
100 $config{cgiauthurl}.'?'.$cgi->query_string());
109 sub pagetemplate (@_) {
111 my $template=$params{template};
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
118 $template->param(editurl => IkiWiki::cgiurl(
119 cgiurl => $config{cgiauthurl},
120 do => "edit", page => $params{page}));