| 1 | package Net::OAuth::Client; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use base qw(Class::Accessor::Fast); | ||
| 5 | __PACKAGE__->mk_accessors(qw/id secret callback is_v1a user_agent site debug session/); | ||
| 6 | use LWP::UserAgent; | ||
| 7 | use URI; | ||
| 8 | use Net::OAuth; | ||
| 9 | use Net::OAuth::Message; | ||
| 10 | use Net::OAuth::AccessToken; | ||
| 11 | use Carp; | ||
| 12 | use Crypt::URandom qw( urandom ); | ||
| 13 | |||
| 14 | our $VERSION = '0.31'; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Net::OAuth::Client - OAuth 1.0A Client | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | # Web Server Example (Dancer) | ||
| 23 | |||
| 24 | # This example is simplified for illustrative purposes, see the complete code in /demo | ||
| 25 | |||
| 26 | # Note that client_id is the Consumer Key and client_secret is the Consumer Secret | ||
| 27 | |||
| 28 | use Dancer; | ||
| 29 | use Net::OAuth::Client; | ||
| 30 | |||
| 31 | sub client { | ||
| 32 | Net::OAuth::Client->new( | ||
| 33 | config->{client_id}, | ||
| 34 | config->{client_secret}, | ||
| 35 | site => 'https://www.google.com/', | ||
| 36 | request_token_path => '/accounts/OAuthGetRequestToken?scope=https%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds%2F', | ||
| 37 | authorize_path => '/accounts/OAuthAuthorizeToken', | ||
| 38 | access_token_path => '/accounts/OAuthGetAccessToken', | ||
| 39 | callback => uri_for("/auth/google/callback"), | ||
| 40 | session => \&session, | ||
| 41 | ); | ||
| 42 | } | ||
| 43 | |||
| 44 | # Send user to authorize with service provider | ||
| 45 | get '/auth/google' => sub { | ||
| 46 | redirect client->authorize_url; | ||
| 47 | }; | ||
| 48 | |||
| 49 | # User has returned with token and verifier appended to the URL. | ||
| 50 | get '/auth/google/callback' => sub { | ||
| 51 | |||
| 52 | # Use the auth code to fetch the access token | ||
| 53 | my $access_token = client->get_access_token(params->{oauth_token}, params->{oauth_verifier}); | ||
| 54 | |||
| 55 | # Use the access token to fetch a protected resource | ||
| 56 | my $response = $access_token->get('/m8/feeds/contacts/default/full'); | ||
| 57 | |||
| 58 | # Do something with said resource... | ||
| 59 | |||
| 60 | if ($response->is_success) { | ||
| 61 | return "Yay, it worked: " . $response->decoded_content; | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | return "Error: " . $response->status_line; | ||
| 65 | } | ||
| 66 | }; | ||
| 67 | |||
| 68 | dance; | ||
| 69 | |||
| 70 | =head1 DESCRIPTION | ||
| 71 | |||
| 72 | Net::OAuth::Client represents an OAuth client or consumer. | ||
| 73 | |||
| 74 | WARNING: Net::OAuth::Client is alpha code. The rest of Net::OAuth is quite | ||
| 75 | stable but this particular module is new, and is under-documented and under-tested. | ||
| 76 | |||
| 77 | |||
| 78 | =head1 METHODS | ||
| 79 | |||
| 80 | =over | ||
| 81 | |||
| 82 | =item new($client_id, $client_secret, %params) | ||
| 83 | |||
| 84 | Create a new Client | ||
| 85 | |||
| 86 | =over | ||
| 87 | |||
| 88 | =item * $client_id | ||
| 89 | |||
| 90 | AKA Consumer Key - you get this from the service provider when you register your application. | ||
| 91 | |||
| 92 | =item * $client_secret | ||
| 93 | |||
| 94 | AKA Consumer Secret - you get this from the service provider when you register your application. | ||
| 95 | |||
| 96 | =item * $params{site} | ||
| 97 | |||
| 98 | =item * $params{request_token_path} | ||
| 99 | |||
| 100 | =item * $params{authorize_path} | ||
| 101 | |||
| 102 | =item * $params{access_token_path} | ||
| 103 | |||
| 104 | =item * $params{callback} | ||
| 105 | |||
| 106 | =item * $params{session} | ||
| 107 | |||
| 108 | =back | ||
| 109 | |||
| 110 | =back | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub new { | ||
| 115 | my $class = shift; | ||
| 116 | my $client_id = shift; | ||
| 117 | my $client_secret = shift; | ||
| 118 | my %opts = @_; | ||
| 119 | $opts{user_agent} ||= LWP::UserAgent->new; | ||
| 120 | $opts{id} = $client_id; | ||
| 121 | $opts{secret} = $client_secret; | ||
| 122 | $opts{is_v1a} = defined $opts{callback}; | ||
| 123 | my $self = bless \%opts, $class; | ||
| 124 | return $self; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub request { | ||
| 128 | my $self = shift; | ||
| 129 | my $response = $self->user_agent->request(@_); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub _parse_oauth_response { | ||
| 133 | my $self = shift; | ||
| 134 | my $do_what = shift; | ||
| 135 | my $http_res = shift; | ||
| 136 | my $msg = "Unable to $do_what: Request for " . $http_res->request->uri . " failed"; | ||
| 137 | unless ($http_res->is_success) { | ||
| 138 | if ($self->debug) { | ||
| 139 | $msg .= "," . $http_res->as_string . " "; | ||
| 140 | } | ||
| 141 | elsif ( | ||
| 142 | $http_res->content_type eq 'application/x-www-form-urlencoded' | ||
| 143 | and $http_res->decoded_content =~ /\boauth_problem=(\w+)/ | ||
| 144 | ) { | ||
| 145 | $msg .= ", reason: " . $1; | ||
| 146 | } | ||
| 147 | else { | ||
| 148 | $msg .= ": " . $http_res->status_line . " (pass debug=>1 to Net::OAuth::Client->new to dump the entire response)"; | ||
| 149 | } | ||
| 150 | croak $msg; | ||
| 151 | } | ||
| 152 | my $oauth_res = _parse_url_encoding($http_res->decoded_content); | ||
| 153 | foreach my $k (qw/token token_secret/) { | ||
| 154 | croak "Unable to $do_what: server response is missing '$k'" unless defined $oauth_res->{$k}; | ||
| 155 | } | ||
| 156 | return $oauth_res; | ||
| 157 | |||
| 158 | } | ||
| 159 | |||
| 160 | sub _parse_url_encoding { | ||
| 161 | my $str = shift; | ||
| 162 | my @pairs = split '&', $str; | ||
| 163 | my %params; | ||
| 164 | foreach my $pair (@pairs) { | ||
| 165 | my ($k,$v) = split /=/, $pair; | ||
| 166 | if (defined $k and defined $v) { | ||
| 167 | $v =~ s/(^"|"$)//g; | ||
| 168 | ($k,$v) = map Net::OAuth::Message::decode($_), $k, $v; | ||
| 169 | $k =~ s/^oauth_//; | ||
| 170 | $params{$k} = $v; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | return \%params; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub get_request_token { | ||
| 177 | my $self = shift; | ||
| 178 | my %params = @_; | ||
| 179 | my $oauth_req = $self->_make_request( | ||
| 180 | "request token", | ||
| 181 | request_method => $self->request_token_method, | ||
| 182 | request_url => $self->_make_url("request_token"), | ||
| 183 | %params | ||
| 184 | ); | ||
| 185 | $oauth_req->sign; | ||
| 186 | my $http_res = $self->request(HTTP::Request->new( | ||
| 187 | $self->request_token_method => $oauth_req->to_url | ||
| 188 | )); | ||
| 189 | my $oauth_res = $self->_parse_oauth_response('get a request token', $http_res); | ||
| 190 | $self->is_v1a(0) unless defined $oauth_res->{callback_confirmed}; | ||
| 191 | return $oauth_res; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub authorize_url { | ||
| 195 | my $self = shift; | ||
| 196 | my %params = @_; | ||
| 197 | # allow user to get request token their own way | ||
| 198 | unless (defined $params{token} and defined $params{token_secret}) { | ||
| 199 | my $request_token = $self->get_request_token; | ||
| 200 | $params{token} = $request_token->{token}; | ||
| 201 | $params{token_secret} = $request_token->{token_secret}; | ||
| 202 | } | ||
| 203 | if (defined $self->session) { | ||
| 204 | $self->session->($params{token} => $params{token_secret}); | ||
| 205 | } | ||
| 206 | my $oauth_req = $self->_make_request( | ||
| 207 | 'user auth', | ||
| 208 | %params | ||
| 209 | ); | ||
| 210 | return $oauth_req->to_url($self->_make_url('authorize')); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub get_access_token { | ||
| 214 | my $self = shift; | ||
| 215 | my $token = shift; | ||
| 216 | my $verifier = shift; | ||
| 217 | my %params = @_; | ||
| 218 | |||
| 219 | if (defined $self->session) { | ||
| 220 | $params{token_secret} = $self->session->($token); | ||
| 221 | } | ||
| 222 | |||
| 223 | my $oauth_req = $self->_make_request( | ||
| 224 | 'access token', | ||
| 225 | request_method => $self->access_token_method, | ||
| 226 | request_url => $self->_make_url('access_token'), | ||
| 227 | token => $token, | ||
| 228 | verifier => $verifier, | ||
| 229 | %params | ||
| 230 | ); | ||
| 231 | $oauth_req->sign; | ||
| 232 | |||
| 233 | my $http_res = $self->request(HTTP::Request->new( | ||
| 234 | $self->access_token_method => $oauth_req->to_url | ||
| 235 | )); | ||
| 236 | |||
| 237 | my $oauth_res = $self->_parse_oauth_response('get an access token', $http_res); | ||
| 238 | |||
| 239 | return Net::OAuth::AccessToken->new(%$oauth_res, client => $self); | ||
| 240 | } | ||
| 241 | |||
| 242 | sub access_token_url { | ||
| 243 | return shift->_make_url('access_token', @_); | ||
| 244 | } | ||
| 245 | |||
| 246 | sub request_token_url { | ||
| 247 | return shift->_make_url('request_token', @_); | ||
| 248 | } | ||
| 249 | |||
| 250 | sub access_token_method { | ||
| 251 | return shift->{access_token_method} || 'GET'; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub request_token_method { | ||
| 255 | return shift->{request_token_method} || 'GET'; | ||
| 256 | } | ||
| 257 | |||
| 258 | sub _make_request { | ||
| 259 | my $self = shift; | ||
| 260 | my $type = shift; | ||
| 261 | my %params = @_; | ||
| 262 | my %defaults = ( | ||
| 263 | timestamp => time, | ||
| 264 | consumer_key => $self->id, | ||
| 265 | consumer_secret => $self->secret, | ||
| 266 | callback => $self->callback, | ||
| 267 | signature_method => 'HMAC-SHA1', | ||
| 268 | request_method => 'GET', | ||
| 269 | ); | ||
| 270 | $defaults{nonce} = unpack("H*", urandom(16)) unless exists $params{nonce}; | ||
| 271 | $defaults{protocol_version} = Net::OAuth::PROTOCOL_VERSION_1_0A if $self->is_v1a; | ||
| 272 | my $req = Net::OAuth->request($type)->new( | ||
| 273 | %defaults, | ||
| 274 | %params | ||
| 275 | ); | ||
| 276 | return $req; | ||
| 277 | } | ||
| 278 | |||
| 279 | sub _make_url { | ||
| 280 | my $self = shift; | ||
| 281 | my $thing = shift; | ||
| 282 | my $path = $self->{"${thing}_url"} || $self->{"${thing}_path"} || "/oauth/${thing}"; | ||
| 283 | return $self->site_url($path, @_); | ||
| 284 | } | ||
| 285 | |||
| 286 | sub site_url { | ||
| 287 | my $self = shift; | ||
| 288 | my $path = shift; | ||
| 289 | my %params = @_; | ||
| 290 | my $url; | ||
| 291 | if (defined $self->{site}) { | ||
| 292 | $url = URI->new_abs($path, $self->{site}); | ||
| 293 | } | ||
| 294 | else { | ||
| 295 | $url = URI->new($path); | ||
| 296 | } | ||
| 297 | if (@_) { | ||
| 298 | $url->query_form($url->query_form , %params); | ||
| 299 | } | ||
| 300 | return $url; | ||
| 301 | } | ||
| 302 | |||
| 303 | =head1 AUTHOR | ||
| 304 | |||
| 305 | Originally by Keith Grennan <kgrennan@cpan.org> | ||
| 306 | |||
| 307 | Currently maintained by Robert Rothenberg <rrwo@cpan.org> | ||
| 308 | |||
| 309 | =head1 LICENSE AND COPYRIGHT | ||
| 310 | |||
| 311 | Copyright 2007-2012, 2024-2025 Keith Grennan | ||
| 312 | |||
| 313 | This program is free software; you can redistribute it and/or modify it | ||
| 314 | under the terms of either: the GNU General Public License as published | ||
| 315 | by the Free Software Foundation; or the Artistic License. | ||
| 316 | |||
| 317 | See http://dev.perl.org/licenses/ for more information. | ||
| 318 | |||
| 319 | =cut | ||
| 320 | |||
| 321 | 1; |