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 my $do_register=defined $cgi->param("do") && $cgi->param("do") eq "register";
110 if ($form->title eq "signin" || $form->title eq "register" || $do_register) {
111 $form->field(name => "name", required => 0);
112 $form->field(name => "password", type => "password", required => 0);
114 if ($form->submitted eq "Register" || $form->submitted eq "Create Account" || $do_register) {
115 $form->field(name => "confirm_password", type => "password");
116 $form->field(name => "account_creation_password", type => "password")
117 if (defined $config{account_creation_password} &&
118 length $config{account_creation_password});
119 $form->field(name => "email", size => 50);
120 $form->title("register");
123 $form->field(name => "confirm_password",
125 shift eq $form->field("password");
128 $form->field(name => "password",
130 shift eq $form->field("confirm_password");
135 if ($form->submitted) {
136 my $submittype=$form->submitted;
137 # Set required fields based on how form was submitted.
139 "Login" => [qw(name password)],
141 "Create Account" => [qw(name password confirm_password email)],
142 "Reset Password" => [qw(name)],
144 foreach my $opt (@{$required{$submittype}}) {
145 $form->field(name => $opt, required => 1);
148 if ($submittype eq "Create Account") {
150 name => "account_creation_password",
152 shift eq $config{account_creation_password};
155 ) if (defined $config{account_creation_password} &&
156 length $config{account_creation_password});
163 # Validate password against name for Login.
164 if ($submittype eq "Login") {
168 checkpassword($form->field("name"), shift);
172 elsif ($submittype eq "Register" ||
173 $submittype eq "Create Account" ||
174 $submittype eq "Reset Password") {
175 $form->field(name => "password", validate => 'VALUE');
178 # And make sure the entered name exists when logging
179 # in or sending email, and does not when registering.
180 if ($submittype eq 'Create Account' ||
181 $submittype eq 'Register') {
187 $name=~/$config{wiki_file_regexp}/ &&
188 ! IkiWiki::userinfo_get($name, "regdate");
192 elsif ($submittype eq "Login" ||
193 $submittype eq "Reset Password") {
199 IkiWiki::userinfo_get($name, "regdate");
205 # First time settings.
206 $form->field(name => "name");
207 if ($session->param("name")) {
208 $form->field(name => "name", value => $session->param("name"));
212 elsif ($form->title eq "preferences") {
213 my $user=$session->param("name");
214 if (! IkiWiki::openiduser($user)) {
215 $form->field(name => "name", disabled => 1,
216 value => $user, force => 1,
217 fieldset => "login");
218 $form->field(name => "password", type => "password",
221 shift eq $form->field("confirm_password");
223 $form->field(name => "confirm_password", type => "password",
226 shift eq $form->field("password");
229 my $userpage=IkiWiki::userpage($user);
230 if (exists $pagesources{$userpage}) {
231 $form->text(gettext("Your user page: ").
232 htmllink("", "", $userpage,
233 noimageinline => 1));
236 $form->text("<a href=\"".
237 IkiWiki::cgiurl(do => "edit", page => $userpage).
238 "\">".gettext("Create your user page")."</a>");
244 sub formbuilder (@) {
247 my $form=$params{form};
248 my $session=$params{session};
249 my $cgi=$params{cgi};
250 my $buttons=$params{buttons};
252 my $do_register=defined $cgi->param("do") && $cgi->param("do") eq "register";
254 if ($form->title eq "signin" || $form->title eq "register") {
255 if (($form->submitted && $form->validate) || $do_register) {
256 if ($form->submitted eq 'Login') {
257 $session->param("name", $form->field("name"));
258 IkiWiki::cgi_postsignin($cgi, $session);
260 elsif ($form->submitted eq 'Create Account') {
261 my $user_name=$form->field('name');
262 if (IkiWiki::userinfo_setall($user_name, {
263 'email' => $form->field('email'),
264 'regdate' => time})) {
265 setpassword($user_name, $form->field('password'));
266 $form->field(name => "confirm_password", type => "hidden");
267 $form->field(name => "email", type => "hidden");
268 $form->text(gettext("Account creation successful. Now you can Login."));
271 error(gettext("Error creating account."));
274 elsif ($form->submitted eq 'Reset Password') {
275 my $user_name=$form->field("name");
276 my $email=IkiWiki::userinfo_get($user_name, "email");
277 if (! length $email) {
278 error(gettext("No email address, so cannot email password reset instructions."));
281 # Store a token that can be used once
282 # to log the user in. This needs to be hard
283 # to guess. Generating a cgi session id will
284 # make it as hard to guess as any cgi session.
285 eval q{use CGI::Session};
287 my $token = CGI::Session->new->id;
288 setpassword($user_name, $token, "resettoken");
290 my $template=template("passwordmail.tmpl");
292 user_name => $user_name,
293 passwordurl => IkiWiki::cgiurl(
295 'name' => $user_name,
298 wikiurl => $config{url},
299 wikiname => $config{wikiname},
300 remote_addr => $session->remote_addr(),
303 eval q{use Mail::Sendmail};
306 To => IkiWiki::userinfo_get($user_name, "email"),
307 From => "$config{wikiname} admin <".
308 (defined $config{adminemail} ? $config{adminemail} : "")
310 Subject => "$config{wikiname} information",
311 Message => $template->output,
312 ) or error(gettext("Failed to send mail"));
314 $form->text(gettext("You have been mailed password reset instructions."));
315 $form->field(name => "name", required => 0);
316 push @$buttons, "Reset Password";
318 elsif ($form->submitted eq "Register" || $do_register) {
319 @$buttons="Create Account";
322 elsif ($form->submitted eq "Create Account") {
323 @$buttons="Create Account";
326 push @$buttons, "Register", "Reset Password";
329 elsif ($form->title eq "preferences") {
330 if ($form->submitted eq "Save Preferences" && $form->validate) {
331 my $user_name=$form->field('name');
332 if ($form->field("password") && length $form->field("password")) {
333 setpassword($user_name, $form->field('password'));
339 sub sessioncgi ($$) {
343 if ($q->param('do') eq 'reset') {
344 my $name=$q->param("name");
345 my $token=$q->param("token");
347 if (! defined $name || ! defined $token ||
348 ! length $name || ! length $token) {
349 error(gettext("incorrect password reset url"));
351 if (! checkpassword($name, $token, "resettoken")) {
352 error(gettext("password reset denied"));
355 $session->param("name", $name);
356 IkiWiki::cgi_prefs($q, $session);
359 elsif ($q->param("do") eq "register") {
360 # After registration, need to go somewhere, so show prefs page.
361 $session->param(postsignin => "do=prefs");
362 # Due to do=register, this will run in registration-only
364 IkiWiki::cgi_signin($q, $session);
370 # While this hook is not currently used, it needs to exist
371 # so ikiwiki knows that the wiki supports logins, and will
372 # enable the Preferences page.