2 # Ikiwiki password authentication.
3 package IkiWiki::Plugin::passwordauth;
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);
24 account_creation_password => {
27 description => "a password that must be entered when signing up for an account",
34 description => "cost of generating a password using Authen::Passphrase::BlowfishCrypt",
40 # Checks if a string matches a user's password, and returns true or false.
41 sub checkpassword ($$;$) {
44 my $field=shift || "password";
46 # It's very important that the user not be allowed to log in with
48 if (! length $password) {
52 my $userinfo=IkiWiki::userinfo_retrieve();
53 if (! length $user || ! defined $userinfo ||
54 ! exists $userinfo->{$user} || ! ref $userinfo->{$user}) {
59 if (exists $userinfo->{$user}->{"crypt".$field}) {
60 eval q{use Authen::Passphrase};
62 my $p = Authen::Passphrase->from_crypt($userinfo->{$user}->{"crypt".$field});
63 $ret=$p->match($password);
65 elsif (exists $userinfo->{$user}->{$field}) {
66 $ret=$password eq $userinfo->{$user}->{$field};
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);
81 sub setpassword ($$;$) {
84 my $field=shift || "password";
86 eval q{use Authen::Passphrase::BlowfishCrypt};
88 my $p = Authen::Passphrase::BlowfishCrypt->new(
89 cost => $config{password_cost} || 8,
91 passphrase => $password,
93 IkiWiki::userinfo_set($user, "crypt$field", $p->as_crypt);
94 IkiWiki::userinfo_set($user, $field, "");
97 IkiWiki::userinfo_set($user, $field, $password);
100 # Setting the password clears any passwordless login token.
101 if ($field ne 'passwordless') {
102 IkiWiki::userinfo_set($user, "cryptpasswordless", "");
103 IkiWiki::userinfo_set($user, "passwordless", "");
107 sub formbuilder_setup (@) {
110 my $form=$params{form};
111 my $session=$params{session};
112 my $cgi=$params{cgi};
114 my $do_register=defined $cgi->param("do") && $cgi->param("do") eq "register";
116 if ($form->title eq "signin" || $form->title eq "register" || $do_register) {
117 $form->field(name => "name", required => 0);
118 $form->field(name => "password", type => "password", required => 0);
120 if ($form->submitted eq "Register" || $form->submitted eq "Create Account" || $do_register) {
121 $form->field(name => "confirm_password", type => "password");
122 $form->field(name => "account_creation_password", type => "password")
123 if (defined $config{account_creation_password} &&
124 length $config{account_creation_password});
125 $form->field(name => "email", size => 50);
126 $form->title("register");
129 $form->field(name => "confirm_password",
131 shift eq $form->field("password");
134 $form->field(name => "password",
136 shift eq $form->field("confirm_password");
141 if ($form->submitted) {
142 my $submittype=$form->submitted;
143 # Set required fields based on how form was submitted.
145 "Login" => [qw(name password)],
147 "Create Account" => [qw(name password confirm_password email)],
148 "Reset Password" => [qw(name)],
150 foreach my $opt (@{$required{$submittype}}) {
151 $form->field(name => $opt, required => 1);
154 if ($submittype eq "Create Account") {
156 name => "account_creation_password",
158 shift eq $config{account_creation_password};
161 ) if (defined $config{account_creation_password} &&
162 length $config{account_creation_password});
169 # Validate password against name for Login.
170 if ($submittype eq "Login") {
174 checkpassword($form->field("name"), shift);
178 elsif ($submittype eq "Register" ||
179 $submittype eq "Create Account" ||
180 $submittype eq "Reset Password") {
181 $form->field(name => "password", validate => 'VALUE');
184 # And make sure the entered name exists when logging
185 # in or sending email, and does not when registering.
186 if ($submittype eq 'Create Account' ||
187 $submittype eq 'Register') {
193 $name=~/$config{wiki_file_regexp}/ &&
194 ! IkiWiki::userinfo_get($name, "regdate");
198 elsif ($submittype eq "Login" ||
199 $submittype eq "Reset Password") {
205 IkiWiki::userinfo_get($name, "regdate");
211 # First time settings.
212 $form->field(name => "name");
213 if ($session->param("name")) {
214 $form->field(name => "name", value => $session->param("name"));
218 elsif ($form->title eq "preferences") {
219 my $user=$session->param("name");
220 if (! IkiWiki::openiduser($user)) {
221 $form->field(name => "name", disabled => 1,
222 value => $user, force => 1,
223 fieldset => "login");
224 $form->field(name => "password", type => "password",
227 shift eq $form->field("confirm_password");
229 $form->field(name => "confirm_password", type => "password",
232 shift eq $form->field("password");
235 my $userpage=IkiWiki::userpage($user);
236 if (exists $pagesources{$userpage}) {
237 $form->text(gettext("Your user page: ").
238 htmllink("", "", $userpage,
239 noimageinline => 1));
242 $form->text("<a href=\"".
243 IkiWiki::cgiurl(do => "edit", page => $userpage).
244 "\">".gettext("Create your user page")."</a>");
250 sub formbuilder (@) {
253 my $form=$params{form};
254 my $session=$params{session};
255 my $cgi=$params{cgi};
256 my $buttons=$params{buttons};
258 my $do_register=defined $cgi->param("do") && $cgi->param("do") eq "register";
260 if ($form->title eq "signin" || $form->title eq "register") {
261 if (($form->submitted && $form->validate) || $do_register) {
262 if ($form->submitted eq 'Login') {
263 $session->param("name", $form->field("name"));
264 IkiWiki::cgi_postsignin($cgi, $session);
266 elsif ($form->submitted eq 'Create Account') {
267 my $user_name=$form->field('name');
268 if (IkiWiki::userinfo_setall($user_name, {
269 'email' => $form->field('email'),
270 'regdate' => time})) {
271 setpassword($user_name, $form->field('password'));
272 $form->field(name => "confirm_password", type => "hidden");
273 $form->field(name => "email", type => "hidden");
274 $form->text(gettext("Account creation successful. Now you can Login."));
277 error(gettext("Error creating account."));
280 elsif ($form->submitted eq 'Reset Password') {
281 my $user_name=$form->field("name");
282 my $email=IkiWiki::userinfo_get($user_name, "email");
283 if (! length $email) {
284 error(gettext("No email address, so cannot email password reset instructions."));
287 # Store a token that can be used once
288 # to log the user in. This needs to be hard
289 # to guess. Generating a cgi session id will
290 # make it as hard to guess as any cgi session.
291 eval q{use CGI::Session};
293 my $token = CGI::Session->new->id;
294 setpassword($user_name, $token, "resettoken");
296 my $template=template("passwordmail.tmpl");
298 user_name => $user_name,
299 passwordurl => IkiWiki::cgiurl(
301 'name' => $user_name,
304 wikiurl => $config{url},
305 wikiname => $config{wikiname},
306 remote_addr => $session->remote_addr(),
309 eval q{use Mail::Sendmail};
312 To => IkiWiki::userinfo_get($user_name, "email"),
313 From => "$config{wikiname} admin <".
314 (defined $config{adminemail} ? $config{adminemail} : "")
316 Subject => "$config{wikiname} information",
317 Message => $template->output,
318 ) or error(gettext("Failed to send mail"));
320 $form->text(gettext("You have been mailed password reset instructions."));
321 $form->field(name => "name", required => 0);
322 push @$buttons, "Reset Password";
324 elsif ($form->submitted eq "Register" || $do_register) {
325 @$buttons="Create Account";
328 elsif ($form->submitted eq "Create Account") {
329 @$buttons="Create Account";
332 push @$buttons, "Register", "Reset Password";
335 elsif ($form->title eq "preferences") {
336 if ($form->submitted eq "Save Preferences" && $form->validate) {
337 my $user_name=$form->field('name');
338 if (defined $form->field("password") && length $form->field("password")) {
339 setpassword($user_name, $form->field('password'));
345 sub sessioncgi ($$) {
349 if ($q->param('do') eq 'reset') {
350 my $name=$q->param("name");
351 my $token=$q->param("token");
353 if (! defined $name || ! defined $token ||
354 ! length $name || ! length $token) {
355 error(gettext("incorrect password reset url"));
357 if (! checkpassword($name, $token, "resettoken")) {
358 error(gettext("password reset denied"));
361 $session->param("name", $name);
362 IkiWiki::cgi_prefs($q, $session);
365 elsif ($q->param("do") eq "register") {
366 # After registration, need to go somewhere, so show prefs page.
367 $session->param(postsignin => "do=prefs");
368 # Due to do=register, this will run in registration-only
370 IkiWiki::cgi_signin($q, $session);
376 # While this hook is not currently used, it needs to exist
377 # so ikiwiki knows that the wiki supports logins, and will
378 # enable the Preferences page.