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