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") {
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") {
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 $form->field(name => "name", disabled => 1,
211 value => $session->param("name"), force => 1,
212 fieldset => "login");
213 $form->field(name => "password", type => "password",
216 shift eq $form->field("confirm_password");
218 $form->field(name => "confirm_password", type => "password",
221 shift eq $form->field("password");
226 sub formbuilder (@) {
229 my $form=$params{form};
230 my $session=$params{session};
231 my $cgi=$params{cgi};
232 my $buttons=$params{buttons};
234 if ($form->title eq "signin" || $form->title eq "register") {
235 if ($form->submitted && $form->validate) {
236 if ($form->submitted eq 'Login') {
237 $session->param("name", $form->field("name"));
238 IkiWiki::cgi_postsignin($cgi, $session);
240 elsif ($form->submitted eq 'Create Account') {
241 my $user_name=$form->field('name');
242 if (IkiWiki::userinfo_setall($user_name, {
243 'email' => $form->field('email'),
244 'regdate' => time})) {
245 setpassword($user_name, $form->field('password'));
246 $form->field(name => "confirm_password", type => "hidden");
247 $form->field(name => "email", type => "hidden");
248 $form->text(gettext("Account creation successful. Now you can Login."));
251 error(gettext("Error creating account."));
254 elsif ($form->submitted eq 'Reset Password') {
255 my $user_name=$form->field("name");
256 my $email=IkiWiki::userinfo_get($user_name, "email");
257 if (! length $email) {
258 error(gettext("No email address, so cannot email password reset instructions."));
261 # Store a token that can be used once
262 # to log the user in. This needs to be hard
263 # to guess. Generating a cgi session id will
264 # make it as hard to guess as any cgi session.
265 eval q{use CGI::Session};
267 my $token = CGI::Session->new->id;
268 setpassword($user_name, $token, "resettoken");
270 my $template=template("passwordmail.tmpl");
272 user_name => $user_name,
273 passwordurl => IkiWiki::cgiurl(
275 'name' => $user_name,
278 wikiurl => $config{url},
279 wikiname => $config{wikiname},
280 REMOTE_ADDR => $ENV{REMOTE_ADDR},
283 eval q{use Mail::Sendmail};
286 To => IkiWiki::userinfo_get($user_name, "email"),
287 From => "$config{wikiname} admin <".
288 (defined $config{adminemail} ? $config{adminemail} : "")
290 Subject => "$config{wikiname} information",
291 Message => $template->output,
292 ) or error(gettext("Failed to send mail"));
294 $form->text(gettext("You have been mailed password reset instructions."));
295 $form->field(name => "name", required => 0);
296 push @$buttons, "Reset Password";
298 elsif ($form->submitted eq "Register") {
299 @$buttons="Create Account";
302 elsif ($form->submitted eq "Create Account") {
303 @$buttons="Create Account";
306 push @$buttons, "Register", "Reset Password";
309 elsif ($form->title eq "preferences") {
310 if ($form->submitted eq "Save Preferences" && $form->validate) {
311 my $user_name=$form->field('name');
312 if ($form->field("password") && length $form->field("password")) {
313 setpassword($user_name, $form->field('password'));
319 sub sessioncgi ($$) {
323 if ($q->param('do') eq 'reset') {
324 my $name=$q->param("name");
325 my $token=$q->param("token");
327 if (! defined $name || ! defined $token ||
328 ! length $name || ! length $token) {
329 error(gettext("incorrect password reset url"));
331 if (! checkpassword($name, $token, "resettoken")) {
332 error(gettext("password reset denied"));
335 $session->param("name", $name);
336 IkiWiki::cgi_prefs($q, $session);
342 # While this hook is not currently used, it needs to exist
343 # so ikiwiki knows that the wiki supports logins, and will
344 # enable the Preferences page.