response
[ikiwiki] / IkiWiki / Plugin / passwordauth.pm
1 #!/usr/bin/perl
2 # Ikiwiki password authentication.
3 package IkiWiki::Plugin::passwordauth;
4
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
8
9 sub import {
10         hook(type => "getsetup", id => "passwordauth", "call" => \&getsetup);
11         hook(type => "formbuilder_setup", id => "passwordauth", call => \&formbuilder_setup);
12         hook(type => "formbuilder", id => "passwordauth", call => \&formbuilder);
13         hook(type => "sessioncgi", id => "passwordauth", call => \&sessioncgi);
14         hook(type => "auth", id => "passwordauth", call => \&auth);
15 }
16
17 sub getsetup () {
18         return
19                 plugin => {
20                         safe => 1,
21                         rebuild => 0,
22                 },
23                 account_creation_password => {
24                         type => "string",
25                         example => "s3cr1t",
26                         description => "a password that must be entered when signing up for an account",
27                         safe => 1,
28                         rebuild => 0,
29                 },
30                 password_cost => {
31                         type => "integer",
32                         example => 8,
33                         description => "cost of generating a password using Authen::Passphrase::BlowfishCrypt",
34                         safe => 1,
35                         rebuild => 0,
36                 },
37 }
38
39 # Checks if a string matches a user's password, and returns true or false.
40 sub checkpassword ($$;$) {
41         my $user=shift;
42         my $password=shift;
43         my $field=shift || "password";
44
45         # It's very important that the user not be allowed to log in with
46         # an empty password!
47         if (! length $password) {
48                 return 0;
49         }
50
51         my $userinfo=IkiWiki::userinfo_retrieve();
52         if (! length $user || ! defined $userinfo ||
53             ! exists $userinfo->{$user} || ! ref $userinfo->{$user}) {
54                 return 0;
55         }
56
57         my $ret=0;
58         if (exists $userinfo->{$user}->{"crypt".$field}) {
59                 eval q{use Authen::Passphrase};
60                 error $@ if $@;
61                 my $p = Authen::Passphrase->from_crypt($userinfo->{$user}->{"crypt".$field});
62                 $ret=$p->match($password);
63         }
64         elsif (exists $userinfo->{$user}->{$field}) {
65                 $ret=$password eq $userinfo->{$user}->{$field};
66         }
67
68         if ($ret &&
69             (exists $userinfo->{$user}->{resettoken} ||
70              exists $userinfo->{$user}->{cryptresettoken})) {
71                 # Clear reset token since the user has successfully logged in.
72                 delete $userinfo->{$user}->{resettoken};
73                 delete $userinfo->{$user}->{cryptresettoken};
74                 IkiWiki::userinfo_store($userinfo);
75         }
76
77         return $ret;
78 }
79
80 sub setpassword ($$;$) {
81         my $user=shift;
82         my $password=shift;
83         my $field=shift || "password";
84
85         eval q{use Authen::Passphrase::BlowfishCrypt};
86         if (! $@) {
87                 my $p = Authen::Passphrase::BlowfishCrypt->new(
88                         cost => $config{password_cost} || 8,
89                         salt_random => 1,
90                         passphrase => $password,
91                 );
92                 IkiWiki::userinfo_set($user, "crypt$field", $p->as_crypt);
93                 IkiWiki::userinfo_set($user, $field, "");
94         }
95         else {
96                 IkiWiki::userinfo_set($user, $field, $password);
97         }
98 }
99
100 sub formbuilder_setup (@) {
101         my %params=@_;
102
103         my $form=$params{form};
104         my $session=$params{session};
105         my $cgi=$params{cgi};
106
107         if ($form->title eq "signin" || $form->title eq "register" || $cgi->param("do") eq "register") {
108                 $form->field(name => "name", required => 0);
109                 $form->field(name => "password", type => "password", required => 0);
110                 
111                 if ($form->submitted eq "Register" || $form->submitted eq "Create Account" || $cgi->param("do") eq "register") {
112                         $form->field(name => "confirm_password", type => "password");
113                         $form->field(name => "account_creation_password", type => "password")
114                                  if (defined $config{account_creation_password} &&
115                                      length $config{account_creation_password});
116                         $form->field(name => "email", size => 50);
117                         $form->title("register");
118                         $form->text("");
119                 
120                         $form->field(name => "confirm_password",
121                                 validate => sub {
122                                         shift eq $form->field("password");
123                                 },
124                         );
125                         $form->field(name => "password",
126                                 validate => sub {
127                                         shift eq $form->field("confirm_password");
128                                 },
129                         );
130                 }
131
132                 if ($form->submitted) {
133                         my $submittype=$form->submitted;
134                         # Set required fields based on how form was submitted.
135                         my %required=(
136                                 "Login" => [qw(name password)],
137                                 "Register" => [],
138                                 "Create Account" => [qw(name password confirm_password email)],
139                                 "Reset Password" => [qw(name)],
140                         );
141                         foreach my $opt (@{$required{$submittype}}) {
142                                 $form->field(name => $opt, required => 1);
143                         }
144         
145                         if ($submittype eq "Create Account") {
146                                 $form->field(
147                                         name => "account_creation_password",
148                                         validate => sub {
149                                                 shift eq $config{account_creation_password};
150                                         },
151                                         required => 1,
152                                 ) if (defined $config{account_creation_password} &&
153                                       length $config{account_creation_password});
154                                 $form->field(
155                                         name => "email",
156                                         validate => "EMAIL",
157                                 );
158                         }
159
160                         # Validate password against name for Login.
161                         if ($submittype eq "Login") {
162                                 $form->field(
163                                         name => "password",
164                                         validate => sub {
165                                                 checkpassword($form->field("name"), shift);
166                                         },
167                                 );
168                         }
169                         elsif ($submittype eq "Register" ||
170                                $submittype eq "Create Account" ||
171                                $submittype eq "Reset Password") {
172                                 $form->field(name => "password", validate => 'VALUE');
173                         }
174                         
175                         # And make sure the entered name exists when logging
176                         # in or sending email, and does not when registering.
177                         if ($submittype eq 'Create Account' ||
178                             $submittype eq 'Register') {
179                                 $form->field(
180                                         name => "name",
181                                         validate => sub {
182                                                 my $name=shift;
183                                                 length $name &&
184                                                 $name=~/$config{wiki_file_regexp}/ &&
185                                                 ! IkiWiki::userinfo_get($name, "regdate");
186                                         },
187                                 );
188                         }
189                         elsif ($submittype eq "Login" ||
190                                $submittype eq "Reset Password") {
191                                 $form->field( 
192                                         name => "name",
193                                         validate => sub {
194                                                 my $name=shift;
195                                                 length $name &&
196                                                 IkiWiki::userinfo_get($name, "regdate");
197                                         },
198                                 );
199                         }
200                 }
201                 else {
202                         # First time settings.
203                         $form->field(name => "name");
204                         if ($session->param("name")) {
205                                 $form->field(name => "name", value => $session->param("name"));
206                         }
207                 }
208         }
209         elsif ($form->title eq "preferences") {
210                 my $user=$session->param("name");
211                 if (! IkiWiki::openiduser($user)) {
212                         $form->field(name => "name", disabled => 1, 
213                                 value => $user, force => 1,
214                                 fieldset => "login");
215                         $form->field(name => "password", type => "password",
216                                 fieldset => "login",
217                                 validate => sub {
218                                         shift eq $form->field("confirm_password");
219                                 });
220                         $form->field(name => "confirm_password", type => "password",
221                                 fieldset => "login",
222                                 validate => sub {
223                                         shift eq $form->field("password");
224                                 });
225                         
226                         my $userpage=IkiWiki::userpage($user);
227                         if (exists $pagesources{$userpage}) {
228                                 $form->text(gettext("Your user page: ").
229                                         htmllink("", "", $userpage,
230                                                 noimageinline => 1));
231                         }
232                         else {
233                                 $form->text("<a href=\"".
234                                         IkiWiki::cgiurl(do => "edit", page => $userpage).
235                                         "\">".gettext("Create your user page")."</a>");
236                         }
237                 }
238         }
239 }
240
241 sub formbuilder (@) {
242         my %params=@_;
243
244         my $form=$params{form};
245         my $session=$params{session};
246         my $cgi=$params{cgi};
247         my $buttons=$params{buttons};
248
249         if ($form->title eq "signin" || $form->title eq "register") {
250                 if (($form->submitted && $form->validate) || $cgi->param("do") eq "register") {
251                         if ($form->submitted eq 'Login') {
252                                 $session->param("name", $form->field("name"));
253                                 IkiWiki::cgi_postsignin($cgi, $session);
254                         }
255                         elsif ($form->submitted eq 'Create Account') {
256                                 my $user_name=$form->field('name');
257                                 if (IkiWiki::userinfo_setall($user_name, {
258                                         'email' => $form->field('email'),
259                                         'regdate' => time})) {
260                                         setpassword($user_name, $form->field('password'));
261                                         $form->field(name => "confirm_password", type => "hidden");
262                                         $form->field(name => "email", type => "hidden");
263                                         $form->text(gettext("Account creation successful. Now you can Login."));
264                                 }
265                                 else {
266                                         error(gettext("Error creating account."));
267                                 }
268                         }
269                         elsif ($form->submitted eq 'Reset Password') {
270                                 my $user_name=$form->field("name");
271                                 my $email=IkiWiki::userinfo_get($user_name, "email");
272                                 if (! length $email) {
273                                         error(gettext("No email address, so cannot email password reset instructions."));
274                                 }
275                                 
276                                 # Store a token that can be used once
277                                 # to log the user in. This needs to be hard
278                                 # to guess. Generating a cgi session id will
279                                 # make it as hard to guess as any cgi session.
280                                 eval q{use CGI::Session};
281                                 error($@) if $@;
282                                 my $token = CGI::Session->new->id;
283                                 setpassword($user_name, $token, "resettoken");
284                                 
285                                 my $template=template("passwordmail.tmpl");
286                                 $template->param(
287                                         user_name => $user_name,
288                                         passwordurl => IkiWiki::cgiurl(
289                                                 'do' => "reset",
290                                                 'name' => $user_name,
291                                                 'token' => $token,
292                                         ),
293                                         wikiurl => $config{url},
294                                         wikiname => $config{wikiname},
295                                         REMOTE_ADDR => $ENV{REMOTE_ADDR},
296                                 );
297                                 
298                                 eval q{use Mail::Sendmail};
299                                 error($@) if $@;
300                                 sendmail(
301                                         To => IkiWiki::userinfo_get($user_name, "email"),
302                                         From => "$config{wikiname} admin <".
303                                                 (defined $config{adminemail} ? $config{adminemail} : "")
304                                                 .">",
305                                         Subject => "$config{wikiname} information",
306                                         Message => $template->output,
307                                 ) or error(gettext("Failed to send mail"));
308                                 
309                                 $form->text(gettext("You have been mailed password reset instructions."));
310                                 $form->field(name => "name", required => 0);
311                                 push @$buttons, "Reset Password";
312                         }
313                         elsif ($form->submitted eq "Register" || $cgi->param("do") eq "register") {
314                                 @$buttons="Create Account";
315                         }
316                 }
317                 elsif ($form->submitted eq "Create Account") {
318                         @$buttons="Create Account";
319                 }
320                 else {
321                         push @$buttons, "Register", "Reset Password";
322                 }
323         }
324         elsif ($form->title eq "preferences") {
325                 if ($form->submitted eq "Save Preferences" && $form->validate) {
326                         my $user_name=$form->field('name');
327                         if ($form->field("password") && length $form->field("password")) {
328                                 setpassword($user_name, $form->field('password'));
329                         }
330                 }
331         }
332 }
333
334 sub sessioncgi ($$) {
335         my $q=shift;
336         my $session=shift;
337
338         if ($q->param('do') eq 'reset') {
339                 my $name=$q->param("name");
340                 my $token=$q->param("token");
341
342                 if (! defined $name || ! defined $token ||
343                     ! length $name  || ! length $token) {
344                         error(gettext("incorrect password reset url"));
345                 }
346                 if (! checkpassword($name, $token, "resettoken")) {
347                         error(gettext("password reset denied"));
348                 }
349
350                 $session->param("name", $name);
351                 IkiWiki::cgi_prefs($q, $session);
352                 exit;
353         }
354         elsif ($q->param("do") eq "register") {
355                 # After registration, need to go somewhere, so show prefs page.
356                 $session->param(postsignin => "do=prefs");
357                 # Due to do=register, this will run in registration-only
358                 # mode.
359                 IkiWiki::cgi_signin($q, $session);
360                 exit;
361         }
362 }
363
364 sub auth ($$) {
365         # While this hook is not currently used, it needs to exist
366         # so ikiwiki knows that the wiki supports logins, and will
367         # enable the Preferences page.
368 }
369
370 1