]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/openid.pm
Removed the openidsignup option.
[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 => "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 }
18 sub checkconfig () {
19         if ($config{cgi}) {
20                 # Intercept normal signin form, so the openid selector
21                 # can be displayed.
22                 # 
23                 # When other auth hooks are registered, give the selector
24                 # a reference to the normal signin form.
25                 require IkiWiki::CGI;
26                 my $real_cgi_signin;
27                 if (keys %{$IkiWiki::hooks{auth}} > 1) {
28                         $real_cgi_signin=\&IkiWiki::cgi_signin;
29                 }
30                 inject(name => "IkiWiki::cgi_signin", call => sub ($$) {
31                         openid_selector($real_cgi_signin, @_);
32                 });
33         }
34 }
36 sub getsetup () {
37         return
38                 plugin => {
39                         safe => 1,
40                         rebuild => 0,
41                         section => "auth",
42                 },
43 }
45 sub openid_selector {
46         my $real_cgi_signin=shift;
47         my $q=shift;
48         my $session=shift;
50         my $openid_url=$q->param('openid_identifier');
51         my $openid_error;
53         if (! load_openid_module()) {
54                 if ($real_cgi_signin) {
55                         $real_cgi_signin->($q, $session);
56                         exit;
57                 }
58                 error(sprintf(gettext("failed to load openid module: "), @_));
59         }
60         elsif (defined $q->param("action") && $q->param("action") eq "verify") {
61                 validate($q, $session, $openid_url, sub {
62                         $openid_error=shift;
63                 });
64         }
65         elsif ($q->param("do") eq "signin" && $real_cgi_signin) {
66                 $real_cgi_signin->($q, $session);
67                 exit;
68         }
70         my $template=IkiWiki::template("openid-selector.tmpl");
71         $template->param(
72                 cgiurl => $config{cgiurl},
73                 (defined $openid_error ? (openid_error => $openid_error) : ()),
74                 (defined $openid_url ? (openid_url => $openid_url) : ()),
75                 ($real_cgi_signin ? (nonopenidurl => IkiWiki::cgiurl(do => "signin")) : ()),
76         );
78         IkiWiki::printheader($session);
79         print IkiWiki::misctemplate("signin", $template->output);
80         exit;
81 }
83 sub formbuilder_setup (@) {
84         my %params=@_;
86         my $form=$params{form};
87         my $session=$params{session};
88         my $cgi=$params{cgi};
89         
90         if ($form->title eq "preferences" &&
91                IkiWiki::openiduser($session->param("name"))) {
92                 $form->field(name => "openid_identifier", disabled => 1,
93                         label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
94                         value => $session->param("name"), 
95                         size => 50, force => 1,
96                         fieldset => "login");
97                 $form->field(name => "email", type => "hidden");
98         }
99 }
101 sub validate ($$$;$) {
102         my $q=shift;
103         my $session=shift;
104         my $openid_url=shift;
105         my $errhandler=shift;
107         my $csr=getobj($q, $session);
109         my $claimed_identity = $csr->claimed_identity($openid_url);
110         if (! $claimed_identity) {
111                 if ($errhandler) {
112                         $errhandler->($csr->err);
113                         return 0;
114                 }
115                 else {
116                         error($csr->err);
117                 }
118         }
120         # Ask for client to provide a name and email, if possible.
121         # Try sreg and ax
122         if ($claimed_identity->can("set_extension_args")) {
123                 $claimed_identity->set_extension_args(
124                         'http://openid.net/extensions/sreg/1.1',
125                         {
126                                 optional => 'email,fullname,nickname',
127                         },
128                 );
129                 $claimed_identity->set_extension_args(
130                         'http://openid.net/srv/ax/1.0',
131                         {
132                                 mode => 'fetch_request',
133                                 'required' => 'email,fullname,nickname,firstname',
134                                 'type.email' => "http://schema.openid.net/contact/email",
135                                 'type.fullname' => "http://axschema.org/namePerson",
136                                 'type.nickname' => "http://axschema.org/namePerson/friendly",
137                                 'type.firstname' => "http://axschema.org/namePerson/first",
138                         },
139                 );
140         }
142         my $check_url = $claimed_identity->check_url(
143                 return_to => IkiWiki::cgiurl(do => "postsignin"),
144                 trust_root => $config{cgiurl},
145                 delayed_return => 1,
146         );
147         # Redirect the user to the OpenID server, which will
148         # eventually bounce them back to auth()
149         IkiWiki::redirect($q, $check_url);
150         exit 0;
153 sub auth ($$) {
154         my $q=shift;
155         my $session=shift;
157         if (defined $q->param('openid.mode')) {
158                 my $csr=getobj($q, $session);
160                 if (my $setup_url = $csr->user_setup_url) {
161                         IkiWiki::redirect($q, $setup_url);
162                 }
163                 elsif ($csr->user_cancel) {
164                         IkiWiki::redirect($q, $config{url});
165                 }
166                 elsif (my $vident = $csr->verified_identity) {
167                         $session->param(name => $vident->url);
169                         my @extensions;
170                         if ($vident->can("signed_extension_fields")) {
171                                 @extensions=grep { defined } (
172                                         $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
173                                         $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
174                                 );
175                         }
176                         foreach my $ext (@extensions) {
177                                 foreach my $field (qw{value.email email}) {
178                                         if (exists $ext->{$field} &&
179                                             defined $ext->{$field} &&
180                                             length $ext->{$field}) {
181                                                 $session->param(email => $ext->{$field});
182                                                 last;
183                                         }
184                                 }
185                                 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
186                                         if (exists $ext->{$field} &&
187                                             defined $ext->{$field} &&
188                                             length $ext->{$field}) {
189                                                 $session->param(username => $ext->{$field});
190                                                 last;
191                                         }
192                                 }
193                         }
194                 }
195                 else {
196                         error("OpenID failure: ".$csr->err);
197                 }
198         }
199         elsif (defined $q->param('openid_identifier')) {
200                 # myopenid.com affiliate support
201                 validate($q, $session, $q->param('openid_identifier'));
202         }
205 sub getobj ($$) {
206         my $q=shift;
207         my $session=shift;
209         eval q{use Net::OpenID::Consumer};
210         error($@) if $@;
212         my $ua;
213         eval q{use LWPx::ParanoidAgent};
214         if (! $@) {
215                 $ua=LWPx::ParanoidAgent->new;
216         }
217         else {
218                 $ua=LWP::UserAgent->new;
219         }
221         # Store the secret in the session.
222         my $secret=$session->param("openid_secret");
223         if (! defined $secret) {
224                 $secret=rand;
225                 $session->param(openid_secret => $secret);
226         }
228         return Net::OpenID::Consumer->new(
229                 ua => $ua,
230                 args => $q,
231                 consumer_secret => sub { return shift()+$secret },
232                 required_root => $config{cgiurl},
233         );
236 sub load_openid_module {
237         # Give up if module is unavailable to avoid needing to depend on it.
238         eval q{use Net::OpenID::Consumer};
239         if ($@) {
240                 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
241                 return;
242         }
243         return 1;