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