2 # Ikiwiki email address as login
3 package IkiWiki::Plugin::emailauth;
10 hook(type => "getsetup", id => "emailauth", "call" => \&getsetup);
11 hook(type => "cgi", id => "emailauth", "call" => \&cgi);
12 hook(type => "formbuilder_setup", id => "emailauth", "call" => \&formbuilder_setup);
13 IkiWiki::loadplugin("loginselector");
14 IkiWiki::Plugin::loginselector::register_login_plugin(
31 sub email_setup ($$) {
38 sub email_check_input ($) {
40 defined $cgi->param('do')
41 && $cgi->param("do") eq "signin"
42 && defined $cgi->param('Email_entry')
43 && length $cgi->param('Email_entry');
46 # Send login link to email.
47 sub email_auth ($$$$) {
50 my $errordisplayer=shift;
51 my $infodisplayer=shift;
53 my $email=$cgi->param('Email_entry');
54 unless ($email =~ /.\@./) {
55 $errordisplayer->(gettext("Invalid email address."));
59 # Implicit account creation.
60 my $userinfo=IkiWiki::userinfo_retrieve();
61 if (! exists $userinfo->{$email} || ! ref $userinfo->{$email}) {
62 IkiWiki::userinfo_setall($email, {
68 my $token=gentoken($email, $session);
69 my $template=template("emailauth.tmpl");
71 wikiname => $config{wikiname},
72 # Intentionally using short field names to keep link short.
73 authurl => IkiWiki::cgiurl_abs(
79 eval q{use Mail::Sendmail};
81 my $shorturl=$config{url};
82 $shorturl=~s/^https?:\/\///i;
85 From => "$config{wikiname} admin <".
86 (defined $config{adminemail} ? $config{adminemail} : "")
88 Subject => "$config{wikiname} login | $shorturl",
89 Message => $template->output,
90 ) or error(gettext("Failed to send mail"));
92 $infodisplayer->(gettext("You have been sent an email, with a link you can open to complete the login process."));
95 # Finish login process.
99 my $email=$cgi->param('e');
100 my $v=$cgi->param('v');
101 if (defined $email && defined $v && length $email && length $v) {
102 my $token=gettoken($email);
105 my $session=getsession($email);
106 IkiWiki::cgi_postsignin($cgi, $session);
108 elsif (length $token ne length $cgi->param('v')) {
109 error(gettext("Wrong login token length. Please check that you pasted in the complete login link from the email!"));
117 sub formbuilder_setup (@) {
119 my $form=$params{form};
120 my $session=$params{session};
122 if ($form->title eq "preferences" &&
123 IkiWiki::emailuser($session->param("name"))) {
124 $form->field(name => "email", disabled => 1);
128 # Generates the token that will be used in the authurl to log the user in.
129 # This needs to be hard to guess, and relatively short. Generating a cgi
130 # session id will make it as hard to guess as any cgi session.
132 # Store token in userinfo; this allows the user to log in
133 # using a different browser session, if it takes a while for the
134 # email to get to them.
136 # The postsignin value from the session is also stored in the userinfo
137 # to allow resuming in a different browser session.
141 eval q{use CGI::Session};
143 my $token = CGI::Session->new->id;
144 IkiWiki::userinfo_set($email, "emailauthexpire", time+(60*60*24));
145 IkiWiki::userinfo_set($email, "emailauth", $token);
146 IkiWiki::userinfo_set($email, "emailauthpostsignin", defined $session->param("postsignin") ? $session->param("postsignin") : "");
150 # Gets the token, checking for expiry.
153 my $val=IkiWiki::userinfo_get($email, "emailauth");
154 my $expire=IkiWiki::userinfo_get($email, "emailauthexpire");
155 if (! length $val || time > $expire) {
161 # Generate a session to use after successful login.
166 IkiWiki::loadindex();
167 my $session=IkiWiki::cgi_getsession();
169 my $postsignin=IkiWiki::userinfo_get($email, "emailauthpostsignin");
170 IkiWiki::userinfo_set($email, "emailauthpostsignin", "");
171 if (defined $postsignin && length $postsignin) {
172 $session->param(postsignin => $postsignin);
175 $session->param(name => $email);
178 $session->param(nickname => Encode::decode_utf8($nickname));
180 IkiWiki::cgi_savesession($session);
187 IkiWiki::userinfo_set($email, "emailauthexpire", 0);
188 IkiWiki::userinfo_set($email, "emailauth", "");
191 sub loginfailure () {
192 error "Bad email authentication token. Please retry login.";