httpauth: When cgiauthurl is configured, httpauth can now be used alongside other...
[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 => "canedit", id => "httpauth", call => \&canedit,
13                 last => 1);
14         hook(type => "formbuilder_setup", id => "httpauth",
15                 call => \&formbuilder_setup);
16 }
17
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 }
32                         
33 sub redir_cgiauthurl ($$) {
34         my $cgi=shift;
35         my $params=shift;
36
37         IkiWiki::redirect($cgi, $config{cgiauthurl}.'?'.$params);
38         exit;
39 }
40
41 sub auth ($$) {
42         my $cgi=shift;
43         my $session=shift;
44
45         if (defined $cgi->remote_user()) {
46                 $session->param("name", $cgi->remote_user());
47         }
48 }
49
50 sub canedit ($$$) {
51         my $page=shift;
52         my $cgi=shift;
53         my $session=shift;
54
55         if (! defined $cgi->remote_user() && defined $config{cgiauthurl}) {
56                 return sub { redir_cgiauthurl($cgi, $cgi->query_string()) };
57         }
58         else {
59                 return undef;
60         }
61 }
62
63 sub formbuilder_setup (@) {
64         my %params=@_;
65
66         my $form=$params{form};
67         my $session=$params{session};
68         my $cgi=$params{cgi};
69         my $buttons=$params{buttons};
70
71         if ($form->title eq "signin" &&
72             ! defined $cgi->remote_user() && defined $config{cgiauthurl}) {
73                 my $button_text="Login with HTTP auth";
74                 push @$buttons, $button_text;
75
76                 if ($form->submitted && $form->submitted eq $button_text) {
77                         redir_cgiauthurl($cgi, "do=postsignin");
78                         exit;
79                 }
80         }
81 }
82
83 1