]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/httpauth.pm
d0d4da0b74e6b7925ae8b85b6ef43d3cb6cff36c
[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 => "canedit", id => "httpauth", call => \&canedit,
13                 last => 1);
14         hook(type => "formbuilder_setup", id => "httpauth",
15                 call => \&formbuilder_setup);
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 }
32                         
33 sub redir_cgiauthurl ($$) {
34         my $cgi=shift;
35         my $params=shift;
37         IkiWiki::redirect($cgi, $config{cgiauthurl}.'?'.$params);
38         exit;
39 }
41 sub auth ($$) {
42         my $cgi=shift;
43         my $session=shift;
45         if (defined $cgi->remote_user()) {
46                 $session->param("name", $cgi->remote_user());
47         }
48 }
50 sub canedit ($$$) {
51         my $page=shift;
52         my $cgi=shift;
53         my $session=shift;
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 }
63 sub formbuilder_setup (@) {
64         my %params=@_;
66         my $form=$params{form};
67         my $session=$params{session};
68         my $cgi=$params{cgi};
69         my $buttons=$params{buttons};
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;
76                 if ($form->submitted && $form->submitted eq $button_text) {
77                         redir_cgiauthurl($cgi, "do=postsignin");
78                         exit;
79                 }
80         }
81 }
83 1