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