3 package IkiWiki::Plugin::openid;
10 add_underlay("openid-selector");
11 add_underlay("jquery");
12 hook(type => "checkconfig", id => "openid", call => \&checkconfig);
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 my $nonopenidform_label=gettext("Other");
29 if (keys %{$IkiWiki::hooks{auth}} > 1) {
30 $real_cgi_signin=\&IkiWiki::cgi_signin;
31 if (keys %{$IkiWiki::hooks{auth}} == 2 && exists $IkiWiki::hooks{auth}->{passwordauth}) {
32 $nonopenidform_label=gettext("Password");
35 inject(name => "IkiWiki::cgi_signin", call => sub ($$) {
36 openid_selector($real_cgi_signin, $nonopenidform_label, @_);
50 description => "url pattern of openid realm (default is cgiurl)",
56 description => "url to ikiwiki cgi to use for openid authentication (default is cgiurl)",
63 my $real_cgi_signin=shift;
64 my $nonopenidform_label=shift;
68 my $openid_url=$q->param('openid_identifier');
71 if (! load_openid_module()) {
72 if ($real_cgi_signin) {
73 $real_cgi_signin->($q, $session);
76 error(sprintf(gettext("failed to load openid module: "), @_));
78 elsif (defined $q->param("action") && $q->param("action") eq "verify") {
79 validate($q, $session, $openid_url, sub {
84 my $template=IkiWiki::template("openid-selector.tmpl");
86 cgiurl => IkiWiki::cgiurl(),
87 (defined $openid_error ? (openid_error => $openid_error) : ()),
88 (defined $openid_url ? (openid_url => $openid_url) : ()),
89 ($real_cgi_signin ? (nonopenidform => $real_cgi_signin->($q, $session, 1)) : ()),
90 nonopenidform_label => $nonopenidform_label,
93 IkiWiki::printheader($session);
94 print IkiWiki::cgitemplate($q, "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),
110 size => 1, force => 1,
112 comment => $session->param("name"));
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 if (ref($errhandler) eq 'CODE') {
128 $errhandler->($csr->err);
137 # Ask for client to provide a name and email, if possible.
139 if ($claimed_identity->can("set_extension_args")) {
140 $claimed_identity->set_extension_args(
141 'http://openid.net/extensions/sreg/1.1',
143 optional => 'email,fullname,nickname',
146 $claimed_identity->set_extension_args(
147 'http://openid.net/srv/ax/1.0',
149 mode => 'fetch_request',
150 'required' => 'email,fullname,nickname,firstname',
151 'type.email' => "http://schema.openid.net/contact/email",
152 'type.fullname' => "http://axschema.org/namePerson",
153 'type.nickname' => "http://axschema.org/namePerson/friendly",
154 'type.firstname' => "http://axschema.org/namePerson/first",
159 my $cgiurl=$config{openid_cgiurl};
160 $cgiurl=$q->url if ! defined $cgiurl;
162 my $trust_root=$config{openid_realm};
163 $trust_root=$cgiurl if ! defined $trust_root;
165 my $check_url = $claimed_identity->check_url(
166 return_to => auto_upgrade_https($q, "$cgiurl?do=postsignin"),
167 trust_root => auto_upgrade_https($q, $trust_root),
170 # Redirect the user to the OpenID server, which will
171 # eventually bounce them back to auth()
172 IkiWiki::redirect($q, $check_url);
180 if (defined $q->param('openid.mode')) {
181 my $csr=getobj($q, $session);
183 if (my $setup_url = $csr->user_setup_url) {
184 IkiWiki::redirect($q, $setup_url);
186 elsif ($csr->user_cancel) {
187 IkiWiki::redirect($q, IkiWiki::baseurl(undef));
189 elsif (my $vident = $csr->verified_identity) {
190 $session->param(name => $vident->url);
193 if ($vident->can("signed_extension_fields")) {
194 @extensions=grep { defined } (
195 $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
196 $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
200 foreach my $ext (@extensions) {
201 foreach my $field (qw{value.email email}) {
202 if (exists $ext->{$field} &&
203 defined $ext->{$field} &&
204 length $ext->{$field}) {
205 $session->param(email => $ext->{$field});
206 if (! defined $nickname &&
207 $ext->{$field}=~/(.+)@.+/) {
213 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
214 if (exists $ext->{$field} &&
215 defined $ext->{$field} &&
216 length $ext->{$field}) {
217 $nickname=$ext->{$field};
222 if (defined $nickname) {
223 $session->param(nickname =>
224 Encode::decode_utf8($nickname));
228 error("OpenID failure: ".$csr->err);
231 elsif (defined $q->param('openid_identifier')) {
232 # myopenid.com affiliate support
233 validate($q, $session, scalar $q->param('openid_identifier'));
241 eval q{use Net::INET6Glue::INET_is_INET6}; # may not be available
242 eval q{use Net::OpenID::Consumer};
246 eval q{use LWPx::ParanoidAgent};
248 $ua=LWPx::ParanoidAgent->new(agent => $config{useragent});
254 # Store the secret in the session.
255 my $secret=$session->param("openid_secret");
256 if (! defined $secret) {
258 $session->param(openid_secret => $secret);
261 my $cgiurl=$config{openid_cgiurl};
262 $cgiurl=$q->url if ! defined $cgiurl;
264 return Net::OpenID::Consumer->new(
267 consumer_secret => sub { return shift()+$secret },
268 required_root => auto_upgrade_https($q, $cgiurl),
272 sub auto_upgrade_https {
276 $url=~s/^http:/https:/i;
281 sub load_openid_module {
282 # Give up if module is unavailable to avoid needing to depend on it.
283 eval q{use Net::OpenID::Consumer};
285 debug("unable to load Net::OpenID::Consumer, not enabling OpenID login ($@)");