summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/passwordauth.pm
blob: f3f1aa4bf2b7b5f761859d7a75a2c0d68f8b4eb7 (plain)
  1. #!/usr/bin/perl
  2. # Ikiwiki password authentication.
  3. package IkiWiki::Plugin::passwordauth;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 2.00;
  7. sub import { #{{{
  8. hook(type => "formbuilder_setup", id => "passwordauth",
  9. call => \&formbuilder_setup);
  10. hook(type => "formbuilder", id => "passwordauth",
  11. call => \&formbuilder);
  12. hook(type => "sessioncgi", id => "passwordauth", call => \&sessioncgi);
  13. } # }}}
  14. # Checks if a string matches a user's password, and returns true or false.
  15. sub checkpassword ($$;$) { #{{{
  16. my $user=shift;
  17. my $password=shift;
  18. my $field=shift || "password";
  19. # It's very important that the user not be allowed to log in with
  20. # an empty password!
  21. if (! length $password) {
  22. return 0;
  23. }
  24. my $userinfo=IkiWiki::userinfo_retrieve();
  25. if (! length $user || ! defined $userinfo ||
  26. ! exists $userinfo->{$user} || ! ref $userinfo->{$user}) {
  27. return 0;
  28. }
  29. my $ret=0;
  30. if (exists $userinfo->{$user}->{"crypt".$field}) {
  31. eval q{use Authen::Passphrase};
  32. error $@ if $@;
  33. my $p = Authen::Passphrase->from_crypt($userinfo->{$user}->{"crypt".$field});
  34. $ret=$p->match($password);
  35. }
  36. elsif (exists $userinfo->{$user}->{$field}) {
  37. $ret=$password eq $userinfo->{$user}->{$field};
  38. }
  39. if ($ret &&
  40. (exists $userinfo->{$user}->{resettoken} ||
  41. exists $userinfo->{$user}->{cryptresettoken})) {
  42. # Clear reset token since the user has successfully logged in.
  43. delete $userinfo->{$user}->{resettoken};
  44. delete $userinfo->{$user}->{cryptresettoken};
  45. IkiWiki::userinfo_store($userinfo);
  46. }
  47. return $ret;
  48. } #}}}
  49. sub setpassword ($$;$) { #{{{
  50. my $user=shift;
  51. my $password=shift;
  52. my $field=shift || "password";
  53. eval q{use Authen::Passphrase::BlowfishCrypt};
  54. if (! $@) {
  55. my $p = Authen::Passphrase::BlowfishCrypt->new(
  56. cost => $config{password_cost} || 8,
  57. salt_random => 1,
  58. passphrase => $password,
  59. );
  60. IkiWiki::userinfo_set($user, "crypt$field", $p->as_crypt);
  61. IkiWiki::userinfo_set($user, $field, "");
  62. }
  63. else {
  64. IkiWiki::userinfo_set($user, $field, $password);
  65. }
  66. } #}}}
  67. sub formbuilder_setup (@) { #{{{
  68. my %params=@_;
  69. my $form=$params{form};
  70. my $session=$params{session};
  71. my $cgi=$params{cgi};
  72. if ($form->title eq "signin" || $form->title eq "register") {
  73. $form->field(name => "name", required => 0);
  74. $form->field(name => "password", type => "password", required => 0);
  75. if ($form->submitted eq "Register" || $form->submitted eq "Create Account") {
  76. $form->field(name => "confirm_password", type => "password");
  77. $form->field(name => "account_creation_password", type => "password") if (length $config{account_creation_password});
  78. $form->field(name => "email", size => 50);
  79. $form->title("register");
  80. $form->text("");
  81. $form->field(name => "confirm_password",
  82. validate => sub {
  83. shift eq $form->field("password");
  84. },
  85. );
  86. $form->field(name => "password",
  87. validate => sub {
  88. shift eq $form->field("confirm_password");
  89. },
  90. );
  91. }
  92. if ($form->submitted) {
  93. my $submittype=$form->submitted;
  94. # Set required fields based on how form was submitted.
  95. my %required=(
  96. "Login" => [qw(name password)],
  97. "Register" => [],
  98. "Create Account" => [qw(name password confirm_password email)],
  99. "Reset Password" => [qw(name)],
  100. );
  101. foreach my $opt (@{$required{$submittype}}) {
  102. $form->field(name => $opt, required => 1);
  103. }
  104. if ($submittype eq "Create Account") {
  105. $form->field(
  106. name => "account_creation_password",
  107. validate => sub {
  108. shift eq $config{account_creation_password};
  109. },
  110. required => 1,
  111. ) if (length $config{account_creation_password});
  112. $form->field(
  113. name => "email",
  114. validate => "EMAIL",
  115. );
  116. }
  117. # Validate password against name for Login.
  118. if ($submittype eq "Login") {
  119. $form->field(
  120. name => "password",
  121. validate => sub {
  122. checkpassword($form->field("name"), shift);
  123. },
  124. );
  125. }
  126. elsif ($submittype eq "Register" ||
  127. $submittype eq "Create Account" ||
  128. $submittype eq "Reset Password") {
  129. $form->field(name => "password", validate => 'VALUE');
  130. }
  131. # And make sure the entered name exists when logging
  132. # in or sending email, and does not when registering.
  133. if ($submittype eq 'Create Account' ||
  134. $submittype eq 'Register') {
  135. $form->field(
  136. name => "name",
  137. validate => sub {
  138. my $name=shift;
  139. length $name &&
  140. $name=~/$config{wiki_file_regexp}/ &&
  141. ! IkiWiki::userinfo_get($name, "regdate");
  142. },
  143. );
  144. }
  145. elsif ($submittype eq "Login" ||
  146. $submittype eq "Reset Password") {
  147. $form->field(
  148. name => "name",
  149. validate => sub {
  150. my $name=shift;
  151. length $name &&
  152. IkiWiki::userinfo_get($name, "regdate");
  153. },
  154. );
  155. }
  156. }
  157. else {
  158. # First time settings.
  159. $form->field(name => "name");
  160. if ($session->param("name")) {
  161. $form->field(name => "name", value => $session->param("name"));
  162. }
  163. }
  164. }
  165. elsif ($form->title eq "preferences") {
  166. $form->field(name => "name", disabled => 1,
  167. value => $session->param("name"), force => 1,
  168. fieldset => "login");
  169. $form->field(name => "password", type => "password",
  170. fieldset => "login",
  171. validate => sub {
  172. shift eq $form->field("confirm_password");
  173. }),
  174. $form->field(name => "confirm_password", type => "password",
  175. fieldset => "login",
  176. validate => sub {
  177. shift eq $form->field("password");
  178. }),
  179. }
  180. }
  181. sub formbuilder (@) { #{{{
  182. my %params=@_;
  183. my $form=$params{form};
  184. my $session=$params{session};
  185. my $cgi=$params{cgi};
  186. my $buttons=$params{buttons};
  187. if ($form->title eq "signin" || $form->title eq "register") {
  188. if ($form->submitted && $form->validate) {
  189. if ($form->submitted eq 'Login') {
  190. $session->param("name", $form->field("name"));
  191. IkiWiki::cgi_postsignin($cgi, $session);
  192. }
  193. elsif ($form->submitted eq 'Create Account') {
  194. my $user_name=$form->field('name');
  195. if (IkiWiki::userinfo_setall($user_name, {
  196. 'email' => $form->field('email'),
  197. 'regdate' => time})) {
  198. setpassword($user_name, $form->field('password'));
  199. $form->field(name => "confirm_password", type => "hidden");
  200. $form->field(name => "email", type => "hidden");
  201. $form->text(gettext("Account creation successful. Now you can Login."));
  202. }
  203. else {
  204. error(gettext("Error creating account."));
  205. }
  206. }
  207. elsif ($form->submitted eq 'Reset Password') {
  208. my $user_name=$form->field("name");
  209. my $email=IkiWiki::userinfo_get($user_name, "email");
  210. if (! length $email) {
  211. error(gettext("No email address, so cannot email password reset instructions."));
  212. }
  213. # Store a token that can be used once
  214. # to log the user in. This needs to be hard
  215. # to guess. Generating a cgi session id will
  216. # make it as hard to guess as any cgi session.
  217. eval q{use CGI::Session};
  218. error($@) if $@;
  219. my $token = CGI::Session->new->id;
  220. setpassword($user_name, $token, "resettoken");
  221. my $template=template("passwordmail.tmpl");
  222. $template->param(
  223. user_name => $user_name,
  224. passwordurl => IkiWiki::cgiurl(
  225. 'do' => "reset",
  226. 'name' => $user_name,
  227. 'token' => $token,
  228. ),
  229. wikiurl => $config{url},
  230. wikiname => $config{wikiname},
  231. REMOTE_ADDR => $ENV{REMOTE_ADDR},
  232. );
  233. eval q{use Mail::Sendmail};
  234. error($@) if $@;
  235. sendmail(
  236. To => IkiWiki::userinfo_get($user_name, "email"),
  237. From => "$config{wikiname} admin <$config{adminemail}>",
  238. Subject => "$config{wikiname} information",
  239. Message => $template->output,
  240. ) or error(gettext("Failed to send mail"));
  241. $form->text(gettext("You have been mailed password reset instructions."));
  242. $form->field(name => "name", required => 0);
  243. push @$buttons, "Reset Password";
  244. }
  245. elsif ($form->submitted eq "Register") {
  246. @$buttons="Create Account";
  247. }
  248. }
  249. elsif ($form->submitted eq "Create Account") {
  250. @$buttons="Create Account";
  251. }
  252. else {
  253. push @$buttons, "Register", "Reset Password";
  254. }
  255. }
  256. elsif ($form->title eq "preferences") {
  257. if ($form->submitted eq "Save Preferences" && $form->validate) {
  258. my $user_name=$form->field('name');
  259. if ($form->field("password") && length $form->field("password")) {
  260. setpassword($user_name, $form->field('password'));
  261. }
  262. }
  263. }
  264. } #}}}
  265. sub sessioncgi ($$) { #{{{
  266. my $q=shift;
  267. my $session=shift;
  268. if ($q->param('do') eq 'reset') {
  269. my $name=$q->param("name");
  270. my $token=$q->param("token");
  271. if (! defined $name || ! defined $token ||
  272. ! length $name || ! length $token) {
  273. error(gettext("incorrect password reset url"));
  274. }
  275. if (! checkpassword($name, $token, "resettoken")) {
  276. error(gettext("password reset denied"));
  277. }
  278. $session->param("name", $name);
  279. IkiWiki::cgi_prefs($q, $session);
  280. exit;
  281. }
  282. } #}}}
  283. 1