]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/openid.pm
add minified jquery.js from openid-selector svn
[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         hook(type => "checkconfig", id => "openid", call => \&checkconfig);
11         hook(type => "getopt", id => "openid", call => \&getopt);
12         hook(type => "getsetup", id => "openid", call => \&getsetup);
13         hook(type => "auth", id => "openid", call => \&auth);
14         hook(type => "formbuilder_setup", id => "openid",
15                 call => \&formbuilder_setup, last => 1);
16 }
17
18 sub checkconfig () {
19         if ($config{cgi}) {
20                 # Intercept normal signin form, so the openid selector
21                 # can be displayed.
22                 require IkiWiki::CGI;
23                 my $real_cgi_signin=\&IkiWiki::cgi_signin;
24                 inject(name => "IkiWiki::cgi_signin", call => sub ($$) {
25                                 openid_selector($real_cgi_signin, @_);
26                 });
27         }
28 }
29
30 sub getopt () {
31         eval q{use Getopt::Long};
32         error($@) if $@;
33         Getopt::Long::Configure('pass_through');
34         GetOptions("openidsignup=s" => \$config{openidsignup});
35 }
36
37 sub getsetup () {
38         return
39                 plugin => {
40                         safe => 1,
41                         rebuild => 0,
42                         section => "auth",
43                 },
44                 openidsignup => {
45                         type => "string",
46                         example => "http://myopenid.com/",
47                         description => "an url where users can signup for an OpenID",
48                         safe => 1,
49                         rebuild => 0,
50                 },
51 }
52
53 sub openid_selector {
54         my $real_cgi_signin=shift;
55         my $q=shift;
56         my $session=shift;
57
58         my $openid_url=$q->param('openid_url');
59         my $openid_error;
60
61         if (defined $q->param("action") && $q->param("action") eq "verify") {
62                 validate($q, $session, $openid_url, sub {
63                         $openid_error=shift;
64                 });
65         }
66         elsif ($q->param("do") eq "signin" || ! load_openid_module()) {
67                 $real_cgi_signin->($q, $session);
68                 exit;
69         }
70
71         my $template=IkiWiki::template("openid-selector.tmpl");
72         $template->param(
73                 cgiurl => $config{cgiurl},
74                 (defined $openid_error ? (openid_error => $openid_error) : ()),
75                 (defined $openid_url ? (openid_url => $openid_url) : ()),
76                 # TODO only if other auth methods are available
77                 nonopenidurl => IkiWiki::cgiurl(do => "signin"),
78                 loginlabel => loginlabel(),
79         );
80
81         IkiWiki::printheader($session);
82         print IkiWiki::misctemplate("signin", $template->output);
83         exit;
84 }
85
86 sub formbuilder_setup (@) {
87         my %params=@_;
88
89         my $form=$params{form};
90         my $session=$params{session};
91         my $cgi=$params{cgi};
92         
93         if ($form->title eq "signin") {
94                 return unless load_openid_module();
95
96                 # This avoids it displaying a redundant label for the
97                 # OpenID fieldset.
98                 $form->fieldsets("OpenID");
99
100                 $form->field(
101                         name => "openid_url",
102                         label => loginlabel(),
103                         fieldset => "OpenID",
104                         size => 30,
105                         comment => ($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">".gettext("Get an OpenID")."</a>" : "")
106                 );
107
108                 # Handle submission of an OpenID as validation.
109                 if ($form->submitted && $form->submitted eq "Login" &&
110                     defined $form->field("openid_url") && 
111                     length $form->field("openid_url")) {
112                         $form->field(
113                                 name => "openid_url",
114                                 validate => sub {
115                                         validate($cgi, $session, shift, sub {
116                                                 # Display error in the form.
117                                                 $form->field(name => "openid_url", comment => shift);
118                                         });
119                                 },
120                         );
121                         # Skip all other required fields in this case.
122                         foreach my $field ($form->field) {
123                                 next if $field eq "openid_url";
124                                 $form->field(name => $field, required => 0,
125                                         validate => '/.*/');
126                         }
127                 }
128         }
129         elsif ($form->title eq "preferences" &&
130                IkiWiki::openiduser($session->param("name"))) {
131                 $form->field(name => "openid_url", disabled => 1,
132                         label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
133                         value => $session->param("name"), 
134                         size => 50, force => 1,
135                         fieldset => "login");
136                 $form->field(name => "email", type => "hidden");
137         }
138 }
139
140 sub validate ($$$;$) {
141         my $q=shift;
142         my $session=shift;
143         my $openid_url=shift;
144         my $errhandler=shift;
145
146         my $csr=getobj($q, $session);
147
148         my $claimed_identity = $csr->claimed_identity($openid_url);
149         if (! $claimed_identity) {
150                 if ($errhandler) {
151                         $errhandler->($csr->err);
152                         return 0;
153                 }
154                 else {
155                         error($csr->err);
156                 }
157         }
158
159         # Ask for client to provide a name and email, if possible.
160         # Try sreg and ax
161         if ($claimed_identity->can("set_extension_args")) {
162                 $claimed_identity->set_extension_args(
163                         'http://openid.net/extensions/sreg/1.1',
164                         {
165                                 optional => 'email,fullname,nickname',
166                         },
167                 );
168                 $claimed_identity->set_extension_args(
169                         'http://openid.net/srv/ax/1.0',
170                         {
171                                 mode => 'fetch_request',
172                                 'required' => 'email,fullname,nickname,firstname',
173                                 'type.email' => "http://schema.openid.net/contact/email",
174                                 'type.fullname' => "http://axschema.org/namePerson",
175                                 'type.nickname' => "http://axschema.org/namePerson/friendly",
176                                 'type.firstname' => "http://axschema.org/namePerson/first",
177                         },
178                 );
179         }
180
181         my $check_url = $claimed_identity->check_url(
182                 return_to => IkiWiki::cgiurl(do => "postsignin"),
183                 trust_root => $config{cgiurl},
184                 delayed_return => 1,
185         );
186         # Redirect the user to the OpenID server, which will
187         # eventually bounce them back to auth()
188         IkiWiki::redirect($q, $check_url);
189         exit 0;
190 }
191
192 sub auth ($$) {
193         my $q=shift;
194         my $session=shift;
195
196         if (defined $q->param('openid.mode')) {
197                 my $csr=getobj($q, $session);
198
199                 if (my $setup_url = $csr->user_setup_url) {
200                         IkiWiki::redirect($q, $setup_url);
201                 }
202                 elsif ($csr->user_cancel) {
203                         IkiWiki::redirect($q, $config{url});
204                 }
205                 elsif (my $vident = $csr->verified_identity) {
206                         $session->param(name => $vident->url);
207
208                         my @extensions;
209                         if ($vident->can("signed_extension_fields")) {
210                                 @extensions=grep { defined } (
211                                         $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
212                                         $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
213                                 );
214                         }
215                         foreach my $ext (@extensions) {
216                                 foreach my $field (qw{value.email email}) {
217                                         if (exists $ext->{$field} &&
218                                             defined $ext->{$field} &&
219                                             length $ext->{$field}) {
220                                                 $session->param(email => $ext->{$field});
221                                                 last;
222                                         }
223                                 }
224                                 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
225                                         if (exists $ext->{$field} &&
226                                             defined $ext->{$field} &&
227                                             length $ext->{$field}) {
228                                                 $session->param(username => $ext->{$field});
229                                                 last;
230                                         }
231                                 }
232                         }
233                 }
234                 else {
235                         error("OpenID failure: ".$csr->err);
236                 }
237         }
238         elsif (defined $q->param('openid_identifier')) {
239                 # myopenid.com affiliate support
240                 validate($q, $session, $q->param('openid_identifier'));
241         }
242 }
243
244 sub getobj ($$) {
245         my $q=shift;
246         my $session=shift;
247
248         eval q{use Net::OpenID::Consumer};
249         error($@) if $@;
250
251         my $ua;
252         eval q{use LWPx::ParanoidAgent};
253         if (! $@) {
254                 $ua=LWPx::ParanoidAgent->new;
255         }
256         else {
257                 $ua=LWP::UserAgent->new;
258         }
259
260         # Store the secret in the session.
261         my $secret=$session->param("openid_secret");
262         if (! defined $secret) {
263                 $secret=rand;
264                 $session->param(openid_secret => $secret);
265         }
266
267         return Net::OpenID::Consumer->new(
268                 ua => $ua,
269                 args => $q,
270                 consumer_secret => sub { return shift()+$secret },
271                 required_root => $config{cgiurl},
272         );
273 }
274
275 sub load_openid_module {
276         # Give up if module is unavailable to avoid needing to depend on it.
277         eval q{use Net::OpenID::Consumer};
278         if ($@) {
279                 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
280                 return;
281         }
282         return 1;
283 }
284
285 sub loginlabel {
286         return gettext("Log in with")." ".htmllink("", "", "ikiwiki/OpenID", noimageinline => 1);
287 }
288
289 1