| 1 | package Net::OAuth; |
|---|
| 2 | use warnings; |
|---|
| 3 | use strict; |
|---|
| 4 | use UNIVERSAL::require; |
|---|
| 5 | |
|---|
| 6 | our $VERSION = '0.11'; |
|---|
| 7 | |
|---|
| 8 | sub request { |
|---|
| 9 | my $self = shift; |
|---|
| 10 | my $what = shift; |
|---|
| 11 | return $self->message($what . ' Request'); |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | sub response { |
|---|
| 15 | my $self = shift; |
|---|
| 16 | my $what = shift; |
|---|
| 17 | return $self->message($what . ' Response'); |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | sub message { |
|---|
| 21 | my $self = shift; |
|---|
| 22 | my $type = camel(shift); |
|---|
| 23 | my $class = 'Net::OAuth::' . $type; |
|---|
| 24 | $class->require; |
|---|
| 25 | return $class; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | sub camel { |
|---|
| 29 | my @words; |
|---|
| 30 | foreach (@_) { |
|---|
| 31 | while (/([A-Za-z0-9]+)/g) { |
|---|
| 32 | (my $word = $1) =~ s/authentication/auth/i; |
|---|
| 33 | push @words, $word; |
|---|
| 34 | } |
|---|
| 35 | } |
|---|
| 36 | my $name = join('', map("\u$_", @words)); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | =head1 NAME |
|---|
| 40 | |
|---|
| 41 | Net::OAuth - OAuth protocol support |
|---|
| 42 | |
|---|
| 43 | =head1 SYNOPSIS |
|---|
| 44 | |
|---|
| 45 | # Consumer sends Request Token Request |
|---|
| 46 | |
|---|
| 47 | use Net::OAuth; |
|---|
| 48 | use HTTP::Request::Common; |
|---|
| 49 | my $ua = LWP::UserAgent->new; |
|---|
| 50 | |
|---|
| 51 | my $request = Net::OAuth->request("request token")->new( |
|---|
| 52 | consumer_key => 'dpf43f3p2l4k3l03', |
|---|
| 53 | consumer_secret => 'kd94hf93k423kf44', |
|---|
| 54 | request_url => 'https://photos.example.net/request_token', |
|---|
| 55 | request_method => 'POST', |
|---|
| 56 | signature_method => 'HMAC-SHA1', |
|---|
| 57 | timestamp => '1191242090', |
|---|
| 58 | nonce => 'hsu94j3884jdopsl', |
|---|
| 59 | extra_params => { |
|---|
| 60 | apple => 'banana', |
|---|
| 61 | kiwi => 'pear', |
|---|
| 62 | } |
|---|
| 63 | ); |
|---|
| 64 | |
|---|
| 65 | $request->sign; |
|---|
| 66 | |
|---|
| 67 | my $res = $ua->request(POST $request->to_url); # Post message to the Service Provider |
|---|
| 68 | |
|---|
| 69 | if ($res->is_success) { |
|---|
| 70 | my $response = Net::OAuth->response('request token')->from_post_body($res->content); |
|---|
| 71 | print "Got Request Token ", $response->token, "\n"; |
|---|
| 72 | print "Got Request Token Secret ", $response->token_secret, "\n"; |
|---|
| 73 | } |
|---|
| 74 | else { |
|---|
| 75 | die "Something went wrong"; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | # Etc.. |
|---|
| 79 | |
|---|
| 80 | # Service Provider receives Request Token Request |
|---|
| 81 | |
|---|
| 82 | use Net::OAuth; |
|---|
| 83 | use CGI; |
|---|
| 84 | my $q = new CGI; |
|---|
| 85 | |
|---|
| 86 | my $request = Net::OAuth->request("request token")->from_hash($q->Vars, |
|---|
| 87 | request_url => 'https://photos.example.net/request_token', |
|---|
| 88 | request_method => $q->request_method, |
|---|
| 89 | consumer_secret => 'kd94hf93k423kf44', |
|---|
| 90 | ); |
|---|
| 91 | |
|---|
| 92 | if (!$request->verify) { |
|---|
| 93 | die "Signature verification failed"; |
|---|
| 94 | } |
|---|
| 95 | else { |
|---|
| 96 | # Service Provider sends Request Token Response |
|---|
| 97 | |
|---|
| 98 | my $response = Net::OAuth->response("request token")->new( |
|---|
| 99 | token => 'abcdef', |
|---|
| 100 | token_secret => '0123456', |
|---|
| 101 | ); |
|---|
| 102 | |
|---|
| 103 | print $response->to_post_body; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | # Etc.. |
|---|
| 107 | |
|---|
| 108 | =head1 ABSTRACT |
|---|
| 109 | |
|---|
| 110 | OAuth is |
|---|
| 111 | |
|---|
| 112 | "An open protocol to allow secure API authentication in a simple and standard method from desktop and web applications." |
|---|
| 113 | |
|---|
| 114 | In practical terms, OAuth is a mechanism for a Consumer to request protected resources from a Service Provider on behalf of a user. |
|---|
| 115 | |
|---|
| 116 | Please refer to the OAuth spec: L<http://oauth.net/documentation/spec> |
|---|
| 117 | |
|---|
| 118 | Net::OAuth provides: |
|---|
| 119 | |
|---|
| 120 | =over |
|---|
| 121 | |
|---|
| 122 | =item * classes that encapsulate OAuth messages (requests and responses). |
|---|
| 123 | |
|---|
| 124 | =item * message signing |
|---|
| 125 | |
|---|
| 126 | =item * message serialization and parsing. |
|---|
| 127 | |
|---|
| 128 | =back |
|---|
| 129 | |
|---|
| 130 | Net::OAuth does not provide: |
|---|
| 131 | |
|---|
| 132 | =over |
|---|
| 133 | |
|---|
| 134 | =item * Consumer or Service Provider encapsulation |
|---|
| 135 | |
|---|
| 136 | =item * token/nonce/key storage/management |
|---|
| 137 | |
|---|
| 138 | =back |
|---|
| 139 | |
|---|
| 140 | =head1 DESCRIPTION |
|---|
| 141 | |
|---|
| 142 | =head2 OAUTH MESSAGES |
|---|
| 143 | |
|---|
| 144 | An OAuth message is a set of key-value pairs. The following message types are supported: |
|---|
| 145 | |
|---|
| 146 | Requests |
|---|
| 147 | |
|---|
| 148 | =over |
|---|
| 149 | |
|---|
| 150 | =item * Request Token (Net::OAuth::RequestTokenRequest) |
|---|
| 151 | |
|---|
| 152 | =item * Access Token (Net::OAuth::AccessTokenRequest) |
|---|
| 153 | |
|---|
| 154 | =item * User Authentication (Net::OAuth::UserAuthRequest) |
|---|
| 155 | |
|---|
| 156 | =item * Protected Resource (Net::OAuth::ProtectedResourceRequest) |
|---|
| 157 | |
|---|
| 158 | =back |
|---|
| 159 | |
|---|
| 160 | Responses |
|---|
| 161 | |
|---|
| 162 | =over |
|---|
| 163 | |
|---|
| 164 | =item * Request Token (Net::OAuth::RequestTokenResponse) |
|---|
| 165 | |
|---|
| 166 | =item * Access Token (Net::OAuth:AccessTokenResponse) |
|---|
| 167 | |
|---|
| 168 | =item * User Authentication (Net::OAuth::UserAuthResponse) |
|---|
| 169 | |
|---|
| 170 | =back |
|---|
| 171 | |
|---|
| 172 | Each OAuth message type has one or more required parameters, zero or more optional parameters, and most allow arbitrary parameters. |
|---|
| 173 | |
|---|
| 174 | All OAuth requests must be signed by the Consumer. Responses from the Service Provider, however, are not signed. |
|---|
| 175 | |
|---|
| 176 | 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: |
|---|
| 177 | |
|---|
| 178 | $request = Net::OAuth->request('user authentication')->new(%params); |
|---|
| 179 | $request = Net::OAuth->request('user_auth')->new(%params); |
|---|
| 180 | $request = Net::OAuth->request('UserAuth')->new(%params); |
|---|
| 181 | $request = Net::OAuth->message('UserAuthRequest')->new(%params); |
|---|
| 182 | |
|---|
| 183 | The more verbose way is to use the class directly: |
|---|
| 184 | |
|---|
| 185 | use Net::OAuth::UserAuthRequest; |
|---|
| 186 | $request = Net::OAuth::UserAuthRequest->new(%params); |
|---|
| 187 | |
|---|
| 188 | You can also create a message by deserializing it from a Authorization header, URL, query hash, or POST body |
|---|
| 189 | |
|---|
| 190 | $request = Net::OAuth->request('protected resource')->from_authorization_header($header, %api_params); |
|---|
| 191 | $request = Net::OAuth->request('protected resource')->from_url($url, %api_params); |
|---|
| 192 | $request = Net::OAuth->request('protected resource')->from_hash($q->Vars, %api_params); # CGI |
|---|
| 193 | $request = Net::OAuth->request('protected resource')->from_hash($c->request->params, %api_params); # Catalyst |
|---|
| 194 | $response = Net::OAuth->response('request token')->from_post_body($response_content, %api_params); |
|---|
| 195 | |
|---|
| 196 | 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. |
|---|
| 197 | |
|---|
| 198 | Before sending a request, the Consumer must first sign it: |
|---|
| 199 | |
|---|
| 200 | $request->sign; |
|---|
| 201 | |
|---|
| 202 | When receiving a request, the Service Provider should first verify the signature: |
|---|
| 203 | |
|---|
| 204 | $request->verify; |
|---|
| 205 | |
|---|
| 206 | 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: |
|---|
| 207 | |
|---|
| 208 | $response->to_post_body # a application/x-www-form-urlencoded POST body |
|---|
| 209 | |
|---|
| 210 | $request->to_url # the query string of a URL |
|---|
| 211 | |
|---|
| 212 | $request->to_authorization_header # the value of an HTTP Authorization header |
|---|
| 213 | |
|---|
| 214 | $request->to_hash # a hash that could be used for some other serialization |
|---|
| 215 | |
|---|
| 216 | =head2 API PARAMETERS vs MESSAGE PARAMETERS |
|---|
| 217 | |
|---|
| 218 | 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). |
|---|
| 219 | |
|---|
| 220 | '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. |
|---|
| 221 | |
|---|
| 222 | There are various methods to inspect a message class to see what parameters are defined: |
|---|
| 223 | |
|---|
| 224 | $request->required_message_params; |
|---|
| 225 | $request->optional_message_params; |
|---|
| 226 | $request->all_message_params; |
|---|
| 227 | $request->required_api_params; |
|---|
| 228 | $request->optional_api_params; |
|---|
| 229 | $request->all_api_params; |
|---|
| 230 | $request->all_params; |
|---|
| 231 | |
|---|
| 232 | E.g. |
|---|
| 233 | |
|---|
| 234 | use Net::OAuth; |
|---|
| 235 | use Data::Dumper; |
|---|
| 236 | print Dumper(Net::OAuth->request("protected resource")->required_message_params); |
|---|
| 237 | |
|---|
| 238 | $VAR1 = [ |
|---|
| 239 | 'consumer_key', |
|---|
| 240 | 'signature_method', |
|---|
| 241 | 'timestamp', |
|---|
| 242 | 'nonce', |
|---|
| 243 | 'token' |
|---|
| 244 | ]; |
|---|
| 245 | |
|---|
| 246 | =head2 ACCESSING PARAMETERS |
|---|
| 247 | |
|---|
| 248 | All parameters can be get/set using accessor methods. E.g. |
|---|
| 249 | |
|---|
| 250 | my $consumer_key = $request->consumer_key; |
|---|
| 251 | $request->request_method('POST'); |
|---|
| 252 | |
|---|
| 253 | =head2 SIGNATURE METHODS |
|---|
| 254 | |
|---|
| 255 | The following signature methods are supported: |
|---|
| 256 | |
|---|
| 257 | =over |
|---|
| 258 | |
|---|
| 259 | =item * PLAINTEXT |
|---|
| 260 | |
|---|
| 261 | =item * HMAC-SHA1 |
|---|
| 262 | |
|---|
| 263 | =item * RSA-SHA1 |
|---|
| 264 | |
|---|
| 265 | =back |
|---|
| 266 | |
|---|
| 267 | The signature method is determined by the value of the signature_method parameter that is passed to the message constructor. |
|---|
| 268 | |
|---|
| 269 | If an unknown signature method is specified, the signing/verification will throw an exception. |
|---|
| 270 | |
|---|
| 271 | =head3 PLAINTEXT SIGNATURES |
|---|
| 272 | |
|---|
| 273 | This method is a trivial signature which adds no security. Not recommended. |
|---|
| 274 | |
|---|
| 275 | =head3 HMAC-SHA1 SIGNATURES |
|---|
| 276 | |
|---|
| 277 | This method is available if you have Digest::HMAC_SHA1 installed. This is by far the most commonly used method. |
|---|
| 278 | |
|---|
| 279 | =head3 RSA-SHA1 SIGNATURES |
|---|
| 280 | |
|---|
| 281 | 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)) |
|---|
| 282 | |
|---|
| 283 | E.g. |
|---|
| 284 | |
|---|
| 285 | Consumer: |
|---|
| 286 | |
|---|
| 287 | use Crypt::OpenSSL::RSA; |
|---|
| 288 | use File::Slurp; |
|---|
| 289 | $keystring = read_file('private_key.pem'); |
|---|
| 290 | $private_key = Crypt::OpenSSL::RSA->new_private_key($keystring); |
|---|
| 291 | $request = Net::OAuth->request('request token')->new(%params); |
|---|
| 292 | $request->sign($private_key); |
|---|
| 293 | |
|---|
| 294 | Service Provider: |
|---|
| 295 | |
|---|
| 296 | use Crypt::OpenSSL::RSA; |
|---|
| 297 | use File::Slurp; |
|---|
| 298 | $keystring = read_file('public_key.pem'); |
|---|
| 299 | $public_key = Crypt::OpenSSL::RSA->new_public_key($keystring); |
|---|
| 300 | $request = Net::OAuth->request('request token')->new(%params); |
|---|
| 301 | if (!$request->verify($public_key)) { |
|---|
| 302 | die "Signature verification failed"; |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | 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. |
|---|
| 306 | |
|---|
| 307 | =head1 DEMO |
|---|
| 308 | |
|---|
| 309 | There is a demo Consumer CGI in this package, also available online at L<http://oauth.kg23.com/> |
|---|
| 310 | |
|---|
| 311 | =head1 SEE ALSO |
|---|
| 312 | |
|---|
| 313 | L<http://oauth.net> |
|---|
| 314 | |
|---|
| 315 | =head1 AUTHOR |
|---|
| 316 | |
|---|
| 317 | Keith Grennan, C<< <kgrennan at cpan.org> >> |
|---|
| 318 | |
|---|
| 319 | =head1 COPYRIGHT & LICENSE |
|---|
| 320 | |
|---|
| 321 | Copyright 2007 Keith Grennan, all rights reserved. |
|---|
| 322 | |
|---|
| 323 | This program is free software; you can redistribute it and/or modify it |
|---|
| 324 | under the same terms as Perl itself. |
|---|
| 325 | |
|---|
| 326 | =cut |
|---|
| 327 | |
|---|
| 328 | 1; |
|---|