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);
23 account_creation_password => {
26 description => "a password that must be entered when signing up for an account",
33 description => "cost of generating a password using Authen::Passphrase::BlowfishCrypt",
39 # Checks if a string matches a user's password, and returns true or false.
40 sub checkpassword ($$;$) {
43 my $field=shift || "password";
45 # It's very important that the user not be allowed to log in with
47 if (! length $password) {
51 my $userinfo=IkiWiki::userinfo_retrieve();
52 if (! length $user || ! defined $userinfo ||
53 ! exists $userinfo->{$user} || ! ref $userinfo->{$user}) {
58 if (exists $userinfo->{$user}->{"crypt".$field}) {
59 eval q{use Authen::Passphrase};
61 my $p = Authen::Passphrase->from_crypt($userinfo->{$user}->{"crypt".$field});
62 $ret=$p->match($password);
64 elsif (exists $userinfo->{$user}->{$field}) {
65 $ret=$password eq $userinfo->{$user}->{$field};
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);
80 sub setpassword ($$;$) {
83 my $field=shift || "password";
85 eval q{use Authen::Passphrase::BlowfishCrypt};
87 my $p = Authen::Passphrase::BlowfishCrypt->new(
88 cost => $config{password_cost} || 8,
90 passphrase => $password,
92 IkiWiki::userinfo_set($user, "crypt$field", $p->as_crypt);
93 IkiWiki::userinfo_set($user, $field, "");
96 IkiWiki::userinfo_set($user, $field, $password);
100 sub formbuilder_setup (@) {
103 my $form=$params{form};
104 my $session=$params{session};
105 my $cgi=$params{cgi};
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);
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");
120 $form->field(name => "confirm_password",
122 shift eq $form->field("password");
125 $form->field(name => "password",
127 shift eq $form->field("confirm_password");
132 if ($form->submitted) {
133 my $submittype=$form->submitted;
134 # Set required fields based on how form was submitted.
136 "Login" => [qw(name password)],
138 "Create Account" => [qw(name password confirm_password email)],
139 "Reset Password" => [qw(name)],
141 foreach my $opt (@{$required{$submittype}}) {
142 $form->field(name => $opt, required => 1);
145 if ($submittype eq "Create Account") {
147 name => "account_creation_password",
149 shift eq $config{account_creation_password};
152 ) if (defined $config{account_creation_password} &&
153 length $config{account_creation_password});
160 # Validate password against name for Login.
161 if ($submittype eq "Login") {
165 checkpassword($form->field("name"), shift);
169 elsif ($submittype eq "Register" ||
170 $submittype eq "Create Account" ||
171 $submittype eq "Reset Password") {
172 $form->field(name => "password", validate => 'VALUE');
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') {
184 $name=~/$config{wiki_file_regexp}/ &&
185 ! IkiWiki::userinfo_get($name, "regdate");
189 elsif ($submittype eq "Login" ||
190 $submittype eq "Reset Password") {
196 IkiWiki::userinfo_get($name, "regdate");
202 # First time settings.
203 $form->field(name => "name");
204 if ($session->param("name")) {
205 $form->field(name => "name", value => $session->param("name"));
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",
218 shift eq $form->field("confirm_password");
220 $form->field(name => "confirm_password", type => "password",
223 shift eq $form->field("password");
226 my $userpage=$config{userdir} ? $config{userdir}."/".$user : $user;
227 if (exists $pagesources{$userpage}) {
228 $form->text(gettext("Your user page: ").
229 htmllink("", "", $userpage,
230 noimageinline => 1));
233 $form->text("<a href=\"".
234 IkiWiki::cgiurl(do => "edit", page => $userpage).
235 "\">".gettext("Create your user page")."</a>");
241 sub formbuilder (@) {
244 my $form=$params{form};
245 my $session=$params{session};
246 my $cgi=$params{cgi};
247 my $buttons=$params{buttons};
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);
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."));
266 error(gettext("Error creating account."));
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."));
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};
282 my $token = CGI::Session->new->id;
283 setpassword($user_name, $token, "resettoken");
285 my $template=template("passwordmail.tmpl");
287 user_name => $user_name,
288 passwordurl => IkiWiki::cgiurl(
290 'name' => $user_name,
293 wikiurl => $config{url},
294 wikiname => $config{wikiname},
295 REMOTE_ADDR => $ENV{REMOTE_ADDR},
298 eval q{use Mail::Sendmail};
301 To => IkiWiki::userinfo_get($user_name, "email"),
302 From => "$config{wikiname} admin <".
303 (defined $config{adminemail} ? $config{adminemail} : "")
305 Subject => "$config{wikiname} information",
306 Message => $template->output,
307 ) or error(gettext("Failed to send mail"));
309 $form->text(gettext("You have been mailed password reset instructions."));
310 $form->field(name => "name", required => 0);
311 push @$buttons, "Reset Password";
313 elsif ($form->submitted eq "Register" || $cgi->param("do") eq "register") {
314 @$buttons="Create Account";
317 elsif ($form->submitted eq "Create Account") {
318 @$buttons="Create Account";
321 push @$buttons, "Register", "Reset Password";
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'));
334 sub sessioncgi ($$) {
338 if ($q->param('do') eq 'reset') {
339 my $name=$q->param("name");
340 my $token=$q->param("token");
342 if (! defined $name || ! defined $token ||
343 ! length $name || ! length $token) {
344 error(gettext("incorrect password reset url"));
346 if (! checkpassword($name, $token, "resettoken")) {
347 error(gettext("password reset denied"));
350 $session->param("name", $name);
351 IkiWiki::cgi_prefs($q, $session);
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
359 IkiWiki::cgi_signin($q, $session);
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.