2 # HTTP basic auth plugin.
3 package IkiWiki::Plugin::httpauth;
10 hook(type => "checkconfig", id => "httpauth", call => \&checkconfig);
11 hook(type => "getsetup", id => "httpauth", call => \&getsetup);
12 hook(type => "auth", id => "httpauth", call => \&auth);
13 hook(type => "formbuilder_setup", id => "httpauth",
14 call => \&formbuilder_setup);
15 hook(type => "canedit", id => "httpauth", call => \&canedit,
28 example => "http://example.com/wiki/auth/ikiwiki.cgi",
29 description => "url to redirect to when authentication is needed",
33 httpauth_pagespec => {
35 example => "!*/Discussion",
36 description => "PageSpec of pages where only httpauth will be used for authentication",
43 if ($config{cgi} && defined $config{cgiauthurl} &&
44 keys %{$IkiWiki::hooks{auth}} < 2) {
45 # There are no other auth hooks registered, so avoid
46 # the normal signin form, and jump right to httpauth.
48 inject(name => "IkiWiki::cgi_signin", call => sub ($$) {
50 redir_cgiauthurl($cgi, $cgi->query_string());
55 sub redir_cgiauthurl ($;@) {
58 IkiWiki::redirect($cgi,
59 @_ > 1 ? IkiWiki::cgiurl(cgiurl => $config{cgiauthurl}, @_)
60 : $config{cgiauthurl}."?@_"
69 if (length $cgi->remote_user()) {
70 $session->param("name", $cgi->remote_user());
74 sub formbuilder_setup (@) {
77 my $form=$params{form};
78 my $session=$params{session};
80 my $buttons=$params{buttons};
82 if ($form->title eq "signin" &&
83 ! length $cgi->remote_user() && defined $config{cgiauthurl}) {
84 my $button_text="Login with HTTP auth";
85 push @$buttons, $button_text;
87 if ($form->submitted && $form->submitted eq $button_text) {
88 # bounce thru cgiauthurl and then back to
89 # the stored postsignin action
90 redir_cgiauthurl($cgi, do => "postsignin");
100 if (! length $cgi->remote_user() &&
101 (! defined $session->param("name") ||
102 ! IkiWiki::userinfo_get($session->param("name"), "regdate")) &&
103 defined $config{httpauth_pagespec} &&
104 length $config{httpauth_pagespec} &&
105 defined $config{cgiauthurl} &&
106 pagespec_match($page, $config{httpauth_pagespec})) {
108 # bounce thru cgiauthurl and back to edit action
109 redir_cgiauthurl($cgi, $cgi->query_string());