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 () { #{{{
28 example => "http://myopenid.com/",
29 description => "an url where users can signup for an OpenID",
35 sub formbuilder_setup (@) { #{{{
38 my $form=$params{form};
39 my $session=$params{session};
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};
47 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login");
51 # This avoids it displaying a redundant label for the
53 $form->fieldsets("OpenID");
57 label => gettext("Log in with")." ".htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
60 comment => ($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">".gettext("Get an OpenID")."</a>" : "")
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")) {
70 validate($cgi, $session, shift, $form);
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,
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,
91 sub validate ($$$;$) { #{{{
97 my $csr=getobj($q, $session);
99 my $claimed_identity = $csr->claimed_identity($openid_url);
100 if (! $claimed_identity) {
102 # Put the error in the form and fail validation.
103 $form->field(name => "openid_url", comment => $csr->err);
111 my $check_url = $claimed_identity->check_url(
112 return_to => IkiWiki::cgiurl(do => "postsignin"),
113 trust_root => $config{cgiurl},
116 # Redirect the user to the OpenID server, which will
117 # eventually bounce them back to auth()
118 IkiWiki::redirect($q, $check_url);
126 if (defined $q->param('openid.mode')) {
127 my $csr=getobj($q, $session);
129 if (my $setup_url = $csr->user_setup_url) {
130 IkiWiki::redirect($q, $setup_url);
132 elsif ($csr->user_cancel) {
133 IkiWiki::redirect($q, $config{url});
135 elsif (my $vident = $csr->verified_identity) {
136 $session->param(name => $vident->url);
139 error("OpenID failure: ".$csr->err);
142 elsif (defined $q->param('openid_identifier')) {
143 # myopenid.com affiliate support
144 validate($q, $session, $q->param('openid_identifier'));
148 sub getobj ($$) { #{{{
152 eval q{use Net::OpenID::Consumer};
156 eval q{use LWPx::ParanoidAgent};
158 $ua=LWPx::ParanoidAgent->new;
161 $ua=LWP::UserAgent->new;
164 # Store the secret in the session.
165 my $secret=$session->param("openid_secret");
166 if (! defined $secret) {
168 $session->param(openid_secret => $secret);
171 return Net::OpenID::Consumer->new(
174 consumer_secret => sub { return shift()+$secret },
175 required_root => $config{cgiurl},
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 ($) { #{{{
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]/;
194 # Convert "http://somehost.com/user" to "user [somehost.com]".
195 if ($display !~ /\[/) {
196 $display=~s/^https?:\/\/(.+)\/([^\/]+)$/$2 [$1]/;
198 $display=~s!^https?://!!; # make sure this is removed
199 eval q{use CGI 'escapeHTML'};
201 return escapeHTML($display);