3 package IkiWiki::Plugin::openid;
10 add_underlay("openid-selector");
11 hook(type => "checkconfig", id => "openid", call => \&checkconfig);
12 hook(type => "getsetup", id => "openid", call => \&getsetup);
13 hook(type => "auth", id => "openid", call => \&auth);
14 hook(type => "formbuilder_setup", id => "openid",
15 call => \&formbuilder_setup, last => 1);
20 # Intercept normal signin form, so the openid selector
23 # When other auth hooks are registered, give the selector
24 # a reference to the normal signin form.
27 if (keys %{$IkiWiki::hooks{auth}} > 1) {
28 $real_cgi_signin=\&IkiWiki::cgi_signin;
30 inject(name => "IkiWiki::cgi_signin", call => sub ($$) {
31 openid_selector($real_cgi_signin, @_);
46 my $real_cgi_signin=shift;
50 my $openid_url=$q->param('openid_identifier');
53 if (! load_openid_module()) {
54 if ($real_cgi_signin) {
55 $real_cgi_signin->($q, $session);
58 error(sprintf(gettext("failed to load openid module: "), @_));
60 elsif (defined $q->param("action") && $q->param("action") eq "verify") {
61 validate($q, $session, $openid_url, sub {
65 elsif ($q->param("do") eq "signin" && $real_cgi_signin) {
66 $real_cgi_signin->($q, $session);
70 my $template=IkiWiki::template("openid-selector.tmpl");
72 cgiurl => $config{cgiurl},
73 (defined $openid_error ? (openid_error => $openid_error) : ()),
74 (defined $openid_url ? (openid_url => $openid_url) : ()),
75 ($real_cgi_signin ? (nonopenidurl => IkiWiki::cgiurl(do => "signin")) : ()),
78 IkiWiki::printheader($session);
79 print IkiWiki::misctemplate("signin", $template->output);
83 sub formbuilder_setup (@) {
86 my $form=$params{form};
87 my $session=$params{session};
90 if ($form->title eq "preferences" &&
91 IkiWiki::openiduser($session->param("name"))) {
92 $form->field(name => "openid_identifier", disabled => 1,
93 label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
94 value => $session->param("name"),
95 size => 50, force => 1,
97 $form->field(name => "email", type => "hidden");
101 sub validate ($$$;$) {
104 my $openid_url=shift;
105 my $errhandler=shift;
107 my $csr=getobj($q, $session);
109 my $claimed_identity = $csr->claimed_identity($openid_url);
110 if (! $claimed_identity) {
112 $errhandler->($csr->err);
120 # Ask for client to provide a name and email, if possible.
122 if ($claimed_identity->can("set_extension_args")) {
123 $claimed_identity->set_extension_args(
124 'http://openid.net/extensions/sreg/1.1',
126 optional => 'email,fullname,nickname',
129 $claimed_identity->set_extension_args(
130 'http://openid.net/srv/ax/1.0',
132 mode => 'fetch_request',
133 'required' => 'email,fullname,nickname,firstname',
134 'type.email' => "http://schema.openid.net/contact/email",
135 'type.fullname' => "http://axschema.org/namePerson",
136 'type.nickname' => "http://axschema.org/namePerson/friendly",
137 'type.firstname' => "http://axschema.org/namePerson/first",
142 my $check_url = $claimed_identity->check_url(
143 return_to => IkiWiki::cgiurl(do => "postsignin"),
144 trust_root => $config{cgiurl},
147 # Redirect the user to the OpenID server, which will
148 # eventually bounce them back to auth()
149 IkiWiki::redirect($q, $check_url);
157 if (defined $q->param('openid.mode')) {
158 my $csr=getobj($q, $session);
160 if (my $setup_url = $csr->user_setup_url) {
161 IkiWiki::redirect($q, $setup_url);
163 elsif ($csr->user_cancel) {
164 IkiWiki::redirect($q, $config{url});
166 elsif (my $vident = $csr->verified_identity) {
167 $session->param(name => $vident->url);
170 if ($vident->can("signed_extension_fields")) {
171 @extensions=grep { defined } (
172 $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
173 $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
176 foreach my $ext (@extensions) {
177 foreach my $field (qw{value.email email}) {
178 if (exists $ext->{$field} &&
179 defined $ext->{$field} &&
180 length $ext->{$field}) {
181 $session->param(email => $ext->{$field});
185 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
186 if (exists $ext->{$field} &&
187 defined $ext->{$field} &&
188 length $ext->{$field}) {
189 $session->param(username => $ext->{$field});
196 error("OpenID failure: ".$csr->err);
199 elsif (defined $q->param('openid_identifier')) {
200 # myopenid.com affiliate support
201 validate($q, $session, $q->param('openid_identifier'));
209 eval q{use Net::OpenID::Consumer};
213 eval q{use LWPx::ParanoidAgent};
215 $ua=LWPx::ParanoidAgent->new;
218 $ua=LWP::UserAgent->new;
221 # Store the secret in the session.
222 my $secret=$session->param("openid_secret");
223 if (! defined $secret) {
225 $session->param(openid_secret => $secret);
228 return Net::OpenID::Consumer->new(
231 consumer_secret => sub { return shift()+$secret },
232 required_root => $config{cgiurl},
236 sub load_openid_module {
237 # Give up if module is unavailable to avoid needing to depend on it.
238 eval q{use Net::OpenID::Consumer};
240 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");