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