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