typos
[ikiwiki.git] / IkiWiki / Plugin / openid.pm
1 #!/usr/bin/perl
2 # OpenID support.
3 package IkiWiki::Plugin::openid;
4
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
8
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 }
18
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 }
36
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 }
57
58 sub openid_selector {
59         my $real_cgi_signin=shift;
60         my $q=shift;
61         my $session=shift;
62
63         my $openid_url=$q->param('openid_identifier');
64         my $openid_error;
65
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         }
78
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         );
86
87         IkiWiki::printheader($session);
88         print IkiWiki::cgitemplate($q, "signin", $template->output);
89         exit;
90 }
91
92 sub formbuilder_setup (@) {
93         my %params=@_;
94
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 => "", 
104                         size => 1, force => 1,
105                         fieldset => "login",
106                         comment => $session->param("name"));
107                 $form->field(name => "email", type => "hidden");
108         }
109 }
110
111 sub validate ($$$;$) {
112         my $q=shift;
113         my $session=shift;
114         my $openid_url=shift;
115         my $errhandler=shift;
116
117         my $csr=getobj($q, $session);
118
119         my $claimed_identity = $csr->claimed_identity($openid_url);
120         if (! $claimed_identity) {
121                 if ($errhandler) {
122                         if (ref($errhandler) eq 'CODE') {
123                                 $errhandler->($csr->err);
124                         }
125                         return 0;
126                 }
127                 else {
128                         error($csr->err);
129                 }
130         }
131
132         # Ask for client to provide a name and email, if possible.
133         # Try sreg and ax
134         if ($claimed_identity->can("set_extension_args")) {
135                 $claimed_identity->set_extension_args(
136                         'http://openid.net/extensions/sreg/1.1',
137                         {
138                                 optional => 'email,fullname,nickname',
139                         },
140                 );
141                 $claimed_identity->set_extension_args(
142                         'http://openid.net/srv/ax/1.0',
143                         {
144                                 mode => 'fetch_request',
145                                 'required' => 'email,fullname,nickname,firstname',
146                                 'type.email' => "http://schema.openid.net/contact/email",
147                                 'type.fullname' => "http://axschema.org/namePerson",
148                                 'type.nickname' => "http://axschema.org/namePerson/friendly",
149                                 'type.firstname' => "http://axschema.org/namePerson/first",
150                         },
151                 );
152         }
153
154         my $cgiurl=$config{openid_cgiurl};
155         $cgiurl=$q->url if ! defined $cgiurl;
156
157         my $trust_root=$config{openid_realm};
158         $trust_root=$cgiurl if ! defined $trust_root;
159
160         my $check_url = $claimed_identity->check_url(
161                 return_to => auto_upgrade_https($q, "$cgiurl?do=postsignin"),
162                 trust_root => auto_upgrade_https($q, $trust_root),
163                 delayed_return => 1,
164         );
165         # Redirect the user to the OpenID server, which will
166         # eventually bounce them back to auth()
167         IkiWiki::redirect($q, $check_url);
168         exit 0;
169 }
170
171 sub auth ($$) {
172         my $q=shift;
173         my $session=shift;
174
175         if (defined $q->param('openid.mode')) {
176                 my $csr=getobj($q, $session);
177
178                 if (my $setup_url = $csr->user_setup_url) {
179                         IkiWiki::redirect($q, $setup_url);
180                 }
181                 elsif ($csr->user_cancel) {
182                         IkiWiki::redirect($q, IkiWiki::baseurl(undef));
183                 }
184                 elsif (my $vident = $csr->verified_identity) {
185                         $session->param(name => $vident->url);
186
187                         my @extensions;
188                         if ($vident->can("signed_extension_fields")) {
189                                 @extensions=grep { defined } (
190                                         $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
191                                         $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
192                                 );
193                         }
194                         my $nickname;
195                         foreach my $ext (@extensions) {
196                                 foreach my $field (qw{value.email email}) {
197                                         if (exists $ext->{$field} &&
198                                             defined $ext->{$field} &&
199                                             length $ext->{$field}) {
200                                                 $session->param(email => $ext->{$field});
201                                                 if (! defined $nickname &&
202                                                     $ext->{$field}=~/(.+)@.+/) {
203                                                         $nickname = $1;
204                                                 }
205                                                 last;
206                                         }
207                                 }
208                                 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
209                                         if (exists $ext->{$field} &&
210                                             defined $ext->{$field} &&
211                                             length $ext->{$field}) {
212                                                 $nickname=$ext->{$field};
213                                                 last;
214                                         }
215                                 }
216                         }
217                         if (defined $nickname) {
218                                 $session->param(nickname =>
219                                         Encode::decode_utf8($nickname));
220                         }
221                 }
222                 else {
223                         error("OpenID failure: ".$csr->err);
224                 }
225         }
226         elsif (defined $q->param('openid_identifier')) {
227                 # myopenid.com affiliate support
228                 validate($q, $session, scalar $q->param('openid_identifier'));
229         }
230 }
231
232 sub getobj ($$) {
233         my $q=shift;
234         my $session=shift;
235
236         eval q{use Net::INET6Glue::INET_is_INET6}; # may not be available
237         eval q{use Net::OpenID::Consumer};
238         error($@) if $@;
239
240         my $ua;
241         eval q{use LWPx::ParanoidAgent};
242         if (! $@) {
243                 $ua=LWPx::ParanoidAgent->new(agent => $config{useragent});
244         }
245         else {
246                 $ua=useragent();
247         }
248
249         # Store the secret in the session.
250         my $secret=$session->param("openid_secret");
251         if (! defined $secret) {
252                 $secret=rand;
253                 $session->param(openid_secret => $secret);
254         }
255         
256         my $cgiurl=$config{openid_cgiurl};
257         $cgiurl=$q->url if ! defined $cgiurl;
258
259         return Net::OpenID::Consumer->new(
260                 ua => $ua,
261                 args => $q,
262                 consumer_secret => sub { return shift()+$secret },
263                 required_root => auto_upgrade_https($q, $cgiurl),
264         );
265 }
266
267 sub auto_upgrade_https {
268         my $q=shift;
269         my $url=shift;
270         if ($q->https()) {
271                 $url=~s/^http:/https:/i;
272         }
273         return $url;
274 }
275
276 sub load_openid_module {
277         # Give up if module is unavailable to avoid needing to depend on it.
278         eval q{use Net::OpenID::Consumer};
279         if ($@) {
280                 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
281                 return;
282         }
283         return 1;
284 }
285
286 1