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);
22 account_creation_password => {
25 description => "a password that must be entered when signing up for an account",
32 description => "cost of generating a password using Authen::Passphrase::BlowfishCrypt",
38 # Checks if a string matches a user's password, and returns true or false.
39 sub checkpassword ($$;$) {
42 my $field=shift || "password";
44 # It's very important that the user not be allowed to log in with
46 if (! length $password) {
50 my $userinfo=IkiWiki::userinfo_retrieve();
51 if (! length $user || ! defined $userinfo ||
52 ! exists $userinfo->{$user} || ! ref $userinfo->{$user}) {
57 if (exists $userinfo->{$user}->{"crypt".$field}) {
58 eval q{use Authen::Passphrase};
60 my $p = Authen::Passphrase->from_crypt($userinfo->{$user}->{"crypt".$field});
61 $ret=$p->match($password);
63 elsif (exists $userinfo->{$user}->{$field}) {
64 $ret=$password eq $userinfo->{$user}->{$field};
68 (exists $userinfo->{$user}->{resettoken} ||
69 exists $userinfo->{$user}->{cryptresettoken})) {
70 # Clear reset token since the user has successfully logged in.
71 delete $userinfo->{$user}->{resettoken};
72 delete $userinfo->{$user}->{cryptresettoken};
73 IkiWiki::userinfo_store($userinfo);
79 sub setpassword ($$;$) {
82 my $field=shift || "password";
84 eval q{use Authen::Passphrase::BlowfishCrypt};
86 my $p = Authen::Passphrase::BlowfishCrypt->new(
87 cost => $config{password_cost} || 8,
89 passphrase => $password,
91 IkiWiki::userinfo_set($user, "crypt$field", $p->as_crypt);
92 IkiWiki::userinfo_set($user, $field, "");
95 IkiWiki::userinfo_set($user, $field, $password);
99 sub formbuilder_setup (@) {
102 my $form=$params{form};
103 my $session=$params{session};
104 my $cgi=$params{cgi};
106 if ($form->title eq "signin" || $form->title eq "register") {
107 $form->field(name => "name", required => 0);
108 $form->field(name => "password", type => "password", required => 0);
110 if ($form->submitted eq "Register" || $form->submitted eq "Create Account") {
111 $form->field(name => "confirm_password", type => "password");
112 $form->field(name => "account_creation_password", type => "password")
113 if (defined $config{account_creation_password} &&
114 length $config{account_creation_password});
115 $form->field(name => "email", size => 50);
116 $form->title("register");
119 $form->field(name => "confirm_password",
121 shift eq $form->field("password");
124 $form->field(name => "password",
126 shift eq $form->field("confirm_password");
131 if ($form->submitted) {
132 my $submittype=$form->submitted;
133 # Set required fields based on how form was submitted.
135 "Login" => [qw(name password)],
137 "Create Account" => [qw(name password confirm_password email)],
138 "Reset Password" => [qw(name)],
140 foreach my $opt (@{$required{$submittype}}) {
141 $form->field(name => $opt, required => 1);
144 if ($submittype eq "Create Account") {
146 name => "account_creation_password",
148 shift eq $config{account_creation_password};
151 ) if (defined $config{account_creation_password} &&
152 length $config{account_creation_password});
159 # Validate password against name for Login.
160 if ($submittype eq "Login") {
164 checkpassword($form->field("name"), shift);
168 elsif ($submittype eq "Register" ||
169 $submittype eq "Create Account" ||
170 $submittype eq "Reset Password") {
171 $form->field(name => "password", validate => 'VALUE');
174 # And make sure the entered name exists when logging
175 # in or sending email, and does not when registering.
176 if ($submittype eq 'Create Account' ||
177 $submittype eq 'Register') {
183 $name=~/$config{wiki_file_regexp}/ &&
184 ! IkiWiki::userinfo_get($name, "regdate");
188 elsif ($submittype eq "Login" ||
189 $submittype eq "Reset Password") {
195 IkiWiki::userinfo_get($name, "regdate");
201 # First time settings.
202 $form->field(name => "name");
203 if ($session->param("name")) {
204 $form->field(name => "name", value => $session->param("name"));
208 elsif ($form->title eq "preferences") {
209 $form->field(name => "name", disabled => 1,
210 value => $session->param("name"), force => 1,
211 fieldset => "login");
212 $form->field(name => "password", type => "password",
215 shift eq $form->field("confirm_password");
217 $form->field(name => "confirm_password", type => "password",
220 shift eq $form->field("password");
225 sub formbuilder (@) {
228 my $form=$params{form};
229 my $session=$params{session};
230 my $cgi=$params{cgi};
231 my $buttons=$params{buttons};
233 if ($form->title eq "signin" || $form->title eq "register") {
234 if ($form->submitted && $form->validate) {
235 if ($form->submitted eq 'Login') {
236 $session->param("name", $form->field("name"));
237 IkiWiki::cgi_postsignin($cgi, $session);
239 elsif ($form->submitted eq 'Create Account') {
240 my $user_name=$form->field('name');
241 if (IkiWiki::userinfo_setall($user_name, {
242 'email' => $form->field('email'),
243 'regdate' => time})) {
244 setpassword($user_name, $form->field('password'));
245 $form->field(name => "confirm_password", type => "hidden");
246 $form->field(name => "email", type => "hidden");
247 $form->text(gettext("Account creation successful. Now you can Login."));
250 error(gettext("Error creating account."));
253 elsif ($form->submitted eq 'Reset Password') {
254 my $user_name=$form->field("name");
255 my $email=IkiWiki::userinfo_get($user_name, "email");
256 if (! length $email) {
257 error(gettext("No email address, so cannot email password reset instructions."));
260 # Store a token that can be used once
261 # to log the user in. This needs to be hard
262 # to guess. Generating a cgi session id will
263 # make it as hard to guess as any cgi session.
264 eval q{use CGI::Session};
266 my $token = CGI::Session->new->id;
267 setpassword($user_name, $token, "resettoken");
269 my $template=template("passwordmail.tmpl");
271 user_name => $user_name,
272 passwordurl => IkiWiki::cgiurl(
274 'name' => $user_name,
277 wikiurl => $config{url},
278 wikiname => $config{wikiname},
279 REMOTE_ADDR => $ENV{REMOTE_ADDR},
282 eval q{use Mail::Sendmail};
285 To => IkiWiki::userinfo_get($user_name, "email"),
286 From => "$config{wikiname} admin <".
287 (defined $config{adminemail} ? $config{adminemail} : "")
289 Subject => "$config{wikiname} information",
290 Message => $template->output,
291 ) or error(gettext("Failed to send mail"));
293 $form->text(gettext("You have been mailed password reset instructions."));
294 $form->field(name => "name", required => 0);
295 push @$buttons, "Reset Password";
297 elsif ($form->submitted eq "Register") {
298 @$buttons="Create Account";
301 elsif ($form->submitted eq "Create Account") {
302 @$buttons="Create Account";
305 push @$buttons, "Register", "Reset Password";
308 elsif ($form->title eq "preferences") {
309 if ($form->submitted eq "Save Preferences" && $form->validate) {
310 my $user_name=$form->field('name');
311 if ($form->field("password") && length $form->field("password")) {
312 setpassword($user_name, $form->field('password'));
318 sub sessioncgi ($$) {
322 if ($q->param('do') eq 'reset') {
323 my $name=$q->param("name");
324 my $token=$q->param("token");
326 if (! defined $name || ! defined $token ||
327 ! length $name || ! length $token) {
328 error(gettext("incorrect password reset url"));
330 if (! checkpassword($name, $token, "resettoken")) {
331 error(gettext("password reset denied"));
334 $session->param("name", $name);
335 IkiWiki::cgi_prefs($q, $session);