2015-05-14 00:50:40 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# Ikiwiki email address as login
|
|
|
|
package IkiWiki::Plugin::emailauth;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use IkiWiki 3.00;
|
|
|
|
|
|
|
|
sub import {
|
|
|
|
hook(type => "getsetup", id => "emailauth", "call" => \&getsetup);
|
2015-05-14 05:32:29 +02:00
|
|
|
hook(type => "cgi", id => "emailauth", "call" => \&cgi);
|
|
|
|
hook(type => "formbuilder_setup", id => "emailauth", "call" => \&formbuilder_setup);
|
2015-05-14 00:50:40 +02:00
|
|
|
IkiWiki::loadplugin("loginselector");
|
|
|
|
IkiWiki::Plugin::loginselector::register_login_plugin(
|
|
|
|
"emailauth",
|
|
|
|
\&email_setup,
|
|
|
|
\&email_check_input,
|
|
|
|
\&email_auth,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub getsetup () {
|
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 0,
|
|
|
|
section => "auth",
|
|
|
|
},
|
2015-10-02 17:49:47 +02:00
|
|
|
emailauth_sender => {
|
|
|
|
type => "string",
|
|
|
|
description => "email address to send emailauth mails as (default: adminemail)",
|
|
|
|
safe => 1,
|
|
|
|
rebuild => 0,
|
|
|
|
},
|
2015-05-14 00:50:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub email_setup ($$) {
|
|
|
|
my $q=shift;
|
|
|
|
my $template=shift;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub email_check_input ($) {
|
|
|
|
my $cgi=shift;
|
|
|
|
defined $cgi->param('do')
|
|
|
|
&& $cgi->param("do") eq "signin"
|
|
|
|
&& defined $cgi->param('Email_entry')
|
|
|
|
&& length $cgi->param('Email_entry');
|
|
|
|
}
|
|
|
|
|
2015-05-14 04:27:03 +02:00
|
|
|
# Send login link to email.
|
|
|
|
sub email_auth ($$$$) {
|
2015-05-14 00:50:40 +02:00
|
|
|
my $cgi=shift;
|
|
|
|
my $session=shift;
|
|
|
|
my $errordisplayer=shift;
|
2015-05-14 04:27:03 +02:00
|
|
|
my $infodisplayer=shift;
|
|
|
|
|
|
|
|
my $email=$cgi->param('Email_entry');
|
|
|
|
unless ($email =~ /.\@./) {
|
|
|
|
$errordisplayer->(gettext("Invalid email address."));
|
2015-05-14 00:50:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-14 04:27:03 +02:00
|
|
|
# Implicit account creation.
|
|
|
|
my $userinfo=IkiWiki::userinfo_retrieve();
|
|
|
|
if (! exists $userinfo->{$email} || ! ref $userinfo->{$email}) {
|
|
|
|
IkiWiki::userinfo_setall($email, {
|
|
|
|
'email' => $email,
|
|
|
|
'regdate' => time,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-14 05:06:52 +02:00
|
|
|
my $token=gentoken($email, $session);
|
2015-05-14 04:27:03 +02:00
|
|
|
my $template=template("emailauth.tmpl");
|
|
|
|
$template->param(
|
|
|
|
wikiname => $config{wikiname},
|
|
|
|
# Intentionally using short field names to keep link short.
|
|
|
|
authurl => IkiWiki::cgiurl_abs(
|
|
|
|
'e' => $email,
|
|
|
|
'v' => $token,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
eval q{use Mail::Sendmail};
|
|
|
|
error($@) if $@;
|
2015-05-19 23:44:20 +02:00
|
|
|
my $shorturl=$config{url};
|
|
|
|
$shorturl=~s/^https?:\/\///i;
|
2015-10-02 17:49:47 +02:00
|
|
|
my $emailauth_sender=$config{emailauth_sender};
|
|
|
|
$emailauth_sender=$config{adminemail} unless defined $emailauth_sender;
|
2015-05-14 04:27:03 +02:00
|
|
|
sendmail(
|
|
|
|
To => $email,
|
|
|
|
From => "$config{wikiname} admin <".
|
2015-10-02 17:49:47 +02:00
|
|
|
(defined $emailauth_sender ? $emailauth_sender : "")
|
2015-05-14 04:27:03 +02:00
|
|
|
.">",
|
2015-05-19 23:44:20 +02:00
|
|
|
Subject => "$config{wikiname} login | $shorturl",
|
2015-05-14 04:27:03 +02:00
|
|
|
Message => $template->output,
|
|
|
|
) or error(gettext("Failed to send mail"));
|
|
|
|
|
|
|
|
$infodisplayer->(gettext("You have been sent an email, with a link you can open to complete the login process."));
|
|
|
|
}
|
|
|
|
|
|
|
|
# Finish login process.
|
|
|
|
sub cgi ($$) {
|
|
|
|
my $cgi=shift;
|
|
|
|
|
|
|
|
my $email=$cgi->param('e');
|
|
|
|
my $v=$cgi->param('v');
|
|
|
|
if (defined $email && defined $v && length $email && length $v) {
|
|
|
|
my $token=gettoken($email);
|
|
|
|
if ($token eq $v) {
|
|
|
|
cleartoken($email);
|
2015-05-14 05:06:52 +02:00
|
|
|
my $session=getsession($email);
|
2015-05-14 04:27:03 +02:00
|
|
|
IkiWiki::cgi_postsignin($cgi, $session);
|
|
|
|
}
|
|
|
|
elsif (length $token ne length $cgi->param('v')) {
|
|
|
|
error(gettext("Wrong login token length. Please check that you pasted in the complete login link from the email!"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
loginfailure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 05:32:29 +02:00
|
|
|
sub formbuilder_setup (@) {
|
|
|
|
my %params=@_;
|
|
|
|
my $form=$params{form};
|
|
|
|
my $session=$params{session};
|
|
|
|
|
|
|
|
if ($form->title eq "preferences" &&
|
|
|
|
IkiWiki::emailuser($session->param("name"))) {
|
|
|
|
$form->field(name => "email", disabled => 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 04:27:03 +02:00
|
|
|
# Generates the token that will be used in the authurl to log the user in.
|
|
|
|
# This needs to be hard to guess, and relatively short. Generating a cgi
|
|
|
|
# session id will make it as hard to guess as any cgi session.
|
2015-05-14 05:06:52 +02:00
|
|
|
#
|
|
|
|
# Store token in userinfo; this allows the user to log in
|
|
|
|
# using a different browser session, if it takes a while for the
|
|
|
|
# email to get to them.
|
|
|
|
#
|
|
|
|
# The postsignin value from the session is also stored in the userinfo
|
|
|
|
# to allow resuming in a different browser session.
|
|
|
|
sub gentoken ($$) {
|
2015-05-14 04:27:03 +02:00
|
|
|
my $email=shift;
|
2015-05-14 05:06:52 +02:00
|
|
|
my $session=shift;
|
2015-05-14 04:27:03 +02:00
|
|
|
eval q{use CGI::Session};
|
|
|
|
error($@) if $@;
|
|
|
|
my $token = CGI::Session->new->id;
|
|
|
|
IkiWiki::userinfo_set($email, "emailauthexpire", time+(60*60*24));
|
|
|
|
IkiWiki::userinfo_set($email, "emailauth", $token);
|
2015-05-14 05:06:52 +02:00
|
|
|
IkiWiki::userinfo_set($email, "emailauthpostsignin", defined $session->param("postsignin") ? $session->param("postsignin") : "");
|
2015-05-14 04:27:03 +02:00
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Gets the token, checking for expiry.
|
|
|
|
sub gettoken ($) {
|
|
|
|
my $email=shift;
|
|
|
|
my $val=IkiWiki::userinfo_get($email, "emailauth");
|
|
|
|
my $expire=IkiWiki::userinfo_get($email, "emailauthexpire");
|
|
|
|
if (! length $val || time > $expire) {
|
|
|
|
loginfailure();
|
|
|
|
}
|
|
|
|
return $val;
|
|
|
|
}
|
|
|
|
|
2015-05-14 05:06:52 +02:00
|
|
|
# Generate a session to use after successful login.
|
|
|
|
sub getsession ($) {
|
|
|
|
my $email=shift;
|
|
|
|
|
|
|
|
IkiWiki::lockwiki();
|
|
|
|
IkiWiki::loadindex();
|
|
|
|
my $session=IkiWiki::cgi_getsession();
|
|
|
|
|
|
|
|
my $postsignin=IkiWiki::userinfo_get($email, "emailauthpostsignin");
|
|
|
|
IkiWiki::userinfo_set($email, "emailauthpostsignin", "");
|
|
|
|
if (defined $postsignin && length $postsignin) {
|
|
|
|
$session->param(postsignin => $postsignin);
|
|
|
|
}
|
|
|
|
|
|
|
|
$session->param(name => $email);
|
|
|
|
my $nickname=$email;
|
|
|
|
$nickname=~s/@.*//;
|
|
|
|
$session->param(nickname => Encode::decode_utf8($nickname));
|
|
|
|
|
|
|
|
IkiWiki::cgi_savesession($session);
|
|
|
|
|
|
|
|
return $session;
|
|
|
|
}
|
|
|
|
|
2015-05-14 04:27:03 +02:00
|
|
|
sub cleartoken ($) {
|
|
|
|
my $email=shift;
|
|
|
|
IkiWiki::userinfo_set($email, "emailauthexpire", 0);
|
|
|
|
IkiWiki::userinfo_set($email, "emailauth", "");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub loginfailure () {
|
|
|
|
error "Bad email authentication token. Please retry login.";
|
2015-05-14 00:50:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
1
|