typo
[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                ! IkiWiki::openiduser($session->param("name"))) {
211                 $form->field(name => "name", disabled => 1, 
212                         value => $session->param("name"), force => 1,
213                         fieldset => "login");
214                 $form->field(name => "password", type => "password",
215                         fieldset => "login",
216                         validate => sub {
217                                 shift eq $form->field("confirm_password");
218                         }),
219                 $form->field(name => "confirm_password", type => "password",
220                         fieldset => "login",
221                         validate => sub {
222                                 shift eq $form->field("password");
223                         }),
224         }
225 }
226
227 sub formbuilder (@) {
228         my %params=@_;
229
230         my $form=$params{form};
231         my $session=$params{session};
232         my $cgi=$params{cgi};
233         my $buttons=$params{buttons};
234
235         if ($form->title eq "signin" || $form->title eq "register") {
236                 if (($form->submitted && $form->validate) || $cgi->param("do") eq "register") {
237                         if ($form->submitted eq 'Login') {
238                                 $session->param("name", $form->field("name"));
239                                 IkiWiki::cgi_postsignin($cgi, $session);
240                         }
241                         elsif ($form->submitted eq 'Create Account') {
242                                 my $user_name=$form->field('name');
243                                 if (IkiWiki::userinfo_setall($user_name, {
244                                         'email' => $form->field('email'),
245                                         'regdate' => time})) {
246                                         setpassword($user_name, $form->field('password'));
247                                         $form->field(name => "confirm_password", type => "hidden");
248                                         $form->field(name => "email", type => "hidden");
249                                         $form->text(gettext("Account creation successful. Now you can Login."));
250                                 }
251                                 else {
252                                         error(gettext("Error creating account."));
253                                 }
254                         }
255                         elsif ($form->submitted eq 'Reset Password') {
256                                 my $user_name=$form->field("name");
257                                 my $email=IkiWiki::userinfo_get($user_name, "email");
258                                 if (! length $email) {
259                                         error(gettext("No email address, so cannot email password reset instructions."));
260                                 }
261                                 
262                                 # Store a token that can be used once
263                                 # to log the user in. This needs to be hard
264                                 # to guess. Generating a cgi session id will
265                                 # make it as hard to guess as any cgi session.
266                                 eval q{use CGI::Session};
267                                 error($@) if $@;
268                                 my $token = CGI::Session->new->id;
269                                 setpassword($user_name, $token, "resettoken");
270                                 
271                                 my $template=template("passwordmail.tmpl");
272                                 $template->param(
273                                         user_name => $user_name,
274                                         passwordurl => IkiWiki::cgiurl(
275                                                 'do' => "reset",
276                                                 'name' => $user_name,
277                                                 'token' => $token,
278                                         ),
279                                         wikiurl => $config{url},
280                                         wikiname => $config{wikiname},
281                                         REMOTE_ADDR => $ENV{REMOTE_ADDR},
282                                 );
283                                 
284                                 eval q{use Mail::Sendmail};
285                                 error($@) if $@;
286                                 sendmail(
287                                         To => IkiWiki::userinfo_get($user_name, "email"),
288                                         From => "$config{wikiname} admin <".
289                                                 (defined $config{adminemail} ? $config{adminemail} : "")
290                                                 .">",
291                                         Subject => "$config{wikiname} information",
292                                         Message => $template->output,
293                                 ) or error(gettext("Failed to send mail"));
294                                 
295                                 $form->text(gettext("You have been mailed password reset instructions."));
296                                 $form->field(name => "name", required => 0);
297                                 push @$buttons, "Reset Password";
298                         }
299                         elsif ($form->submitted eq "Register" || $cgi->param("do") eq "register") {
300                                 @$buttons="Create Account";
301                         }
302                 }
303                 elsif ($form->submitted eq "Create Account") {
304                         @$buttons="Create Account";
305                 }
306                 else {
307                         push @$buttons, "Register", "Reset Password";
308                 }
309         }
310         elsif ($form->title eq "preferences") {
311                 if ($form->submitted eq "Save Preferences" && $form->validate) {
312                         my $user_name=$form->field('name');
313                         if ($form->field("password") && length $form->field("password")) {
314                                 setpassword($user_name, $form->field('password'));
315                         }
316                 }
317         }
318 }
319
320 sub sessioncgi ($$) {
321         my $q=shift;
322         my $session=shift;
323
324         if ($q->param('do') eq 'reset') {
325                 my $name=$q->param("name");
326                 my $token=$q->param("token");
327
328                 if (! defined $name || ! defined $token ||
329                     ! length $name  || ! length $token) {
330                         error(gettext("incorrect password reset url"));
331                 }
332                 if (! checkpassword($name, $token, "resettoken")) {
333                         error(gettext("password reset denied"));
334                 }
335
336                 $session->param("name", $name);
337                 IkiWiki::cgi_prefs($q, $session);
338                 exit;
339         }
340         elsif ($q->param("do") eq "register") {
341                 # After registration, need to go somewhere, so show prefs page.
342                 $session->param(postsignin => "do=prefs");
343                 # Due to do=register, this will run in registration-only
344                 # mode.
345                 IkiWiki::cgi_signin($q, $session);
346                 exit;
347         }
348 }
349
350 sub auth ($$) {
351         # While this hook is not currently used, it needs to exist
352         # so ikiwiki knows that the wiki supports logins, and will
353         # enable the Preferences page.
354 }
355
356 1