3 package IkiWiki::Plugin::openid;
10 hook(type => "checkconfig", id => "openid", call => \&checkconfig);
11 hook(type => "getopt", id => "openid", call => \&getopt);
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 my $real_cgi_signin=\&IkiWiki::cgi_signin;
24 inject(name => "IkiWiki::cgi_signin", call => sub ($$) {
25 openid_selector($real_cgi_signin, @_);
31 eval q{use Getopt::Long};
33 Getopt::Long::Configure('pass_through');
34 GetOptions("openidsignup=s" => \$config{openidsignup});
46 example => "http://myopenid.com/",
47 description => "an url where users can signup for an OpenID",
54 my $real_cgi_signin=shift;
58 my $openid_url=$q->param('openid_url');
61 if (defined $q->param("action") && $q->param("action") eq "verify") {
62 validate($q, $session, $openid_url, sub {
66 elsif ($q->param("do") eq "signin" || ! load_openid_module()) {
67 $real_cgi_signin->($q, $session);
71 my $template=IkiWiki::template("openid-selector.tmpl");
73 cgiurl => $config{cgiurl},
74 (defined $openid_error ? (openid_error => $openid_error) : ()),
75 (defined $openid_url ? (openid_url => $openid_url) : ()),
76 # TODO only if other auth methods are available
77 nonopenidurl => IkiWiki::cgiurl(do => "signin"),
78 loginlabel => loginlabel(),
81 IkiWiki::printheader($session);
82 print IkiWiki::misctemplate("signin", $template->output);
86 sub formbuilder_setup (@) {
89 my $form=$params{form};
90 my $session=$params{session};
93 if ($form->title eq "signin") {
94 return unless load_openid_module();
96 # This avoids it displaying a redundant label for the
98 $form->fieldsets("OpenID");
101 name => "openid_url",
102 label => loginlabel(),
103 fieldset => "OpenID",
105 comment => ($config{openidsignup} ? " | <a href=\"$config{openidsignup}\">".gettext("Get an OpenID")."</a>" : "")
108 # Handle submission of an OpenID as validation.
109 if ($form->submitted && $form->submitted eq "Login" &&
110 defined $form->field("openid_url") &&
111 length $form->field("openid_url")) {
113 name => "openid_url",
115 validate($cgi, $session, shift, sub {
116 # Display error in the form.
117 $form->field(name => "openid_url", comment => shift);
121 # Skip all other required fields in this case.
122 foreach my $field ($form->field) {
123 next if $field eq "openid_url";
124 $form->field(name => $field, required => 0,
129 elsif ($form->title eq "preferences" &&
130 IkiWiki::openiduser($session->param("name"))) {
131 $form->field(name => "openid_url", disabled => 1,
132 label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
133 value => $session->param("name"),
134 size => 50, force => 1,
135 fieldset => "login");
136 $form->field(name => "email", type => "hidden");
140 sub validate ($$$;$) {
143 my $openid_url=shift;
144 my $errhandler=shift;
146 my $csr=getobj($q, $session);
148 my $claimed_identity = $csr->claimed_identity($openid_url);
149 if (! $claimed_identity) {
151 $errhandler->($csr->err);
159 # Ask for client to provide a name and email, if possible.
161 if ($claimed_identity->can("set_extension_args")) {
162 $claimed_identity->set_extension_args(
163 'http://openid.net/extensions/sreg/1.1',
165 optional => 'email,fullname,nickname',
168 $claimed_identity->set_extension_args(
169 'http://openid.net/srv/ax/1.0',
171 mode => 'fetch_request',
172 'required' => 'email,fullname,nickname,firstname',
173 'type.email' => "http://schema.openid.net/contact/email",
174 'type.fullname' => "http://axschema.org/namePerson",
175 'type.nickname' => "http://axschema.org/namePerson/friendly",
176 'type.firstname' => "http://axschema.org/namePerson/first",
181 my $check_url = $claimed_identity->check_url(
182 return_to => IkiWiki::cgiurl(do => "postsignin"),
183 trust_root => $config{cgiurl},
186 # Redirect the user to the OpenID server, which will
187 # eventually bounce them back to auth()
188 IkiWiki::redirect($q, $check_url);
196 if (defined $q->param('openid.mode')) {
197 my $csr=getobj($q, $session);
199 if (my $setup_url = $csr->user_setup_url) {
200 IkiWiki::redirect($q, $setup_url);
202 elsif ($csr->user_cancel) {
203 IkiWiki::redirect($q, $config{url});
205 elsif (my $vident = $csr->verified_identity) {
206 $session->param(name => $vident->url);
209 if ($vident->can("signed_extension_fields")) {
210 @extensions=grep { defined } (
211 $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
212 $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
215 foreach my $ext (@extensions) {
216 foreach my $field (qw{value.email email}) {
217 if (exists $ext->{$field} &&
218 defined $ext->{$field} &&
219 length $ext->{$field}) {
220 $session->param(email => $ext->{$field});
224 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
225 if (exists $ext->{$field} &&
226 defined $ext->{$field} &&
227 length $ext->{$field}) {
228 $session->param(username => $ext->{$field});
235 error("OpenID failure: ".$csr->err);
238 elsif (defined $q->param('openid_identifier')) {
239 # myopenid.com affiliate support
240 validate($q, $session, $q->param('openid_identifier'));
248 eval q{use Net::OpenID::Consumer};
252 eval q{use LWPx::ParanoidAgent};
254 $ua=LWPx::ParanoidAgent->new;
257 $ua=LWP::UserAgent->new;
260 # Store the secret in the session.
261 my $secret=$session->param("openid_secret");
262 if (! defined $secret) {
264 $session->param(openid_secret => $secret);
267 return Net::OpenID::Consumer->new(
270 consumer_secret => sub { return shift()+$secret },
271 required_root => $config{cgiurl},
275 sub load_openid_module {
276 # Give up if module is unavailable to avoid needing to depend on it.
277 eval q{use Net::OpenID::Consumer};
279 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");
286 return gettext("Log in with")." ".htmllink("", "", "ikiwiki/OpenID", noimageinline => 1);