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