<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
# HTTP basic auth plugin.
package IkiWiki::Plugin::httpauth;

use warnings;
use strict;
use IkiWiki 3.00;

sub import {
	hook(type =&gt; "getsetup", id =&gt; "httpauth", call =&gt; \&amp;getsetup);
	hook(type =&gt; "auth", id =&gt; "httpauth", call =&gt; \&amp;auth);
	hook(type =&gt; "formbuilder_setup", id =&gt; "httpauth",
		call =&gt; \&amp;formbuilder_setup);
	hook(type =&gt; "canedit", id =&gt; "httpauth", call =&gt; \&amp;canedit,
		first =&gt; 1);
}

sub getsetup () {
	return
		plugin =&gt; {
			safe =&gt; 1,
			rebuild =&gt; 0,
			section =&gt; "auth",
		},
		cgiauthurl =&gt; {
			type =&gt; "string",
			example =&gt; "http://example.com/wiki/auth/ikiwiki.cgi",
			description =&gt; "url to redirect to when authentication is needed",
			safe =&gt; 1,
			rebuild =&gt; 0,
		},
		httpauth_pagespec =&gt; {
			type =&gt; "pagespec",
			example =&gt; "!*/Discussion",
			description =&gt; "PageSpec of pages where only httpauth will be used for authentication",
			safe =&gt; 0,
			rebuild =&gt; 0,
		},
}
			
sub redir_cgiauthurl ($;@) {
	my $cgi=shift;

	IkiWiki::redirect($cgi, 
		@_ &gt; 1 ? IkiWiki::cgiurl(cgiurl =&gt; $config{cgiauthurl}, @_)
		       : $config{cgiauthurl}."?@_"
	);
	exit;
}

sub auth ($$) {
	my $cgi=shift;
	my $session=shift;

	if (defined $cgi-&gt;remote_user()) {
		$session-&gt;param("name", $cgi-&gt;remote_user());
	}
}

sub formbuilder_setup (@) {
	my %params=@_;

	my $form=$params{form};
	my $session=$params{session};
	my $cgi=$params{cgi};
	my $buttons=$params{buttons};

	if ($form-&gt;title eq "signin" &amp;&amp;
	    ! defined $cgi-&gt;remote_user() &amp;&amp; defined $config{cgiauthurl}) {
		my $button_text="Login with HTTP auth";
		push @$buttons, $button_text;

		if ($form-&gt;submitted &amp;&amp; $form-&gt;submitted eq $button_text) {
			# bounce thru cgiauthurl and then back to
			# the stored postsignin action
			redir_cgiauthurl($cgi, do =&gt; "postsignin");
		}
	}
}

sub canedit ($$$) {
	my $page=shift;
	my $cgi=shift;
	my $session=shift;

	if (! defined $cgi-&gt;remote_user() &amp;&amp;
	    (! defined $session-&gt;param("name") ||
             ! IkiWiki::userinfo_get($session-&gt;param("name"), "regdate")) &amp;&amp;
	    defined $config{httpauth_pagespec} &amp;&amp;
	    length $config{httpauth_pagespec} &amp;&amp;
	    defined $config{cgiauthurl} &amp;&amp;
	    pagespec_match($page, $config{httpauth_pagespec})) {
		return sub {
			# bounce thru cgiauthurl and back to edit action
			redir_cgiauthurl($cgi, $cgi-&gt;query_string());
		};
	}
	else {
		return undef;
	}
}

1
</pre></body></html>