Move sort hooks to the IkiWiki::SortSpec namespace
[ikiwiki] / IkiWiki / Plugin / httpauth.pm
1 #!/usr/bin/perl
2 # HTTP basic auth plugin.
3 package IkiWiki::Plugin::httpauth;
4
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
8
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 }
17
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;
43
44         IkiWiki::redirect($cgi, 
45                 @_ > 1 ? IkiWiki::cgiurl(cgiurl => $config{cgiauthurl}, @_)
46                        : $config{cgiauthurl}."?@_"
47         );
48         exit;
49 }
50
51 sub auth ($$) {
52         my $cgi=shift;
53         my $session=shift;
54
55         if (defined $cgi->remote_user()) {
56                 $session->param("name", $cgi->remote_user());
57         }
58 }
59
60 sub formbuilder_setup (@) {
61         my %params=@_;
62
63         my $form=$params{form};
64         my $session=$params{session};
65         my $cgi=$params{cgi};
66         my $buttons=$params{buttons};
67
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;
72
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 }
80
81 sub test_httpauth_pagespec ($) {
82         my $page=shift;
83
84         return (
85        );
86 }
87
88 sub canedit ($$$) {
89         my $page=shift;
90         my $cgi=shift;
91         my $session=shift;
92
93         if (! defined $cgi->remote_user() &&
94             defined $config{httpauth_pagespec} &&
95             length $config{httpauth_pagespec} &&
96             defined $config{cgiauthurl} &&
97             pagespec_match($page, $config{httpauth_pagespec})) {
98                 return sub {
99                         # bounce thru cgiauthurl and back to edit action
100                         redir_cgiauthurl($cgi, $cgi->query_string());
101                 };
102         }
103         else {
104                 return undef;
105         }
106 }
107
108 1