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