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 {
|
2010-05-08 02:14:25 +02:00
|
|
|
add_underlay("openid-selector");
|
2011-06-16 01:15:06 +02:00
|
|
|
add_underlay("jquery");
|
2010-05-08 01:10:50 +02:00
|
|
|
hook(type => "checkconfig", id => "openid", call => \&checkconfig);
|
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
|
|
|
|
2010-05-08 01:10:50 +02:00
|
|
|
sub checkconfig () {
|
|
|
|
if ($config{cgi}) {
|
|
|
|
# Intercept normal signin form, so the openid selector
|
|
|
|
# can be displayed.
|
2010-05-08 02:14:25 +02:00
|
|
|
#
|
|
|
|
# When other auth hooks are registered, give the selector
|
|
|
|
# a reference to the normal signin form.
|
2010-05-08 03:28:59 +02:00
|
|
|
require IkiWiki::CGI;
|
2010-05-08 02:14:25 +02:00
|
|
|
my $real_cgi_signin;
|
|
|
|
if (keys %{$IkiWiki::hooks{auth}} > 1) {
|
|
|
|
$real_cgi_signin=\&IkiWiki::cgi_signin;
|
|
|
|
}
|
2010-05-08 01:10:50 +02:00
|
|
|
inject(name => "IkiWiki::cgi_signin", call => sub ($$) {
|
2010-05-08 02:14:25 +02:00
|
|
|
openid_selector($real_cgi_signin, @_);
|
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
|
|
|
|
2010-05-08 01:10:50 +02:00
|
|
|
sub openid_selector {
|
|
|
|
my $real_cgi_signin=shift;
|
|
|
|
my $q=shift;
|
|
|
|
my $session=shift;
|
|
|
|
|
2010-05-08 02:14:25 +02:00
|
|
|
my $openid_url=$q->param('openid_identifier');
|
2010-05-08 01:10:50 +02:00
|
|
|
my $openid_error;
|
|
|
|
|
2010-05-08 02:14:25 +02:00
|
|
|
if (! load_openid_module()) {
|
|
|
|
if ($real_cgi_signin) {
|
|
|
|
$real_cgi_signin->($q, $session);
|
2010-05-08 03:27:02 +02:00
|
|
|
exit;
|
2010-05-08 02:14:25 +02:00
|
|
|
}
|
|
|
|
error(sprintf(gettext("failed to load openid module: "), @_));
|
|
|
|
}
|
|
|
|
elsif (defined $q->param("action") && $q->param("action") eq "verify") {
|
2010-05-08 01:10:50 +02:00
|
|
|
validate($q, $session, $openid_url, sub {
|
|
|
|
$openid_error=shift;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
my $template=IkiWiki::template("openid-selector.tmpl");
|
|
|
|
$template->param(
|
2010-11-23 01:12:17 +01:00
|
|
|
cgiurl => IkiWiki::cgiurl(),
|
2010-05-08 01:10:50 +02:00
|
|
|
(defined $openid_error ? (openid_error => $openid_error) : ()),
|
|
|
|
(defined $openid_url ? (openid_url => $openid_url) : ()),
|
2010-05-08 21:57:39 +02:00
|
|
|
($real_cgi_signin ? (nonopenidform => $real_cgi_signin->($q, $session, 1)) : ()),
|
2010-05-08 01:10:50 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
IkiWiki::printheader($session);
|
2011-01-05 22:15:38 +01:00
|
|
|
print IkiWiki::cgitemplate($q, "signin", $template->output);
|
2010-05-08 01:10:50 +02:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
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"));
|
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;
|
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) {
|
|
|
|
$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
|
|
|
|
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;
|
|
|
|
|
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 $@;
|
|
|
|
|
|
|
|
my $ua;
|
|
|
|
eval q{use LWPx::ParanoidAgent};
|
|
|
|
if (! $@) {
|
|
|
|
$ua=LWPx::ParanoidAgent->new;
|
|
|
|
}
|
|
|
|
else {
|
2013-07-27 14:12:01 +02:00
|
|
|
$ua=useragent();
|
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
|