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);
101 sub formbuilder_setup (@) {
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);
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");
121 $form->field(name => "confirm_password",
123 shift eq $form->field("password");
126 $form->field(name => "password",
128 shift eq $form->field("confirm_password");
133 if ($form->submitted) {
134 my $submittype=$form->submitted;
135 # Set required fields based on how form was submitted.
137 "Login" => [qw(name password)],
139 "Create Account" => [qw(name password confirm_password email)],
140 "Reset Password" => [qw(name)],
142 foreach my $opt (@{$required{$submittype}}) {
143 $form->field(name => $opt, required => 1);
146 if ($submittype eq "Create Account") {
148 name => "account_creation_password",
150 shift eq $config{account_creation_password};
153 ) if (defined $config{account_creation_password} &&
154 length $config{account_creation_password});
161 # Validate password against name for Login.
162 if ($submittype eq "Login") {
166 checkpassword($form->field("name"), shift);
170 elsif ($submittype eq "Register" ||
171 $submittype eq "Create Account" ||
172 $submittype eq "Reset Password") {
173 $form->field(name => "password", validate => 'VALUE');
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') {
185 $name=~/$config{wiki_file_regexp}/ &&
186 ! IkiWiki::userinfo_get($name, "regdate");
190 elsif ($submittype eq "Login" ||
191 $submittype eq "Reset Password") {
197 IkiWiki::userinfo_get($name, "regdate");
203 # First time settings.
204 $form->field(name => "name");
205 if ($session->param("name")) {
206 $form->field(name => "name", value => $session->param("name"));
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",
219 shift eq $form->field("confirm_password");
221 $form->field(name => "confirm_password", type => "password",
224 shift eq $form->field("password");
227 my $userpage=IkiWiki::userpage($user);
228 if (exists $pagesources{$userpage}) {
229 $form->text(gettext("Your user page: ").
230 htmllink("", "", $userpage,
231 noimageinline => 1));
234 $form->text("<a href=\"".
235 IkiWiki::cgiurl(do => "edit", page => $userpage).
236 "\">".gettext("Create your user page")."</a>");
242 sub formbuilder (@) {
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);
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."));
267 error(gettext("Error creating account."));
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."));
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};
283 my $token = CGI::Session->new->id;
284 setpassword($user_name, $token, "resettoken");
286 my $template=template("passwordmail.tmpl");
288 user_name => $user_name,
289 passwordurl => IkiWiki::cgiurl(
291 'name' => $user_name,
294 wikiurl => $config{url},
295 wikiname => $config{wikiname},
296 REMOTE_ADDR => $ENV{REMOTE_ADDR},
299 eval q{use Mail::Sendmail};
302 To => IkiWiki::userinfo_get($user_name, "email"),
303 From => "$config{wikiname} admin <".
304 (defined $config{adminemail} ? $config{adminemail} : "")
306 Subject => "$config{wikiname} information",
307 Message => $template->output,
308 ) or error(gettext("Failed to send mail"));
310 $form->text(gettext("You have been mailed password reset instructions."));
311 $form->field(name => "name", required => 0);
312 push @$buttons, "Reset Password";
314 elsif ($form->submitted eq "Register" || $cgi->param("do") eq "register") {
315 @$buttons="Create Account";
318 elsif ($form->submitted eq "Create Account") {
319 @$buttons="Create Account";
322 push @$buttons, "Register", "Reset Password";
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'));
335 sub sessioncgi ($$) {
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"));
347 if (! checkpassword($name, $token, "resettoken")) {
348 error(gettext("password reset denied"));
351 $session->param("name", $name);
352 IkiWiki::cgi_prefs($q, $session);
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
360 IkiWiki::cgi_signin($q, $session);
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.