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