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 {
|
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);
|
2015-05-27 09:52:01 +02:00
|
|
|
IkiWiki::loadplugin("emailauth");
|
2015-05-14 00:37:24 +02:00
|
|
|
IkiWiki::loadplugin("loginselector");
|
|
|
|
IkiWiki::Plugin::loginselector::register_login_plugin(
|
|
|
|
"openid",
|
|
|
|
\&openid_setup,
|
|
|
|
\&openid_check_input,
|
|
|
|
\&openid_auth,
|
|
|
|
);
|
2010-05-08 01:10:50 +02: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
|
|
|
},
|
2010-06-11 19:53:56 +02:00
|
|
|
openid_realm => {
|
|
|
|
type => "string",
|
2010-06-11 20:14:20 +02:00
|
|
|
description => "url pattern of openid realm (default is cgiurl)",
|
2010-06-11 19:53:56 +02:00
|
|
|
safe => 0,
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
|
|
|
openid_cgiurl => {
|
|
|
|
type => "string",
|
|
|
|
description => "url to ikiwiki cgi to use for openid authentication (default is cgiurl)",
|
|
|
|
safe => 0,
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-07-26 00:05:55 +02:00
|
|
|
|
2015-05-14 00:37:24 +02:00
|
|
|
sub openid_setup ($$) {
|
|
|
|
my $q=shift;
|
|
|
|
my $template=shift;
|
2010-05-08 01:10:50 +02:00
|
|
|
|
2015-05-14 00:37:24 +02:00
|
|
|
if (load_openid_module()) {
|
|
|
|
my $openid_url=$q->param('openid_identifier');
|
|
|
|
if (defined $openid_url) {
|
|
|
|
$template->param(openid_url => $openid_url);
|
2010-05-08 02:14:25 +02:00
|
|
|
}
|
2015-05-14 00:37:24 +02:00
|
|
|
return 1;
|
2010-05-08 02:14:25 +02:00
|
|
|
}
|
2015-05-14 00:37:24 +02:00
|
|
|
else {
|
|
|
|
return 0;
|
2010-05-08 01:10:50 +02:00
|
|
|
}
|
2015-05-14 00:37:24 +02:00
|
|
|
}
|
2010-05-08 01:10:50 +02:00
|
|
|
|
2015-05-14 00:37:24 +02:00
|
|
|
sub openid_check_input ($) {
|
|
|
|
my $q=shift;
|
|
|
|
my $openid_url=$q->param('openid_identifier');
|
|
|
|
defined $q->param("action") && $q->param("action") eq "verify" && defined $openid_url && length $openid_url;
|
|
|
|
}
|
2010-05-08 01:10:50 +02:00
|
|
|
|
2015-05-14 04:27:03 +02:00
|
|
|
sub openid_auth ($$$$) {
|
2015-05-14 00:37:24 +02:00
|
|
|
my $q=shift;
|
|
|
|
my $session=shift;
|
|
|
|
my $errordisplayer=shift;
|
|
|
|
my $openid_url=$q->param('openid_identifier');
|
|
|
|
validate($q, $session, $openid_url, $errordisplayer);
|
2010-05-08 01:10:50 +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
|
|
|
|
2010-05-08 02:14:25 +02:00
|
|
|
if ($form->title eq "preferences" &&
|
2010-02-04 21:07:10 +01:00
|
|
|
IkiWiki::openiduser($session->param("name"))) {
|
2010-05-08 02:14:25 +02:00
|
|
|
$form->field(name => "openid_identifier", disabled => 1,
|
2010-02-04 21:07:10 +01:00
|
|
|
label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
|
2012-12-04 18:59:19 +01:00
|
|
|
value => "",
|
|
|
|
size => 1, force => 1,
|
|
|
|
fieldset => "login",
|
|
|
|
comment => $session->param("name"));
|
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;
|
2010-05-08 01:10:50 +02:00
|
|
|
my $errhandler=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) {
|
2010-05-08 01:10:50 +02:00
|
|
|
if ($errhandler) {
|
2014-10-16 00:52:43 +02:00
|
|
|
if (ref($errhandler) eq 'CODE') {
|
|
|
|
$errhandler->($csr->err);
|
|
|
|
}
|
2006-11-20 10:40:09 +01:00
|
|
|
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
|
|
|
|
2010-06-11 19:53:56 +02:00
|
|
|
my $cgiurl=$config{openid_cgiurl};
|
2010-12-25 20:08:22 +01:00
|
|
|
$cgiurl=$q->url if ! defined $cgiurl;
|
2010-06-11 19:53:56 +02:00
|
|
|
|
|
|
|
my $trust_root=$config{openid_realm};
|
|
|
|
$trust_root=$cgiurl if ! defined $trust_root;
|
|
|
|
|
2006-11-20 02:52:18 +01:00
|
|
|
my $check_url = $claimed_identity->check_url(
|
2013-06-29 19:31:47 +02:00
|
|
|
return_to => auto_upgrade_https($q, "$cgiurl?do=postsignin"),
|
|
|
|
trust_root => auto_upgrade_https($q, $trust_root),
|
2006-11-20 02:52:18 +01:00
|
|
|
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) {
|
2010-11-23 01:00:11 +01:00
|
|
|
IkiWiki::redirect($q, IkiWiki::baseurl(undef));
|
2006-11-20 21:37:27 +01:00
|
|
|
}
|
|
|
|
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-06-24 02:12:26 +02:00
|
|
|
my $nickname;
|
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});
|
2010-06-24 02:12:26 +02:00
|
|
|
if (! defined $nickname &&
|
2010-06-24 01:36:23 +02:00
|
|
|
$ext->{$field}=~/(.+)@.+/) {
|
2010-06-24 02:12:26 +02:00
|
|
|
$nickname = $1;
|
2010-06-24 01:36:23 +02:00
|
|
|
}
|
2010-03-14 01:08:15 +01:00
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
|
|
|
|
if (exists $ext->{$field} &&
|
|
|
|
defined $ext->{$field} &&
|
|
|
|
length $ext->{$field}) {
|
2010-06-24 02:12:26 +02:00
|
|
|
$nickname=$ext->{$field};
|
2010-03-14 01:08:15 +01:00
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-24 02:12:26 +02:00
|
|
|
if (defined $nickname) {
|
2010-07-11 19:38:37 +02:00
|
|
|
$session->param(nickname =>
|
|
|
|
Encode::decode_utf8($nickname));
|
2010-06-24 01:36:23 +02:00
|
|
|
}
|
2006-11-20 21:37:27 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
error("OpenID failure: ".$csr->err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif (defined $q->param('openid_identifier')) {
|
|
|
|
# myopenid.com affiliate support
|
2014-10-11 10:28:22 +02:00
|
|
|
validate($q, $session, scalar $q->param('openid_identifier'));
|
2006-11-20 21:37:27 +01:00
|
|
|
}
|
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;
|
|
|
|
|
2011-05-10 00:15:35 +02:00
|
|
|
eval q{use Net::INET6Glue::INET_is_INET6}; # may not be available
|
2006-11-20 02:52:18 +01:00
|
|
|
eval q{use Net::OpenID::Consumer};
|
|
|
|
error($@) if $@;
|
|
|
|
|
2019-02-10 18:22:06 +01:00
|
|
|
# We pass the for_url parameter, even though it's undef, because
|
|
|
|
# that will make sure we crash if used with an older IkiWiki.pm
|
|
|
|
# that didn't automatically try to use LWPx::ParanoidAgent.
|
|
|
|
my $ua=useragent(for_url => undef);
|
2006-11-20 02:52:18 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
}
|
2010-06-11 19:53:56 +02:00
|
|
|
|
|
|
|
my $cgiurl=$config{openid_cgiurl};
|
2010-12-25 20:08:22 +01:00
|
|
|
$cgiurl=$q->url if ! defined $cgiurl;
|
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 },
|
2013-06-29 19:31:47 +02:00
|
|
|
required_root => auto_upgrade_https($q, $cgiurl),
|
2006-11-20 02:52:18 +01:00
|
|
|
);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-11-20 02:52:18 +01:00
|
|
|
|
2013-06-29 19:31:47 +02:00
|
|
|
sub auto_upgrade_https {
|
|
|
|
my $q=shift;
|
|
|
|
my $url=shift;
|
|
|
|
if ($q->https()) {
|
|
|
|
$url=~s/^http:/https:/i;
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2010-05-08 01:10:50 +02:00
|
|
|
sub load_openid_module {
|
|
|
|
# Give up if module is unavailable to avoid needing to depend on it.
|
|
|
|
eval q{use Net::OpenID::Consumer};
|
|
|
|
if ($@) {
|
|
|
|
debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-11-20 02:52:18 +01:00
|
|
|
1
|