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});
32 example => "http://myopenid.com/",
33 description => "an url where users can signup for an OpenID",
39 sub formbuilder_setup (@) {
42 my $form=$params{form};
43 my $session=$params{session};
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};
51 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
55 # This avoids it displaying a redundant label for the
57 $form->fieldsets("OpenID");
61 label => gettext("Log in with")." ".htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
64 comment => ($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">".gettext("Get an OpenID")."</a>" : "")
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")) {
74 validate($cgi, $session, shift, $form);
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,
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,
95 sub validate ($$$;$) {
101 my $csr=getobj($q, $session);
103 my $claimed_identity = $csr->claimed_identity($openid_url);
104 if (! $claimed_identity) {
106 # Put the error in the form and fail validation.
107 $form->field(name => "openid_url", comment => $csr->err);
115 my $check_url = $claimed_identity->check_url(
116 return_to => IkiWiki::cgiurl(do => "postsignin"),
117 trust_root => $config{cgiurl},
120 # Redirect the user to the OpenID server, which will
121 # eventually bounce them back to auth()
122 IkiWiki::redirect($q, $check_url);
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);
136 elsif ($csr->user_cancel) {
137 IkiWiki::redirect($q, $config{url});
139 elsif (my $vident = $csr->verified_identity) {
140 $session->param(name => $vident->url);
143 error("OpenID failure: ".$csr->err);
146 elsif (defined $q->param('openid_identifier')) {
147 # myopenid.com affiliate support
148 validate($q, $session, $q->param('openid_identifier'));
156 eval q{use Net::OpenID::Consumer};
160 eval q{use LWPx::ParanoidAgent};
162 $ua=LWPx::ParanoidAgent->new;
165 $ua=LWP::UserAgent->new;
168 # Store the secret in the session.
169 my $secret=$session->param("openid_secret");
170 if (! defined $secret) {
172 $session->param(openid_secret => $secret);
175 return Net::OpenID::Consumer->new(
178 consumer_secret => sub { return shift()+$secret },
179 required_root => $config{cgiurl},
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.
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]/;
198 # Convert "http://somehost.com/user" to "user [somehost.com]".
199 if ($display !~ /\[/) {
200 $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
202 $display=~s!^https?://!!; # make sure this is removed
203 eval q{use CGI 'escapeHTML'};
205 return escapeHTML($display);