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