2006-11-20 02:52:18 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# OpenID support.
|
|
|
|
package IkiWiki::Plugin::openid;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.00;
|
2006-11-20 02:52:18 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2006-11-20 10:40:09 +01:00
|
|
|
hook(type => "getopt", id => "openid", call => \&getopt);
|
2008-07-26 00:05:55 +02:00
|
|
|
hook(type => "getsetup", id => "openid", call => \&getsetup);
|
2006-11-20 10:40:09 +01:00
|
|
|
hook(type => "auth", id => "openid", call => \&auth);
|
2006-11-20 21:37:27 +01:00
|
|
|
hook(type => "formbuilder_setup", id => "openid",
|
|
|
|
call => \&formbuilder_setup, last => 1);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-11-20 02:52:18 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getopt () {
|
2006-11-20 10:40:09 +01:00
|
|
|
eval q{use Getopt::Long};
|
|
|
|
error($@) if $@;
|
|
|
|
Getopt::Long::Configure('pass_through');
|
|
|
|
GetOptions("openidsignup=s" => \$config{openidsignup});
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-11-20 10:40:09 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-07-26 00:05:55 +02:00
|
|
|
return
|
2008-08-03 23:20:21 +02:00
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
2008-07-26 00:05:55 +02:00
|
|
|
openidsignup => {
|
|
|
|
type => "string",
|
|
|
|
example => "http://myopenid.com/",
|
|
|
|
description => "an url where users can signup for an OpenID",
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-26 00:05:55 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub formbuilder_setup (@) {
|
2006-11-20 21:37:27 +01:00
|
|
|
my %params=@_;
|
|
|
|
|
|
|
|
my $form=$params{form};
|
|
|
|
my $session=$params{session};
|
|
|
|
my $cgi=$params{cgi};
|
2007-04-30 06:41:53 +02:00
|
|
|
|
2006-11-20 21:37:27 +01:00
|
|
|
if ($form->title eq "signin") {
|
2008-03-20 02:12:18 +01:00
|
|
|
# Give up if module is unavailable to avoid
|
|
|
|
# needing to depend on it.
|
|
|
|
eval q{use Net::OpenID::Consumer};
|
|
|
|
if ($@) {
|
2008-09-23 18:59:43 +02:00
|
|
|
debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
|
2008-03-20 02:12:18 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-04-30 05:49:35 +02:00
|
|
|
# This avoids it displaying a redundant label for the
|
|
|
|
# OpenID fieldset.
|
|
|
|
$form->fieldsets("OpenID");
|
|
|
|
|
2006-11-20 21:37:27 +01:00
|
|
|
$form->field(
|
|
|
|
name => "openid_url",
|
2007-12-08 21:59:08 +01:00
|
|
|
label => gettext("Log in with")." ".htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
|
2007-04-30 05:49:35 +02:00
|
|
|
fieldset => "OpenID",
|
2006-11-20 21:37:27 +01:00
|
|
|
size => 30,
|
2007-04-30 00:19:37 +02:00
|
|
|
comment => ($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">".gettext("Get an OpenID")."</a>" : "")
|
2006-11-20 21:37:27 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
# Handle submission of an OpenID as validation.
|
|
|
|
if ($form->submitted && $form->submitted eq "Login" &&
|
|
|
|
defined $form->field("openid_url") &&
|
|
|
|
length $form->field("openid_url")) {
|
|
|
|
$form->field(
|
|
|
|
name => "openid_url",
|
|
|
|
validate => sub {
|
|
|
|
validate($cgi, $session, shift, $form);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
# Skip all other required fields in this case.
|
|
|
|
foreach my $field ($form->field) {
|
|
|
|
next if $field eq "openid_url";
|
|
|
|
$form->field(name => $field, required => 0,
|
|
|
|
validate => '/.*/');
|
|
|
|
}
|
2006-11-20 02:52:18 +01:00
|
|
|
}
|
|
|
|
}
|
2010-02-04 21:07:10 +01:00
|
|
|
elsif ($form->title eq "preferences" &&
|
|
|
|
IkiWiki::openiduser($session->param("name"))) {
|
|
|
|
$form->field(name => "openid_url", disabled => 1,
|
|
|
|
label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
|
|
|
|
value => $session->param("name"),
|
|
|
|
size => 50, force => 1,
|
|
|
|
fieldset => "login");
|
2006-11-21 02:40:47 +01:00
|
|
|
}
|
2006-11-20 21:37:27 +01:00
|
|
|
}
|
2006-11-20 02:52:18 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub validate ($$$;$) {
|
2006-11-20 02:52:18 +01:00
|
|
|
my $q=shift;
|
|
|
|
my $session=shift;
|
|
|
|
my $openid_url=shift;
|
2006-11-20 10:40:09 +01:00
|
|
|
my $form=shift;
|
2006-11-20 02:52:18 +01:00
|
|
|
|
|
|
|
my $csr=getobj($q, $session);
|
|
|
|
|
|
|
|
my $claimed_identity = $csr->claimed_identity($openid_url);
|
|
|
|
if (! $claimed_identity) {
|
2006-11-20 10:40:09 +01:00
|
|
|
if ($form) {
|
|
|
|
# Put the error in the form and fail validation.
|
|
|
|
$form->field(name => "openid_url", comment => $csr->err);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error($csr->err);
|
|
|
|
}
|
2006-11-20 02:52:18 +01:00
|
|
|
}
|
2006-11-20 10:40:09 +01:00
|
|
|
|
2006-11-20 02:52:18 +01:00
|
|
|
my $check_url = $claimed_identity->check_url(
|
2006-11-20 10:40:09 +01:00
|
|
|
return_to => IkiWiki::cgiurl(do => "postsignin"),
|
2006-11-20 02:52:18 +01:00
|
|
|
trust_root => $config{cgiurl},
|
|
|
|
delayed_return => 1,
|
|
|
|
);
|
|
|
|
# Redirect the user to the OpenID server, which will
|
2006-11-20 21:37:27 +01:00
|
|
|
# eventually bounce them back to auth()
|
2006-11-20 02:52:18 +01:00
|
|
|
IkiWiki::redirect($q, $check_url);
|
|
|
|
exit 0;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-11-20 02:52:18 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub auth ($$) {
|
2006-11-20 21:37:27 +01:00
|
|
|
my $q=shift;
|
|
|
|
my $session=shift;
|
|
|
|
|
|
|
|
if (defined $q->param('openid.mode')) {
|
|
|
|
my $csr=getobj($q, $session);
|
|
|
|
|
|
|
|
if (my $setup_url = $csr->user_setup_url) {
|
|
|
|
IkiWiki::redirect($q, $setup_url);
|
|
|
|
}
|
|
|
|
elsif ($csr->user_cancel) {
|
|
|
|
IkiWiki::redirect($q, $config{url});
|
|
|
|
}
|
|
|
|
elsif (my $vident = $csr->verified_identity) {
|
|
|
|
$session->param(name => $vident->url);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error("OpenID failure: ".$csr->err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif (defined $q->param('openid_identifier')) {
|
|
|
|
# myopenid.com affiliate support
|
|
|
|
validate($q, $session, $q->param('openid_identifier'));
|
|
|
|
}
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-11-20 21:37:27 +01:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getobj ($$) {
|
2006-11-20 02:52:18 +01:00
|
|
|
my $q=shift;
|
|
|
|
my $session=shift;
|
|
|
|
|
|
|
|
eval q{use Net::OpenID::Consumer};
|
|
|
|
error($@) if $@;
|
|
|
|
|
|
|
|
my $ua;
|
|
|
|
eval q{use LWPx::ParanoidAgent};
|
|
|
|
if (! $@) {
|
|
|
|
$ua=LWPx::ParanoidAgent->new;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$ua=LWP::UserAgent->new;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Store the secret in the session.
|
|
|
|
my $secret=$session->param("openid_secret");
|
|
|
|
if (! defined $secret) {
|
2007-01-30 08:21:06 +01:00
|
|
|
$secret=rand;
|
|
|
|
$session->param(openid_secret => $secret);
|
2006-11-20 02:52:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Net::OpenID::Consumer->new(
|
|
|
|
ua => $ua,
|
|
|
|
args => $q,
|
2007-01-30 08:21:06 +01:00
|
|
|
consumer_secret => sub { return shift()+$secret },
|
2006-11-20 02:52:18 +01:00
|
|
|
required_root => $config{cgiurl},
|
|
|
|
);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-11-20 02:52:18 +01:00
|
|
|
|
|
|
|
1
|