summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/openid.pm
blob: 55b1c4b1729cfa0ef97bf704dd182340eff33ab7 (plain)
  1. #!/usr/bin/perl
  2. # OpenID support.
  3. package IkiWiki::Plugin::openid;
  4. use warnings;
  5. use strict;
  6. use IkiWiki;
  7. sub import { #{{{
  8. hook(type => "checkconfig", id => "smiley", call => \&checkconfig);
  9. hook(type => "auth", id => "skeleton", call => \&auth);
  10. } # }}}
  11. sub checkconfig () { #{{{
  12. # Currently part of the OpenID code is in CGI.pm, and is enabled by
  13. # this setting.
  14. # TODO: modularise it all out into this plugin..
  15. $config{openid}=1;
  16. } #}}}
  17. sub auth ($$) { #{{{
  18. my $q=shift;
  19. my $session=shift;
  20. if (defined $q->param('openid.mode')) {
  21. my $csr=getobj($q, $session);
  22. if (my $setup_url = $csr->user_setup_url) {
  23. IkiWiki::redirect($q, $setup_url);
  24. }
  25. elsif ($csr->user_cancel) {
  26. IkiWiki::redirect($q, $config{url});
  27. }
  28. elsif (my $vident = $csr->verified_identity) {
  29. $session->param(name => $vident->url);
  30. }
  31. }
  32. } #}}}
  33. sub validate ($$$$) { #{{{
  34. my $q=shift;
  35. my $session=shift;
  36. my $form=shift;
  37. my $openid_url=shift;
  38. my $csr=getobj($q, $session);
  39. my $claimed_identity = $csr->claimed_identity($openid_url);
  40. if (! $claimed_identity) {
  41. # Put the error in the form and fail validation.
  42. $form->field(name => "openid_url", comment => $csr->err);
  43. return 0;
  44. }
  45. my $check_url = $claimed_identity->check_url(
  46. return_to => IkiWiki::cgiurl(
  47. do => $form->field("do"),
  48. page => $form->field("page"),
  49. title => $form->field("title"),
  50. from => $form->field("from"),
  51. subpage => $form->field("subpage")
  52. ),
  53. trust_root => $config{cgiurl},
  54. delayed_return => 1,
  55. );
  56. # Redirect the user to the OpenID server, which will
  57. # eventually bounce them back to auth() above.
  58. IkiWiki::redirect($q, $check_url);
  59. exit 0;
  60. } #}}}
  61. sub getobj ($$) { #{{{
  62. my $q=shift;
  63. my $session=shift;
  64. eval q{use Net::OpenID::Consumer};
  65. error($@) if $@;
  66. my $ua;
  67. eval q{use LWPx::ParanoidAgent};
  68. if (! $@) {
  69. $ua=LWPx::ParanoidAgent->new;
  70. }
  71. else {
  72. $ua=LWP::UserAgent->new;
  73. }
  74. # Store the secret in the session.
  75. my $secret=$session->param("openid_secret");
  76. if (! defined $secret) {
  77. $secret=$session->param(openid_secret => time);
  78. }
  79. return Net::OpenID::Consumer->new(
  80. ua => $ua,
  81. args => $q,
  82. consumer_secret => $secret,
  83. required_root => $config{cgiurl},
  84. );
  85. } #}}}
  86. 1