summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/openid.pm
blob: 9355cd85ad1cc27178b9c993d0f6d2a0245d36bf (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. $form->field(name => "email", type => "hidden");
  84. }
  85. }
  86. sub validate ($$$;$) {
  87. my $q=shift;
  88. my $session=shift;
  89. my $openid_url=shift;
  90. my $form=shift;
  91. my $csr=getobj($q, $session);
  92. my $claimed_identity = $csr->claimed_identity($openid_url);
  93. if (! $claimed_identity) {
  94. if ($form) {
  95. # Put the error in the form and fail validation.
  96. $form->field(name => "openid_url", comment => $csr->err);
  97. return 0;
  98. }
  99. else {
  100. error($csr->err);
  101. }
  102. }
  103. # Ask for client to provide a name and email, if possible.
  104. # Try sreg and ax
  105. $claimed_identity->set_extension_args(
  106. 'http://openid.net/extensions/sreg/1.1',
  107. {
  108. optional => 'email,fullname,nickname',
  109. },
  110. );
  111. $claimed_identity->set_extension_args(
  112. 'http://openid.net/srv/ax/1.0',
  113. {
  114. mode => 'fetch_request',
  115. 'required' => 'email,fullname,nickname,firstname',
  116. 'type.email' => "http://schema.openid.net/contact/email",
  117. 'type.fullname' => "http://axschema.org/namePerson",
  118. 'type.nickname' => "http://axschema.org/namePerson/friendly",
  119. 'type.firstname' => "http://axschema.org/namePerson/first",
  120. },
  121. );
  122. my $check_url = $claimed_identity->check_url(
  123. return_to => IkiWiki::cgiurl(do => "postsignin"),
  124. trust_root => $config{cgiurl},
  125. delayed_return => 1,
  126. );
  127. # Redirect the user to the OpenID server, which will
  128. # eventually bounce them back to auth()
  129. IkiWiki::redirect($q, $check_url);
  130. exit 0;
  131. }
  132. sub auth ($$) {
  133. my $q=shift;
  134. my $session=shift;
  135. if (defined $q->param('openid.mode')) {
  136. my $csr=getobj($q, $session);
  137. if (my $setup_url = $csr->user_setup_url) {
  138. IkiWiki::redirect($q, $setup_url);
  139. }
  140. elsif ($csr->user_cancel) {
  141. IkiWiki::redirect($q, $config{url});
  142. }
  143. elsif (my $vident = $csr->verified_identity) {
  144. $session->param(name => $vident->url);
  145. my @extensions=grep { defined } (
  146. $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
  147. $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
  148. );
  149. foreach my $ext (@extensions) {
  150. foreach my $field (qw{value.email email}) {
  151. if (exists $ext->{$field} &&
  152. defined $ext->{$field} &&
  153. length $ext->{$field}) {
  154. $session->param(email => $ext->{$field});
  155. last;
  156. }
  157. }
  158. foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
  159. if (exists $ext->{$field} &&
  160. defined $ext->{$field} &&
  161. length $ext->{$field}) {
  162. $session->param(username => $ext->{$field});
  163. last;
  164. }
  165. }
  166. }
  167. }
  168. else {
  169. error("OpenID failure: ".$csr->err);
  170. }
  171. }
  172. elsif (defined $q->param('openid_identifier')) {
  173. # myopenid.com affiliate support
  174. validate($q, $session, $q->param('openid_identifier'));
  175. }
  176. }
  177. sub getobj ($$) {
  178. my $q=shift;
  179. my $session=shift;
  180. eval q{use Net::OpenID::Consumer};
  181. error($@) if $@;
  182. my $ua;
  183. eval q{use LWPx::ParanoidAgent};
  184. if (! $@) {
  185. $ua=LWPx::ParanoidAgent->new;
  186. }
  187. else {
  188. $ua=LWP::UserAgent->new;
  189. }
  190. # Store the secret in the session.
  191. my $secret=$session->param("openid_secret");
  192. if (! defined $secret) {
  193. $secret=rand;
  194. $session->param(openid_secret => $secret);
  195. }
  196. return Net::OpenID::Consumer->new(
  197. ua => $ua,
  198. args => $q,
  199. consumer_secret => sub { return shift()+$secret },
  200. required_root => $config{cgiurl},
  201. );
  202. }
  203. 1