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