Merge branch 'tova' into autoconfig
[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 2.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                 openidsignup => {
27                         type => "string",
28                         example => "http://myopenid.com/",
29                         description => "an url where users can signup for an OpenID",
30                         safe => 1,
31                         rebuild => 0,
32                 },
33 } #}}}
34
35 sub formbuilder_setup (@) { #{{{
36         my %params=@_;
37
38         my $form=$params{form};
39         my $session=$params{session};
40         my $cgi=$params{cgi};
41         
42         if ($form->title eq "signin") {
43                 # Give up if module is unavailable to avoid
44                 # needing to depend on it.
45                 eval q{use Net::OpenID::Consumer};
46                 if ($@) {
47                         debug("unable to load Net::OpenID::Consumer, not enabling OpenID login");
48                         return;
49                 }
50
51                 # This avoids it displaying a redundant label for the
52                 # OpenID fieldset.
53                 $form->fieldsets("OpenID");
54
55                 $form->field(
56                         name => "openid_url",
57                         label => gettext("Log in with")." ".htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
58                         fieldset => "OpenID",
59                         size => 30,
60                         comment => ($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">".gettext("Get an OpenID")."</a>" : "")
61                 );
62
63                 # Handle submission of an OpenID as validation.
64                 if ($form->submitted && $form->submitted eq "Login" &&
65                     defined $form->field("openid_url") && 
66                     length $form->field("openid_url")) {
67                         $form->field(
68                                 name => "openid_url",
69                                 validate => sub {
70                                         validate($cgi, $session, shift, $form);
71                                 },
72                         );
73                         # Skip all other required fields in this case.
74                         foreach my $field ($form->field) {
75                                 next if $field eq "openid_url";
76                                 $form->field(name => $field, required => 0,
77                                         validate => '/.*/');
78                         }
79                 }
80         }
81         elsif ($form->title eq "preferences") {
82                 if (! defined $form->field(name => "name")) {
83                         $form->field(name => "OpenID", disabled => 1,
84                                 value => $session->param("name"), 
85                                 size => 50, force => 1,
86                                 fieldset => "login");
87                 }
88         }
89 }
90
91 sub validate ($$$;$) { #{{{
92         my $q=shift;
93         my $session=shift;
94         my $openid_url=shift;
95         my $form=shift;
96
97         my $csr=getobj($q, $session);
98
99         my $claimed_identity = $csr->claimed_identity($openid_url);
100         if (! $claimed_identity) {
101                 if ($form) {
102                         # Put the error in the form and fail validation.
103                         $form->field(name => "openid_url", comment => $csr->err);
104                         return 0;
105                 }
106                 else {
107                         error($csr->err);
108                 }
109         }
110
111         my $check_url = $claimed_identity->check_url(
112                 return_to => IkiWiki::cgiurl(do => "postsignin"),
113                 trust_root => $config{cgiurl},
114                 delayed_return => 1,
115         );
116         # Redirect the user to the OpenID server, which will
117         # eventually bounce them back to auth()
118         IkiWiki::redirect($q, $check_url);
119         exit 0;
120 } #}}}
121
122 sub auth ($$) { #{{{
123         my $q=shift;
124         my $session=shift;
125
126         if (defined $q->param('openid.mode')) {
127                 my $csr=getobj($q, $session);
128
129                 if (my $setup_url = $csr->user_setup_url) {
130                         IkiWiki::redirect($q, $setup_url);
131                 }
132                 elsif ($csr->user_cancel) {
133                         IkiWiki::redirect($q, $config{url});
134                 }
135                 elsif (my $vident = $csr->verified_identity) {
136                         $session->param(name => $vident->url);
137                 }
138                 else {
139                         error("OpenID failure: ".$csr->err);
140                 }
141         }
142         elsif (defined $q->param('openid_identifier')) {
143                 # myopenid.com affiliate support
144                 validate($q, $session, $q->param('openid_identifier'));
145         }
146 } #}}}
147
148 sub getobj ($$) { #{{{
149         my $q=shift;
150         my $session=shift;
151
152         eval q{use Net::OpenID::Consumer};
153         error($@) if $@;
154
155         my $ua;
156         eval q{use LWPx::ParanoidAgent};
157         if (! $@) {
158                 $ua=LWPx::ParanoidAgent->new;
159         }
160         else {
161                 $ua=LWP::UserAgent->new;
162         }
163
164         # Store the secret in the session.
165         my $secret=$session->param("openid_secret");
166         if (! defined $secret) {
167                 $secret=rand;
168                 $session->param(openid_secret => $secret);
169         }
170
171         return Net::OpenID::Consumer->new(
172                 ua => $ua,
173                 args => $q,
174                 consumer_secret => sub { return shift()+$secret },
175                 required_root => $config{cgiurl},
176         );
177 } #}}}
178
179 package IkiWiki;
180
181 # This is not used by this plugin, but this seems the best place to put it.
182 # Used elsewhere to pretty-display the name of an openid user.
183 sub openiduser ($) { #{{{
184         my $user=shift;
185
186         if ($user =~ m!^https?://! &&
187             eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
188                 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
189                 my $display=$oid->display;
190                 # Convert "user.somehost.com" to "user [somehost.com]".
191                 if ($display !~ /\[/) {
192                         $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
193                 }
194                 # Convert "http://somehost.com/user" to "user [somehost.com]".
195                 if ($display !~ /\[/) {
196                         $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
197                 }
198                 $display=~s!^https?://!!; # make sure this is removed
199                 eval q{use CGI 'escapeHTML'};
200                 error($@) if $@;
201                 return escapeHTML($display);
202         }
203         return;
204 }
205
206 1