Changeset 5275
- Timestamp:
- 01/22/08 16:35:40 (5 years ago)
- Location:
- lang/perl/OAuth-Lite/trunk
- Files:
-
- 6 added
- 5 modified
-
Makefile.PL (modified) (1 diff)
-
lib/OAuth/Lite.pm (modified) (1 diff)
-
lib/OAuth/Lite/AuthMethod.pm (modified) (2 diffs)
-
lib/OAuth/Lite/Consumer.pm (modified) (9 diffs)
-
lib/OAuth/Lite/ParamMethod.pm (added)
-
lib/OAuth/Lite/RealmDifinition (added)
-
lib/OAuth/Lite/RealmDifinition.pm (added)
-
lib/OAuth/Lite/RealmDifinition/Consumer.pm (added)
-
lib/OAuth/Lite/RealmDifinition/User.pm (added)
-
lib/OAuth/Lite/Server/mod_perl2.pm (modified) (11 diffs)
-
lib/OAuth/Lite/ServiceType.pm (added)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/OAuth-Lite/trunk/Makefile.PL
r4675 r5275 14 14 requires 'List::MoreUtils' => '0.21'; 15 15 requires 'MIME::Base64' => '3.07'; 16 requires 'Net::Yadis' => '1.00'; 17 requires 'HTML::RelExtor' => '0.01'; 16 18 auto_include; 17 19 WriteAll; -
lang/perl/OAuth-Lite/trunk/lib/OAuth/Lite.pm
r5043 r5275 4 4 use warnings; 5 5 6 our $VERSION = "1.0 7";6 our $VERSION = "1.08"; 7 7 our $OAUTH_DEFAULT_VERSION = "1.0"; 8 8 -
lang/perl/OAuth-Lite/trunk/lib/OAuth/Lite/AuthMethod.pm
r4759 r5275 11 11 our @EXPORT_OK = map { @$_ } values %EXPORT_TAGS; 12 12 13 use constant AUTH_HEADER => ' auth_header';14 use constant POST_BODY => ' post_body';15 use constant URL_QUERY => ' url_query';13 use constant AUTH_HEADER => 'AUTH-HEADER'; 14 use constant POST_BODY => 'POST-BODY'; 15 use constant URL_QUERY => 'URL-QUERY'; 16 16 17 17 sub validate_method { … … 25 25 =head1 NAME 26 26 27 OAuth::Lite::AuthMethod - auth method constants.27 OAuth::Lite::AuthMethod - OBSOLUTE use ParamMethod instead of this. 28 28 29 29 =head1 SYNOPSIS -
lang/perl/OAuth-Lite/trunk/lib/OAuth/Lite/Consumer.pm
r5043 r5275 26 26 use HTTP::Request; 27 27 use HTTP::Headers; 28 use Net::Yadis; 29 use HTML::RelExtor; 28 30 use UNIVERSAL::require; 29 31 use List::MoreUtils qw(any); … … 34 36 use OAuth::Lite::Util qw(:all); 35 37 use OAuth::Lite::AuthMethod qw(:all); 38 use OAuth::Lite::RealmDifinition; 36 39 37 40 =head1 NAME … … 83 86 84 87 unless ($res->is_success) { 85 if ($res-> status == 400 || $res->status== 401) {88 if ($res->code == 400 || $res->code == 401) { 86 89 my $auth_header = $res->header('WWW-Authenticate'); 87 90 if ($auth_header && $auth_header =~ /^OAuth/) { … … 128 131 HTTP method (GET or POST) when the request is for request token or access token. (optional, 'POST' is set by default) 129 132 133 =item param_method 134 135 L<OAuth::Lite::AuthMethod>'s value you can choose from AUTH_HEADER, POST_BODY and URL_QUERY (optional, AUTH_HEADER is set by default) 136 130 137 =item auth_method 131 138 132 L<OAuth::Lite::AuthMethod>'s value you can choose from AUTH_HEADER, POST_BODY and URL_QUERY (optional, AUTH_HEADER is set by default) 139 OBSOLUTE - use param method instead of this 133 140 134 141 =item realm … … 236 243 my %args_for_parent = %args; 237 244 delete $args_for_parent{$_} 238 for qw/consumer_key consumer_secret signature_method http_method auth_method realm 239 site request_token_path access_token_path authorize_path 240 callback_url/; 245 for qw/consumer_key consumer_secret signature_method http_method auth_method param_method 246 realm site request_token_path access_token_path authorize_path callback_url/; 241 247 my $self = $class->SUPER::new(%args_for_parent); 242 248 $self = bless $self, $class; … … 266 272 $self->{consumer_secret} = $args{consumer_secret} || ''; 267 273 $self->{http_method} = $args{http_method} || 'POST'; 268 $self->{ auth_method} =$args{auth_method} || AUTH_HEADER;269 unless ( OAuth::Lite::AuthMethod->validate_method( $self->{ auth_method} ) ) {274 $self->{param_method} = $args{param_method} || $args{auth_method} || AUTH_HEADER; 275 unless ( OAuth::Lite::AuthMethod->validate_method( $self->{param_method} ) ) { 270 276 Carp::croak( sprintf 271 qq/Invalid auth method "%s"./, $self->{ auth_method} );277 qq/Invalid auth method "%s"./, $self->{param_method} ); 272 278 } 273 279 $self->{signature_method} = $signature_method_class; … … 518 524 } 519 525 520 my $ auth_method = $self->{auth_method};526 my $param_method = $self->{param_method}; 521 527 if (any { $method eq $_ } @non_send_data_methods) { 522 $ auth_method = AUTH_HEADER523 unless $ auth_method eq URL_QUERY;528 $param_method = AUTH_HEADER 529 unless $param_method eq URL_QUERY; 524 530 } else { # POST or PUT 525 $ auth_method = AUTH_HEADER526 unless $ auth_method eq POST_BODY;527 } 528 529 if ($ auth_method eq URL_QUERY) {531 $param_method = AUTH_HEADER 532 unless $param_method eq POST_BODY; 533 } 534 535 if ($param_method eq URL_QUERY) { 530 536 my $query = $self->gen_auth_query($method, $url, $token, $extra); 531 537 $url = sprintf q{%s?%s}, $url, $query; 532 } elsif ($ auth_method eq POST_BODY) {538 } elsif ($param_method eq POST_BODY) { 533 539 my $query = $self->gen_auth_query($method, $url, $token, $extra); 534 540 $content = $query; … … 604 610 my ($self, %args) = @_; 605 611 $args{token} ||= $self->access_token; 606 $self->__request(%args); 612 my $res = $self->__request(%args); 613 $res; 614 } 615 616 =head2 discover 617 618 Discover realm-definition using last response. 619 620 my $difinition = $consumer->discover(); 621 unless ($difinition) { 622 die "Discovery failed"; 623 } 624 625 =cut 626 627 sub discover { 628 my ($self, $finders) = @_; 629 my $res = $self->oauth_response; 630 my $difinition; 631 if ($res->code == 400 || $res->code == 401) { 632 my $auth_header = $res->header('WWW-Authenticate'); 633 if ($auth_header && $auth_header =~ /^OAuth/) { 634 my ($realm, $params) = parse_auth_header($auth_header); 635 $difinition = $self->__discover([ 636 sub { 637 return exists $params->{xoauth_realm} 638 ? $params->{xoauth_realm} : undef; 639 }, 640 sub { 641 return $realm; 642 }, 643 sub { 644 for my $pair (split /&/, $res->content) { 645 my ($key, $value) = split /=/, $pair; 646 if ($key && $key eq 'xoauth_realm') { 647 return decode_param($value); 648 } 649 } 650 return; 651 }, 652 sub { 653 my $parser = HTML::RelExtor->new; 654 $parser->parse($res->content); 655 for my $link ($parser->links) { 656 if ($link->has_rel('auth')) { 657 my $attr = $link->attr; 658 if ( exists $attr->{type} 659 && $attr->{type} eq q{application/xrds+xml}) { 660 return $link->href; 661 } 662 } 663 } 664 return; 665 }, 666 ]); 667 } 668 } 669 $difinition; 670 } 671 672 sub __discover { 673 my ($self, $url_finders) = @_; 674 for my $finder ( @$url_finders ) { 675 my $url = $finder->() or next; 676 my $yadis; 677 eval { $yadis = Net::Yadis->discover($url); }; 678 next if $@; 679 my $difinition = OAuth::Lite::RealmDifinition->parse($yadis); 680 next unless $difinition; 681 return $difinition; 682 } 683 return; 607 684 } 608 685 … … 746 823 747 824 my $req_token = $consumer->get_request_token(...); 748 say $consumer->oauth_response-> status;825 say $consumer->oauth_response->code; 749 826 750 827 my $req_token = $consumer->get_access_token(...); 751 say $consumer->oauth_response-> status;828 say $consumer->oauth_response->code; 752 829 753 830 =head2 oauth_clear -
lang/perl/OAuth-Lite/trunk/lib/OAuth/Lite/Server/mod_perl2.pm
r4677 r5275 30 30 use constant ACCESS_TOKEN => 'ACCESS_TOKEN'; 31 31 32 __PACKAGE__->mk_accessors(qw/request realm oauth/);32 __PACKAGE__->mk_accessors(qw/request realm xrealm oauth/); 33 33 34 34 =head1 NAME … … 291 291 The realm value you set in httpd.conf by PerlSetVar. 292 292 293 =head2 xrealm 294 295 296 This value should be the url of Discovery Difinition Document. 297 See OAuth Discovery 1.0 spec. 298 299 And in authentication header, this is put in as xoauth_realm. 300 301 To use this, you have to set XRealm on httpd.conf 302 303 SetParVar XRealm "http://api.example.com/realm" 304 293 305 =head2 oauth 294 306 … … 319 331 320 332 Set proper 'WWW-Authentication' response header 333 334 =head2 is_requied_request_token 335 336 Check if current request requires request-token. 337 338 =head2 is_requied_access_token 339 340 Check if current request requires access-token. 341 342 =head2 is_requied_protected_resource 343 344 Check if current request requires protected-resource. 345 346 =head2 accepts_consumer_request 347 348 You can adopt OAuth Consumer Request 1.0. 349 350 See http://oauth.googlecode.com/svn/spec/ext/consumer_request/1.0/drafts/1/spec.html 351 352 To adopt this spec, you have to set var 'AcceptConsumerRequest' on httpd.conf 353 354 <Location /resource> 355 PerlSetVar Mode PROTECTED_RESOURCE 356 PerlSetVar AcceptConsumerRequest 1 357 PerlResponseHandler MyServiceWithOAuth 358 </Location> 359 360 Then override service method for protected resource. 361 362 sub service { 363 my ($self, $params) = @_; 364 365 my $resource_owner_id; 366 367 if (exists $params->{oauth_token}) { 368 369 my $access_token_value = $params->{oauth_token}; 370 $resource_owner_id = $self->get_user_id_of_access_token($access_token_value); 371 372 } else { 373 374 my $consumer_key = $params->{oauth_consumer_key}; 375 $resource_owner_id = $self->get_user_id_of_consumer_developer($consumer_key); 376 377 } 378 379 my @resources = MyDB::Scheme->resultset('SomeResource')->search({ 380 user_id => $resource_owner_id, 381 }); 382 383 # output resource data in the manner your api defines. 384 ... 385 386 return Apache2::Const::OK; 387 388 } 321 389 322 390 =head2 error … … 380 448 oauth => OAuth::Lite::ServerUtil->new, 381 449 realm => undef, 450 xrealm => undef, 382 451 secure => 0, 383 452 mode => PROTECTED_RESOURCE, 453 accepts_consumer_request => 0, 384 454 }, $class; 385 455 my $realm = $self->request->dir_config('Realm'); 386 456 $self->{realm} = $realm if $realm; 457 my $xrealm = $self->request->dir_config('XRealm'); 458 $self->{xrealm} = $xrealm if $xrealm; 459 my $accept = $self->request->dir_config('AcceptConsumerRequest'); 460 $self->{accepts_consumer_request} = $accept if $accept; 387 461 my $mode = $self->request->dir_config('Mode'); 462 my @valid_modes = (PROTECTED_RESOURCE, REQUEST_TOKEN, ACCESS_TOKEN); 388 463 if ($mode) { 389 if (none { $mode eq $_ } (PROTECTED_RESOURCE, REQUEST_TOKEN, ACCESS_TOKEN)) {464 if (none { $mode eq $_ } @valid_modes) { 390 465 die "Invalid mode."; 391 466 } else { … … 439 514 } 440 515 441 my $needs_to_check_token = $self->__is_required_request_token 516 my $needs_to_check_token = ( $self->is_required_request_token 517 || ( $self->is_required_protected_resource && $self->accepts_consumer_request ) ) 442 518 ? 0 443 519 : 1; … … 467 543 my $request_uri = $uri->as_string; 468 544 469 if ($self-> __is_required_request_token) {545 if ($self->is_required_request_token) { 470 546 471 547 $self->oauth->verify_signature( … … 479 555 return $self->__output_token($request_token); 480 556 481 } elsif ($self-> __is_required_access_token) {557 } elsif ($self->is_required_access_token) { 482 558 483 559 my $token_value = $params->{oauth_token}; … … 499 575 } else { 500 576 501 my $token_value = $params->{oauth_token}; 502 my $token_secret = $self->get_access_token_secret($token_value); 503 unless (defined $token_secret) { 504 return $self->errout(401, q{Invalid token}); 505 } 577 my $token_secret = ''; 578 if (exists $params->{oauth_token}) { 579 my $token_value = $params->{oauth_token}; 580 $token_secret = $self->get_access_token_secret($token_value); 581 unless (defined $token_secret) { 582 return $self->errout(401, q{Invalid token}); 583 } 584 } 506 585 507 586 $self->oauth->verify_signature( … … 510 589 url => $request_uri, 511 590 consumer_secret => $consumer_secret || '', 512 token_secret => $token_secret || '',591 token_secret => $token_secret, 513 592 ) or return $self->errout(401, q{Invalid signature}); 514 593 … … 528 607 } 529 608 530 sub __is_required_request_token {609 sub is_required_request_token { 531 610 my $self = shift; 532 611 return ($self->{mode} eq REQUEST_TOKEN) ? 1 : 0; 533 612 } 534 613 535 sub __is_required_access_token {614 sub is_required_access_token { 536 615 my $self = shift; 537 616 return ($self->{mode} eq ACCESS_TOKEN) ? 1 : 0; 617 } 618 619 sub is_required_protected_resource { 620 my $self = shift; 621 return ($self->{mode} eq PROTECTED_RESOURCE) ? 1 : 0; 622 } 623 624 sub accepts_consumer_request { 625 my $self = shift; 626 return $self->{accepts_consumer_request}; 538 627 } 539 628 … … 585 674 sub set_authenticate_header { 586 675 my $self = shift; 587 $self->request->err_headers_out->add( 'WWW-Authenticate', 588 sprintf(q{OAuth realm="%s"}, $self->realm)); 676 my $header = $self->{xrealm} 677 ? sprintf(q{OAuth realm="%s", xoauth_realm="%s"}, $self->realm, $self->xrealm) 678 : sprintf(q{OAuth realm="%s"}, $self->realm); 679 $self->request->err_headers_out->add( 'WWW-Authenticate', $header ); 589 680 } 590 681
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)