2 # Ikiwiki password authentication.
3 package IkiWiki::Plugin::passwordauth;
10 hook(type => "formbuilder_setup", id => "passwordauth",
11 call => \&formbuilder_setup);
12 hook(type => "formbuilder", id => "passwordauth",
13 call => \&formbuilder);
14 hook(type => "sessioncgi", id => "passwordauth", call => \&sessioncgi);
17 # Checks if a string matches a user's password, and returns true or false.
18 sub checkpassword ($$;$) { #{{{
21 my $field=shift || "password";
23 # It's very important that the user not be allowed to log in with
25 if (! length $password) {
29 my $userinfo=IkiWiki::userinfo_retrieve();
30 if (! length $user || ! defined $userinfo ||
31 ! exists $userinfo->{$user} || ! ref $userinfo->{$user}) {
36 if (exists $userinfo->{$user}->{"crypt".$field}) {
37 eval q{use Authen::Passphrase};
39 my $p = Authen::Passphrase->from_crypt($userinfo->{$user}->{"crypt".$field});
40 $ret=$p->match($password);
42 elsif (exists $userinfo->{$user}->{$field}) {
43 $ret=$password eq $userinfo->{$user}->{$field};
47 (exists $userinfo->{$user}->{resettoken} ||
48 exists $userinfo->{$user}->{cryptresettoken})) {
49 # Clear reset token since the user has successfully logged in.
50 delete $userinfo->{$user}->{resettoken};
51 delete $userinfo->{$user}->{cryptresettoken};
52 IkiWiki::userinfo_store($userinfo);
58 sub setpassword ($$;$) { #{{{
61 my $field=shift || "password";
63 eval q{use Authen::Passphrase::BlowfishCrypt};
65 my $p = Authen::Passphrase::BlowfishCrypt->new(
66 cost => $config{password_cost} || 8,
68 passphrase => $password,
70 IkiWiki::userinfo_set($user, "crypt$field", $p->as_crypt);
71 IkiWiki::userinfo_set($user, $field, "");
74 IkiWiki::userinfo_set($user, $field, $password);
78 sub formbuilder_setup (@) { #{{{
81 my $form=$params{form};
82 my $session=$params{session};
85 if ($form->title eq "signin" || $form->title eq "register") {
86 $form->field(name => "name", required => 0);
87 $form->field(name => "password", type => "password", required => 0);
89 if ($form->submitted eq "Register" || $form->submitted eq "Create Account") {
90 $form->field(name => "confirm_password", type => "password");
91 $form->field(name => "account_creation_password", type => "password") if (length $config{account_creation_password});
92 $form->field(name => "email", size => 50);
93 $form->title("register");
96 $form->field(name => "confirm_password",
98 shift eq $form->field("password");
101 $form->field(name => "password",
103 shift eq $form->field("confirm_password");
108 if ($form->submitted) {
109 my $submittype=$form->submitted;
110 # Set required fields based on how form was submitted.
112 "Login" => [qw(name password)],
114 "Create Account" => [qw(name password confirm_password email)],
115 "Reset Password" => [qw(name)],
117 foreach my $opt (@{$required{$submittype}}) {
118 $form->field(name => $opt, required => 1);
121 if ($submittype eq "Create Account") {
123 name => "account_creation_password",
125 shift eq $config{account_creation_password};
128 ) if (length $config{account_creation_password});
135 # Validate password against name for Login.
136 if ($submittype eq "Login") {
140 checkpassword($form->field("name"), shift);
144 elsif ($submittype eq "Register" ||
145 $submittype eq "Create Account" ||
146 $submittype eq "Reset Password") {
147 $form->field(name => "password", validate => 'VALUE');
150 # And make sure the entered name exists when logging
151 # in or sending email, and does not when registering.
152 if ($submittype eq 'Create Account' ||
153 $submittype eq 'Register') {
159 $name=~/$config{wiki_file_regexp}/ &&
160 ! IkiWiki::userinfo_get($name, "regdate");
164 elsif ($submittype eq "Login" ||
165 $submittype eq "Reset Password") {
171 IkiWiki::userinfo_get($name, "regdate");
177 # First time settings.
178 $form->field(name => "name");
179 if ($session->param("name")) {
180 $form->field(name => "name", value => $session->param("name"));
184 elsif ($form->title eq "preferences") {
185 $form->field(name => "name", disabled => 1,
186 value => $session->param("name"), force => 1,
187 fieldset => "login");
188 $form->field(name => "password", type => "password",
191 shift eq $form->field("confirm_password");
193 $form->field(name => "confirm_password", type => "password",
196 shift eq $form->field("password");
201 sub formbuilder (@) { #{{{
204 my $form=$params{form};
205 my $session=$params{session};
206 my $cgi=$params{cgi};
207 my $buttons=$params{buttons};
209 if ($form->title eq "signin" || $form->title eq "register") {
210 if ($form->submitted && $form->validate) {
211 if ($form->submitted eq 'Login') {
212 $session->param("name", $form->field("name"));
213 IkiWiki::cgi_postsignin($cgi, $session);
215 elsif ($form->submitted eq 'Create Account') {
216 my $user_name=$form->field('name');
217 if (IkiWiki::userinfo_setall($user_name, {
218 'email' => $form->field('email'),
219 'regdate' => time})) {
220 setpassword($user_name, $form->field('password'));
221 $form->field(name => "confirm_password", type => "hidden");
222 $form->field(name => "email", type => "hidden");
223 $form->text(gettext("Account creation successful. Now you can Login."));
226 error(gettext("Error creating account."));
229 elsif ($form->submitted eq 'Reset Password') {
230 my $user_name=$form->field("name");
231 my $email=IkiWiki::userinfo_get($user_name, "email");
232 if (! length $email) {
233 error(gettext("No email address, so cannot email password reset instructions."));
236 # Store a token that can be used once
237 # to log the user in. This needs to be hard
238 # to guess. Generating a cgi session id will
239 # make it as hard to guess as any cgi session.
240 eval q{use CGI::Session};
242 my $token = CGI::Session->new->id;
243 setpassword($user_name, $token, "resettoken");
245 my $template=template("passwordmail.tmpl");
247 user_name => $user_name,
248 passwordurl => IkiWiki::cgiurl(
250 'name' => $user_name,
253 wikiurl => $config{url},
254 wikiname => $config{wikiname},
255 REMOTE_ADDR => $ENV{REMOTE_ADDR},
258 eval q{use Mail::Sendmail};
261 To => IkiWiki::userinfo_get($user_name, "email"),
262 From => "$config{wikiname} admin <$config{adminemail}>",
263 Subject => "$config{wikiname} information",
264 Message => $template->output,
265 ) or error(gettext("Failed to send mail"));
267 $form->text(gettext("You have been mailed password reset instructions."));
268 $form->field(name => "name", required => 0);
269 push @$buttons, "Reset Password";
271 elsif ($form->submitted eq "Register") {
272 @$buttons="Create Account";
275 elsif ($form->submitted eq "Create Account") {
276 @$buttons="Create Account";
279 push @$buttons, "Register", "Reset Password";
282 elsif ($form->title eq "preferences") {
283 if ($form->submitted eq "Save Preferences" && $form->validate) {
284 my $user_name=$form->field('name');
285 if ($form->field("password") && length $form->field("password")) {
286 setpassword($user_name, $form->field('password'));
292 sub sessioncgi ($$) { #{{{
296 if ($q->param('do') eq 'reset') {
297 my $name=$q->param("name");
298 my $token=$q->param("token");
300 if (! defined $name || ! defined $token ||
301 ! length $name || ! length $token) {
302 error(gettext("incorrect password reset url"));
304 if (! checkpassword($name, $token, "resettoken")) {
305 error(gettext("password reset denied"));
308 $session->param("name", $name);
309 IkiWiki::cgi_prefs($q, $session);