clear sandbox/test commit
[ikiwiki.git] / IkiWiki / Plugin / httpauth.pm
blobcb488449dd680b2ae36aefde23636ba3fdfe6ab8
1 #!/usr/bin/perl
2 # HTTP basic auth plugin.
3 package IkiWiki::Plugin::httpauth;
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
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 first => 1);
18 sub getsetup () {
19 return
20 plugin => {
21 safe => 1,
22 rebuild => 0,
23 section => "auth",
25 cgiauthurl => {
26 type => "string",
27 example => "http://example.com/wiki/auth/ikiwiki.cgi",
28 description => "url to redirect to when authentication is needed",
29 safe => 1,
30 rebuild => 0,
32 httpauth_pagespec => {
33 type => "pagespec",
34 example => "!*/Discussion",
35 description => "PageSpec of pages where only httpauth will be used for authentication",
36 safe => 0,
37 rebuild => 0,
41 sub redir_cgiauthurl ($;@) {
42 my $cgi=shift;
44 IkiWiki::redirect($cgi,
45 @_ > 1 ? IkiWiki::cgiurl(cgiurl => $config{cgiauthurl}, @_)
46 : $config{cgiauthurl}."?@_"
48 exit;
51 sub auth ($$) {
52 my $cgi=shift;
53 my $session=shift;
55 if (defined $cgi->remote_user()) {
56 $session->param("name", $cgi->remote_user());
60 sub formbuilder_setup (@) {
61 my %params=@_;
63 my $form=$params{form};
64 my $session=$params{session};
65 my $cgi=$params{cgi};
66 my $buttons=$params{buttons};
68 if ($form->title eq "signin" &&
69 ! defined $cgi->remote_user() && defined $config{cgiauthurl}) {
70 my $button_text="Login with HTTP auth";
71 push @$buttons, $button_text;
73 if ($form->submitted && $form->submitted eq $button_text) {
74 # bounce thru cgiauthurl and then back to
75 # the stored postsignin action
76 redir_cgiauthurl($cgi, do => "postsignin");
81 sub canedit ($$$) {
82 my $page=shift;
83 my $cgi=shift;
84 my $session=shift;
86 if (! defined $cgi->remote_user() &&
87 (! defined $session->param("name") ||
88 ! IkiWiki::userinfo_get($session->param("name"), "regdate")) &&
89 defined $config{httpauth_pagespec} &&
90 length $config{httpauth_pagespec} &&
91 defined $config{cgiauthurl} &&
92 pagespec_match($page, $config{httpauth_pagespec})) {
93 return sub {
94 # bounce thru cgiauthurl and back to edit action
95 redir_cgiauthurl($cgi, $cgi->query_string());
98 else {
99 return undef;