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