summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/httpauth.pm
blob: b2bb2701a3f7bb65054b5432faaf0f36cee9dfe1 (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. },
  21. cgiauthurl => {
  22. type => "string",
  23. example => "http://example.com/wiki/auth/ikiwiki.cgi",
  24. description => "url to redirect to when authentication is needed",
  25. safe => 1,
  26. rebuild => 0,
  27. },
  28. httpauth_pagespec => {
  29. type => "pagespec",
  30. example => "!*/Discussion",
  31. description => "PageSpec of pages where only httpauth will be used for authentication",
  32. safe => 0,
  33. rebuild => 0,
  34. },
  35. }
  36. sub redir_cgiauthurl ($;@) {
  37. my $cgi=shift;
  38. IkiWiki::redirect($cgi,
  39. @_ > 1 ? IkiWiki::cgiurl(cgiurl => $config{cgiauthurl}, @_)
  40. : $config{cgiauthurl}."?@_"
  41. );
  42. exit;
  43. }
  44. sub auth ($$) {
  45. my $cgi=shift;
  46. my $session=shift;
  47. if (defined $cgi->remote_user()) {
  48. $session->param("name", $cgi->remote_user());
  49. }
  50. }
  51. sub formbuilder_setup (@) {
  52. my %params=@_;
  53. my $form=$params{form};
  54. my $session=$params{session};
  55. my $cgi=$params{cgi};
  56. my $buttons=$params{buttons};
  57. if ($form->title eq "signin" &&
  58. ! defined $cgi->remote_user() && defined $config{cgiauthurl}) {
  59. my $button_text="Login with HTTP auth";
  60. push @$buttons, $button_text;
  61. if ($form->submitted && $form->submitted eq $button_text) {
  62. # bounce thru cgiauthurl and then back to
  63. # the stored postsignin action
  64. redir_cgiauthurl($cgi, do => "postsignin");
  65. }
  66. }
  67. }
  68. sub test_httpauth_pagespec ($) {
  69. my $page=shift;
  70. return (
  71. );
  72. }
  73. sub canedit ($$$) {
  74. my $page=shift;
  75. my $cgi=shift;
  76. my $session=shift;
  77. if (! defined $cgi->remote_user() &&
  78. defined $config{httpauth_pagespec} &&
  79. length $config{httpauth_pagespec} &&
  80. defined $config{cgiauthurl} &&
  81. pagespec_match($page, $config{httpauth_pagespec})) {
  82. return sub {
  83. # bounce thru cgiauthurl and back to edit action
  84. redir_cgiauthurl($cgi, $cgi->query_string());
  85. };
  86. }
  87. else {
  88. return undef;
  89. }
  90. }
  91. 1