]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/openid.pm
pretty openid login
[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         hook(type => "checkconfig", id => "openid", call => \&checkconfig);
12         hook(type => "getopt", id => "openid", call => \&getopt);
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                 my $real_cgi_signin;
27                 if (keys %{$IkiWiki::hooks{auth}} > 1) {
28                         require IkiWiki::CGI;
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 getopt () {
38         eval q{use Getopt::Long};
39         error($@) if $@;
40         Getopt::Long::Configure('pass_through');
41         GetOptions("openidsignup=s" => \$config{openidsignup});
42 }
44 sub getsetup () {
45         return
46                 plugin => {
47                         safe => 1,
48                         rebuild => 0,
49                         section => "auth",
50                 },
51                 openidsignup => {
52                         type => "string",
53                         example => "http://myopenid.com/",
54                         description => "an url where users can signup for an OpenID",
55                         safe => 1,
56                         rebuild => 0,
57                 },
58 }
60 sub openid_selector {
61         my $real_cgi_signin=shift;
62         my $q=shift;
63         my $session=shift;
65         my $openid_url=$q->param('openid_identifier');
66         my $openid_error;
68         if (! load_openid_module()) {
69                 if ($real_cgi_signin) {
70                         $real_cgi_signin->($q, $session);
71                 }
72                 error(sprintf(gettext("failed to load openid module: "), @_));
73         }
74         elsif (defined $q->param("action") && $q->param("action") eq "verify") {
75                 validate($q, $session, $openid_url, sub {
76                         $openid_error=shift;
77                 });
78         }
79         elsif ($q->param("do") eq "signin" && $real_cgi_signin) {
80                 $real_cgi_signin->($q, $session);
81                 exit;
82         }
84         my $template=IkiWiki::template("openid-selector.tmpl");
85         $template->param(
86                 cgiurl => $config{cgiurl},
87                 (defined $openid_error ? (openid_error => $openid_error) : ()),
88                 (defined $openid_url ? (openid_url => $openid_url) : ()),
89                 ($real_cgi_signin ? (nonopenidurl => IkiWiki::cgiurl(do => "signin")) : ()),
90                 loginlabel => loginlabel(),
91         );
93         IkiWiki::printheader($session);
94         print IkiWiki::misctemplate("signin", $template->output);
95         exit;
96 }
98 sub formbuilder_setup (@) {
99         my %params=@_;
101         my $form=$params{form};
102         my $session=$params{session};
103         my $cgi=$params{cgi};
104         
105         if ($form->title eq "preferences" &&
106                IkiWiki::openiduser($session->param("name"))) {
107                 $form->field(name => "openid_identifier", disabled => 1,
108                         label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
109                         value => $session->param("name"), 
110                         size => 50, force => 1,
111                         fieldset => "login");
112                 $form->field(name => "email", type => "hidden");
113         }
116 sub validate ($$$;$) {
117         my $q=shift;
118         my $session=shift;
119         my $openid_url=shift;
120         my $errhandler=shift;
122         my $csr=getobj($q, $session);
124         my $claimed_identity = $csr->claimed_identity($openid_url);
125         if (! $claimed_identity) {
126                 if ($errhandler) {
127                         $errhandler->($csr->err);
128                         return 0;
129                 }
130                 else {
131                         error($csr->err);
132                 }
133         }
135         # Ask for client to provide a name and email, if possible.
136         # Try sreg and ax
137         if ($claimed_identity->can("set_extension_args")) {
138                 $claimed_identity->set_extension_args(
139                         'http://openid.net/extensions/sreg/1.1',
140                         {
141                                 optional => 'email,fullname,nickname',
142                         },
143                 );
144                 $claimed_identity->set_extension_args(
145                         'http://openid.net/srv/ax/1.0',
146                         {
147                                 mode => 'fetch_request',
148                                 'required' => 'email,fullname,nickname,firstname',
149                                 'type.email' => "http://schema.openid.net/contact/email",
150                                 'type.fullname' => "http://axschema.org/namePerson",
151                                 'type.nickname' => "http://axschema.org/namePerson/friendly",
152                                 'type.firstname' => "http://axschema.org/namePerson/first",
153                         },
154                 );
155         }
157         my $check_url = $claimed_identity->check_url(
158                 return_to => IkiWiki::cgiurl(do => "postsignin"),
159                 trust_root => $config{cgiurl},
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, $config{url});
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                         foreach my $ext (@extensions) {
192                                 foreach my $field (qw{value.email email}) {
193                                         if (exists $ext->{$field} &&
194                                             defined $ext->{$field} &&
195                                             length $ext->{$field}) {
196                                                 $session->param(email => $ext->{$field});
197                                                 last;
198                                         }
199                                 }
200                                 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
201                                         if (exists $ext->{$field} &&
202                                             defined $ext->{$field} &&
203                                             length $ext->{$field}) {
204                                                 $session->param(username => $ext->{$field});
205                                                 last;
206                                         }
207                                 }
208                         }
209                 }
210                 else {
211                         error("OpenID failure: ".$csr->err);
212                 }
213         }
214         elsif (defined $q->param('openid_identifier')) {
215                 # myopenid.com affiliate support
216                 validate($q, $session, $q->param('openid_identifier'));
217         }
220 sub getobj ($$) {
221         my $q=shift;
222         my $session=shift;
224         eval q{use Net::OpenID::Consumer};
225         error($@) if $@;
227         my $ua;
228         eval q{use LWPx::ParanoidAgent};
229         if (! $@) {
230                 $ua=LWPx::ParanoidAgent->new;
231         }
232         else {
233                 $ua=LWP::UserAgent->new;
234         }
236         # Store the secret in the session.
237         my $secret=$session->param("openid_secret");
238         if (! defined $secret) {
239                 $secret=rand;
240                 $session->param(openid_secret => $secret);
241         }
243         return Net::OpenID::Consumer->new(
244                 ua => $ua,
245                 args => $q,
246                 consumer_secret => sub { return shift()+$secret },
247                 required_root => $config{cgiurl},
248         );
251 sub load_openid_module {
252         # Give up if module is unavailable to avoid needing to depend on it.
253         eval q{use Net::OpenID::Consumer};
254         if ($@) {
255                 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
256                 return;
257         }
258         return 1;
261 sub loginlabel {
262         return gettext("Log in with")." ".htmllink("", "", "ikiwiki/OpenID", noimageinline => 1);