file to name it for gitweb.
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" => [],