3 package IkiWiki::Plugin::openid;
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);
18 eval q{use Getopt::Long};
20 Getopt::Long::Configure('pass_through');
21 GetOptions("openidsignup=s" => \$config{openidsignup});
24 sub getsetup () { #{{{
29 example => "http://myopenid.com/",
30 description => "an url where users can signup for an OpenID",
36 sub formbuilder_setup (@) { #{{{
39 my $form=$params{form};
40 my $session=$params{session};
43 if ($form->title eq "signin") {
44 # Give up if module is unavailable to avoid
45 # needing to depend on it.
46 eval q{use Net::OpenID::Consumer};
48 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login");
52 # This avoids it displaying a redundant label for the
54 $form->fieldsets("OpenID");
58 label => gettext("Log in with")." ".htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
61 comment => ($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">".gettext("Get an OpenID")."</a>" : "")
64 # Handle submission of an OpenID as validation.
65 if ($form->submitted && $form->submitted eq "Login" &&
66 defined $form->field("openid_url") &&
67 length $form->field("openid_url")) {
71 validate($cgi, $session, shift, $form);
74 # Skip all other required fields in this case.
75 foreach my $field ($form->field) {
76 next if $field eq "openid_url";
77 $form->field(name => $field, required => 0,
82 elsif ($form->title eq "preferences") {
83 if (! defined $form->field(name => "name")) {
84 $form->field(name => "OpenID", disabled => 1,
85 value => $session->param("name"),
86 size => 50, force => 1,
92 sub validate ($$$;$) { #{{{
98 my $csr=getobj($q, $session);
100 my $claimed_identity = $csr->claimed_identity($openid_url);
101 if (! $claimed_identity) {
103 # Put the error in the form and fail validation.
104 $form->field(name => "openid_url", comment => $csr->err);
112 my $check_url = $claimed_identity->check_url(
113 return_to => IkiWiki::cgiurl(do => "postsignin"),
114 trust_root => $config{cgiurl},
117 # Redirect the user to the OpenID server, which will
118 # eventually bounce them back to auth()
119 IkiWiki::redirect($q, $check_url);
127 if (defined $q->param('openid.mode')) {
128 my $csr=getobj($q, $session);
130 if (my $setup_url = $csr->user_setup_url) {
131 IkiWiki::redirect($q, $setup_url);
133 elsif ($csr->user_cancel) {
134 IkiWiki::redirect($q, $config{url});
136 elsif (my $vident = $csr->verified_identity) {
137 $session->param(name => $vident->url);
140 error("OpenID failure: ".$csr->err);
143 elsif (defined $q->param('openid_identifier')) {
144 # myopenid.com affiliate support
145 validate($q, $session, $q->param('openid_identifier'));
149 sub getobj ($$) { #{{{
153 eval q{use Net::OpenID::Consumer};
157 eval q{use LWPx::ParanoidAgent};
159 $ua=LWPx::ParanoidAgent->new;
162 $ua=LWP::UserAgent->new;
165 # Store the secret in the session.
166 my $secret=$session->param("openid_secret");
167 if (! defined $secret) {
169 $session->param(openid_secret => $secret);
172 return Net::OpenID::Consumer->new(
175 consumer_secret => sub { return shift()+$secret },
176 required_root => $config{cgiurl},
182 # This is not used by this plugin, but this seems the best place to put it.
183 # Used elsewhere to pretty-display the name of an openid user.
184 sub openiduser ($) { #{{{
187 if ($user =~ m!^https?://! &&
188 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
189 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
190 my $display=$oid->display;
191 # Convert "user.somehost.com" to "user [somehost.com]".
192 if ($display !~ /\[/) {
193 $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
195 # Convert "http://somehost.com/user" to "user [somehost.com]".
196 if ($display !~ /\[/) {
197 $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
199 $display=~s!^https?://!!; # make sure this is removed
200 eval q{use CGI 'escapeHTML'};
202 return escapeHTML($display);