3 package IkiWiki::Plugin::openid;
10 add_underlay("openid-selector");
11 hook(type => "checkconfig", id => "openid", call => \&checkconfig);
12 hook(type => "getopt", id => "openid", call => \&getopt);
13 hook(type => "getsetup", id => "openid", call => \&getsetup);
14 hook(type => "auth", id => "openid", call => \&auth);
15 hook(type => "formbuilder_setup", id => "openid",
16 call => \&formbuilder_setup, last => 1);
21 # Intercept normal signin form, so the openid selector
24 # When other auth hooks are registered, give the selector
25 # a reference to the normal signin form.
27 if (keys %{$IkiWiki::hooks{auth}} > 1) {
29 $real_cgi_signin=\&IkiWiki::cgi_signin;
31 inject(name => "IkiWiki::cgi_signin", call => sub ($$) {
32 openid_selector($real_cgi_signin, @_);
38 eval q{use Getopt::Long};
40 Getopt::Long::Configure('pass_through');
41 GetOptions("openidsignup=s" => \$config{openidsignup});
53 example => "http://myopenid.com/",
54 description => "an url where users can signup for an OpenID",
61 my $real_cgi_signin=shift;
65 my $openid_url=$q->param('openid_identifier');
68 if (! load_openid_module()) {
69 if ($real_cgi_signin) {
70 $real_cgi_signin->($q, $session);
72 error(sprintf(gettext("failed to load openid module: "), @_));
74 elsif (defined $q->param("action") && $q->param("action") eq "verify") {
75 validate($q, $session, $openid_url, sub {
79 elsif ($q->param("do") eq "signin" && $real_cgi_signin) {
80 $real_cgi_signin->($q, $session);
84 my $template=IkiWiki::template("openid-selector.tmpl");
86 cgiurl => $config{cgiurl},
87 (defined $openid_error ? (openid_error => $openid_error) : ()),
88 (defined $openid_url ? (openid_url => $openid_url) : ()),
89 ($real_cgi_signin ? (nonopenidurl => IkiWiki::cgiurl(do => "signin")) : ()),
92 IkiWiki::printheader($session);
93 print IkiWiki::misctemplate("signin", $template->output);
97 sub formbuilder_setup (@) {
100 my $form=$params{form};
101 my $session=$params{session};
102 my $cgi=$params{cgi};
104 if ($form->title eq "preferences" &&
105 IkiWiki::openiduser($session->param("name"))) {
106 $form->field(name => "openid_identifier", disabled => 1,
107 label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
108 value => $session->param("name"),
109 size => 50, force => 1,
110 fieldset => "login");
111 $form->field(name => "email", type => "hidden");
115 sub validate ($$$;$) {
118 my $openid_url=shift;
119 my $errhandler=shift;
121 my $csr=getobj($q, $session);
123 my $claimed_identity = $csr->claimed_identity($openid_url);
124 if (! $claimed_identity) {
126 $errhandler->($csr->err);
134 # Ask for client to provide a name and email, if possible.
136 if ($claimed_identity->can("set_extension_args")) {
137 $claimed_identity->set_extension_args(
138 'http://openid.net/extensions/sreg/1.1',
140 optional => 'email,fullname,nickname',
143 $claimed_identity->set_extension_args(
144 'http://openid.net/srv/ax/1.0',
146 mode => 'fetch_request',
147 'required' => 'email,fullname,nickname,firstname',
148 'type.email' => "http://schema.openid.net/contact/email",
149 'type.fullname' => "http://axschema.org/namePerson",
150 'type.nickname' => "http://axschema.org/namePerson/friendly",
151 'type.firstname' => "http://axschema.org/namePerson/first",
156 my $check_url = $claimed_identity->check_url(
157 return_to => IkiWiki::cgiurl(do => "postsignin"),
158 trust_root => $config{cgiurl},
161 # Redirect the user to the OpenID server, which will
162 # eventually bounce them back to auth()
163 IkiWiki::redirect($q, $check_url);
171 if (defined $q->param('openid.mode')) {
172 my $csr=getobj($q, $session);
174 if (my $setup_url = $csr->user_setup_url) {
175 IkiWiki::redirect($q, $setup_url);
177 elsif ($csr->user_cancel) {
178 IkiWiki::redirect($q, $config{url});
180 elsif (my $vident = $csr->verified_identity) {
181 $session->param(name => $vident->url);
184 if ($vident->can("signed_extension_fields")) {
185 @extensions=grep { defined } (
186 $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
187 $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
190 foreach my $ext (@extensions) {
191 foreach my $field (qw{value.email email}) {
192 if (exists $ext->{$field} &&
193 defined $ext->{$field} &&
194 length $ext->{$field}) {
195 $session->param(email => $ext->{$field});
199 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
200 if (exists $ext->{$field} &&
201 defined $ext->{$field} &&
202 length $ext->{$field}) {
203 $session->param(username => $ext->{$field});
210 error("OpenID failure: ".$csr->err);
213 elsif (defined $q->param('openid_identifier')) {
214 # myopenid.com affiliate support
215 validate($q, $session, $q->param('openid_identifier'));
223 eval q{use Net::OpenID::Consumer};
227 eval q{use LWPx::ParanoidAgent};
229 $ua=LWPx::ParanoidAgent->new;
232 $ua=LWP::UserAgent->new;
235 # Store the secret in the session.
236 my $secret=$session->param("openid_secret");
237 if (! defined $secret) {
239 $session->param(openid_secret => $secret);
242 return Net::OpenID::Consumer->new(
245 consumer_secret => sub { return shift()+$secret },
246 required_root => $config{cgiurl},
250 sub load_openid_module {
251 # Give up if module is unavailable to avoid needing to depend on it.
252 eval q{use Net::OpenID::Consumer};
254 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");