summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/httpauth.pm
blob: cb488449dd680b2ae36aefde23636ba3fdfe6ab8 (plain)
  1. #!/usr/bin/perl
  2. # HTTP basic auth plugin.
  3. package IkiWiki::Plugin::httpauth;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 3.00;
  7. sub import {
  8. hook(type => "getsetup", id => "httpauth", call => \&getsetup);
  9. hook(type => "auth", id => "httpauth", call => \&auth);
  10. hook(type => "formbuilder_setup", id => "httpauth",
  11. call => \&formbuilder_setup);
  12. hook(type => "canedit", id => "httpauth", call => \&canedit,
  13. first => 1);
  14. }
  15. sub getsetup () {
  16. return
  17. plugin => {
  18. safe => 1,
  19. rebuild => 0,
  20. section => "auth",
  21. },
  22. cgiauthurl => {
  23. type => "string",
  24. example => "http://example.com/wiki/auth/ikiwiki.cgi",
  25. description => "url to redirect to when authentication is needed",
  26. safe => 1,
  27. rebuild => 0,
  28. },
  29. httpauth_pagespec => {
  30. type => "pagespec",
  31. example => "!*/Discussion",
  32. description => "PageSpec of pages where only httpauth will be used for authentication",
  33. safe => 0,
  34. rebuild => 0,
  35. },
  36. }
  37. sub redir_cgiauthurl ($;@) {
  38. my $cgi=shift;
  39. IkiWiki::redirect($cgi,
  40. @_ > 1 ? IkiWiki::cgiurl(cgiurl => $config{cgiauthurl}, @_)
  41. : $config{cgiauthurl}."?@_"
  42. );
  43. exit;
  44. }
  45. sub auth ($$) {
  46. my $cgi=shift;
  47. my $session=shift;
  48. if (defined $cgi->remote_user()) {
  49. $session->param("name", $cgi->remote_user());
  50. }
  51. }
  52. sub formbuilder_setup (@) {
  53. my %params=@_;
  54. my $form=$params{form};
  55. my $session=$params{session};
  56. my $cgi=$params{cgi};
  57. my $buttons=$params{buttons};
  58. if ($form->title eq "signin" &&
  59. ! defined $cgi->remote_user() && defined $config{cgiauthurl}) {
  60. my $button_text="Login with HTTP auth";
  61. push @$buttons, $button_text;
  62. if ($form->submitted && $form->submitted eq $button_text) {
  63. # bounce thru cgiauthurl and then back to
  64. # the stored postsignin action
  65. redir_cgiauthurl($cgi, do => "postsignin");
  66. }
  67. }
  68. }
  69. sub canedit ($$$) {
  70. my $page=shift;
  71. my $cgi=shift;
  72. my $session=shift;
  73. if (! defined $cgi->remote_user() &&
  74. (! defined $session->param("name") ||
  75. ! IkiWiki::userinfo_get($session->param("name"), "regdate")) &&
  76. defined $config{httpauth_pagespec} &&
  77. length $config{httpauth_pagespec} &&
  78. defined $config{cgiauthurl} &&
  79. pagespec_match($page, $config{httpauth_pagespec})) {
  80. return sub {
  81. # bounce thru cgiauthurl and back to edit action
  82. redir_cgiauthurl($cgi, $cgi->query_string());
  83. };
  84. }
  85. else {
  86. return undef;
  87. }
  88. }
  89. 1