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.
28 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);
73 error(sprintf(gettext("failed to load openid module: "), @_));
75 elsif (defined $q->param("action") && $q->param("action") eq "verify") {
76 validate($q, $session, $openid_url, sub {
80 elsif ($q->param("do") eq "signin" && $real_cgi_signin) {
81 $real_cgi_signin->($q, $session);
85 my $template=IkiWiki::template("openid-selector.tmpl");
87 cgiurl => $config{cgiurl},
88 (defined $openid_error ? (openid_error => $openid_error) : ()),
89 (defined $openid_url ? (openid_url => $openid_url) : ()),
90 ($real_cgi_signin ? (nonopenidurl => IkiWiki::cgiurl(do => "signin")) : ()),
93 IkiWiki::printheader($session);
94 print IkiWiki::misctemplate("signin", $template->output);
98 sub formbuilder_setup (@) {
101 my $form=$params{form};
102 my $session=$params{session};
103 my $cgi=$params{cgi};
105 if ($form->title eq "preferences" &&
106 IkiWiki::openiduser($session->param("name"))) {
107 $form->field(name => "openid_identifier", disabled => 1,
108 label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
109 value => $session->param("name"),
110 size => 50, force => 1,
111 fieldset => "login");
112 $form->field(name => "email", type => "hidden");
116 sub validate ($$$;$) {
119 my $openid_url=shift;
120 my $errhandler=shift;
122 my $csr=getobj($q, $session);
124 my $claimed_identity = $csr->claimed_identity($openid_url);
125 if (! $claimed_identity) {
127 $errhandler->($csr->err);
135 # Ask for client to provide a name and email, if possible.
137 if ($claimed_identity->can("set_extension_args")) {
138 $claimed_identity->set_extension_args(
139 'http://openid.net/extensions/sreg/1.1',
141 optional => 'email,fullname,nickname',
144 $claimed_identity->set_extension_args(
145 'http://openid.net/srv/ax/1.0',
147 mode => 'fetch_request',
148 'required' => 'email,fullname,nickname,firstname',
149 'type.email' => "http://schema.openid.net/contact/email",
150 'type.fullname' => "http://axschema.org/namePerson",
151 'type.nickname' => "http://axschema.org/namePerson/friendly",
152 'type.firstname' => "http://axschema.org/namePerson/first",
157 my $check_url = $claimed_identity->check_url(
158 return_to => IkiWiki::cgiurl(do => "postsignin"),
159 trust_root => $config{cgiurl},
162 # Redirect the user to the OpenID server, which will
163 # eventually bounce them back to auth()
164 IkiWiki::redirect($q, $check_url);
172 if (defined $q->param('openid.mode')) {
173 my $csr=getobj($q, $session);
175 if (my $setup_url = $csr->user_setup_url) {
176 IkiWiki::redirect($q, $setup_url);
178 elsif ($csr->user_cancel) {
179 IkiWiki::redirect($q, $config{url});
181 elsif (my $vident = $csr->verified_identity) {
182 $session->param(name => $vident->url);
185 if ($vident->can("signed_extension_fields")) {
186 @extensions=grep { defined } (
187 $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
188 $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
191 foreach my $ext (@extensions) {
192 foreach my $field (qw{value.email email}) {
193 if (exists $ext->{$field} &&
194 defined $ext->{$field} &&
195 length $ext->{$field}) {
196 $session->param(email => $ext->{$field});
200 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
201 if (exists $ext->{$field} &&
202 defined $ext->{$field} &&
203 length $ext->{$field}) {
204 $session->param(username => $ext->{$field});
211 error("OpenID failure: ".$csr->err);
214 elsif (defined $q->param('openid_identifier')) {
215 # myopenid.com affiliate support
216 validate($q, $session, $q->param('openid_identifier'));
224 eval q{use Net::OpenID::Consumer};
228 eval q{use LWPx::ParanoidAgent};
230 $ua=LWPx::ParanoidAgent->new;
233 $ua=LWP::UserAgent->new;
236 # Store the secret in the session.
237 my $secret=$session->param("openid_secret");
238 if (! defined $secret) {
240 $session->param(openid_secret => $secret);
243 return Net::OpenID::Consumer->new(
246 consumer_secret => sub { return shift()+$secret },
247 required_root => $config{cgiurl},
251 sub load_openid_module {
252 # Give up if module is unavailable to avoid needing to depend on it.
253 eval q{use Net::OpenID::Consumer};
255 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");