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") {
36 htmllink("", "", "OpenID", 1, 0, gettext("What's this?"))
37 .($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">".gettext("Get an OpenID")."</a>" : "")
41 # Handle submission of an OpenID as validation.
42 if ($form->submitted && $form->submitted eq "Login" &&
43 defined $form->field("openid_url") &&
44 length $form->field("openid_url")) {
48 validate($cgi, $session, shift, $form);
51 # Skip all other required fields in this case.
52 foreach my $field ($form->field) {
53 next if $field eq "openid_url";
54 $form->field(name => $field, required => 0,
59 elsif ($form->title eq "preferences") {
60 if (! defined $form->field(name => "name")) {
61 $form->field(name => "OpenID", disabled => 1, value =>
62 $session->param("name"), size => 30, force => 1);
67 sub validate ($$$;$) { #{{{
73 my $csr=getobj($q, $session);
75 my $claimed_identity = $csr->claimed_identity($openid_url);
76 if (! $claimed_identity) {
78 # Put the error in the form and fail validation.
79 $form->field(name => "openid_url", comment => $csr->err);
87 my $check_url = $claimed_identity->check_url(
88 return_to => IkiWiki::cgiurl(do => "postsignin"),
89 trust_root => $config{cgiurl},
92 # Redirect the user to the OpenID server, which will
93 # eventually bounce them back to auth()
94 IkiWiki::redirect($q, $check_url);
102 if (defined $q->param('openid.mode')) {
103 my $csr=getobj($q, $session);
105 if (my $setup_url = $csr->user_setup_url) {
106 IkiWiki::redirect($q, $setup_url);
108 elsif ($csr->user_cancel) {
109 IkiWiki::redirect($q, $config{url});
111 elsif (my $vident = $csr->verified_identity) {
112 $session->param(name => $vident->url);
115 error("OpenID failure: ".$csr->err);
118 elsif (defined $q->param('openid_identifier')) {
119 # myopenid.com affiliate support
120 validate($q, $session, $q->param('openid_identifier'));
124 sub getobj ($$) { #{{{
128 eval q{use Net::OpenID::Consumer};
132 eval q{use LWPx::ParanoidAgent};
134 $ua=LWPx::ParanoidAgent->new;
137 $ua=LWP::UserAgent->new;
140 # Store the secret in the session.
141 my $secret=$session->param("openid_secret");
142 if (! defined $secret) {
144 $session->param(openid_secret => $secret);
147 return Net::OpenID::Consumer->new(
150 consumer_secret => sub { return shift()+$secret },
151 required_root => $config{cgiurl},