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