summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/openid.pm
blob: e10e21f4d6f9eed60be8cfa8650b9ddf9391b1a6 (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. add_underlay("openid-selector");
  9. hook(type => "checkconfig", id => "openid", call => \&checkconfig);
  10. hook(type => "getsetup", id => "openid", call => \&getsetup);
  11. hook(type => "auth", id => "openid", call => \&auth);
  12. hook(type => "formbuilder_setup", id => "openid",
  13. call => \&formbuilder_setup, last => 1);
  14. }
  15. sub checkconfig () {
  16. if ($config{cgi}) {
  17. # Intercept normal signin form, so the openid selector
  18. # can be displayed.
  19. #
  20. # When other auth hooks are registered, give the selector
  21. # a reference to the normal signin form.
  22. require IkiWiki::CGI;
  23. my $real_cgi_signin;
  24. if (keys %{$IkiWiki::hooks{auth}} > 1) {
  25. $real_cgi_signin=\&IkiWiki::cgi_signin;
  26. }
  27. inject(name => "IkiWiki::cgi_signin", call => sub ($$) {
  28. openid_selector($real_cgi_signin, @_);
  29. });
  30. }
  31. }
  32. sub getsetup () {
  33. return
  34. plugin => {
  35. safe => 1,
  36. rebuild => 0,
  37. section => "auth",
  38. },
  39. }
  40. sub openid_selector {
  41. my $real_cgi_signin=shift;
  42. my $q=shift;
  43. my $session=shift;
  44. my $openid_url=$q->param('openid_identifier');
  45. my $openid_error;
  46. if (! load_openid_module()) {
  47. if ($real_cgi_signin) {
  48. $real_cgi_signin->($q, $session);
  49. exit;
  50. }
  51. error(sprintf(gettext("failed to load openid module: "), @_));
  52. }
  53. elsif (defined $q->param("action") && $q->param("action") eq "verify") {
  54. validate($q, $session, $openid_url, sub {
  55. $openid_error=shift;
  56. });
  57. }
  58. my $template=IkiWiki::template("openid-selector.tmpl");
  59. $template->param(
  60. cgiurl => $config{cgiurl},
  61. (defined $openid_error ? (openid_error => $openid_error) : ()),
  62. (defined $openid_url ? (openid_url => $openid_url) : ()),
  63. ($real_cgi_signin ? (nonopenidform => $real_cgi_signin->($q, $session, 1)) : ()),
  64. );
  65. IkiWiki::printheader($session);
  66. print IkiWiki::misctemplate("signin", $template->output);
  67. exit;
  68. }
  69. sub formbuilder_setup (@) {
  70. my %params=@_;
  71. my $form=$params{form};
  72. my $session=$params{session};
  73. my $cgi=$params{cgi};
  74. if ($form->title eq "preferences" &&
  75. IkiWiki::openiduser($session->param("name"))) {
  76. $form->field(name => "openid_identifier", disabled => 1,
  77. label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
  78. value => $session->param("name"),
  79. size => length($session->param("name")), force => 1,
  80. fieldset => "login");
  81. $form->field(name => "email", type => "hidden");
  82. }
  83. }
  84. sub validate ($$$;$) {
  85. my $q=shift;
  86. my $session=shift;
  87. my $openid_url=shift;
  88. my $errhandler=shift;
  89. my $csr=getobj($q, $session);
  90. my $claimed_identity = $csr->claimed_identity($openid_url);
  91. if (! $claimed_identity) {
  92. if ($errhandler) {
  93. $errhandler->($csr->err);
  94. return 0;
  95. }
  96. else {
  97. error($csr->err);
  98. }
  99. }
  100. # Ask for client to provide a name and email, if possible.
  101. # Try sreg and ax
  102. if ($claimed_identity->can("set_extension_args")) {
  103. $claimed_identity->set_extension_args(
  104. 'http://openid.net/extensions/sreg/1.1',
  105. {
  106. optional => 'email,fullname,nickname',
  107. },
  108. );
  109. $claimed_identity->set_extension_args(
  110. 'http://openid.net/srv/ax/1.0',
  111. {
  112. mode => 'fetch_request',
  113. 'required' => 'email,fullname,nickname,firstname',
  114. 'type.email' => "http://schema.openid.net/contact/email",
  115. 'type.fullname' => "http://axschema.org/namePerson",
  116. 'type.nickname' => "http://axschema.org/namePerson/friendly",
  117. 'type.firstname' => "http://axschema.org/namePerson/first",
  118. },
  119. );
  120. }
  121. my $check_url = $claimed_identity->check_url(
  122. return_to => IkiWiki::cgiurl(do => "postsignin"),
  123. trust_root => $config{cgiurl},
  124. delayed_return => 1,
  125. );
  126. # Redirect the user to the OpenID server, which will
  127. # eventually bounce them back to auth()
  128. IkiWiki::redirect($q, $check_url);
  129. exit 0;
  130. }
  131. sub auth ($$) {
  132. my $q=shift;
  133. my $session=shift;
  134. if (defined $q->param('openid.mode')) {
  135. my $csr=getobj($q, $session);
  136. if (my $setup_url = $csr->user_setup_url) {
  137. IkiWiki::redirect($q, $setup_url);
  138. }
  139. elsif ($csr->user_cancel) {
  140. IkiWiki::redirect($q, $config{url});
  141. }
  142. elsif (my $vident = $csr->verified_identity) {
  143. $session->param(name => $vident->url);
  144. my @extensions;
  145. if ($vident->can("signed_extension_fields")) {
  146. @extensions=grep { defined } (
  147. $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
  148. $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
  149. );
  150. }
  151. foreach my $ext (@extensions) {
  152. foreach my $field (qw{value.email email}) {
  153. if (exists $ext->{$field} &&
  154. defined $ext->{$field} &&
  155. length $ext->{$field}) {
  156. $session->param(email => $ext->{$field});
  157. last;
  158. }
  159. }
  160. foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
  161. if (exists $ext->{$field} &&
  162. defined $ext->{$field} &&
  163. length $ext->{$field}) {
  164. $session->param(username => $ext->{$field});
  165. last;
  166. }
  167. }
  168. }
  169. }
  170. else {
  171. error("OpenID failure: ".$csr->err);
  172. }
  173. }
  174. elsif (defined $q->param('openid_identifier')) {
  175. # myopenid.com affiliate support
  176. validate($q, $session, $q->param('openid_identifier'));
  177. }
  178. }
  179. sub getobj ($$) {
  180. my $q=shift;
  181. my $session=shift;
  182. eval q{use Net::OpenID::Consumer};
  183. error($@) if $@;
  184. my $ua;
  185. eval q{use LWPx::ParanoidAgent};
  186. if (! $@) {
  187. $ua=LWPx::ParanoidAgent->new;
  188. }
  189. else {
  190. $ua=LWP::UserAgent->new;
  191. }
  192. # Store the secret in the session.
  193. my $secret=$session->param("openid_secret");
  194. if (! defined $secret) {
  195. $secret=rand;
  196. $session->param(openid_secret => $secret);
  197. }
  198. return Net::OpenID::Consumer->new(
  199. ua => $ua,
  200. args => $q,
  201. consumer_secret => sub { return shift()+$secret },
  202. required_root => $config{cgiurl},
  203. );
  204. }
  205. sub load_openid_module {
  206. # Give up if module is unavailable to avoid needing to depend on it.
  207. eval q{use Net::OpenID::Consumer};
  208. if ($@) {
  209. debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
  210. return;
  211. }
  212. return 1;
  213. }
  214. 1