summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/openid.pm
blob: fae9fb77f8388a9e750b1d8e7dfdcee700f76a92 (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. my $nickname;
  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. if (! defined $nickname &&
  175. $ext->{$field}=~/(.+)@.+/) {
  176. $nickname = $1;
  177. }
  178. last;
  179. }
  180. }
  181. foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
  182. if (exists $ext->{$field} &&
  183. defined $ext->{$field} &&
  184. length $ext->{$field}) {
  185. $nickname=$ext->{$field};
  186. last;
  187. }
  188. }
  189. }
  190. if (defined $nickname) {
  191. $session->param(nickname =>
  192. Encode::decode_utf8($nickname));
  193. }
  194. }
  195. else {
  196. error("OpenID failure: ".$csr->err);
  197. }
  198. }
  199. elsif (defined $q->param('openid_identifier')) {
  200. # myopenid.com affiliate support
  201. validate($q, $session, $q->param('openid_identifier'));
  202. }
  203. }
  204. sub getobj ($$) {
  205. my $q=shift;
  206. my $session=shift;
  207. eval q{use Net::OpenID::Consumer};
  208. error($@) if $@;
  209. my $ua;
  210. eval q{use LWPx::ParanoidAgent};
  211. if (! $@) {
  212. $ua=LWPx::ParanoidAgent->new;
  213. }
  214. else {
  215. $ua=LWP::UserAgent->new;
  216. }
  217. # Store the secret in the session.
  218. my $secret=$session->param("openid_secret");
  219. if (! defined $secret) {
  220. $secret=rand;
  221. $session->param(openid_secret => $secret);
  222. }
  223. my $cgiurl=$config{openid_cgiurl};
  224. $cgiurl=$config{cgiurl} if ! defined $cgiurl;
  225. return Net::OpenID::Consumer->new(
  226. ua => $ua,
  227. args => $q,
  228. consumer_secret => sub { return shift()+$secret },
  229. required_root => $cgiurl,
  230. );
  231. }
  232. sub load_openid_module {
  233. # Give up if module is unavailable to avoid needing to depend on it.
  234. eval q{use Net::OpenID::Consumer};
  235. if ($@) {
  236. debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
  237. return;
  238. }
  239. return 1;
  240. }
  241. 1