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,
|
2010-02-12 04:24:15 +01:00
|
|
|
section => "auth",
|
2008-08-03 23:20:21 +02:00
|
|
|
},
|
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");
|
|
|
|
|
2010-02-11 23:26:09 +01:00
|
|
|
$form->field(
|
2006-11-20 21:37:27 +01:00
|
|
|
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");
|
2010-03-14 01:08:15 +01:00
|
|
|
$form->field(name => "email", type => "hidden");
|
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
|
|
|
|
2010-03-14 01:08:15 +01:00
|
|
|
# Ask for client to provide a name and email, if possible.
|
|
|
|
# Try sreg and ax
|
2010-03-24 20:32:35 +01:00
|
|
|
if ($claimed_identity->can("set_extension_args")) {
|
|
|
|
$claimed_identity->set_extension_args(
|
|
|
|
'http://openid.net/extensions/sreg/1.1',
|
|
|
|
{
|
|
|
|
optional => 'email,fullname,nickname',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
$claimed_identity->set_extension_args(
|
|
|
|
'http://openid.net/srv/ax/1.0',
|
|
|
|
{
|
|
|
|
mode => 'fetch_request',
|
|
|
|
'required' => 'email,fullname,nickname,firstname',
|
|
|
|
'type.email' => "http://schema.openid.net/contact/email",
|
|
|
|
'type.fullname' => "http://axschema.org/namePerson",
|
|
|
|
'type.nickname' => "http://axschema.org/namePerson/friendly",
|
|
|
|
'type.firstname' => "http://axschema.org/namePerson/first",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2010-03-14 01:08:15 +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);
|
2010-03-14 01:08:15 +01:00
|
|
|
|
2010-03-24 20:32:35 +01:00
|
|
|
my @extensions;
|
|
|
|
if ($vident->can("signed_extension_fields")) {
|
|
|
|
@extensions=grep { defined } (
|
|
|
|
$vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
|
|
|
|
$vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
|
|
|
|
);
|
|
|
|
}
|
2010-03-14 01:08:15 +01:00
|
|
|
foreach my $ext (@extensions) {
|
|
|
|
foreach my $field (qw{value.email email}) {
|
|
|
|
if (exists $ext->{$field} &&
|
|
|
|
defined $ext->{$field} &&
|
|
|
|
length $ext->{$field}) {
|
|
|
|
$session->param(email => $ext->{$field});
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
|
|
|
|
if (exists $ext->{$field} &&
|
|
|
|
defined $ext->{$field} &&
|
|
|
|
length $ext->{$field}) {
|
|
|
|
$session->param(username => $ext->{$field});
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-11-20 21:37:27 +01:00
|
|
|
}
|
|
|
|
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
|