]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/openid.pm
Merge remote-tracking branch 'smcv/comments-metadata'
[git.ikiwiki.info.git] / IkiWiki / Plugin / openid.pm
1 #!/usr/bin/perl
2 # OpenID support.
3 package IkiWiki::Plugin::openid;
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
9 sub import {
10         add_underlay("openid-selector");
11         add_underlay("jquery");
12         hook(type => "checkconfig", id => "openid", call => \&checkconfig);
13         hook(type => "getsetup", id => "openid", call => \&getsetup);
14         hook(type => "auth", id => "openid", call => \&auth);
15         hook(type => "formbuilder_setup", id => "openid",
16                 call => \&formbuilder_setup, last => 1);
17 }
19 sub checkconfig () {
20         if ($config{cgi}) {
21                 # Intercept normal signin form, so the openid selector
22                 # can be displayed.
23                 # 
24                 # When other auth hooks are registered, give the selector
25                 # a reference to the normal signin form.
26                 require IkiWiki::CGI;
27                 my $real_cgi_signin;
28                 if (keys %{$IkiWiki::hooks{auth}} > 1) {
29                         $real_cgi_signin=\&IkiWiki::cgi_signin;
30                 }
31                 inject(name => "IkiWiki::cgi_signin", call => sub ($$) {
32                         openid_selector($real_cgi_signin, @_);
33                 });
34         }
35 }
37 sub getsetup () {
38         return
39                 plugin => {
40                         safe => 1,
41                         rebuild => 0,
42                         section => "auth",
43                 },
44                 openid_realm => {
45                         type => "string",
46                         description => "url pattern of openid realm (default is cgiurl)",
47                         safe => 0,
48                         rebuild => 0,
49                 },
50                 openid_cgiurl => {
51                         type => "string",
52                         description => "url to ikiwiki cgi to use for openid authentication (default is cgiurl)",
53                         safe => 0,
54                         rebuild => 0,
55                 },
56 }
58 sub openid_selector {
59         my $real_cgi_signin=shift;
60         my $q=shift;
61         my $session=shift;
63         my $openid_url=$q->param('openid_identifier');
64         my $openid_error;
66         if (! load_openid_module()) {
67                 if ($real_cgi_signin) {
68                         $real_cgi_signin->($q, $session);
69                         exit;
70                 }
71                 error(sprintf(gettext("failed to load openid module: "), @_));
72         }
73         elsif (defined $q->param("action") && $q->param("action") eq "verify") {
74                 validate($q, $session, $openid_url, sub {
75                         $openid_error=shift;
76                 });
77         }
79         my $template=IkiWiki::template("openid-selector.tmpl");
80         $template->param(
81                 cgiurl => IkiWiki::cgiurl(),
82                 (defined $openid_error ? (openid_error => $openid_error) : ()),
83                 (defined $openid_url ? (openid_url => $openid_url) : ()),
84                 ($real_cgi_signin ? (nonopenidform => $real_cgi_signin->($q, $session, 1)) : ()),
85         );
87         IkiWiki::printheader($session);
88         print IkiWiki::cgitemplate($q, "signin", $template->output);
89         exit;
90 }
92 sub formbuilder_setup (@) {
93         my %params=@_;
95         my $form=$params{form};
96         my $session=$params{session};
97         my $cgi=$params{cgi};
98         
99         if ($form->title eq "preferences" &&
100                IkiWiki::openiduser($session->param("name"))) {
101                 $form->field(name => "openid_identifier", disabled => 1,
102                         label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
103                         value => $session->param("name"), 
104                         size => length($session->param("name")), force => 1,
105                         fieldset => "login");
106                 $form->field(name => "email", type => "hidden");
107         }
110 sub validate ($$$;$) {
111         my $q=shift;
112         my $session=shift;
113         my $openid_url=shift;
114         my $errhandler=shift;
116         my $csr=getobj($q, $session);
118         my $claimed_identity = $csr->claimed_identity($openid_url);
119         if (! $claimed_identity) {
120                 if ($errhandler) {
121                         $errhandler->($csr->err);
122                         return 0;
123                 }
124                 else {
125                         error($csr->err);
126                 }
127         }
129         # Ask for client to provide a name and email, if possible.
130         # Try sreg and ax
131         if ($claimed_identity->can("set_extension_args")) {
132                 $claimed_identity->set_extension_args(
133                         'http://openid.net/extensions/sreg/1.1',
134                         {
135                                 optional => 'email,fullname,nickname',
136                         },
137                 );
138                 $claimed_identity->set_extension_args(
139                         'http://openid.net/srv/ax/1.0',
140                         {
141                                 mode => 'fetch_request',
142                                 'required' => 'email,fullname,nickname,firstname',
143                                 'type.email' => "http://schema.openid.net/contact/email",
144                                 'type.fullname' => "http://axschema.org/namePerson",
145                                 'type.nickname' => "http://axschema.org/namePerson/friendly",
146                                 'type.firstname' => "http://axschema.org/namePerson/first",
147                         },
148                 );
149         }
151         my $cgiurl=$config{openid_cgiurl};
152         $cgiurl=$q->url if ! defined $cgiurl;
154         my $trust_root=$config{openid_realm};
155         $trust_root=$cgiurl if ! defined $trust_root;
157         my $check_url = $claimed_identity->check_url(
158                 return_to => "$cgiurl?do=postsignin",
159                 trust_root => $trust_root,
160                 delayed_return => 1,
161         );
162         # Redirect the user to the OpenID server, which will
163         # eventually bounce them back to auth()
164         IkiWiki::redirect($q, $check_url);
165         exit 0;
168 sub auth ($$) {
169         my $q=shift;
170         my $session=shift;
172         if (defined $q->param('openid.mode')) {
173                 my $csr=getobj($q, $session);
175                 if (my $setup_url = $csr->user_setup_url) {
176                         IkiWiki::redirect($q, $setup_url);
177                 }
178                 elsif ($csr->user_cancel) {
179                         IkiWiki::redirect($q, IkiWiki::baseurl(undef));
180                 }
181                 elsif (my $vident = $csr->verified_identity) {
182                         $session->param(name => $vident->url);
184                         my @extensions;
185                         if ($vident->can("signed_extension_fields")) {
186                                 @extensions=grep { defined } (
187                                         $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
188                                         $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
189                                 );
190                         }
191                         my $nickname;
192                         foreach my $ext (@extensions) {
193                                 foreach my $field (qw{value.email email}) {
194                                         if (exists $ext->{$field} &&
195                                             defined $ext->{$field} &&
196                                             length $ext->{$field}) {
197                                                 $session->param(email => $ext->{$field});
198                                                 if (! defined $nickname &&
199                                                     $ext->{$field}=~/(.+)@.+/) {
200                                                         $nickname = $1;
201                                                 }
202                                                 last;
203                                         }
204                                 }
205                                 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
206                                         if (exists $ext->{$field} &&
207                                             defined $ext->{$field} &&
208                                             length $ext->{$field}) {
209                                                 $nickname=$ext->{$field};
210                                                 last;
211                                         }
212                                 }
213                         }
214                         if (defined $nickname) {
215                                 $session->param(nickname =>
216                                         Encode::decode_utf8($nickname));
217                         }
218                 }
219                 else {
220                         error("OpenID failure: ".$csr->err);
221                 }
222         }
223         elsif (defined $q->param('openid_identifier')) {
224                 # myopenid.com affiliate support
225                 validate($q, $session, $q->param('openid_identifier'));
226         }
229 sub getobj ($$) {
230         my $q=shift;
231         my $session=shift;
233         eval q{use Net::INET6Glue::INET_is_INET6}; # may not be available
234         eval q{use Net::OpenID::Consumer};
235         error($@) if $@;
237         my $ua;
238         eval q{use LWPx::ParanoidAgent};
239         if (! $@) {
240                 $ua=LWPx::ParanoidAgent->new;
241         }
242         else {
243                 $ua=LWP::UserAgent->new;
244         }
246         # Store the secret in the session.
247         my $secret=$session->param("openid_secret");
248         if (! defined $secret) {
249                 $secret=rand;
250                 $session->param(openid_secret => $secret);
251         }
252         
253         my $cgiurl=$config{openid_cgiurl};
254         $cgiurl=$q->url if ! defined $cgiurl;
256         return Net::OpenID::Consumer->new(
257                 ua => $ua,
258                 args => $q,
259                 consumer_secret => sub { return shift()+$secret },
260                 required_root => $cgiurl,
261         );
264 sub load_openid_module {
265         # Give up if module is unavailable to avoid needing to depend on it.
266         eval q{use Net::OpenID::Consumer};
267         if ($@) {
268                 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
269                 return;
270         }
271         return 1;