]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/httpauth.pm
Merge branch 'master' of git://git.ikiwiki.info
[git.ikiwiki.info.git] / IkiWiki / Plugin / httpauth.pm
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);
16 }
18 sub getsetup () {
19         return
20                 plugin => {
21                         safe => 1,
22                         rebuild => 0,
23                         section => "auth",
24                 },
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,
31                 },
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,
38                 },
39 }
40                         
41 sub redir_cgiauthurl ($;@) {
42         my $cgi=shift;
44         IkiWiki::redirect($cgi, 
45                 @_ > 1 ? IkiWiki::cgiurl(cgiurl => $config{cgiauthurl}, @_)
46                        : $config{cgiauthurl}."?@_"
47         );
48         exit;
49 }
51 sub auth ($$) {
52         my $cgi=shift;
53         my $session=shift;
55         if (defined $cgi->remote_user()) {
56                 $session->param("name", $cgi->remote_user());
57         }
58 }
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");
77                 }
78         }
79 }
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());
96                 };
97         }
98         else {
99                 return undef;
100         }