3 package IkiWiki::Plugin::openid;
10 hook(type => "getopt", id => "openid", call => \&getopt);
11 hook(type => "auth", id => "openid", call => \&auth);
12 hook(type => "formbuilder_setup", id => "openid",
13 call => \&formbuilder_setup, last => 1);
17 eval q{use Getopt::Long};
19 Getopt::Long::Configure('pass_through');
20 GetOptions("openidsignup=s" => \$config{openidsignup});
23 sub formbuilder_setup (@) { #{{{
26 my $form=$params{form};
27 my $session=$params{session};
30 if ($form->title eq "signin") {
31 # Give up if module is unavailable to avoid
32 # needing to depend on it.
33 eval q{use Net::OpenID::Consumer};
35 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login");
39 # This avoids it displaying a redundant label for the
41 $form->fieldsets("OpenID");
45 label => gettext("Log in with")." ".htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
48 comment => ($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">".gettext("Get an OpenID")."</a>" : "")
51 # Handle submission of an OpenID as validation.
52 if ($form->submitted && $form->submitted eq "Login" &&
53 defined $form->field("openid_url") &&
54 length $form->field("openid_url")) {
58 validate($cgi, $session, shift, $form);
61 # Skip all other required fields in this case.
62 foreach my $field ($form->field) {
63 next if $field eq "openid_url";
64 $form->field(name => $field, required => 0,
69 elsif ($form->title eq "preferences") {
70 if (! defined $form->field(name => "name")) {
71 $form->field(name => "OpenID", disabled => 1,
72 value => $session->param("name"),
73 size => 50, force => 1,
79 sub validate ($$$;$) { #{{{
85 my $csr=getobj($q, $session);
87 my $claimed_identity = $csr->claimed_identity($openid_url);
88 if (! $claimed_identity) {
90 # Put the error in the form and fail validation.
91 $form->field(name => "openid_url", comment => $csr->err);
99 my $check_url = $claimed_identity->check_url(
100 return_to => IkiWiki::cgiurl(do => "postsignin"),
101 trust_root => $config{cgiurl},
104 # Redirect the user to the OpenID server, which will
105 # eventually bounce them back to auth()
106 IkiWiki::redirect($q, $check_url);
114 if (defined $q->param('openid.mode')) {
115 my $csr=getobj($q, $session);
117 if (my $setup_url = $csr->user_setup_url) {
118 IkiWiki::redirect($q, $setup_url);
120 elsif ($csr->user_cancel) {
121 IkiWiki::redirect($q, $config{url});
123 elsif (my $vident = $csr->verified_identity) {
124 $session->param(name => $vident->url);
127 error("OpenID failure: ".$csr->err);
130 elsif (defined $q->param('openid_identifier')) {
131 # myopenid.com affiliate support
132 validate($q, $session, $q->param('openid_identifier'));
136 sub getobj ($$) { #{{{
140 eval q{use Net::OpenID::Consumer};
144 eval q{use LWPx::ParanoidAgent};
146 $ua=LWPx::ParanoidAgent->new;
149 $ua=LWP::UserAgent->new;
152 # Store the secret in the session.
153 my $secret=$session->param("openid_secret");
154 if (! defined $secret) {
156 $session->param(openid_secret => $secret);
159 return Net::OpenID::Consumer->new(
162 consumer_secret => sub { return shift()+$secret },
163 required_root => $config{cgiurl},
169 # This is not used by this plugin, but this seems the best place to put it.
170 # Used elsewhere to pretty-display the name of an openid user.
171 sub openiduser ($) { #{{{
174 if ($user =~ m!^https?://! &&
175 eval q{use Net::OpenID::VerifiedIdentity; 1} && !$@) {
176 my $oid=Net::OpenID::VerifiedIdentity->new(identity => $user);
177 my $display=$oid->display;
178 # Convert "user.somehost.com" to "user [somehost.com]".
179 if ($display !~ /\[/) {
180 $display=~s/^(.*?)\.([^.]+\.[a-z]+)$/$1 [$2]/;
182 # Convert "http://somehost.com/user" to "user [somehost.com]".
183 if ($display !~ /\[/) {
184 $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
186 $display=~s!^https?://!!; # make sure this is removed
187 eval q{use CGI 'escapeHTML'};
189 return escapeHTML($display);