| 1 | # LICENSE: You're free to distribute this under the same terms as Perl itself. | ||
| 2 | |||
| 3 | 5 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 5 | ▶ | use Carp (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | ############################################################################ | ||
| 7 | package Net::OpenID::Consumer; | ||
| 8 | BEGIN { | ||
| 9 | 5 | ▶ | $Net::OpenID::Consumer::VERSION = '1.100099_002'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | } | ||
| 11 | |||
| 12 | |||
| 13 | use fields ( | ||
| 14 | 5 | 'cache', # Cache object to store HTTP responses, | |
| 15 | # associations, and nonces | ||
| 16 | 'ua', # LWP::UserAgent instance to use | ||
| 17 | 'args', # how to get at your args | ||
| 18 | 'message', # args interpreted as an IndirectMessage, if possible | ||
| 19 | 'consumer_secret', # scalar/subref | ||
| 20 | 'required_root', # the default required_root value, or undef | ||
| 21 | 'last_errcode', # last error code we got | ||
| 22 | 'last_errtext', # last error code we got | ||
| 23 | 'debug', # debug flag or codeblock | ||
| 24 | 'minimum_version', # The minimum protocol version to support | ||
| 25 | 'assoc_options', # options for establishing ID provider associations | ||
| 26 | 'nonce_options', # options for dealing with nonces | ||
| 27 | 5 | ▶ | ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 28 | |||
| 29 | 5 | ▶ | use Net::OpenID::ClaimedIdentity; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | 5 | ▶ | use Net::OpenID::VerifiedIdentity; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | 5 | ▶ | use Net::OpenID::Association; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 32 | use Net::OpenID::Yadis; | ||
| 33 | use Net::OpenID::IndirectMessage; | ||
| 34 | use Net::OpenID::URIFetch; | ||
| 35 | use Net::OpenID::Common; # To get the OpenID::util package | ||
| 36 | |||
| 37 | use MIME::Base64 (); | ||
| 38 | use Digest::SHA qw(hmac_sha1_hex); | ||
| 39 | use Time::Local; | ||
| 40 | use HTTP::Request; | ||
| 41 | use LWP::UserAgent; | ||
| 42 | use Storable; | ||
| 43 | use JSON qw(encode_json); | ||
| 44 | use URI::Escape qw(uri_escape); | ||
| 45 | use HTML::Parser; | ||
| 46 | |||
| 47 | sub new { | ||
| 48 | my Net::OpenID::Consumer $self = shift; | ||
| 49 | $self = fields::new( $self ) unless ref $self; | ||
| 50 | my %opts = @_; | ||
| 51 | |||
| 52 | $self->{ua} = delete $opts{ua}; | ||
| 53 | $self->args ( delete $opts{args} ); | ||
| 54 | $self->cache ( delete $opts{cache} ); | ||
| 55 | $self->consumer_secret ( delete $opts{consumer_secret} ); | ||
| 56 | $self->required_root ( delete $opts{required_root} ); | ||
| 57 | $self->minimum_version ( delete $opts{minimum_version} ); | ||
| 58 | $self->assoc_options ( delete $opts{assoc_options} ); | ||
| 59 | $self->nonce_options ( delete $opts{nonce_options} ); | ||
| 60 | |||
| 61 | $self->{debug} = delete $opts{debug}; | ||
| 62 | |||
| 63 | Carp::croak("Unknown options: " . join(", ", keys %opts)) if %opts; | ||
| 64 | return $self; | ||
| 65 | } | ||
| 66 | |||
| 67 | # NOTE: This method is here only to support the openid-test library. | ||
| 68 | # Don't call it from anywhere else, or you'll break when it gets | ||
| 69 | # removed. Instead, call minimum_version(2). | ||
| 70 | # FIXME: Can we just make openid-test do that and get rid of this? | ||
| 71 | sub disable_version_1 { | ||
| 72 | $_[0]->minimum_version(2); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub cache { &_getset; } | ||
| 76 | sub consumer_secret { &_getset; } | ||
| 77 | sub required_root { &_getset; } | ||
| 78 | sub assoc_options { &_hashgetset } | ||
| 79 | sub nonce_options { &_hashgetset } | ||
| 80 | |||
| 81 | sub _getset { | ||
| 82 | my Net::OpenID::Consumer $self = shift; | ||
| 83 | my $param = (caller(1))[3]; | ||
| 84 | $param =~ s/.+:://; | ||
| 85 | |||
| 86 | if (@_) { | ||
| 87 | my $val = shift; | ||
| 88 | Carp::croak("Too many parameters") if @_; | ||
| 89 | $self->{$param} = $val; | ||
| 90 | } | ||
| 91 | return $self->{$param}; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub _hashgetset { | ||
| 95 | my Net::OpenID::Consumer $self = shift; | ||
| 96 | my $param = (caller(1))[3]; | ||
| 97 | $param =~ s/.+:://; | ||
| 98 | my $check_param = "_canonicalize_$param"; | ||
| 99 | |||
| 100 | my $v; | ||
| 101 | if (scalar(@_) == 1) { | ||
| 102 | $v = shift; | ||
| 103 | unless ($v) { | ||
| 104 | $v = {}; | ||
| 105 | } | ||
| 106 | elsif (ref $v eq 'ARRAY') { | ||
| 107 | $v = {@$v}; | ||
| 108 | } | ||
| 109 | elsif (ref $v) { | ||
| 110 | # assume it's a hash and hope for the best | ||
| 111 | $v = {%$v}; | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | Carp::croak("single argument must be HASH or ARRAY reference"); | ||
| 115 | } | ||
| 116 | $self->{$param} = $self->$check_param($v); | ||
| 117 | } | ||
| 118 | elsif (@_) { | ||
| 119 | Carp::croak("odd number of parameters?") | ||
| 120 | if scalar(@_)%2; | ||
| 121 | $self->{$param} = $self->$check_param({@_}); | ||
| 122 | } | ||
| 123 | return $self->{$param}; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub minimum_version { | ||
| 127 | my Net::OpenID::Consumer $self = shift; | ||
| 128 | |||
| 129 | if (@_) { | ||
| 130 | my $minv = shift; | ||
| 131 | Carp::croak("Too many parameters") if @_; | ||
| 132 | $minv = 1 unless $minv && $minv > 1; | ||
| 133 | $self->{minimum_version} = $minv; | ||
| 134 | } | ||
| 135 | return $self->{minimum_version}; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _canonicalize_assoc_options { return $_[1]; } | ||
| 139 | |||
| 140 | sub _debug { | ||
| 141 | my Net::OpenID::Consumer $self = shift; | ||
| 142 | return unless $self->{debug}; | ||
| 143 | |||
| 144 | if (ref $self->{debug} eq "CODE") { | ||
| 145 | $self->{debug}->($_[0]); | ||
| 146 | } else { | ||
| 147 | print STDERR "[DEBUG Net::OpenID::Consumer] $_[0]\n"; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | # given something that can have GET arguments, returns a subref to get them: | ||
| 152 | # Apache | ||
| 153 | # Apache::Request | ||
| 154 | # CGI | ||
| 155 | # HASH of get args | ||
| 156 | # CODE returning get arg, given key | ||
| 157 | |||
| 158 | # ... | ||
| 159 | |||
| 160 | sub args { | ||
| 161 | my Net::OpenID::Consumer $self = shift; | ||
| 162 | |||
| 163 | if (my $what = shift) { | ||
| 164 | unless (ref $what) { | ||
| 165 | return $self->{args} ? $self->{args}->($what) : Carp::croak("No args defined"); | ||
| 166 | } | ||
| 167 | Carp::croak("Too many parameters") if @_; | ||
| 168 | |||
| 169 | # since we do not require field setters to be called in any particular order, | ||
| 170 | # we cannot pass minimum_version here as it might change later. | ||
| 171 | my $message = Net::OpenID::IndirectMessage->new($what); | ||
| 172 | $self->{message} = $message; | ||
| 173 | if ($message) { | ||
| 174 | $self->{args} = $message->getter; | ||
| 175 | |||
| 176 | # handle OpenID 2.0 'error' mode | ||
| 177 | # (may as well do this here; we may not get another chance | ||
| 178 | # since handle_server_response is not a required part of the API) | ||
| 179 | if ($message->protocol_version >= 2 && $message->mode eq 'error') { | ||
| 180 | $self->_fail('provider_error',$message->get('error')); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | $self->{args} = sub { undef }; | ||
| 185 | } | ||
| 186 | } | ||
| 187 | $self->{args}; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub message { | ||
| 191 | my Net::OpenID::Consumer $self = shift; | ||
| 192 | my $message = $self->{message}; | ||
| 193 | return undef | ||
| 194 | unless $message && | ||
| 195 | ($self->{minimum_version} <= $message->protocol_version); | ||
| 196 | |||
| 197 | if (@_) { | ||
| 198 | return $message->get($_[0]); | ||
| 199 | } | ||
| 200 | else { | ||
| 201 | return $message; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | sub _message_mode_is { | ||
| 206 | return (($_[0]->message('mode')||' ') eq $_[1]); | ||
| 207 | } | ||
| 208 | |||
| 209 | sub _message_version { | ||
| 210 | my $message = $_[0]->message; | ||
| 211 | return $message ? $message->protocol_version : 0; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub ua { | ||
| 215 | my Net::OpenID::Consumer $self = shift; | ||
| 216 | $self->{ua} = shift if @_; | ||
| 217 | Carp::croak("Too many parameters") if @_; | ||
| 218 | |||
| 219 | # make default one on first access | ||
| 220 | unless ($self->{ua}) { | ||
| 221 | my $ua = $self->{ua} = LWP::UserAgent->new; | ||
| 222 | $ua->timeout(10); | ||
| 223 | } | ||
| 224 | |||
| 225 | $self->{ua}; | ||
| 226 | } | ||
| 227 | |||
| 228 | our %Error_text = | ||
| 229 | ( | ||
| 230 | 'bad_mode' => "The openid.mode argument is not correct", | ||
| 231 | 'bogus_return_to' => "Return URL does not match required_root.", | ||
| 232 | 'bogus_url' => "URL scheme must be http: or https:", | ||
| 233 | 'empty_url' => "No URL entered.", | ||
| 234 | 'expired_association' => "Association between ID provider and relying party has expired.", | ||
| 235 | 'naive_verify_failed_network' => "Could not contact ID provider to verify response.", | ||
| 236 | 'naive_verify_failed_return' => "Direct contact invalidated ID provider response.", | ||
| 237 | 'no_head_tag' => "Could not determine ID provider; URL document has no <head>.", | ||
| 238 | 'no_identity' => "Identity is missing from ID provider response.", | ||
| 239 | 'no_identity_server' => "Could not determine ID provider from URL.", | ||
| 240 | 'no_return_to' => "Return URL is missing from ID provider response.", | ||
| 241 | 'no_sig' => "Signature is missing from ID provider response.", | ||
| 242 | 'protocol_version_incorrect' => "ID provider does not support minimum protocol version", | ||
| 243 | 'provider_error' => "ID provider-specific error", | ||
| 244 | 'signature_mismatch' => "Prior association invalidated ID provider response.", | ||
| 245 | 'time_bad_sig' => "Return_to signature is not valid.", | ||
| 246 | 'time_expired' => "Return_to signature is stale.", | ||
| 247 | 'time_in_future' => "Return_to signature is from the future.", | ||
| 248 | 'unsigned_field' => sub { "Field(s) must be signed: " . join(", ", @_) }, | ||
| 249 | 'url_fetch_err' => "Error fetching the provided URL.", | ||
| 250 | 'nonce_reused' => 'Re-used response_nonce; possible replay attempt.', | ||
| 251 | 'nonce_stale' => 'Stale response_nonce; could have been used before.', | ||
| 252 | 'nonce_format' => 'Bad timestamp format in response_nonce.', | ||
| 253 | 'nonce_future' => 'Provider clock is too far forward.', | ||
| 254 | |||
| 255 | ); | ||
| 256 | |||
| 257 | sub _fail { | ||
| 258 | my Net::OpenID::Consumer $self = shift; | ||
| 259 | my ($code, $text, @params) = @_; | ||
| 260 | |||
| 261 | # 'bad_mode' is only an error if we survive to the end of | ||
| 262 | # .mode dispatch without having figured out what to do; | ||
| 263 | # it should not overwrite other errors. | ||
| 264 | unless ($self->{last_errcode} && $code eq 'bad_mode') { | ||
| 265 | $text ||= $Error_text{$code}; | ||
| 266 | $text = $text->(@params) if ref($text) && ref($text) eq 'CODE'; | ||
| 267 | $self->{last_errcode} = $code; | ||
| 268 | $self->{last_errtext} = $text; | ||
| 269 | $self->_debug("fail($code) $text"); | ||
| 270 | } | ||
| 271 | wantarray ? () : undef; | ||
| 272 | } | ||
| 273 | |||
| 274 | sub json_err { | ||
| 275 | my Net::OpenID::Consumer $self = shift; | ||
| 276 | return encode_json({ | ||
| 277 | err_code => $self->{last_errcode}, | ||
| 278 | err_text => $self->{last_errtext}, | ||
| 279 | }); | ||
| 280 | } | ||
| 281 | |||
| 282 | sub err { | ||
| 283 | my Net::OpenID::Consumer $self = shift; | ||
| 284 | $self->{last_errcode} . ": " . $self->{last_errtext}; | ||
| 285 | } | ||
| 286 | |||
| 287 | sub errcode { | ||
| 288 | my Net::OpenID::Consumer $self = shift; | ||
| 289 | $self->{last_errcode}; | ||
| 290 | } | ||
| 291 | |||
| 292 | sub errtext { | ||
| 293 | my Net::OpenID::Consumer $self = shift; | ||
| 294 | $self->{last_errtext}; | ||
| 295 | } | ||
| 296 | |||
| 297 | # make sure you change the $prefix every time you change the $hook format | ||
| 298 | # so that when user installs a new version and the old cache server is | ||
| 299 | # still running the old cache entries won't confuse things. | ||
| 300 | sub _get_url_contents { | ||
| 301 | my Net::OpenID::Consumer $self = shift; | ||
| 302 | my ($url, $final_url_ref, $hook, $prefix) = @_; | ||
| 303 | $final_url_ref ||= do { my $dummy; \$dummy; }; | ||
| 304 | |||
| 305 | my $res = Net::OpenID::URIFetch->fetch($url, $self, $hook, $prefix); | ||
| 306 | |||
| 307 | $$final_url_ref = $res->final_uri; | ||
| 308 | |||
| 309 | return $res ? $res->content : undef; | ||
| 310 | } | ||
| 311 | |||
| 312 | |||
| 313 | # List of head elements that matter for HTTP discovery. | ||
| 314 | # Each entry defines a key+value that will appear in the | ||
| 315 | # _find_semantic_info hash if the specified element exists | ||
| 316 | # [ | ||
| 317 | # FSI_KEY -- key name | ||
| 318 | # TAG_NAME -- must be 'link' or 'meta' | ||
| 319 | # | ||
| 320 | # ELT_VALUES -- string (default = FSI_KEY) | ||
| 321 | # what join(';',values of ELT_KEYS) has to match | ||
| 322 | # in order for a given html element to provide | ||
| 323 | # the value for FSI_KEY | ||
| 324 | # | ||
| 325 | # ELT_KEYS -- list-ref of html attribute names | ||
| 326 | # default = ['rel'] for <link...> | ||
| 327 | # default = ['name'] for <meta...> | ||
| 328 | # | ||
| 329 | # FSI_VALUE -- name of html attribute where value lives | ||
| 330 | # default = 'href' for <link...> | ||
| 331 | # default = 'content' for <meta...> | ||
| 332 | # ] | ||
| 333 | # | ||
| 334 | our @HTTP_discovery_link_meta_tags = | ||
| 335 | map { | ||
| 336 | my ($fsi_key, $tag, $elt_value, $elt_keys, $fsi_value) = @{$_}; | ||
| 337 | [$fsi_key, $tag, | ||
| 338 | $elt_value || $fsi_key, | ||
| 339 | $elt_keys || [$tag eq 'link' ? 'rel' : 'name'], | ||
| 340 | $fsi_value || ($tag eq 'link' ? 'href' : 'content'), | ||
| 341 | ] | ||
| 342 | } | ||
| 343 | # OpenID providers / delegated identities | ||
| 344 | # <link rel="openid.server" | ||
| 345 | # href="http://www.livejournal.com/misc/openid.bml" /> | ||
| 346 | # <link rel="openid.delegate" | ||
| 347 | # href="whatever" /> | ||
| 348 | # | ||
| 349 | [qw(openid.server link)], # 'openid.server' => ['rel'], 'href' | ||
| 350 | [qw(openid.delegate link)], | ||
| 351 | |||
| 352 | # OpenID2 providers / local identifiers | ||
| 353 | # <link rel="openid2.provider" | ||
| 354 | # href="http://www.livejournal.com/misc/openid.bml" /> | ||
| 355 | # <link rel="openid2.local_id" href="whatever" /> | ||
| 356 | # | ||
| 357 | [qw(openid2.provider link)], | ||
| 358 | [qw(openid2.local_id link)], | ||
| 359 | |||
| 360 | # FOAF maker info | ||
| 361 | # <meta name="foaf:maker" | ||
| 362 | # content="foaf:mbox_sha1sum '4caa1d6f6203d21705a00a7aca86203e82a9cf7a'"/> | ||
| 363 | # | ||
| 364 | [qw(foaf.maker meta foaf:maker)], # == .name | ||
| 365 | |||
| 366 | # FOAF documents | ||
| 367 | # <link rel="meta" type="application/rdf+xml" title="FOAF" | ||
| 368 | # href="http://brad.livejournal.com/data/foaf" /> | ||
| 369 | # | ||
| 370 | [qw(foaf link), 'meta;foaf;application/rdf+xml' => [qw(rel title type)]], | ||
| 371 | |||
| 372 | # RSS | ||
| 373 | # <link rel="alternate" type="application/rss+xml" title="RSS" | ||
| 374 | # href="http://www.livejournal.com/~brad/data/rss" /> | ||
| 375 | # | ||
| 376 | [qw(rss link), 'alternate;application/rss+xml' => [qw(rel type)]], | ||
| 377 | |||
| 378 | # Atom | ||
| 379 | # <link rel="alternate" type="application/atom+xml" title="Atom" | ||
| 380 | # href="http://www.livejournal.com/~brad/data/rss" /> | ||
| 381 | # | ||
| 382 | [qw(atom link), 'alternate;application/atom+xml' => [qw(rel type)]], | ||
| 383 | ; | ||
| 384 | |||
| 385 | sub _document_to_semantic_info { | ||
| 386 | my $doc = shift; | ||
| 387 | my $info = {}; | ||
| 388 | |||
| 389 | my $elts = OpenID::util::html_extract_linkmetas($doc); | ||
| 390 | for (@HTTP_discovery_link_meta_tags) { | ||
| 391 | my ($key, $tag, $elt_value, $elt_keys, $vattrib) = @$_; | ||
| 392 | for my $lm (@{$elts->{$tag}}) { | ||
| 393 | $info->{$key} = $lm->{$vattrib} | ||
| 394 | if $elt_value eq join ';', map {lc($lm->{$_}||'')} @$elt_keys; | ||
| 395 | } | ||
| 396 | } | ||
| 397 | return $info; | ||
| 398 | } | ||
| 399 | |||
| 400 | sub _find_semantic_info { | ||
| 401 | my Net::OpenID::Consumer $self = shift; | ||
| 402 | my $url = shift; | ||
| 403 | my $final_url_ref = shift; | ||
| 404 | |||
| 405 | my $doc = $self->_get_url_contents($url, $final_url_ref); | ||
| 406 | my $info = _document_to_semantic_info($doc); | ||
| 407 | $self->_debug("semantic info ($url) = " . join(", ", map { $_.' => '.$info->{$_} } keys %$info)) if $self->{debug}; | ||
| 408 | |||
| 409 | return $info; | ||
| 410 | } | ||
| 411 | |||
| 412 | sub _find_openid_server { | ||
| 413 | my Net::OpenID::Consumer $self = shift; | ||
| 414 | my $url = shift; | ||
| 415 | my $final_url_ref = shift; | ||
| 416 | |||
| 417 | my $sem_info = $self->_find_semantic_info($url, $final_url_ref) or | ||
| 418 | return; | ||
| 419 | |||
| 420 | return $self->_fail("no_identity_server") unless $sem_info->{"openid.server"}; | ||
| 421 | $sem_info->{"openid.server"}; | ||
| 422 | } | ||
| 423 | |||
| 424 | sub is_server_response { | ||
| 425 | my Net::OpenID::Consumer $self = shift; | ||
| 426 | return $self->message ? 1 : 0; | ||
| 427 | } | ||
| 428 | |||
| 429 | my $_warned_about_setup_required = 0; | ||
| 430 | sub handle_server_response { | ||
| 431 | my Net::OpenID::Consumer $self = shift; | ||
| 432 | my %callbacks_in = @_; | ||
| 433 | my %callbacks = (); | ||
| 434 | |||
| 435 | foreach my $cb (qw(not_openid cancelled verified error)) { | ||
| 436 | $callbacks{$cb} = delete($callbacks_in{$cb}) || sub { Carp::croak("No ".$cb." callback") }; | ||
| 437 | } | ||
| 438 | |||
| 439 | # backwards compatibility: | ||
| 440 | # 'setup_needed' is expected as of 1.04 | ||
| 441 | # 'setup_required' is deprecated but allowed in its place, | ||
| 442 | my $found_setup_callback = 0; | ||
| 443 | foreach my $cb (qw(setup_needed setup_required)) { | ||
| 444 | $callbacks{$cb} = delete($callbacks_in{$cb}) and $found_setup_callback++; | ||
| 445 | } | ||
| 446 | Carp::croak($found_setup_callback > 1 | ||
| 447 | ? "Cannot have both setup_needed and setup_required" | ||
| 448 | : "No setup_needed callback") | ||
| 449 | unless $found_setup_callback == 1; | ||
| 450 | |||
| 451 | if (warnings::enabled('deprecated') && | ||
| 452 | $callbacks{setup_required} && | ||
| 453 | !$_warned_about_setup_required++ | ||
| 454 | ) { | ||
| 455 | warnings::warn | ||
| 456 | ("deprecated", | ||
| 457 | "'setup_required' callback is deprecated, use 'setup_needed'"); | ||
| 458 | } | ||
| 459 | |||
| 460 | Carp::croak("Unknown callbacks: ".join(',', keys %callbacks_in)) | ||
| 461 | if %callbacks_in; | ||
| 462 | |||
| 463 | unless ($self->is_server_response) { | ||
| 464 | return $callbacks{not_openid}->(); | ||
| 465 | } | ||
| 466 | |||
| 467 | if ($self->setup_needed) { | ||
| 468 | return $callbacks{setup_needed}->() | ||
| 469 | unless ($callbacks{setup_required}); | ||
| 470 | |||
| 471 | my $setup_url = $self->user_setup_url; | ||
| 472 | return $callbacks{setup_required}->($setup_url) | ||
| 473 | if $setup_url; | ||
| 474 | # otherwise FALL THROUGH to preserve prior behavior, | ||
| 475 | # Even though this is broken, old clients could have | ||
| 476 | # put a workaround into the 'error' callback to handle | ||
| 477 | # the setup_needed+(setup_url=undef) case | ||
| 478 | } | ||
| 479 | |||
| 480 | if ($self->user_cancel) { | ||
| 481 | return $callbacks{cancelled}->(); | ||
| 482 | } | ||
| 483 | elsif (my $vident = $self->verified_identity) { | ||
| 484 | return $callbacks{verified}->($vident); | ||
| 485 | } | ||
| 486 | else { | ||
| 487 | return $callbacks{error}->($self->errcode, $self->errtext); | ||
| 488 | } | ||
| 489 | |||
| 490 | } | ||
| 491 | |||
| 492 | sub _discover_acceptable_endpoints { | ||
| 493 | my Net::OpenID::Consumer $self = shift; | ||
| 494 | my $url = shift; | ||
| 495 | my %opts = @_; | ||
| 496 | |||
| 497 | # if return_early is set, we'll return as soon as we have enough | ||
| 498 | # information to determine the "primary" endpoint, and return | ||
| 499 | # that as the first (and possibly only) item in our response. | ||
| 500 | my $primary_only = delete $opts{primary_only} ? 1 : 0; | ||
| 501 | |||
| 502 | my $force_version = delete $opts{force_version}; | ||
| 503 | |||
| 504 | Carp::croak("Unknown option(s) ".join(', ', keys(%opts))) if %opts; | ||
| 505 | |||
| 506 | # trim whitespace | ||
| 507 | $url =~ s/^\s+//; | ||
| 508 | $url =~ s/\s+$//; | ||
| 509 | return $self->_fail("empty_url") unless $url; | ||
| 510 | |||
| 511 | # do basic canonicalization | ||
| 512 | $url = "http://$url" if $url && $url !~ m!^\w+://!; | ||
| 513 | return $self->_fail("bogus_url") unless $url =~ m!^https?://!i; | ||
| 514 | # add a slash, if none exists | ||
| 515 | $url .= "/" unless $url =~ m!^https?://.+/!i; | ||
| 516 | |||
| 517 | my @discovered_endpoints = (); | ||
| 518 | my $result = sub { | ||
| 519 | # We always prefer 2.0 endpoints to 1.1 ones, regardless of | ||
| 520 | # the priority chosen by the identifier. | ||
| 521 | return [ | ||
| 522 | (grep { $_->{version} == 2 } @discovered_endpoints), | ||
| 523 | (grep { $_->{version} == 1 } @discovered_endpoints), | ||
| 524 | ]; | ||
| 525 | }; | ||
| 526 | |||
| 527 | # TODO: Support XRI too? | ||
| 528 | |||
| 529 | # First we Yadis service discovery | ||
| 530 | my $yadis = Net::OpenID::Yadis->new(consumer => $self); | ||
| 531 | if ($yadis->discover($url)) { | ||
| 532 | # FIXME: Currently we don't ever do _find_semantic_info in the Yadis | ||
| 533 | # code path, so an extra redundant HTTP request is done later | ||
| 534 | # when the semantic info is accessed. | ||
| 535 | |||
| 536 | my $final_url = $yadis->identity_url; | ||
| 537 | my @services = $yadis->services( | ||
| 538 | OpenID::util::version_2_xrds_service_url(), | ||
| 539 | OpenID::util::version_2_xrds_directed_service_url(), | ||
| 540 | OpenID::util::version_1_xrds_service_url(), | ||
| 541 | ); | ||
| 542 | my $version2 = OpenID::util::version_2_xrds_service_url(); | ||
| 543 | my $version1 = OpenID::util::version_1_xrds_service_url(); | ||
| 544 | my $version2_directed = OpenID::util::version_2_xrds_directed_service_url(); | ||
| 545 | |||
| 546 | foreach my $service (@services) { | ||
| 547 | my $service_uris = $service->URI; | ||
| 548 | |||
| 549 | # Service->URI seems to return all sorts of bizarre things, so let's | ||
| 550 | # normalize it to always be an arrayref. | ||
| 551 | if (ref($service_uris) eq 'ARRAY') { | ||
| 552 | my @sorted_id_servers = sort { | ||
| 553 | my $pa = $a->{priority}; | ||
| 554 | my $pb = $b->{priority}; | ||
| 555 | defined($pb) <=> defined($pa) | ||
| 556 | || (defined($pa) ? ($pa <=> $pb) : 0) | ||
| 557 | } @$service_uris; | ||
| 558 | $service_uris = \@sorted_id_servers; | ||
| 559 | } | ||
| 560 | if (ref($service_uris) eq 'HASH') { | ||
| 561 | $service_uris = [ $service_uris->{content} ]; | ||
| 562 | } | ||
| 563 | unless (ref($service_uris)) { | ||
| 564 | $service_uris = [ $service_uris ]; | ||
| 565 | } | ||
| 566 | |||
| 567 | my $delegate = undef; | ||
| 568 | my @versions = (); | ||
| 569 | |||
| 570 | if (grep(/^${version2}$/, $service->Type)) { | ||
| 571 | # We have an OpenID 2.0 end-user identifier | ||
| 572 | $delegate = $service->extra_field("LocalID"); | ||
| 573 | push @versions, 2; | ||
| 574 | } | ||
| 575 | if (grep(/^${version1}$/, $service->Type)) { | ||
| 576 | # We have an OpenID 1.1 end-user identifier | ||
| 577 | $delegate = $service->extra_field("Delegate", "http://openid.net/xmlns/1.0"); | ||
| 578 | push @versions, 1; | ||
| 579 | } | ||
| 580 | |||
| 581 | if (@versions) { | ||
| 582 | foreach my $version (@versions) { | ||
| 583 | next if defined($force_version) && $force_version != $version; | ||
| 584 | foreach my $uri (@$service_uris) { | ||
| 585 | push @discovered_endpoints, { | ||
| 586 | uri => $uri, | ||
| 587 | version => $version, | ||
| 588 | final_url => $final_url, | ||
| 589 | delegate => $delegate, | ||
| 590 | sem_info => undef, | ||
| 591 | mechanism => "Yadis", | ||
| 592 | }; | ||
| 593 | } | ||
| 594 | } | ||
| 595 | } | ||
| 596 | |||
| 597 | if (grep(/^${version2_directed}$/, $service->Type)) { | ||
| 598 | # We have an OpenID 2.0 OP identifier (i.e. we're doing directed identity) | ||
| 599 | my $version = 2; | ||
| 600 | # In this case, the user's claimed identifier is a magic value | ||
| 601 | # and the actual identifier will be determined by the provider. | ||
| 602 | my $final_url = OpenID::util::version_2_identifier_select_url(); | ||
| 603 | my $delegate = OpenID::util::version_2_identifier_select_url(); | ||
| 604 | |||
| 605 | foreach my $uri (@$service_uris) { | ||
| 606 | push @discovered_endpoints, { | ||
| 607 | uri => $uri, | ||
| 608 | version => $version, | ||
| 609 | final_url => $final_url, | ||
| 610 | delegate => $delegate, | ||
| 611 | sem_info => undef, | ||
| 612 | mechanism => "Yadis", | ||
| 613 | }; | ||
| 614 | } | ||
| 615 | } | ||
| 616 | |||
| 617 | if ($primary_only && scalar(@discovered_endpoints)) { | ||
| 618 | # We've got at least one endpoint now, so return early | ||
| 619 | return $result->(); | ||
| 620 | } | ||
| 621 | } | ||
| 622 | } | ||
| 623 | |||
| 624 | # Now HTML-based discovery, both 2.0- and 1.1-style. | ||
| 625 | { | ||
| 626 | my $final_url = undef; | ||
| 627 | my $sem_info = $self->_find_semantic_info($url, \$final_url); | ||
| 628 | |||
| 629 | if ($sem_info) { | ||
| 630 | if ($sem_info->{"openid2.provider"}) { | ||
| 631 | unless (defined($force_version) && $force_version != 2) { | ||
| 632 | push @discovered_endpoints, { | ||
| 633 | uri => $sem_info->{"openid2.provider"}, | ||
| 634 | version => 2, | ||
| 635 | final_url => $final_url, | ||
| 636 | delegate => $sem_info->{"openid2.local_id"}, | ||
| 637 | sem_info => $sem_info, | ||
| 638 | mechanism => "HTML", | ||
| 639 | }; | ||
| 640 | } | ||
| 641 | } | ||
| 642 | if ($sem_info->{"openid.server"}) { | ||
| 643 | unless (defined($force_version) && $force_version != 1) { | ||
| 644 | push @discovered_endpoints, { | ||
| 645 | uri => $sem_info->{"openid.server"}, | ||
| 646 | version => 1, | ||
| 647 | final_url => $final_url, | ||
| 648 | delegate => $sem_info->{"openid.delegate"}, | ||
| 649 | sem_info => $sem_info, | ||
| 650 | mechanism => "HTML", | ||
| 651 | }; | ||
| 652 | } | ||
| 653 | } | ||
| 654 | } | ||
| 655 | } | ||
| 656 | |||
| 657 | return $result->(); | ||
| 658 | |||
| 659 | } | ||
| 660 | |||
| 661 | # returns Net::OpenID::ClaimedIdentity | ||
| 662 | sub claimed_identity { | ||
| 663 | my Net::OpenID::Consumer $self = shift; | ||
| 664 | my $url = shift; | ||
| 665 | Carp::croak("Too many parameters") if @_; | ||
| 666 | |||
| 667 | # trim whitespace | ||
| 668 | $url =~ s/^\s+//; | ||
| 669 | $url =~ s/\s+$//; | ||
| 670 | return $self->_fail("empty_url") unless $url; | ||
| 671 | |||
| 672 | # do basic canonicalization | ||
| 673 | $url = "http://$url" if $url && $url !~ m!^\w+://!; | ||
| 674 | return $self->_fail("bogus_url") unless $url =~ m!^https?://!i; | ||
| 675 | # add a slash, if none exists | ||
| 676 | $url .= "/" unless $url =~ m!^https?://.+/!i; | ||
| 677 | |||
| 678 | my $endpoints = $self->_discover_acceptable_endpoints($url, primary_only => 1); | ||
| 679 | |||
| 680 | if (ref($endpoints) && @$endpoints) { | ||
| 681 | foreach my $endpoint (@$endpoints) { | ||
| 682 | |||
| 683 | next unless $endpoint->{version} >= $self->minimum_version; | ||
| 684 | |||
| 685 | $self->_debug("Discovered version $endpoint->{version} endpoint at $endpoint->{uri} via $endpoint->{mechanism}"); | ||
| 686 | $self->_debug("Delegate is $endpoint->{delegate}") if $endpoint->{delegate}; | ||
| 687 | |||
| 688 | return Net::OpenID::ClaimedIdentity->new( | ||
| 689 | identity => $endpoint->{final_url}, | ||
| 690 | server => $endpoint->{uri}, | ||
| 691 | consumer => $self, | ||
| 692 | delegate => $endpoint->{delegate}, | ||
| 693 | protocol_version => $endpoint->{version}, | ||
| 694 | semantic_info => $endpoint->{sem_info}, | ||
| 695 | ); | ||
| 696 | |||
| 697 | } | ||
| 698 | |||
| 699 | # If we've fallen out here, then none of the available services are of the required version. | ||
| 700 | return $self->_fail("protocol_version_incorrect"); | ||
| 701 | |||
| 702 | } | ||
| 703 | else { | ||
| 704 | return $self->_fail("no_identity_server"); | ||
| 705 | } | ||
| 706 | |||
| 707 | } | ||
| 708 | |||
| 709 | sub user_cancel { | ||
| 710 | my Net::OpenID::Consumer $self = shift; | ||
| 711 | return $self->_message_mode_is("cancel"); | ||
| 712 | } | ||
| 713 | |||
| 714 | sub setup_needed { | ||
| 715 | my Net::OpenID::Consumer $self = shift; | ||
| 716 | if ($self->_message_version == 1) { | ||
| 717 | return $self->_message_mode_is("id_res") && $self->message("user_setup_url"); | ||
| 718 | } | ||
| 719 | else { | ||
| 720 | return $self->_message_mode_is('setup_needed'); | ||
| 721 | } | ||
| 722 | } | ||
| 723 | |||
| 724 | sub user_setup_url { | ||
| 725 | my Net::OpenID::Consumer $self = shift; | ||
| 726 | my %opts = @_; | ||
| 727 | my $post_grant = delete $opts{'post_grant'}; | ||
| 728 | Carp::croak("Unknown options: " . join(", ", keys %opts)) if %opts; | ||
| 729 | |||
| 730 | if ($self->_message_version == 1) { | ||
| 731 | return $self->_fail("bad_mode") unless $self->_message_mode_is("id_res"); | ||
| 732 | } | ||
| 733 | else { | ||
| 734 | return undef unless $self->_message_mode_is('setup_needed'); | ||
| 735 | } | ||
| 736 | my $setup_url = $self->message("user_setup_url"); | ||
| 737 | |||
| 738 | OpenID::util::push_url_arg(\$setup_url, "openid.post_grant", $post_grant) | ||
| 739 | if $setup_url && $post_grant; | ||
| 740 | |||
| 741 | return $setup_url; | ||
| 742 | } | ||
| 743 | |||
| 744 | sub verified_identity { | ||
| 745 | my Net::OpenID::Consumer $self = shift; | ||
| 746 | my %opts = @_; | ||
| 747 | |||
| 748 | my $rr = delete $opts{'required_root'} || $self->{required_root}; | ||
| 749 | Carp::croak("Unknown options: " . join(", ", keys %opts)) if %opts; | ||
| 750 | |||
| 751 | return $self->_fail("bad_mode") unless $self->_message_mode_is("id_res"); | ||
| 752 | |||
| 753 | # the asserted identity (the delegated one, if there is one, since the protocol | ||
| 754 | # knows nothing of the original URL) | ||
| 755 | my $a_ident = $self->message("identity") or return $self->_fail("no_identity"); | ||
| 756 | |||
| 757 | my $sig64 = $self->message("sig") or return $self->_fail("no_sig"); | ||
| 758 | |||
| 759 | # fix sig if the OpenID provider failed to properly escape pluses (+) in the sig | ||
| 760 | $sig64 =~ s/ /+/g; | ||
| 761 | |||
| 762 | my $returnto = $self->message("return_to") or return $self->_fail("no_return_to"); | ||
| 763 | my $signed = $self->message("signed"); | ||
| 764 | |||
| 765 | my $possible_endpoints; | ||
| 766 | my $server; | ||
| 767 | my $claimed_identity; | ||
| 768 | |||
| 769 | my $real_ident; | ||
| 770 | if ($self->_message_version == 1) { | ||
| 771 | $real_ident = $self->args("oic.identity") || $a_ident; | ||
| 772 | |||
| 773 | # In version 1, we have to assume that the primary server | ||
| 774 | # found during discovery is the one sending us this message. | ||
| 775 | $possible_endpoints = $self->_discover_acceptable_endpoints($real_ident, force_version => 1); | ||
| 776 | |||
| 777 | if ($possible_endpoints && @$possible_endpoints) { | ||
| 778 | $possible_endpoints = [ $possible_endpoints->[0] ]; | ||
| 779 | $server = $possible_endpoints->[0]{uri}; | ||
| 780 | } | ||
| 781 | else { | ||
| 782 | # We just fall out of here and bail out below for having no endpoints. | ||
| 783 | } | ||
| 784 | } | ||
| 785 | else { | ||
| 786 | $real_ident = $self->message("claimed_id") || $a_ident; | ||
| 787 | |||
| 788 | # In version 2, the OpenID provider tells us its URL. | ||
| 789 | $server = $self->message("op_endpoint"); | ||
| 790 | $possible_endpoints = $self->_discover_acceptable_endpoints($real_ident, force_version => 2); | ||
| 791 | |||
| 792 | # FIXME: It kinda sucks that the above will always do both Yadis and HTML discovery, even though | ||
| 793 | # in most cases only one will be in use. | ||
| 794 | } | ||
| 795 | |||
| 796 | $self->_debug("Server is $server"); | ||
| 797 | |||
| 798 | unless ($possible_endpoints && @$possible_endpoints) { | ||
| 799 | return $self->_fail("no_identity_server"); | ||
| 800 | } | ||
| 801 | |||
| 802 | # check that returnto is for the right host | ||
| 803 | return $self->_fail("bogus_return_to") if $rr && $returnto !~ /^\Q$rr\E/; | ||
| 804 | |||
| 805 | my $now = time(); | ||
| 806 | |||
| 807 | # check that we have not seen response_nonce before | ||
| 808 | my $response_nonce = $self->message("response_nonce"); | ||
| 809 | unless ($response_nonce) { | ||
| 810 | # 1.0/1.1 does not require nonces | ||
| 811 | return $self->_fail("nonce_missing") | ||
| 812 | if $self->_message_version == 2; | ||
| 813 | } | ||
| 814 | else { | ||
| 815 | return unless $self->_nonce_check_succeeds($now, $server, $response_nonce); | ||
| 816 | } | ||
| 817 | |||
| 818 | # check age/signature of return_to | ||
| 819 | { | ||
| 820 | my ($sig_time, $sig) = split(/\-/, $self->args("oic.time") || ""); | ||
| 821 | # complain if more than an hour since we sent them off | ||
| 822 | return $self->_fail("time_expired") if $sig_time < $now - 3600; | ||
| 823 | # also complain if the signature is from the future by more than 30 seconds, | ||
| 824 | # which compensates for potential clock drift between nodes in a web farm. | ||
| 825 | return $self->_fail("time_in_future") if $sig_time - 30 > $now; | ||
| 826 | # and check that the time isn't faked | ||
| 827 | my $c_secret = $self->_get_consumer_secret($sig_time); | ||
| 828 | my $good_sig = substr(hmac_sha1_hex($sig_time, $c_secret), 0, 20); | ||
| 829 | return $self->_fail("time_bad_sig") unless OpenID::util::timing_indep_eq($sig, $good_sig); | ||
| 830 | } | ||
| 831 | |||
| 832 | my $last_error = undef; | ||
| 833 | |||
| 834 | foreach my $endpoint (@$possible_endpoints) { | ||
| 835 | my $final_url = $endpoint->{final_url}; | ||
| 836 | my $endpoint_uri = $endpoint->{uri}; | ||
| 837 | my $delegate = $endpoint->{delegate}; | ||
| 838 | |||
| 839 | my $error = sub { | ||
| 840 | $self->_debug("$endpoint_uri not acceptable: ".$_[0]); | ||
| 841 | $last_error = $_[0]; | ||
| 842 | }; | ||
| 843 | |||
| 844 | # The endpoint_uri must match our $server (provider) | ||
| 845 | if ($endpoint_uri ne $server) { | ||
| 846 | $error->("server_not_allowed"); | ||
| 847 | next; | ||
| 848 | } | ||
| 849 | |||
| 850 | # OpenID 2.0 wants us to exclude the fragment part of the URL when doing equality checks | ||
| 851 | my $a_ident_nofragment = $a_ident; | ||
| 852 | my $real_ident_nofragment = $real_ident; | ||
| 853 | my $final_url_nofragment = $final_url; | ||
| 854 | if ($self->_message_version >= 2) { | ||
| 855 | $a_ident_nofragment =~ s/\#.*$//x; | ||
| 856 | $real_ident_nofragment =~ s/\#.*$//x; | ||
| 857 | $final_url_nofragment =~ s/\#.*$//x; | ||
| 858 | } | ||
| 859 | unless ($final_url_nofragment eq $real_ident_nofragment) { | ||
| 860 | $error->("unexpected_url_redirect"); | ||
| 861 | next; | ||
| 862 | } | ||
| 863 | |||
| 864 | # Protocol version must match | ||
| 865 | unless ($endpoint->{version} == $self->_message_version) { | ||
| 866 | $error->("protocol_version_incorrect"); | ||
| 867 | next; | ||
| 868 | } | ||
| 869 | |||
| 870 | # if openid.delegate was used, check that it was done correctly | ||
| 871 | if ($a_ident_nofragment ne $real_ident_nofragment) { | ||
| 872 | unless ($delegate eq $a_ident_nofragment) { | ||
| 873 | $error->("bogus_delegation"); | ||
| 874 | next; | ||
| 875 | } | ||
| 876 | } | ||
| 877 | |||
| 878 | # If we've got this far then we've found the right endpoint. | ||
| 879 | |||
| 880 | $claimed_identity = Net::OpenID::ClaimedIdentity->new( | ||
| 881 | identity => $endpoint->{final_url}, | ||
| 882 | server => $endpoint->{uri}, | ||
| 883 | consumer => $self, | ||
| 884 | delegate => $endpoint->{delegate}, | ||
| 885 | protocol_version => $endpoint->{version}, | ||
| 886 | semantic_info => $endpoint->{sem_info}, | ||
| 887 | ); | ||
| 888 | last; | ||
| 889 | |||
| 890 | } | ||
| 891 | |||
| 892 | unless ($claimed_identity) { | ||
| 893 | # We failed to find a good endpoint in the above loop, so | ||
| 894 | # lets bail out. | ||
| 895 | return $self->_fail($last_error); | ||
| 896 | } | ||
| 897 | |||
| 898 | my $assoc_handle = $self->message("assoc_handle"); | ||
| 899 | |||
| 900 | $self->_debug("verified_identity: assoc_handle: $assoc_handle"); | ||
| 901 | my $assoc = Net::OpenID::Association::handle_assoc($self, $server, $assoc_handle); | ||
| 902 | |||
| 903 | my %signed_fields; # key (without openid.) -> value | ||
| 904 | |||
| 905 | # Auth 2.0 requires certain keys to be signed. | ||
| 906 | if ($self->_message_version >= 2) { | ||
| 907 | my %signed_fields = map {$_ => 1} split /,/, $signed; | ||
| 908 | my %unsigned_fields; | ||
| 909 | # these fields must be signed unconditionally | ||
| 910 | foreach my $f (qw/op_endpoint return_to response_nonce assoc_handle/) { | ||
| 911 | $unsigned_fields{$f}++ if !$signed_fields{$f}; | ||
| 912 | } | ||
| 913 | # these fields must be signed if present | ||
| 914 | foreach my $f (qw/claimed_id identity/) { | ||
| 915 | next unless $self->args("openid.$f"); | ||
| 916 | $unsigned_fields{$f}++ if !$signed_fields{$f}; | ||
| 917 | } | ||
| 918 | if (%unsigned_fields) { | ||
| 919 | return $self->_fail("unsigned_field", undef, keys %unsigned_fields); | ||
| 920 | } | ||
| 921 | } | ||
| 922 | |||
| 923 | if ($assoc) { | ||
| 924 | $self->_debug("verified_identity: verifying with found association"); | ||
| 925 | |||
| 926 | return $self->_fail("expired_association") | ||
| 927 | if $assoc->expired; | ||
| 928 | |||
| 929 | # verify the token | ||
| 930 | my $token = ""; | ||
| 931 | foreach my $param (split(/,/, $signed)) { | ||
| 932 | my $val = $self->args("openid.$param"); | ||
| 933 | $token .= "$param:$val\n"; | ||
| 934 | $signed_fields{$param} = $val; | ||
| 935 | } | ||
| 936 | |||
| 937 | utf8::encode($token); | ||
| 938 | my $good_sig = $assoc->generate_signature($token); | ||
| 939 | return $self->_fail("signature_mismatch") unless OpenID::util::timing_indep_eq($sig64, $good_sig); | ||
| 940 | |||
| 941 | } else { | ||
| 942 | $self->_debug("verified_identity: verifying using HTTP (dumb mode)"); | ||
| 943 | # didn't find an association. have to do dumb consumer mode | ||
| 944 | # and check it with a POST | ||
| 945 | my %post; | ||
| 946 | my @mkeys; | ||
| 947 | if ($self->_message_version >= 2 | ||
| 948 | && (@mkeys = $self->message->all_parameters)) { | ||
| 949 | # OpenID 2.0: copy *EVERYTHING*, not just signed parameters. | ||
| 950 | # (XXX: Do we need to copy non "openid." parameters as well? | ||
| 951 | # For now, assume if provider is sending them, there is a reason) | ||
| 952 | %post = map {$_ eq 'openid.mode' ? () : ($_, $self->args($_)) } @mkeys; | ||
| 953 | } | ||
| 954 | else { | ||
| 955 | # OpenID 1.1 *OR* legacy client did not provide a proper | ||
| 956 | # enumerator; in the latter case under 2.0 we have no | ||
| 957 | # choice but to send a partial (1.1-style) | ||
| 958 | # check_authentication request and hope for the best. | ||
| 959 | |||
| 960 | %post = ( | ||
| 961 | "openid.assoc_handle" => $assoc_handle, | ||
| 962 | "openid.signed" => $signed, | ||
| 963 | "openid.sig" => $sig64, | ||
| 964 | ); | ||
| 965 | |||
| 966 | if ($self->_message_version >= 2) { | ||
| 967 | $post{'openid.ns'} = OpenID::util::VERSION_2_NAMESPACE(); | ||
| 968 | } | ||
| 969 | |||
| 970 | # and copy in all signed parameters that we don't already have into %post | ||
| 971 | foreach my $param (split(/,/, $signed)) { | ||
| 972 | next unless $param =~ /^[\w\.]+$/; | ||
| 973 | my $val = $self->args('openid.'.$param); | ||
| 974 | $signed_fields{$param} = $val; | ||
| 975 | next if $post{"openid.$param"}; | ||
| 976 | $post{"openid.$param"} = $val; | ||
| 977 | } | ||
| 978 | |||
| 979 | # if the provider told us our handle as bogus, let's ask in our | ||
| 980 | # check_authentication mode whether that's true | ||
| 981 | if (my $ih = $self->message("invalidate_handle")) { | ||
| 982 | $post{"openid.invalidate_handle"} = $ih; | ||
| 983 | } | ||
| 984 | } | ||
| 985 | $post{"openid.mode"} = "check_authentication"; | ||
| 986 | |||
| 987 | my $req = HTTP::Request->new(POST => $server); | ||
| 988 | $req->header("Content-Type" => "application/x-www-form-urlencoded"); | ||
| 989 | $req->content(join("&", map { "$_=" . uri_escape($post{$_}) } keys %post)); | ||
| 990 | |||
| 991 | my $ua = $self->ua; | ||
| 992 | my $res = $ua->request($req); | ||
| 993 | |||
| 994 | return $self->_fail("naive_verify_failed_network") | ||
| 995 | unless $res && $res->is_success; | ||
| 996 | |||
| 997 | my $content = $res->content; | ||
| 998 | my %args = OpenID::util::parse_keyvalue($content); | ||
| 999 | |||
| 1000 | # delete the handle from our cache | ||
| 1001 | if (my $ih = $args{'invalidate_handle'}) { | ||
| 1002 | Net::OpenID::Association::invalidate_handle($self, $server, $ih); | ||
| 1003 | } | ||
| 1004 | |||
| 1005 | return $self->_fail("naive_verify_failed_return") unless | ||
| 1006 | $args{'is_valid'} eq "true" || # protocol 1.1 | ||
| 1007 | $args{'lifetime'} > 0; # DEPRECATED protocol 1.0 | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | $self->_debug("verified identity! = $real_ident"); | ||
| 1011 | |||
| 1012 | # verified! | ||
| 1013 | return Net::OpenID::VerifiedIdentity->new( | ||
| 1014 | claimed_identity => $claimed_identity, | ||
| 1015 | consumer => $self, | ||
| 1016 | signed_fields => \%signed_fields, | ||
| 1017 | ); | ||
| 1018 | } | ||
| 1019 | |||
| 1020 | sub supports_consumer_secret { 1; } | ||
| 1021 | |||
| 1022 | sub _get_consumer_secret { | ||
| 1023 | my Net::OpenID::Consumer $self = shift; | ||
| 1024 | my $time = shift; | ||
| 1025 | |||
| 1026 | my $ss; | ||
| 1027 | if (ref $self->{consumer_secret} eq "CODE") { | ||
| 1028 | $ss = $self->{consumer_secret}; | ||
| 1029 | } elsif ($self->{consumer_secret}) { | ||
| 1030 | $ss = sub { return $self->{consumer_secret}; }; | ||
| 1031 | } else { | ||
| 1032 | Carp::croak("You haven't defined a consumer_secret value or subref.\n"); | ||
| 1033 | } | ||
| 1034 | |||
| 1035 | my $sec = $ss->($time); | ||
| 1036 | Carp::croak("Consumer secret too long") if length($sec) > 255; | ||
| 1037 | return $sec; | ||
| 1038 | } | ||
| 1039 | |||
| 1040 | our $nonce_default_delay = 1200; | ||
| 1041 | our $nonce_default_skew = 300; | ||
| 1042 | |||
| 1043 | sub _canonicalize_nonce_options { | ||
| 1044 | my Net::OpenID::Consumer $self = shift; | ||
| 1045 | my $o = shift; | ||
| 1046 | my ($no_check,$ignore_time,$lifetime,$window,$start,$skew,$timecop) = | ||
| 1047 | delete @{$o}{qw(no_check ignore_time lifetime window start skew timecop)}; | ||
| 1048 | Carp::croak("Unrecognized nonce_options: ".join(',',keys %$o)) | ||
| 1049 | if keys %$o; | ||
| 1050 | |||
| 1051 | return +{ no_check => 1 } | ||
| 1052 | if ($no_check); | ||
| 1053 | |||
| 1054 | return +{ window => 0, | ||
| 1055 | lifetime => ($lifetime && $lifetime > 0 ? $lifetime : 0), | ||
| 1056 | } | ||
| 1057 | if ($ignore_time); | ||
| 1058 | |||
| 1059 | $window = | ||
| 1060 | defined($lifetime) ? $lifetime : | ||
| 1061 | $nonce_default_delay + 2*(defined($skew) && $skew > $nonce_default_skew | ||
| 1062 | ? $skew : $nonce_default_skew) | ||
| 1063 | unless (defined($window)); | ||
| 1064 | |||
| 1065 | $lifetime = $window | ||
| 1066 | unless (defined($lifetime)); | ||
| 1067 | |||
| 1068 | $lifetime = 0 if $lifetime < 0; | ||
| 1069 | $window = 0 if $window < 0; | ||
| 1070 | |||
| 1071 | $skew = $window < 2*$nonce_default_skew ? $window/2 : $nonce_default_skew | ||
| 1072 | unless (defined($skew)); | ||
| 1073 | |||
| 1074 | Carp::croak("Unrecognized nonce_options: ".join(',',keys %$o)) | ||
| 1075 | if keys %$o; | ||
| 1076 | |||
| 1077 | return | ||
| 1078 | +{ | ||
| 1079 | window => $window, | ||
| 1080 | lifetime => $lifetime, | ||
| 1081 | skew => $skew, | ||
| 1082 | defined($start) ? (start => $start) : (), | ||
| 1083 | }; | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | # The contract: | ||
| 1087 | # IF the provider adheres to protocol and is properly configured | ||
| 1088 | # which, for our purposes here means | ||
| 1089 | # (1) it sends properly formatted nonces | ||
| 1090 | # that reflect provider clock time and | ||
| 1091 | # (2) provider clock is not skewed from our own by more than | ||
| 1092 | # <skew> (the maximum acceptable) | ||
| 1093 | # AND | ||
| 1094 | # we have a cache that can reliably hold onto entries | ||
| 1095 | # for at least <lifetime> seconds | ||
| 1096 | # THEN we must not accept a duplicate nonce. | ||
| 1097 | # | ||
| 1098 | # Preconditions imply that no message with this nonce will be received | ||
| 1099 | # prior to <nonce_time>-<skew> (i.e., provider clock is running | ||
| 1100 | # maximally fast and there is no transmission delay). If our cache | ||
| 1101 | # start time is prior to this and the lifetime of cache entries is | ||
| 1102 | # long enough, then we can know for certain that it's not a duplicate, | ||
| 1103 | # otherwise we do not and therefore must reject it. | ||
| 1104 | # | ||
| 1105 | # If we detect an instance where preconditions do not hold, there is | ||
| 1106 | # not much we can do: rejecting nonces in this case will not make the | ||
| 1107 | # protocol more secure. As long as the provider's clock is skewed too | ||
| 1108 | # far forward, an attacker will be able to take advantage of it. Best | ||
| 1109 | # we can do is issue warnings, which is the point of 'timecop', but if | ||
| 1110 | # there's no place to send the warnings, then it's a waste of time. | ||
| 1111 | # | ||
| 1112 | sub _nonce_check_succeeds { | ||
| 1113 | my Net::OpenID::Consumer $self = shift; | ||
| 1114 | my ($now, $uri, $nonce) = @_; | ||
| 1115 | |||
| 1116 | my $o = $self->nonce_options; | ||
| 1117 | my $cache = $self->cache; | ||
| 1118 | return 1 | ||
| 1119 | if $o->{no_check} || !$cache; | ||
| 1120 | |||
| 1121 | my $cache_key = "nonce:$uri:$nonce"; | ||
| 1122 | |||
| 1123 | return $self->_fail('nonce_reused') if ($cache->get($cache_key)); | ||
| 1124 | $cache->set($cache_key, 1, | ||
| 1125 | ($o->{lifetime} ? ($now + $o->{lifetime}) : ())); | ||
| 1126 | |||
| 1127 | return 1 | ||
| 1128 | unless $o->{window} || $o->{start}; | ||
| 1129 | |||
| 1130 | # parse RFC3336 timestamp restricted as per 10.1 | ||
| 1131 | my ($year,$mon,$day,$hour,$min,$sec) = | ||
| 1132 | $nonce =~ m/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})Z/ | ||
| 1133 | or return $self->_fail('nonce_format'); | ||
| 1134 | |||
| 1135 | # $nonce_time is a lower bound on when the nonce could have been | ||
| 1136 | # received according to our clock | ||
| 1137 | my $nonce_time = eval { timegm($sec,$min,$hour,$day,$mon-1,$year) - $o->{skew} }; | ||
| 1138 | return $self->_fail('nonce_format') if $@; | ||
| 1139 | |||
| 1140 | # nonces from the future indicate misconfigured providers | ||
| 1141 | # that we can do nothing about except give warnings | ||
| 1142 | return !$o->{timecop} || $self->_fail('nonce_future') | ||
| 1143 | if ($now < $nonce_time); | ||
| 1144 | |||
| 1145 | # the check that matters | ||
| 1146 | return $self->_fail('nonce_stale') | ||
| 1147 | if ($o->{window} && $nonce_time < $now - $o->{window}) | ||
| 1148 | || ($o->{start} && $nonce_time < $o->{start}); | ||
| 1149 | |||
| 1150 | # win | ||
| 1151 | return 1; | ||
| 1152 | } | ||
| 1153 | |||
| 1154 | |||
| 1155 | |||
| 1156 | 1; | ||
| 1157 | __END__ | ||