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