]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/httpauth.pm
202ca11532e357322d1a14b2b285460c2b500f11
[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         hook(type => "pagetemplate", id => "httpauth", call => \&pagetemplate);
16 }
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;
43         IkiWiki::redirect($cgi, 
44                 IkiWiki::cgiurl(cgiurl => $config{cgiauthurl}, @_));
45         exit;
46 }
48 sub auth ($$) {
49         my $cgi=shift;
50         my $session=shift;
52         if (defined $cgi->remote_user()) {
53                 $session->param("name", $cgi->remote_user());
54         }
55 }
57 sub formbuilder_setup (@) {
58         my %params=@_;
60         my $form=$params{form};
61         my $session=$params{session};
62         my $cgi=$params{cgi};
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");
74                 }
75         }
76 }
78 sub test_httpauth_pagespec ($) {
79         my $page=shift;
81         return defined $config{httpauth_pagespec} &&
82                length $config{httpauth_pagespec} &&
83                defined $config{cgiauthurl} &&
84                pagespec_match($page, $config{httpauth_pagespec});
85 }
87 sub canedit ($$$) {
88         my $page=shift;
89         my $cgi=shift;
90         my $session=shift;
92         if (! defined $cgi->remote_user() && test_httpauth_pagespec($page)) {
93                 return sub {
94                         IkiWiki::redirect($cgi, 
95                                 $config{cgiauthurl}.'?'.$cgi->query_string());
96                         exit;
97                 };
98         }
99         else {
100                 return undef;
101         }
104 sub pagetemplate (@_) {
105         my %params=@_;
106         my $template=$params{template};
108         if ($template->param("editurl") &&
109             test_httpauth_pagespec($params{page})) {
110                 # go directly to cgiauthurl when editing a page matching
111                 # the pagespec
112                 $template->param(editurl => IkiWiki::cgiurl(
113                         cgiurl => $config{cgiauthurl},
114                         do => "edit", page => $params{page}));
115         }