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