3 package IkiWiki::Plugin::openid;
10 hook(type => "getsetup", id => "openid", call => \&getsetup);
11 hook(type => "auth", id => "openid", call => \&auth);
12 hook(type => "formbuilder_setup", id => "openid",
13 call => \&formbuilder_setup, last => 1);
14 IkiWiki::loadplugin("emailauth");
15 IkiWiki::loadplugin("loginselector");
16 IkiWiki::Plugin::loginselector::register_login_plugin(
33 description => "url pattern of openid realm (default is cgiurl)",
39 description => "url to ikiwiki cgi to use for openid authentication (default is cgiurl)",
45 sub openid_setup ($$) {
49 if (load_openid_module()) {
50 my $openid_url=$q->param('openid_identifier');
51 if (defined $openid_url) {
52 $template->param(openid_url => $openid_url);
61 sub openid_check_input ($) {
63 my $openid_url=$q->param('openid_identifier');
64 defined $q->param("action") && $q->param("action") eq "verify" && defined $openid_url && length $openid_url;
67 sub openid_auth ($$$$) {
70 my $errordisplayer=shift;
71 my $openid_url=$q->param('openid_identifier');
72 validate($q, $session, $openid_url, $errordisplayer);
75 sub formbuilder_setup (@) {
78 my $form=$params{form};
79 my $session=$params{session};
82 if ($form->title eq "preferences" &&
83 IkiWiki::openiduser($session->param("name"))) {
84 $form->field(name => "openid_identifier", disabled => 1,
85 label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
87 size => 1, force => 1,
89 comment => $session->param("name"));
93 sub validate ($$$;$) {
99 my $csr=getobj($q, $session);
101 my $claimed_identity = $csr->claimed_identity($openid_url);
102 if (! $claimed_identity) {
104 if (ref($errhandler) eq 'CODE') {
105 $errhandler->($csr->err);
114 # Ask for client to provide a name and email, if possible.
116 if ($claimed_identity->can("set_extension_args")) {
117 $claimed_identity->set_extension_args(
118 'http://openid.net/extensions/sreg/1.1',
120 optional => 'email,fullname,nickname',
123 $claimed_identity->set_extension_args(
124 'http://openid.net/srv/ax/1.0',
126 mode => 'fetch_request',
127 'required' => 'email,fullname,nickname,firstname',
128 'type.email' => "http://schema.openid.net/contact/email",
129 'type.fullname' => "http://axschema.org/namePerson",
130 'type.nickname' => "http://axschema.org/namePerson/friendly",
131 'type.firstname' => "http://axschema.org/namePerson/first",
136 my $cgiurl=$config{openid_cgiurl};
137 $cgiurl=$q->url if ! defined $cgiurl;
139 my $trust_root=$config{openid_realm};
140 $trust_root=$cgiurl if ! defined $trust_root;
142 my $check_url = $claimed_identity->check_url(
143 return_to => auto_upgrade_https($q, "$cgiurl?do=postsignin"),
144 trust_root => auto_upgrade_https($q, $trust_root),
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, IkiWiki::baseurl(undef));
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'),
177 foreach my $ext (@extensions) {
178 foreach my $field (qw{value.email email}) {
179 if (exists $ext->{$field} &&
180 defined $ext->{$field} &&
181 length $ext->{$field}) {
182 $session->param(email => $ext->{$field});
183 if (! defined $nickname &&
184 $ext->{$field}=~/(.+)@.+/) {
190 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
191 if (exists $ext->{$field} &&
192 defined $ext->{$field} &&
193 length $ext->{$field}) {
194 $nickname=$ext->{$field};
199 if (defined $nickname) {
200 $session->param(nickname =>
201 Encode::decode_utf8($nickname));
205 error("OpenID failure: ".$csr->err);
208 elsif (defined $q->param('openid_identifier')) {
209 # myopenid.com affiliate support
210 validate($q, $session, scalar $q->param('openid_identifier'));
218 eval q{use Net::INET6Glue::INET_is_INET6}; # may not be available
219 eval q{use Net::OpenID::Consumer};
222 # We pass the for_url parameter, even though it's undef, because
223 # that will make sure we crash if used with an older IkiWiki.pm
224 # that didn't automatically try to use LWPx::ParanoidAgent.
225 my $ua=useragent(for_url => undef);
227 # Store the secret in the session.
228 my $secret=$session->param("openid_secret");
229 if (! defined $secret) {
231 $session->param(openid_secret => $secret);
234 my $cgiurl=$config{openid_cgiurl};
235 $cgiurl=$q->url if ! defined $cgiurl;
237 return Net::OpenID::Consumer->new(
240 consumer_secret => sub { return shift()+$secret },
241 required_root => auto_upgrade_https($q, $cgiurl),
245 sub auto_upgrade_https {
249 $url=~s/^http:/https:/i;
254 sub load_openid_module {
255 # Give up if module is unavailable to avoid needing to depend on it.
256 eval q{use Net::OpenID::Consumer};
258 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");