Debian Codegraph @

README

raw
1 NAME
2
3 WWW::OAuth - Portable OAuth 1.0 authentication
4
5 SYNOPSIS
6
7 use WWW::OAuth;
8
9 my $oauth = WWW::OAuth->new(
10 client_id => $client_id,
11 client_secret => $client_secret,
12 token => $token,
13 token_secret => $token_secret,
14 );
15
16 # Just retrieve authorization header
17 my $auth_header = $oauth->authorization_header($http_request, { oauth_callback => $url });
18 $http_request->header(Authorization => $auth_header);
19
20 # HTTP::Tiny
21 use HTTP::Tiny;
22 my $res = $oauth->authenticate(Basic => { method => 'GET', url => $url })
23 ->request_with(HTTP::Tiny->new);
24
25 # HTTP::Request
26 use HTTP::Request::Common;
27 use LWP::UserAgent;
28 my $res = $oauth->authenticate(GET $url)->request_with(LWP::UserAgent->new);
29
30 # Mojo::Message::Request
31 use Mojo::UserAgent;
32 my $tx = $ua->build_tx(get => $url);
33 $tx = $oauth->authenticate($tx->req)->request_with(Mojo::UserAgent->new);
34
35
36 DESCRIPTION
37
38 WWW::OAuth implements OAuth 1.0 request authentication according to RFC
39 5849 <http://tools.ietf.org/html/rfc5849> (sometimes referred to as
40 OAuth 1.0A). It does not implement the user agent requests needed for
41 the complete OAuth 1.0 authorization flow; it only prepares and signs
42 requests, leaving the rest up to your application. It can authenticate
43 requests for LWP::UserAgent, Mojo::UserAgent, HTTP::Tiny, and can be
44 extended to operate on other types of requests.
45
46 Some user agents can be configured to automatically authenticate each
47 request with a WWW::OAuth object.
48
49 # LWP::UserAgent
50 my $ua = LWP::UserAgent->new;
51 $ua->add_handler(request_prepare => sub { $oauth->authenticate($_[0]) });
52
53 # Mojo::UserAgent
54 my $ua = Mojo::UserAgent->new;
55 $ua->on(start => sub { $oauth->authenticate($_[1]->req) });
56
57 RETRIEVING ACCESS TOKENS
58
59 The process of retrieving access tokens and token secrets for
60 authorization on behalf of a user may differ among various APIs, but it
61 follows this general format (error checking is left as an exercise to
62 the reader):
63
64 use WWW::OAuth;
65 use WWW::OAuth::Util 'form_urldecode';
66 use HTTP::Tiny;
67 my $ua = HTTP::Tiny->new;
68 my $oauth = WWW::OAuth->new(
69 client_id => $client_id,
70 client_secret => $client_secret,
71 );
72
73 # Request token request
74 my $res = $oauth->authenticate({ method => 'POST', url => $request_token_url },
75 { oauth_callback => $callback_url })->request_with($ua);
76 my %res_data = @{form_urldecode $res->{content}};
77 my ($request_token, $request_secret) = @res_data{'oauth_token','oauth_token_secret'};
78
79
80 Now, the returned request token must be used to construct a URL for the
81 user to go to and authorize your application. The exact method differs
82 by API. The user will usually be redirected to the $callback_url passed
83 earlier after authorizing, with a verifier token that can be used to
84 retrieve the access token and secret.
85
86 # Access token request
87 $oauth->token($request_token);
88 $oauth->token_secret($request_secret);
89 my $res = $oauth->authenticate({ method => 'POST', url => $access_token_url },
90 { oauth_verifier => $verifier_token })->request_with($ua);
91 my %res_data = @{form_urldecode $res->{content}};
92 my ($access_token, $access_secret) = @res_data{'oauth_token','oauth_token_secret'};
93
94
95 Finally, the access token and secret can now be stored and used to
96 authorize your application on behalf of this user.
97
98 $oauth->token($access_token);
99 $oauth->token_secret($access_secret);
100
101 ATTRIBUTES
102
103 WWW::OAuth implements the following attributes.
104
105 client_id
106
107 my $client_id = $oauth->client_id;
108 $oauth = $oauth->client_id($client_id);
109
110 Client ID used to identify application (sometimes called an API key or
111 consumer key). Required for all requests.
112
113 client_secret
114
115 my $client_secret = $oauth->client_secret;
116 $oauth = $oauth->client_secret($client_secret);
117
118 Client secret used to authenticate application (sometimes called an API
119 secret or consumer secret). Required for all requests.
120
121 token
122
123 my $token = $oauth->token;
124 $oauth = $oauth->token($token);
125
126 Request or access token used to identify resource owner. Leave
127 undefined for temporary credentials requests (request token requests).
128
129 token_secret
130
131 my $token_secret = $oauth->token_secret;
132 $oauth = $oauth->token_secret($token_secret);
133
134 Request or access token secret used to authenticate on behalf of
135 resource owner. Leave undefined for temporary credentials requests
136 (request token requests).
137
138 signature_method
139
140 my $method = $oauth->signature_method;
141 $oauth = $oauth->signature_method($method);
142
143 Signature method, can be PLAINTEXT, HMAC-SHA1, RSA-SHA1, or a custom
144 signature method. For RSA-SHA1 or custom signature methods, a "signer"
145 must be provided. Defaults to HMAC-SHA1.
146
147 signer
148
149 my $signer = $oauth->signer;
150 $oauth = $oauth->signer(sub {
151 my ($base_str, $client_secret, $token_secret) = @_;
152 ...
153 return $signature;
154 });
155
156 Coderef which implements the "signature_method". A default signer is
157 provided for signature methods PLAINTEXT and HMAC-SHA1; this attribute
158 is required for other signature methods. For signature method RSA-SHA1,
159 this attribute may also be an object which has a sign method like
160 Crypt::OpenSSL::RSA.
161
162 The signer is passed the computed signature base string, the client
163 secret, and (if present) the token secret, and must return the
164 signature string.
165
166 METHODS
167
168 WWW::OAuth implements the following methods.
169
170 authenticate
171
172 $container = $oauth->authenticate($container, \%oauth_params);
173 my $container = $oauth->authenticate($http_request, \%oauth_params);
174 my $container = $oauth->authenticate(Basic => { method => 'GET', url => $url }, \%oauth_params);
175
176 Wraps the HTTP request in a container with "oauth_request" in
177 WWW::OAuth::Util, then sets the Authorization header using
178 "authorization_header" to sign the request for OAuth 1.0. An optional
179 hashref of OAuth parameters will be passed through to
180 "authorization_header". Returns the container object.
181
182 authorization_header
183
184 my $auth_header = $oauth->authorization_header($container, \%oauth_params);
185 my $auth_header = $oauth->authorization_header($http_request, \%oauth_params);
186 my $auth_header = $oauth->authorization_header(Basic => { method => 'GET', url => $url }, \%oauth_params);
187
188 Forms an OAuth 1.0 signed Authorization header for the passed request.
189 As in "authenticate", the request may be specified in any form accepted
190 by "oauth_request" in WWW::OAuth::Util. OAuth protocol parameters
191 (starting with oauth_ or the special parameter realm) may be optionally
192 specified in a hashref and will override any generated protocol
193 parameters of the same name (they should not be present in the request
194 URL or body parameters). Returns the signed header value.
195
196 HTTP REQUEST CONTAINERS
197
198 Request containers provide a unified interface for "authenticate" to
199 parse and update HTTP requests. They must perform the Role::Tiny role
200 WWW::OAuth::Request. Custom container classes can be instantiated
201 directly or via "oauth_request" in WWW::OAuth::Util.
202
203 Basic
204
205 WWW::OAuth::Request::Basic contains the request attributes directly,
206 for user agents such as HTTP::Tiny that do not use request objects.
207
208 HTTP_Request
209
210 WWW::OAuth::Request::HTTP_Request wraps a HTTP::Request object, which
211 is compatible with several user agents including LWP::UserAgent,
212 HTTP::Thin, and Net::Async::HTTP.
213
214 Mojo
215
216 WWW::OAuth::Request::Mojo wraps a Mojo::Message::Request object, which
217 is used by Mojo::UserAgent via Mojo::Transaction.
218
219 BUGS
220
221 Report any issues on the public bugtracker.
222
223 This module was developed primarily to interface with the Twitter API,
224 which is now functionally unusable, so further development of this
225 module is unlikely without another use case.
226
227 AUTHOR
228
229 Dan Book <[email protected]>
230
231 COPYRIGHT AND LICENSE
232
233 This software is Copyright (c) 2015 by Dan Book.
234
235 This is free software, licensed under:
236
237 The Artistic License 2.0 (GPL Compatible)
238
239 SEE ALSO
240
241 Net::OAuth, Mojolicious::Plugin::OAuth2
242