summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/passwordauth.pm
blob: b70f9b788ed90dc0a30fd6935b1f43389a29ac19 (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. IkiWiki::openiduser($session->param("name"))) {
  191. $form->field(name => "name", disabled => 1,
  192. value => $session->param("name"), force => 1,
  193. fieldset => "login");
  194. $form->field(name => "password", type => "password",
  195. fieldset => "login",
  196. validate => sub {
  197. shift eq $form->field("confirm_password");
  198. }),
  199. $form->field(name => "confirm_password", type => "password",
  200. fieldset => "login",
  201. validate => sub {
  202. shift eq $form->field("password");
  203. }),
  204. }
  205. }
  206. sub formbuilder (@) {
  207. my %params=@_;
  208. my $form=$params{form};
  209. my $session=$params{session};
  210. my $cgi=$params{cgi};
  211. my $buttons=$params{buttons};
  212. if ($form->title eq "signin" || $form->title eq "register") {
  213. if (($form->submitted && $form->validate) || $cgi->param("do") eq "register") {
  214. if ($form->submitted eq 'Login') {
  215. $session->param("name", $form->field("name"));
  216. IkiWiki::cgi_postsignin($cgi, $session);
  217. }
  218. elsif ($form->submitted eq 'Create Account') {
  219. my $user_name=$form->field('name');
  220. if (IkiWiki::userinfo_setall($user_name, {
  221. 'email' => $form->field('email'),
  222. 'regdate' => time})) {
  223. setpassword($user_name, $form->field('password'));
  224. $form->field(name => "confirm_password", type => "hidden");
  225. $form->field(name => "email", type => "hidden");
  226. $form->text(gettext("Account creation successful. Now you can Login."));
  227. }
  228. else {
  229. error(gettext("Error creating account."));
  230. }
  231. }
  232. elsif ($form->submitted eq 'Reset Password') {
  233. my $user_name=$form->field("name");
  234. my $email=IkiWiki::userinfo_get($user_name, "email");
  235. if (! length $email) {
  236. error(gettext("No email address, so cannot email password reset instructions."));
  237. }
  238. # Store a token that can be used once
  239. # to log the user in. This needs to be hard
  240. # to guess. Generating a cgi session id will
  241. # make it as hard to guess as any cgi session.
  242. eval q{use CGI::Session};
  243. error($@) if $@;
  244. my $token = CGI::Session->new->id;
  245. setpassword($user_name, $token, "resettoken");
  246. my $template=template("passwordmail.tmpl");
  247. $template->param(
  248. user_name => $user_name,
  249. passwordurl => IkiWiki::cgiurl(
  250. 'do' => "reset",
  251. 'name' => $user_name,
  252. 'token' => $token,
  253. ),
  254. wikiurl => $config{url},
  255. wikiname => $config{wikiname},
  256. REMOTE_ADDR => $ENV{REMOTE_ADDR},
  257. );
  258. eval q{use Mail::Sendmail};
  259. error($@) if $@;
  260. sendmail(
  261. To => IkiWiki::userinfo_get($user_name, "email"),
  262. From => "$config{wikiname} admin <".
  263. (defined $config{adminemail} ? $config{adminemail} : "")
  264. .">",
  265. Subject => "$config{wikiname} information",
  266. Message => $template->output,
  267. ) or error(gettext("Failed to send mail"));
  268. $form->text(gettext("You have been mailed password reset instructions."));
  269. $form->field(name => "name", required => 0);
  270. push @$buttons, "Reset Password";
  271. }
  272. elsif ($form->submitted eq "Register" || $cgi->param("do") eq "register") {
  273. @$buttons="Create Account";
  274. }
  275. }
  276. elsif ($form->submitted eq "Create Account") {
  277. @$buttons="Create Account";
  278. }
  279. else {
  280. push @$buttons, "Register", "Reset Password";
  281. }
  282. }
  283. elsif ($form->title eq "preferences") {
  284. if ($form->submitted eq "Save Preferences" && $form->validate) {
  285. my $user_name=$form->field('name');
  286. if ($form->field("password") && length $form->field("password")) {
  287. setpassword($user_name, $form->field('password'));
  288. }
  289. }
  290. }
  291. }
  292. sub sessioncgi ($$) {
  293. my $q=shift;
  294. my $session=shift;
  295. if ($q->param('do') eq 'reset') {
  296. my $name=$q->param("name");
  297. my $token=$q->param("token");
  298. if (! defined $name || ! defined $token ||
  299. ! length $name || ! length $token) {
  300. error(gettext("incorrect password reset url"));
  301. }
  302. if (! checkpassword($name, $token, "resettoken")) {
  303. error(gettext("password reset denied"));
  304. }
  305. $session->param("name", $name);
  306. IkiWiki::cgi_prefs($q, $session);
  307. exit;
  308. }
  309. elsif ($q->param("do") eq "register") {
  310. # After registration, need to go somewhere, so show prefs page.
  311. $session->param(postsignin => "do=prefs");
  312. # Due to do=register, this will run in registration-only
  313. # mode.
  314. IkiWiki::cgi_signin($q, $session);
  315. exit;
  316. }
  317. }
  318. sub auth ($$) {
  319. # While this hook is not currently used, it needs to exist
  320. # so ikiwiki knows that the wiki supports logins, and will
  321. # enable the Preferences page.
  322. }
  323. 1