summaryrefslogtreecommitdiffabout
authorMichael Krelin <hacker@klever.net>2008-03-07 19:45:26 (UTC)
committer Michael Krelin <hacker@klever.net>2008-03-07 19:45:26 (UTC)
commit0d19bbaa23b7bd07ec4f5e3683ffdd8df4641bc9 (patch) (unidiff)
tree589401b7985fc8b6c9afeafe48a19569d65dcc4c
parentf56409b28ff418a5317743ad01821609881cd664 (diff)
downloadlibopkele-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>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--include/opkele/oauth/consumer.h5
-rw-r--r--lib/oauth-consumer.cc28
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,124 +1,129 @@
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
8namespace opkele { 9namespace 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,
70 const basic_fields& qf,const basic_fields& pf, 75 const basic_fields& qf,const basic_fields& pf,
71 const service_endpoint_t& sep, 76 const service_endpoint_t& sep,
72 const token_t *t=0,const string& realm=""); 77 const token_t *t=0,const string& realm="");
73 78
74 const string signature( 79 const string signature(
75 const string& method, 80 const string& method,
76 const string& url, 81 const string& url,
77 const basic_fields& fields, 82 const basic_fields& fields,
78 const token_t* rt=0); 83 const token_t* rt=0);
79 84
80 token_t acquire_token( 85 token_t acquire_token(
81 const service_endpoint_t& sep, 86 const service_endpoint_t& sep,
82 const token_t* rt=0); 87 const token_t* rt=0);
83 }; 88 };
84 89
85 class simple_provider_endpoints : public basic_provider_endpoints { 90 class simple_provider_endpoints : public basic_provider_endpoints {
86 public: 91 public:
87 service_endpoint_t sep_request_token; 92 service_endpoint_t sep_request_token;
88 service_endpoint_t sep_authorize_user; 93 service_endpoint_t sep_authorize_user;
89 service_endpoint_t sep_access_token; 94 service_endpoint_t sep_access_token;
90 service_endpoint_t sep_generic; 95 service_endpoint_t sep_generic;
91 96
92 simple_provider_endpoints( 97 simple_provider_endpoints(
93 const string& rt,const string& au,const string& at, 98 const string& rt,const string& au,const string& at,
94 const string& sm, 99 const string& sm,
95 oauth_method_t ams=oauth_post_body, 100 oauth_method_t ams=oauth_post_body,
96 oauth_method_t amr=oauth_auth_header ) 101 oauth_method_t amr=oauth_auth_header )
97 : sep_request_token(rt,sm,ams), 102 : sep_request_token(rt,sm,ams),
98 sep_authorize_user(au,sm,oauth_url_query), 103 sep_authorize_user(au,sm,oauth_url_query),
99 sep_access_token(at,sm,ams), 104 sep_access_token(at,sm,ams),
100 sep_generic("",sm,amr) { } 105 sep_generic("",sm,amr) { }
101 106
102 const service_endpoint_t& get_request_token_endpoint() const; 107 const service_endpoint_t& get_request_token_endpoint() const;
103 const service_endpoint_t& get_authorize_user_endpoint() const; 108 const service_endpoint_t& get_authorize_user_endpoint() const;
104 const service_endpoint_t& get_access_token_endpoint() const; 109 const service_endpoint_t& get_access_token_endpoint() const;
105 service_endpoint_t& get_url_endpoint(service_endpoint_t& sep, 110 service_endpoint_t& get_url_endpoint(service_endpoint_t& sep,
106 const string& url) const; 111 const string& url) const;
107 }; 112 };
108 113
109 class simple_consumer : public basic_consumer { 114 class simple_consumer : public basic_consumer {
110 public: 115 public:
111 simple_provider_endpoints peps; 116 simple_provider_endpoints peps;
112 117
113 simple_consumer(const simple_provider_endpoints& eps, 118 simple_consumer(const simple_provider_endpoints& eps,
114 const token_t& ct) 119 const token_t& ct)
115 : basic_consumer(ct), peps(eps) { } 120 : basic_consumer(ct), peps(eps) { }
116 121
117 const basic_provider_endpoints& get_endpoints() const; 122 const basic_provider_endpoints& get_endpoints() const;
118 const string allocate_nonce(time_t ts); 123 const string allocate_nonce(time_t ts);
119 }; 124 };
120 125
121 } 126 }
122} 127}
123 128
124#endif /* __OPKELE_OAUTH_CONSUMER_H */ 129#endif /* __OPKELE_OAUTH_CONSUMER_H */
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
@@ -129,112 +129,140 @@ namespace opkele {
129 if( (r=curl.easy_setopt(CURLOPT_URL,hr.url.c_str())) ) 129 if( (r=curl.easy_setopt(CURLOPT_URL,hr.url.c_str())) )
130 throw exception_curl(OPKELE_CP_ "failed to set curly urlie",r); 130 throw exception_curl(OPKELE_CP_ "failed to set curly urlie",r);
131 if( (r=curl.easy_perform()) ) 131 if( (r=curl.easy_perform()) )
132 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); 132 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
133 token_t rv; 133 token_t rv;
134 string::size_type p=0; 134 string::size_type p=0;
135 while(p!=string::npos) { 135 while(p!=string::npos) {
136 string::size_type np = curl.response.find('&',p); 136 string::size_type np = curl.response.find('&',p);
137 string part; 137 string part;
138 if(np==string::npos) { 138 if(np==string::npos) {
139 part.assign(curl.response.c_str()+p); p = string::npos; 139 part.assign(curl.response.c_str()+p); p = string::npos;
140 }else{ 140 }else{
141 part.assign(curl.response,p,np-p); p = np+1; 141 part.assign(curl.response,p,np-p); p = np+1;
142 } 142 }
143 string::size_type eq = part.find('='); 143 string::size_type eq = part.find('=');
144 if(eq==string::npos) continue; 144 if(eq==string::npos) continue;
145 string n(part,0,eq); 145 string n(part,0,eq);
146 if(n=="oauth_token") { 146 if(n=="oauth_token") {
147 if(!rv.key.empty()) /* TODO: specialize */ 147 if(!rv.key.empty()) /* TODO: specialize */
148 throw opkele::exception(OPKELE_CP_ "found oauth_token twice"); 148 throw opkele::exception(OPKELE_CP_ "found oauth_token twice");
149 rv.key = util::url_decode(part.substr(eq+1)); 149 rv.key = util::url_decode(part.substr(eq+1));
150 }else if(n=="oauth_token_secret") { 150 }else if(n=="oauth_token_secret") {
151 if(!rv.secret.empty()) /* TODO: specialize */ 151 if(!rv.secret.empty()) /* TODO: specialize */
152 throw opkele::exception(OPKELE_CP_ "found oauth_secret twice"); 152 throw opkele::exception(OPKELE_CP_ "found oauth_secret twice");
153 rv.secret = util::url_decode(part.substr(eq+1)); 153 rv.secret = util::url_decode(part.substr(eq+1));
154 } 154 }
155 } 155 }
156 return rv; 156 return rv;
157 } 157 }
158 158
159 void basic_consumer::prepare_request( 159 void basic_consumer::prepare_request(
160 http_request_t& req, 160 http_request_t& req,
161 const basic_fields& qf,const basic_fields& pf, 161 const basic_fields& qf,const basic_fields& pf,
162 oauth_method_t om,const string& sm, 162 oauth_method_t om,const string& sm,
163 const token_t *t,const string& realm) { 163 const token_t *t,const string& realm) {
164 fields_t op; 164 fields_t op;
165 op.set_field("oauth_consumer_key",consumer_token.key); 165 op.set_field("oauth_consumer_key",consumer_token.key);
166 if(t) op.set_field("oauth_token",t->key); 166 if(t) op.set_field("oauth_token",t->key);
167 op.set_field("oauth_signature_method",sm); 167 op.set_field("oauth_signature_method",sm);
168 time_t now; 168 time_t now;
169 op.set_field("oauth_timestamp", 169 op.set_field("oauth_timestamp",
170 util::long_to_string(time(&now))); 170 util::long_to_string(time(&now)));
171 op.set_field("oauth_nonce",allocate_nonce(now)); 171 op.set_field("oauth_nonce",allocate_nonce(now));
172 op.set_field("oauth_version","1.0"); 172 op.set_field("oauth_version","1.0");
173 /* TODO: normalize and strip down url */ 173 /* TODO: normalize and strip down url */
174 { 174 {
175 fields_t af; /* TODO: optimize, I don't want it to be copied */ 175 fields_t af; /* TODO: optimize, I don't want it to be copied */
176 qf.copy_to(af); pf.append_to(af); op.append_to(af); 176 qf.copy_to(af); pf.append_to(af); op.append_to(af);
177 op.set_field("oauth_signature", signature( 177 op.set_field("oauth_signature", signature(
178 req.method,req.url,af,t) ); 178 req.method,req.url,af,t) );
179 } 179 }
180 req.authorize_header.clear(); 180 req.authorize_header.clear();
181 if(om==oauth_auth_header) { 181 if(om==oauth_auth_header) {
182 req.authorize_header = "OAuth "; 182 req.authorize_header = "OAuth ";
183 req.authorize_header += "realm=\""; 183 req.authorize_header += "realm=\"";
184 req.authorize_header += util::url_encode(realm); 184 req.authorize_header += util::url_encode(realm);
185 req.authorize_header += '\"'; 185 req.authorize_header += '\"';
186 for(basic_fields::fields_iterator 186 for(basic_fields::fields_iterator
187 i=op.fields_begin(),ie=op.fields_end(); 187 i=op.fields_begin(),ie=op.fields_end();
188 i!=ie;++i) { 188 i!=ie;++i) {
189 req.authorize_header += ", "; 189 req.authorize_header += ", ";
190 req.authorize_header += *i; 190 req.authorize_header += *i;
191 req.authorize_header += "=\""; 191 req.authorize_header += "=\"";
192 req.authorize_header += util::url_encode(op.get_field(*i)); 192 req.authorize_header += util::url_encode(op.get_field(*i));
193 req.authorize_header += "\""; 193 req.authorize_header += "\"";
194 } 194 }
195 req.url = qf.append_query(req.url); 195 req.url = qf.append_query(req.url);
196 req.body = pf.query_string(); 196 req.body = pf.query_string();
197 }else if(om==oauth_post_body) { 197 }else if(om==oauth_post_body) {
198 assert(req.method=="POST"); 198 assert(req.method=="POST");
199 /* TODO: optimize, don't copy it over and over */ 199 /* TODO: optimize, don't copy it over and over */
200 fields_t p; 200 fields_t p;
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}