author | Michael Krelin <hacker@klever.net> | 2008-03-07 19:45:26 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-03-07 19:45:26 (UTC) |
commit | 0d19bbaa23b7bd07ec4f5e3683ffdd8df4641bc9 (patch) (unidiff) | |
tree | 589401b7985fc8b6c9afeafe48a19569d65dcc4c | |
parent | f56409b28ff418a5317743ad01821609881cd664 (diff) | |
download | libopkele-0d19bbaa23b7bd07ec4f5e3683ffdd8df4641bc9.zip libopkele-0d19bbaa23b7bd07ec4f5e3683ffdd8df4641bc9.tar.gz libopkele-0d19bbaa23b7bd07ec4f5e3683ffdd8df4641bc9.tar.bz2 |
added a helper to prepare curl request based on http_request_t
* added curl_slist to http_request_t structure
* added http_request_t::setup_curl(CURL*)
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | include/opkele/oauth/consumer.h | 5 | ||||
-rw-r--r-- | lib/oauth-consumer.cc | 28 |
2 files changed, 33 insertions, 0 deletions
diff --git a/include/opkele/oauth/consumer.h b/include/opkele/oauth/consumer.h index 1e2784c..9196297 100644 --- a/include/opkele/oauth/consumer.h +++ b/include/opkele/oauth/consumer.h | |||
@@ -1,69 +1,74 @@ | |||
1 | #ifndef __OPKELE_OAUTH_CONSUMER_H | 1 | #ifndef __OPKELE_OAUTH_CONSUMER_H |
2 | #define __OPKELE_OAUTH_CONSUMER_H | 2 | #define __OPKELE_OAUTH_CONSUMER_H |
3 | 3 | ||
4 | #include <string> | 4 | #include <string> |
5 | #include <opkele/types.h> | 5 | #include <opkele/types.h> |
6 | #include <opkele/oauth.h> | 6 | #include <opkele/oauth.h> |
7 | #include <opkele/curl.h> | ||
7 | 8 | ||
8 | namespace opkele { | 9 | namespace opkele { |
9 | namespace oauth { | 10 | namespace oauth { |
10 | using std::string; | 11 | using std::string; |
11 | 12 | ||
12 | enum oauth_method_t { | 13 | enum oauth_method_t { |
13 | oauth_auth_header, oauth_post_body, oauth_url_query | 14 | oauth_auth_header, oauth_post_body, oauth_url_query |
14 | }; | 15 | }; |
15 | 16 | ||
16 | struct service_endpoint_t { | 17 | struct service_endpoint_t { |
17 | string url; | 18 | string url; |
18 | string signature_method; | 19 | string signature_method; |
19 | oauth_method_t oauth_method; | 20 | oauth_method_t oauth_method; |
20 | 21 | ||
21 | service_endpoint_t(const string& u,const string& sm,oauth_method_t om) | 22 | service_endpoint_t(const string& u,const string& sm,oauth_method_t om) |
22 | : url(u), signature_method(sm), oauth_method(om) { } | 23 | : url(u), signature_method(sm), oauth_method(om) { } |
23 | }; | 24 | }; |
24 | 25 | ||
25 | class basic_provider_endpoints { | 26 | class basic_provider_endpoints { |
26 | public: | 27 | public: |
27 | 28 | ||
28 | virtual ~basic_provider_endpoints() { } | 29 | virtual ~basic_provider_endpoints() { } |
29 | 30 | ||
30 | virtual const service_endpoint_t& get_request_token_endpoint() const = 0; | 31 | virtual const service_endpoint_t& get_request_token_endpoint() const = 0; |
31 | virtual const service_endpoint_t& get_authorize_user_endpoint() const = 0; | 32 | virtual const service_endpoint_t& get_authorize_user_endpoint() const = 0; |
32 | virtual const service_endpoint_t& get_access_token_endpoint() const = 0; | 33 | virtual const service_endpoint_t& get_access_token_endpoint() const = 0; |
33 | 34 | ||
34 | virtual service_endpoint_t& get_url_endpoint(service_endpoint_t& sep, | 35 | virtual service_endpoint_t& get_url_endpoint(service_endpoint_t& sep, |
35 | const string& url) const = 0; | 36 | const string& url) const = 0; |
36 | }; | 37 | }; |
37 | 38 | ||
38 | struct http_request_t { | 39 | struct http_request_t { |
39 | string authorize_header; | 40 | string authorize_header; |
40 | string method; | 41 | string method; |
41 | string url; | 42 | string url; |
42 | string body; | 43 | string body; |
43 | 44 | ||
45 | util::curl_slist_t _curl_headers_list; | ||
46 | |||
44 | http_request_t(const string& m,const string& u) | 47 | http_request_t(const string& m,const string& u) |
45 | : method(m), url(u) { } | 48 | : method(m), url(u) { } |
49 | |||
50 | void setup_curl(CURL *curl); | ||
46 | }; | 51 | }; |
47 | 52 | ||
48 | class basic_consumer { | 53 | class basic_consumer { |
49 | public: | 54 | public: |
50 | token_t consumer_token; | 55 | token_t consumer_token; |
51 | 56 | ||
52 | basic_consumer(const token_t& ct) | 57 | basic_consumer(const token_t& ct) |
53 | : consumer_token(ct) { } | 58 | : consumer_token(ct) { } |
54 | virtual ~basic_consumer() { } | 59 | virtual ~basic_consumer() { } |
55 | 60 | ||
56 | virtual const basic_provider_endpoints& get_endpoints() const = 0; | 61 | virtual const basic_provider_endpoints& get_endpoints() const = 0; |
57 | virtual const string allocate_nonce(time_t ts) = 0; | 62 | virtual const string allocate_nonce(time_t ts) = 0; |
58 | 63 | ||
59 | token_t get_request_token(); | 64 | token_t get_request_token(); |
60 | const string get_authorize_url(const token_t& rt,const string& callback=""); | 65 | const string get_authorize_url(const token_t& rt,const string& callback=""); |
61 | token_t get_access_token(const token_t& rt); | 66 | token_t get_access_token(const token_t& rt); |
62 | 67 | ||
63 | void prepare_request( | 68 | void prepare_request( |
64 | http_request_t& req, | 69 | http_request_t& req, |
65 | const basic_fields& qf,const basic_fields& pf, | 70 | const basic_fields& qf,const basic_fields& pf, |
66 | oauth_method_t om,const string& sm, | 71 | oauth_method_t om,const string& sm, |
67 | const token_t *t=0,const string& realm=""); | 72 | const token_t *t=0,const string& realm=""); |
68 | void prepare_request( | 73 | void prepare_request( |
69 | http_request_t& req, | 74 | http_request_t& req, |
diff --git a/lib/oauth-consumer.cc b/lib/oauth-consumer.cc index d717ed3..0c4c9e3 100644 --- a/lib/oauth-consumer.cc +++ b/lib/oauth-consumer.cc | |||
@@ -201,40 +201,68 @@ namespace opkele { | |||
201 | pf.append_to(p); op.append_to(p); | 201 | pf.append_to(p); op.append_to(p); |
202 | req.url = qf.append_query(req.url); | 202 | req.url = qf.append_query(req.url); |
203 | req.body = p.query_string(); | 203 | req.body = p.query_string(); |
204 | }else if(om==oauth_url_query) { | 204 | }else if(om==oauth_url_query) { |
205 | fields_t q; | 205 | fields_t q; |
206 | qf.append_to(q); op.append_to(q); | 206 | qf.append_to(q); op.append_to(q); |
207 | req.url = q.append_query(req.url); | 207 | req.url = q.append_query(req.url); |
208 | req.body = pf.query_string(); | 208 | req.body = pf.query_string(); |
209 | }else | 209 | }else |
210 | throw opkele::exception(OPKELE_CP_ /* TODO: specialize */ | 210 | throw opkele::exception(OPKELE_CP_ /* TODO: specialize */ |
211 | "Unknown oauth method"); | 211 | "Unknown oauth method"); |
212 | } | 212 | } |
213 | 213 | ||
214 | void basic_consumer::prepare_request( | 214 | void basic_consumer::prepare_request( |
215 | http_request_t& req, | 215 | http_request_t& req, |
216 | const basic_fields& qf,const basic_fields& pf, | 216 | const basic_fields& qf,const basic_fields& pf, |
217 | const service_endpoint_t& sep, | 217 | const service_endpoint_t& sep, |
218 | const token_t *t,const string& realm) { | 218 | const token_t *t,const string& realm) { |
219 | prepare_request( | 219 | prepare_request( |
220 | req, qf, pf, | 220 | req, qf, pf, |
221 | sep.oauth_method,sep.signature_method, | 221 | sep.oauth_method,sep.signature_method, |
222 | t,realm); | 222 | t,realm); |
223 | } | 223 | } |
224 | 224 | ||
225 | void http_request_t::setup_curl(CURL *curl) { | ||
226 | CURLcode r; | ||
227 | r = curl_easy_setopt(curl,CURLOPT_URL,url.c_str()); | ||
228 | if(r) | ||
229 | throw exception_curl(OPKELE_CP_ "failed to set curly urlie",r); | ||
230 | if(method=="POST") { | ||
231 | (r = curl_easy_setopt(curl,CURLOPT_POST,1)) | ||
232 | || (r = curl_easy_setopt(curl,CURLOPT_POSTFIELDS,body.c_str())) | ||
233 | || (r = curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,body.size())); | ||
234 | }else if(method=="GET") { | ||
235 | r = curl_easy_setopt(curl,CURLOPT_HTTPGET,1); | ||
236 | }else if(method=="HEAD") { | ||
237 | r = curl_easy_setopt(curl,CURLOPT_NOBODY,1); | ||
238 | }else /* TODO: specialize exception */ | ||
239 | throw exception(OPKELE_CP_ "don't know how to handle http method"); | ||
240 | if(r) | ||
241 | throw exception_curl(OPKELE_CP_ "failed to set curly options",r); | ||
242 | if(!authorize_header.empty()) { | ||
243 | r = curl_easy_setopt(curl,CURLOPT_HTTPHEADER,(curl_slist*)( | ||
244 | _curl_headers_list = curl_slist_append( | ||
245 | 0,string("Authorization: "+authorize_header).c_str() | ||
246 | ) | ||
247 | ) ); | ||
248 | if(r) | ||
249 | throw exception_curl(OPKELE_CP_ "failed to setup curlie header"); | ||
250 | } | ||
251 | } | ||
252 | |||
225 | 253 | ||
226 | const basic_provider_endpoints& simple_consumer::get_endpoints() const { | 254 | const basic_provider_endpoints& simple_consumer::get_endpoints() const { |
227 | return peps; } | 255 | return peps; } |
228 | 256 | ||
229 | const string simple_consumer::allocate_nonce(time_t ts) { | 257 | const string simple_consumer::allocate_nonce(time_t ts) { |
230 | # ifndef HAVE_LIBUUID | 258 | # ifndef HAVE_LIBUUID |
231 | throw opkele::not_implemented(OPKELE_CP_ | 259 | throw opkele::not_implemented(OPKELE_CP_ |
232 | "not implemented consumer's allocate_nonce()"); | 260 | "not implemented consumer's allocate_nonce()"); |
233 | # else /* HAVE_LIBUUID */ | 261 | # else /* HAVE_LIBUUID */ |
234 | uuid_t uuid; uuid_generate(uuid); | 262 | uuid_t uuid; uuid_generate(uuid); |
235 | return util::encode_base64(uuid,sizeof(uuid)); | 263 | return util::encode_base64(uuid,sizeof(uuid)); |
236 | # endif /* HAVE_LIBUUID */ | 264 | # endif /* HAVE_LIBUUID */ |
237 | } | 265 | } |
238 | 266 | ||
239 | } | 267 | } |
240 | } | 268 | } |