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 if (keys %{$IkiWiki::hooks{auth}} > 1) {
29 $real_cgi_signin=\&IkiWiki::cgi_signin;
31 inject(name => "IkiWiki::cgi_signin", call => sub ($$) {
32 openid_selector($real_cgi_signin, @_);
46 description => "url pattern of openid realm (default is cgiurl)",
52 description => "url to ikiwiki cgi to use for openid authentication (default is cgiurl)",
59 my $real_cgi_signin=shift;
63 my $openid_url=$q->param('openid_identifier');
66 if (! load_openid_module()) {
67 if ($real_cgi_signin) {
68 $real_cgi_signin->($q, $session);
71 error(sprintf(gettext("failed to load openid module: "), @_));
73 elsif (defined $q->param("action") && $q->param("action") eq "verify") {
74 validate($q, $session, $openid_url, sub {
79 my $template=IkiWiki::template("openid-selector.tmpl");
81 cgiurl => IkiWiki::cgiurl(),
82 (defined $openid_error ? (openid_error => $openid_error) : ()),
83 (defined $openid_url ? (openid_url => $openid_url) : ()),
84 ($real_cgi_signin ? (nonopenidform => $real_cgi_signin->($q, $session, 1)) : ()),
87 IkiWiki::printheader($session);
88 print IkiWiki::cgitemplate($q, "signin", $template->output);
92 sub formbuilder_setup (@) {
95 my $form=$params{form};
96 my $session=$params{session};
99 if ($form->title eq "preferences" &&
100 IkiWiki::openiduser($session->param("name"))) {
101 $form->field(name => "openid_identifier", disabled => 1,
102 label => htmllink("", "", "ikiwiki/OpenID", noimageinline => 1),
104 size => 1, force => 1,
106 comment => $session->param("name"));
110 sub validate ($$$;$) {
113 my $openid_url=shift;
114 my $errhandler=shift;
116 my $csr=getobj($q, $session);
118 my $claimed_identity = $csr->claimed_identity($openid_url);
119 if (! $claimed_identity) {
121 if (ref($errhandler) eq 'CODE') {
122 $errhandler->($csr->err);
131 # Ask for client to provide a name and email, if possible.
133 if ($claimed_identity->can("set_extension_args")) {
134 $claimed_identity->set_extension_args(
135 'http://openid.net/extensions/sreg/1.1',
137 optional => 'email,fullname,nickname',
140 $claimed_identity->set_extension_args(
141 'http://openid.net/srv/ax/1.0',
143 mode => 'fetch_request',
144 'required' => 'email,fullname,nickname,firstname',
145 'type.email' => "http://schema.openid.net/contact/email",
146 'type.fullname' => "http://axschema.org/namePerson",
147 'type.nickname' => "http://axschema.org/namePerson/friendly",
148 'type.firstname' => "http://axschema.org/namePerson/first",
153 my $cgiurl=$config{openid_cgiurl};
154 $cgiurl=$q->url if ! defined $cgiurl;
156 my $trust_root=$config{openid_realm};
157 $trust_root=$cgiurl if ! defined $trust_root;
159 my $check_url = $claimed_identity->check_url(
160 return_to => auto_upgrade_https($q, "$cgiurl?do=postsignin"),
161 trust_root => auto_upgrade_https($q, $trust_root),
164 # Redirect the user to the OpenID server, which will
165 # eventually bounce them back to auth()
166 IkiWiki::redirect($q, $check_url);
174 if (defined $q->param('openid.mode')) {
175 my $csr=getobj($q, $session);
177 if (my $setup_url = $csr->user_setup_url) {
178 IkiWiki::redirect($q, $setup_url);
180 elsif ($csr->user_cancel) {
181 IkiWiki::redirect($q, IkiWiki::baseurl(undef));
183 elsif (my $vident = $csr->verified_identity) {
184 $session->param(name => $vident->url);
187 if ($vident->can("signed_extension_fields")) {
188 @extensions=grep { defined } (
189 $vident->signed_extension_fields('http://openid.net/extensions/sreg/1.1'),
190 $vident->signed_extension_fields('http://openid.net/srv/ax/1.0'),
194 foreach my $ext (@extensions) {
195 foreach my $field (qw{value.email email}) {
196 if (exists $ext->{$field} &&
197 defined $ext->{$field} &&
198 length $ext->{$field}) {
199 $session->param(email => $ext->{$field});
200 if (! defined $nickname &&
201 $ext->{$field}=~/(.+)@.+/) {
207 foreach my $field (qw{value.nickname nickname value.fullname fullname value.firstname}) {
208 if (exists $ext->{$field} &&
209 defined $ext->{$field} &&
210 length $ext->{$field}) {
211 $nickname=$ext->{$field};
216 if (defined $nickname) {
217 $session->param(nickname =>
218 Encode::decode_utf8($nickname));
222 error("OpenID failure: ".$csr->err);
225 elsif (defined $q->param('openid_identifier')) {
226 # myopenid.com affiliate support
227 validate($q, $session, scalar $q->param('openid_identifier'));
235 eval q{use Net::INET6Glue::INET_is_INET6}; # may not be available
236 eval q{use Net::OpenID::Consumer};
240 eval q{use LWPx::ParanoidAgent};
242 $ua=LWPx::ParanoidAgent->new(agent => $config{useragent});
248 # Store the secret in the session.
249 my $secret=$session->param("openid_secret");
250 if (! defined $secret) {
252 $session->param(openid_secret => $secret);
255 my $cgiurl=$config{openid_cgiurl};
256 $cgiurl=$q->url if ! defined $cgiurl;
258 return Net::OpenID::Consumer->new(
261 consumer_secret => sub { return shift()+$secret },
262 required_root => auto_upgrade_https($q, $cgiurl),
266 sub auto_upgrade_https {
270 $url=~s/^http:/https:/i;
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 ($@)");