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 # This avoids it displaying a redundant label for the
33 $form->fieldsets("OpenID");
37 label => gettext("Log in with")." ".htmllink("", "", "OpenID", noimageinline => 1),
40 comment => ($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">".gettext("Get an OpenID")."</a>" : "")
43 # Handle submission of an OpenID as validation.
44 if ($form->submitted && $form->submitted eq "Login" &&
45 defined $form->field("openid_url") &&
46 length $form->field("openid_url")) {
50 validate($cgi, $session, shift, $form);
53 # Skip all other required fields in this case.
54 foreach my $field ($form->field) {
55 next if $field eq "openid_url";
56 $form->field(name => $field, required => 0,
61 elsif ($form->title eq "preferences") {
62 if (! defined $form->field(name => "name")) {
63 $form->field(name => "OpenID", disabled => 1,
64 value => $session->param("name"),
65 size => 50, force => 1,
71 sub validate ($$$;$) { #{{{
77 my $csr=getobj($q, $session);
79 my $claimed_identity = $csr->claimed_identity($openid_url);
80 if (! $claimed_identity) {
82 # Put the error in the form and fail validation.
83 $form->field(name => "openid_url", comment => $csr->err);
91 my $check_url = $claimed_identity->check_url(
92 return_to => IkiWiki::cgiurl(do => "postsignin"),
93 trust_root => $config{cgiurl},
96 # Redirect the user to the OpenID server, which will
97 # eventually bounce them back to auth()
98 IkiWiki::redirect($q, $check_url);
106 if (defined $q->param('openid.mode')) {
107 my $csr=getobj($q, $session);
109 if (my $setup_url = $csr->user_setup_url) {
110 IkiWiki::redirect($q, $setup_url);
112 elsif ($csr->user_cancel) {
113 IkiWiki::redirect($q, $config{url});
115 elsif (my $vident = $csr->verified_identity) {
116 $session->param(name => $vident->url);
119 error("OpenID failure: ".$csr->err);
122 elsif (defined $q->param('openid_identifier')) {
123 # myopenid.com affiliate support
124 validate($q, $session, $q->param('openid_identifier'));
128 sub getobj ($$) { #{{{
132 eval q{use Net::OpenID::Consumer};
136 eval q{use LWPx::ParanoidAgent};
138 $ua=LWPx::ParanoidAgent->new;
141 $ua=LWP::UserAgent->new;
144 # Store the secret in the session.
145 my $secret=$session->param("openid_secret");
146 if (! defined $secret) {
148 $session->param(openid_secret => $secret);
151 return Net::OpenID::Consumer->new(
154 consumer_secret => sub { return shift()+$secret },
155 required_root => $config{cgiurl},