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 {
66 my $template=IkiWiki::template("openid-selector.tmpl");
68 cgiurl => $config{cgiurl},
69 (defined $openid_error ? (openid_error => $openid_error) : ()),
70 (defined $openid_url ? (openid_url => $openid_url) : ()),
71 ($real_cgi_signin ? (nonopenidform => $real_cgi_signin->($q, $session, 1)) : ()),
74 IkiWiki::printheader($session);
75 print IkiWiki::misctemplate("signin", $template->output);
79 sub formbuilder_setup (@) {
82 my $form=$params{form};
83 my $session=$params{session};
86 if ($form->title eq "preferences" &&
87 IkiWiki::openiduser($session->param("name"))) {
88 $form->field(name => "openid_identifier", disabled => 1,
89 label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
90 value => $session->param("name"),
91 size => length($session->param("name")), force => 1,
93 $form->field(name => "email", type => "hidden");
97 sub validate ($$$;$) {
100 my $openid_url=shift;
101 my $errhandler=shift;
103 my $csr=getobj($q, $session);
105 my $claimed_identity = $csr->claimed_identity($openid_url);
106 if (! $claimed_identity) {
108 $errhandler->($csr->err);
116 # Ask for client to provide a name and email, if possible.
118 if ($claimed_identity->can("set_extension_args")) {
119 $claimed_identity->set_extension_args(
120 'http://openid.net/extensions/sreg/1.1',
122 optional => 'email,fullname,nickname',
125 $claimed_identity->set_extension_args(
126 'http://openid.net/srv/ax/1.0',
128 mode => 'fetch_request',
129 'required' => 'email,fullname,nickname,firstname',
130 'type.email' => "http://schema.openid.net/contact/email",
131 'type.fullname' => "http://axschema.org/namePerson",
132 'type.nickname' => "http://axschema.org/namePerson/friendly",
133 'type.firstname' => "http://axschema.org/namePerson/first",
138 my $check_url = $claimed_identity->check_url(
139 return_to => IkiWiki::cgiurl(do => "postsignin"),
140 trust_root => $config{cgiurl},
143 # Redirect the user to the OpenID server, which will
144 # eventually bounce them back to auth()
145 IkiWiki::redirect($q, $check_url);
153 if (defined $q->param('openid.mode')) {
154 my $csr=getobj($q, $session);
156 if (my $setup_url = $csr->user_setup_url) {
157 IkiWiki::redirect($q, $setup_url);
159 elsif ($csr->user_cancel) {
160 IkiWiki::redirect($q, $config{url});
162 elsif (my $vident = $csr->verified_identity) {
163 $session->param(name => $vident->url);
166 if ($vident->can("signed_extension_fields")) {
167 @extensions=grep { defined } (
168 $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
169 $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
172 foreach my $ext (@extensions) {
173 foreach my $field (qw{value.email email}) {
174 if (exists $ext->{$field} &&
175 defined $ext->{$field} &&
176 length $ext->{$field}) {
177 $session->param(email => $ext->{$field});
181 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
182 if (exists $ext->{$field} &&
183 defined $ext->{$field} &&
184 length $ext->{$field}) {
185 $session->param(username => $ext->{$field});
192 error("OpenID failure: ".$csr->err);
195 elsif (defined $q->param('openid_identifier')) {
196 # myopenid.com affiliate support
197 validate($q, $session, $q->param('openid_identifier'));
205 eval q{use Net::OpenID::Consumer};
209 eval q{use LWPx::ParanoidAgent};
211 $ua=LWPx::ParanoidAgent->new;
214 $ua=LWP::UserAgent->new;
217 # Store the secret in the session.
218 my $secret=$session->param("openid_secret");
219 if (! defined $secret) {
221 $session->param(openid_secret => $secret);
224 return Net::OpenID::Consumer->new(
227 consumer_secret => sub { return shift()+$secret },
228 required_root => $config{cgiurl},
232 sub load_openid_module {
233 # Give up if module is unavailable to avoid needing to depend on it.
234 eval q{use Net::OpenID::Consumer};
236 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");