]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/openid.pm
remove loginlabel, not used
[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         );
92         IkiWiki::printheader($session);
93         print IkiWiki::misctemplate("signin", $template->output);
94         exit;
95 }
97 sub formbuilder_setup (@) {
98         my %params=@_;
100         my $form=$params{form};
101         my $session=$params{session};
102         my $cgi=$params{cgi};
103         
104         if ($form->title eq "preferences" &&
105                IkiWiki::openiduser($session->param("name"))) {
106                 $form->field(name => "openid_identifier", disabled => 1,
107                         label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
108                         value => $session->param("name"), 
109                         size => 50, force => 1,
110                         fieldset => "login");
111                 $form->field(name => "email", type => "hidden");
112         }
115 sub validate ($$$;$) {
116         my $q=shift;
117         my $session=shift;
118         my $openid_url=shift;
119         my $errhandler=shift;
121         my $csr=getobj($q, $session);
123         my $claimed_identity = $csr->claimed_identity($openid_url);
124         if (! $claimed_identity) {
125                 if ($errhandler) {
126                         $errhandler->($csr->err);
127                         return 0;
128                 }
129                 else {
130                         error($csr->err);
131                 }
132         }
134         # Ask for client to provide a name and email, if possible.
135         # Try sreg and ax
136         if ($claimed_identity->can("set_extension_args")) {
137                 $claimed_identity->set_extension_args(
138                         'http://openid.net/extensions/sreg/1.1',
139                         {
140                                 optional => 'email,fullname,nickname',
141                         },
142                 );
143                 $claimed_identity->set_extension_args(
144                         'http://openid.net/srv/ax/1.0',
145                         {
146                                 mode => 'fetch_request',
147                                 'required' => 'email,fullname,nickname,firstname',
148                                 'type.email' => "http://schema.openid.net/contact/email",
149                                 'type.fullname' => "http://axschema.org/namePerson",
150                                 'type.nickname' => "http://axschema.org/namePerson/friendly",
151                                 'type.firstname' => "http://axschema.org/namePerson/first",
152                         },
153                 );
154         }
156         my $check_url = $claimed_identity->check_url(
157                 return_to => IkiWiki::cgiurl(do => "postsignin"),
158                 trust_root => $config{cgiurl},
159                 delayed_return => 1,
160         );
161         # Redirect the user to the OpenID server, which will
162         # eventually bounce them back to auth()
163         IkiWiki::redirect($q, $check_url);
164         exit 0;
167 sub auth ($$) {
168         my $q=shift;
169         my $session=shift;
171         if (defined $q->param('openid.mode')) {
172                 my $csr=getobj($q, $session);
174                 if (my $setup_url = $csr->user_setup_url) {
175                         IkiWiki::redirect($q, $setup_url);
176                 }
177                 elsif ($csr->user_cancel) {
178                         IkiWiki::redirect($q, $config{url});
179                 }
180                 elsif (my $vident = $csr->verified_identity) {
181                         $session->param(name => $vident->url);
183                         my @extensions;
184                         if ($vident->can("signed_extension_fields")) {
185                                 @extensions=grep { defined } (
186                                         $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
187                                         $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
188                                 );
189                         }
190                         foreach my $ext (@extensions) {
191                                 foreach my $field (qw{value.email email}) {
192                                         if (exists $ext->{$field} &&
193                                             defined $ext->{$field} &&
194                                             length $ext->{$field}) {
195                                                 $session->param(email => $ext->{$field});
196                                                 last;
197                                         }
198                                 }
199                                 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
200                                         if (exists $ext->{$field} &&
201                                             defined $ext->{$field} &&
202                                             length $ext->{$field}) {
203                                                 $session->param(username => $ext->{$field});
204                                                 last;
205                                         }
206                                 }
207                         }
208                 }
209                 else {
210                         error("OpenID failure: ".$csr->err);
211                 }
212         }
213         elsif (defined $q->param('openid_identifier')) {
214                 # myopenid.com affiliate support
215                 validate($q, $session, $q->param('openid_identifier'));
216         }
219 sub getobj ($$) {
220         my $q=shift;
221         my $session=shift;
223         eval q{use Net::OpenID::Consumer};
224         error($@) if $@;
226         my $ua;
227         eval q{use LWPx::ParanoidAgent};
228         if (! $@) {
229                 $ua=LWPx::ParanoidAgent->new;
230         }
231         else {
232                 $ua=LWP::UserAgent->new;
233         }
235         # Store the secret in the session.
236         my $secret=$session->param("openid_secret");
237         if (! defined $secret) {
238                 $secret=rand;
239                 $session->param(openid_secret => $secret);
240         }
242         return Net::OpenID::Consumer->new(
243                 ua => $ua,
244                 args => $q,
245                 consumer_secret => sub { return shift()+$secret },
246                 required_root => $config{cgiurl},
247         );
250 sub load_openid_module {
251         # Give up if module is unavailable to avoid needing to depend on it.
252         eval q{use Net::OpenID::Consumer};
253         if ($@) {
254                 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
255                 return;
256         }
257         return 1;