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 # Give up if module is unavailable to avoid needing to depend on
32 eval q{use Net::OpenID::Consumer};
34 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login");
38 if ($form->title eq "signin") {
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},