summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/openid.pm
blob: bb99446b4db2138f82dcce277caa097bff9e5ca1 (plain)
  1. #!/usr/bin/perl
  2. # OpenID support.
  3. package IkiWiki::Plugin::openid;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 3.00;
  7. sub import {
  8. hook(type => "getopt", id => "openid", call => \&getopt);
  9. hook(type => "getsetup", id => "openid", call => \&getsetup);
  10. hook(type => "auth", id => "openid", call => \&auth);
  11. hook(type => "formbuilder_setup", id => "openid",
  12. call => \&formbuilder_setup, last => 1);
  13. }
  14. sub getopt () {
  15. eval q{use Getopt::Long};
  16. error($@) if $@;
  17. Getopt::Long::Configure('pass_through');
  18. GetOptions("openidsignup=s" => \$config{openidsignup});
  19. }
  20. sub getsetup () {
  21. return
  22. plugin => {
  23. safe => 1,
  24. rebuild => 0,
  25. section => "auth",
  26. },
  27. openidsignup => {
  28. type => "string",
  29. example => "http://myopenid.com/",
  30. description => "an url where users can signup for an OpenID",
  31. safe => 1,
  32. rebuild => 0,
  33. },
  34. }
  35. sub formbuilder_setup (@) {
  36. my %params=@_;
  37. my $form=$params{form};
  38. my $session=$params{session};
  39. my $cgi=$params{cgi};
  40. if ($form->title eq "signin") {
  41. # Give up if module is unavailable to avoid
  42. # needing to depend on it.
  43. eval q{use Net::OpenID::Consumer};
  44. if ($@) {
  45. debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
  46. return;
  47. }
  48. # This avoids it displaying a redundant label for the
  49. # OpenID fieldset.
  50. $form->fieldsets("OpenID");
  51. $form->field(
  52. name => "openid_url",
  53. label => gettext("Log in with")." ".htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
  54. fieldset => "OpenID",
  55. size => 30,
  56. comment => ($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">".gettext("Get an OpenID")."</a>" : "")
  57. );
  58. # Handle submission of an OpenID as validation.
  59. if ($form->submitted && $form->submitted eq "Login" &&
  60. defined $form->field("openid_url") &&
  61. length $form->field("openid_url")) {
  62. $form->field(
  63. name => "openid_url",
  64. validate => sub {
  65. validate($cgi, $session, shift, $form);
  66. },
  67. );
  68. # Skip all other required fields in this case.
  69. foreach my $field ($form->field) {
  70. next if $field eq "openid_url";
  71. $form->field(name => $field, required => 0,
  72. validate => '/.*/');
  73. }
  74. }
  75. }
  76. elsif ($form->title eq "preferences" &&
  77. IkiWiki::openiduser($session->param("name"))) {
  78. $form->field(name => "openid_url", disabled => 1,
  79. label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
  80. value => $session->param("name"),
  81. size => 50, force => 1,
  82. fieldset => "login");
  83. }
  84. }
  85. sub validate ($$$;$) {
  86. my $q=shift;
  87. my $session=shift;
  88. my $openid_url=shift;
  89. my $form=shift;
  90. my $csr=getobj($q, $session);
  91. my $claimed_identity = $csr->claimed_identity($openid_url);
  92. if (! $claimed_identity) {
  93. if ($form) {
  94. # Put the error in the form and fail validation.
  95. $form->field(name => "openid_url", comment => $csr->err);
  96. return 0;
  97. }
  98. else {
  99. error($csr->err);
  100. }
  101. }
  102. my $check_url = $claimed_identity->check_url(
  103. return_to => IkiWiki::cgiurl(do => "postsignin"),
  104. trust_root => $config{cgiurl},
  105. delayed_return => 1,
  106. );
  107. # Redirect the user to the OpenID server, which will
  108. # eventually bounce them back to auth()
  109. IkiWiki::redirect($q, $check_url);
  110. exit 0;
  111. }
  112. sub auth ($$) {
  113. my $q=shift;
  114. my $session=shift;
  115. if (defined $q->param('openid.mode')) {
  116. my $csr=getobj($q, $session);
  117. if (my $setup_url = $csr->user_setup_url) {
  118. IkiWiki::redirect($q, $setup_url);
  119. }
  120. elsif ($csr->user_cancel) {
  121. IkiWiki::redirect($q, $config{url});
  122. }
  123. elsif (my $vident = $csr->verified_identity) {
  124. $session->param(name => $vident->url);
  125. }
  126. else {
  127. error("OpenID failure: ".$csr->err);
  128. }
  129. }
  130. elsif (defined $q->param('openid_identifier')) {
  131. # myopenid.com affiliate support
  132. validate($q, $session, $q->param('openid_identifier'));
  133. }
  134. }
  135. sub getobj ($$) {
  136. my $q=shift;
  137. my $session=shift;
  138. eval q{use Net::OpenID::Consumer};
  139. error($@) if $@;
  140. my $ua;
  141. eval q{use LWPx::ParanoidAgent};
  142. if (! $@) {
  143. $ua=LWPx::ParanoidAgent->new;
  144. }
  145. else {
  146. $ua=LWP::UserAgent->new;
  147. }
  148. # Store the secret in the session.
  149. my $secret=$session->param("openid_secret");
  150. if (! defined $secret) {
  151. $secret=rand;
  152. $session->param(openid_secret => $secret);
  153. }
  154. return Net::OpenID::Consumer->new(
  155. ua => $ua,
  156. args => $q,
  157. consumer_secret => sub { return shift()+$secret },
  158. required_root => $config{cgiurl},
  159. );
  160. }
  161. 1