/usr/share/perl5/Net/OAuth.pm is in libnet-oauth-perl 0.28-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | package Net::OAuth;
use warnings;
use strict;
use Carp;
sub PROTOCOL_VERSION_1_0() {1}
sub PROTOCOL_VERSION_1_0A() {1.001}
sub OAUTH_VERSION() {'1.0'}
our $VERSION = '0.28';
our $SKIP_UTF8_DOUBLE_ENCODE_CHECK = 0;
our $PROTOCOL_VERSION = PROTOCOL_VERSION_1_0;
sub request {
my $self = shift;
my $what = shift;
return $self->message($what . ' Request');
}
sub response {
my $self = shift;
my $what = shift;
return $self->message($what . ' Response');
}
sub message {
my $self = shift;
my $base_class = ref $self || $self;
my $type = camel(shift);
my $class = $base_class . '::' . $type;
smart_require($class, 1);
return $class;
}
sub camel {
my @words;
foreach (@_) {
while (/([A-Za-z0-9]+)/g) {
(my $word = $1) =~ s/authentication/auth/i;
push @words, $word;
}
}
my $name = join('', map("\u$_", @words));
}
our %ALREADY_REQUIRED = (); # class_name => rv of ->require
sub smart_require {
my $required_class = shift;
my $croak_on_error = shift || 0;
unless (exists $ALREADY_REQUIRED{$required_class}) {
$ALREADY_REQUIRED{$required_class} = eval "require $required_class";
croak $@ if $@ and $croak_on_error;
}
return $ALREADY_REQUIRED{$required_class};
}
=head1 NAME
Net::OAuth - OAuth 1.0 for Perl
=head1 SYNOPSIS
# Web Server Example (Dancer)
# This example is simplified for illustrative purposes, see the complete code in /demo
# Note that client_id is the Consumer Key and client_secret is the Consumer Secret
use Dancer;
use Net::OAuth::Client;
sub client {
Net::OAuth::Client->new(
config->{client_id},
config->{client_secret},
site => 'https://www.google.com/',
request_token_path => '/accounts/OAuthGetRequestToken?scope=https%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds%2F',
authorize_path => '/accounts/OAuthAuthorizeToken',
access_token_path => '/accounts/OAuthGetAccessToken',
callback => uri_for("/auth/google/callback"),
session => \&session,
);
}
# Send user to authorize with service provider
get '/auth/google' => sub {
redirect client->authorize_url;
};
# User has returned with token and verifier appended to the URL.
get '/auth/google/callback' => sub {
# Use the auth code to fetch the access token
my $access_token = client->get_access_token(params->{oauth_token}, params->{oauth_verifier});
# Use the access token to fetch a protected resource
my $response = $access_token->get('/m8/feeds/contacts/default/full');
# Do something with said resource...
if ($response->is_success) {
return "Yay, it worked: " . $response->decoded_content;
}
else {
return "Error: " . $response->status_line;
}
};
dance;
=head1 IMPORTANT
Net::OAuth provides a low-level API for reading and writing OAuth messages.
You probably should start with L<Net::OAuth::Client>.
=head1 ABSTRACT
OAuth is
"An open protocol to allow secure API authentication in a simple and standard method from desktop and web applications."
In practical terms, OAuth is a mechanism for a Consumer to request protected resources from a Service Provider on behalf of a user.
Please refer to the OAuth spec: L<http://oauth.net/documentation/spec>
Net::OAuth provides:
=over
=item * classes that encapsulate OAuth messages (requests and responses).
=item * message signing
=item * message serialization and parsing.
=item * 2-legged requests (aka. tokenless requests, aka. consumer requests), see L</"CONSUMER REQUESTS">
=back
Net::OAuth does not provide:
=over
=item * Consumer or Service Provider encapsulation
=item * token/nonce/key storage/management
=back
=head1 DESCRIPTION
=head2 OAUTH MESSAGES
An OAuth message is a set of key-value pairs. The following message types are supported:
Requests
=over
=item * Request Token (Net::OAuth::RequestTokenRequest)
=item * Access Token (Net::OAuth::AccessTokenRequest)
=item * User Authentication (Net::OAuth::UserAuthRequest)
=item * Protected Resource (Net::OAuth::ProtectedResourceRequest)
=item * Consumer Request (Net::OAuth::ConsumerRequest) (2-legged / token-less request)
=back
Responses
=over
=item * Request Token (Net::OAuth::RequestTokenResponse)
=item * Access Token (Net::OAuth:AccessTokenResponse)
=item * User Authentication (Net::OAuth::UserAuthResponse)
=back
Each OAuth message type has one or more required parameters, zero or more optional parameters, and most allow arbitrary parameters.
All OAuth requests must be signed by the Consumer. Responses from the Service Provider, however, are not signed.
To create a message, the easiest way is to use the factory methods (Net::OAuth->request, Net::OAuth->response, Net::OAuth->message). The following method invocations are all equivalent:
$request = Net::OAuth->request('user authentication')->new(%params);
$request = Net::OAuth->request('user_auth')->new(%params);
$request = Net::OAuth->request('UserAuth')->new(%params);
$request = Net::OAuth->message('UserAuthRequest')->new(%params);
The more verbose way is to use the class directly:
use Net::OAuth::UserAuthRequest;
$request = Net::OAuth::UserAuthRequest->new(%params);
You can also create a message by deserializing it from a Authorization header, URL, query hash, or POST body
$request = Net::OAuth->request('protected resource')->from_authorization_header($ENV{HTTP_AUTHORIZATION}, %api_params);
$request = Net::OAuth->request('protected resource')->from_url($url, %api_params);
$request = Net::OAuth->request('protected resource')->from_hash({$q->Vars}, %api_params); # CGI
$request = Net::OAuth->request('protected resource')->from_hash($c->request->params, %api_params); # Catalyst
$response = Net::OAuth->response('request token')->from_post_body($response_content, %api_params);
Note that the deserialization methods (as opposed to new()) expect OAuth protocol parameters to be prefixed with 'oauth_', as you would expect in a valid OAuth message.
Before sending a request, the Consumer must first sign it:
$request->sign;
When receiving a request, the Service Provider should first verify the signature:
die "Signature verification failed" unless $request->verify;
When sending a message the last step is to serialize it and send it to wherever it needs to go. The following serialization methods are available:
$response->to_post_body # a application/x-www-form-urlencoded POST body
$request->to_url # the query string of a URL
$request->to_authorization_header # the value of an HTTP Authorization header
$request->to_hash # a hash that could be used for some other serialization
=head2 API PARAMETERS vs MESSAGE PARAMETERS
Net::OAuth defines 'message parameters' as parameters that are part of the transmitted OAuth message. These include any protocol parameter (prefixed with 'oauth_' in the message), and any additional message parameters (the extra_params hash).
'API parameters' are parameters required to build a message object that are not transmitted with the message, e.g. consumer_secret, token_secret, request_url, request_method.
There are various methods to inspect a message class to see what parameters are defined:
$request->required_message_params;
$request->optional_message_params;
$request->all_message_params;
$request->required_api_params;
$request->optional_api_params;
$request->all_api_params;
$request->all_params;
E.g.
use Net::OAuth;
use Data::Dumper;
print Dumper(Net::OAuth->request("protected resource")->required_message_params);
$VAR1 = [
'consumer_key',
'signature_method',
'timestamp',
'nonce',
'token'
];
=head2 ACCESSING PARAMETERS
All parameters can be get/set using accessor methods. E.g.
my $consumer_key = $request->consumer_key;
$request->request_method('POST');
=head2 THE REQUEST_URL PARAMETER
Any query parameters in the request_url are removed and added to the extra_params hash when generating the signature.
E.g. the following requests are pretty much equivalent:
my $request = Net::OAuth->request('Request Token')->new(
%params,
request_url => 'https://photos.example.net/request_token',
extra_params => {
foo => 'bar'
},
);
my $request = Net::OAuth->request('Request Token')->new(
%params,
request_url => 'https://photos.example.net/request_token?foo=bar',
);
Calling $request->request_url will still return whatever you set it to originally. If you want to get the request_url with the query parameters removed, you can do:
my $url = $request->normalized_request_url;
=head2 SIGNATURE METHODS
The following signature methods are supported:
=over
=item * PLAINTEXT
=item * HMAC-SHA1
=item * HMAC-SHA256
=item * RSA-SHA1
=back
The signature method is determined by the value of the signature_method parameter that is passed to the message constructor.
If an unknown signature method is specified, the signing/verification will throw an exception.
=head3 PLAINTEXT SIGNATURES
This method is a trivial signature which adds no security. Not recommended.
=head3 HMAC-SHA1 SIGNATURES
This method is available if you have Digest::HMAC_SHA1 installed. This is by far the most commonly used method.
=head3 HMAC-SHA256 SIGNATURES
This method is available if you have Digest::SHA installed.
=head3 RSA-SHA1 SIGNATURES
To use RSA-SHA1 signatures, pass in a Crypt::OpenSSL::RSA object (or any object that can do $o->sign($str) and/or $o->verify($str, $sig))
E.g.
Consumer:
use Crypt::OpenSSL::RSA;
use File::Slurp;
$keystring = read_file('private_key.pem');
$private_key = Crypt::OpenSSL::RSA->new_private_key($keystring);
$request = Net::OAuth->request('request token')->new(%params);
$request->sign($private_key);
Service Provider:
use Crypt::OpenSSL::RSA;
use File::Slurp;
$keystring = read_file('public_key.pem');
$public_key = Crypt::OpenSSL::RSA->new_public_key($keystring);
$request = Net::OAuth->request('request token')->new(%params);
if (!$request->verify($public_key)) {
die "Signature verification failed";
}
Note that you can pass the key in as a parameter called 'signature_key' to the message constructor, rather than passing it to the sign/verify method, if you like.
=head2 CONSUMER REQUESTS
To send a request without including a token, use a Consumer Request:
my $request = Net::OAuth->request('consumer')->new(
consumer_key => 'dpf43f3p2l4k3l03',
consumer_secret => 'kd94hf93k423kf44',
request_url => 'http://provider.example.net/profile',
request_method => 'GET',
signature_method => 'HMAC-SHA1',
timestamp => '1191242096',
nonce => 'kllo9940pd9333jh',
);
$request->sign;
See L<Net::OAuth::ConsumerRequest>
=head2 I18N
Per the OAuth spec, when making the signature Net::OAuth first encodes parameters to UTF-8. This means that any parameters you pass to Net::OAuth, if they might be outside of ASCII character set, should be run through Encode::decode() (or an equivalent PerlIO layer) first to decode them to Perl's internal character sructure.
=head2 OAUTH 1.0A
Background:
L<http://mojodna.net/2009/05/20/an-idiots-guide-to-oauth-10a.html>
L<http://oauth.googlecode.com/svn/spec/core/1.0a/drafts/3/oauth-core-1_0a.html>
Net::OAuth defaults to OAuth 1.0 spec compliance, and supports OAuth 1.0 Rev A with an optional switch:
use Net::OAuth
$Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
It is recommended that any new projects use this switch if possible, and existing projects move to supporting this switch as soon as possible. Probably the easiest way for existing projects to do this is to turn on the switch and run your test suite. The Net::OAuth constructor will throw an exception where the new protocol parameters (callback, callback_confirmed, verifier) are missing.
Internally, the Net::OAuth::Message constructor checks $Net::OAuth::PROTOCOL_VERSION and attempts to load the equivalent subclass in the Net::OAuth::V1_0A:: namespace. So if you instantiate a Net::OAuth::RequestTokenRequest object, you will end up with a Net::OAuth::V1_0A::RequestTokenRequest (a subclass of Net::OAuth::RequestTokenRequest) if the protocol version is set to PROTOCOL_VERSION_1_0A. You can also select a 1.0a subclass on a per-message basis by passing
protocol_version => Net::OAuth::PROTOCOL_VERSION_1_0A
in the API parameters hash.
If you are not sure whether the entity you are communicating with is 1.0A compliant, you can try instantiating a 1.0A message first and then fall back to 1.0 if that fails:
use Net::OAuth
$Net::OAuth::PROTOCOL_VERSION = Net::OAuth::PROTOCOL_VERSION_1_0A;
my $is_oauth_1_0 = 0;
my $response = eval{Net::OAuth->response('request token')->from_post_body($res->content)};
if ($@) {
if ($@ =~ /Missing required parameter 'callback_confirmed'/) {
# fall back to OAuth 1.0
$response = Net::OAuth->response('request token')->from_post_body(
$res->content,
protocol_version => Net::OAuth::PROTOCOL_VERSION_1_0
);
$is_oauth_1_0 = 1; # from now on treat the server as OAuth 1.0 compliant
}
else {
die $@;
}
}
At some point in the future, Net::OAuth will default to Net::OAuth::PROTOCOL_VERSION_1_0A.
=head1 DEMO
There is a demo Consumer CGI in this package, also available online at L<http://oauth.kg23.com/>
=head1 SEE ALSO
L<http://oauth.net>
Check out L<Net::OAuth::Simple> - it has a simpler API that may be more to your liking
Check out L<Net::Twitter::OAuth> for a Twitter-specific OAuth API
Check out L<WWW::Netflix::API> for a Netflix-specific OAuth API
=head1 TODO
=over
=item * Support for repeating/multivalued parameters
=item * Add convenience methods for SPs
Something like:
# direct from CGI.pm object
$request = Net::OAuth->request('Request Token')->from_cgi_query($cgi, %api_params);
# direct from Catalyst::Request object
$request = Net::OAuth->request('Request Token')->from_catalyst_request($c->req, %api_params);
# from Auth header and GET and POST params in one
local $/;
my $post_body = <STDIN>;
$request = Net::OAuth->request('Request Token')->from_auth_get_and_post(
$ENV{HTTP_AUTHORIZATION},
$ENV{QUERY_STRING},
$post_body,
%api_params
);
=back
=head1 AUTHOR
Keith Grennan, C<< <kgrennan at cpan.org> >>
=head1 COPYRIGHT & LICENSE
Copyright 2009 Keith Grennan, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1;
|