summaryrefslogtreecommitdiffabout
path: root/lib/consumer.cc
Unidiff
Diffstat (limited to 'lib/consumer.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/consumer.cc50
1 files changed, 28 insertions, 22 deletions
diff --git a/lib/consumer.cc b/lib/consumer.cc
index c155157..62bec71 100644
--- a/lib/consumer.cc
+++ b/lib/consumer.cc
@@ -15,12 +15,31 @@
15#include <pcre.h> 15#include <pcre.h>
16 16
17namespace opkele { 17namespace opkele {
18 using namespace std; 18 using namespace std;
19 using util::curl_t; 19 using util::curl_t;
20 20
21 template<int lim>
22 class curl_fetch_string_t : public curl_t {
23 public:
24 curl_fetch_string_t(CURL *c)
25 : curl_t(c) { }
26 ~curl_fetch_string_t() throw() { }
27
28 string response;
29
30 size_t write(void *p,size_t size,size_t nmemb) {
31 size_t bytes = size*nmemb;
32 size_t get = min(lim-response.length(),bytes);
33 response.append((const char *)p,get);
34 return get;
35 }
36 };
37
38 typedef curl_fetch_string_t<16384> curl_pick_t;
39
21 class pcre_matches_t { 40 class pcre_matches_t {
22 public: 41 public:
23 int *_ov; 42 int *_ov;
24 int _s; 43 int _s;
25 44
26 pcre_matches_t() : _ov(0), _s(0) { } 45 pcre_matches_t() : _ov(0), _s(0) { }
@@ -59,20 +78,12 @@ namespace opkele {
59 if(!_p) 78 if(!_p)
60 throw internal_error(OPKELE_CP_ "Trying to execute absent regexp"); 79 throw internal_error(OPKELE_CP_ "Trying to execute absent regexp");
61 return pcre_exec(_p,NULL,s.c_str(),s.length(),0,0,m._ov,m._s); 80 return pcre_exec(_p,NULL,s.c_str(),s.length(),0,0,m._ov,m._s);
62 } 81 }
63 }; 82 };
64 83
65 static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) {
66 string *str = (string*)stream;
67 size_t bytes = size*nmemb;
68 size_t get = min(16384-str->length(),bytes);
69 str->append((const char*)ptr,get);
70 return get;
71 }
72
73 assoc_t consumer_t::associate(const string& server) { 84 assoc_t consumer_t::associate(const string& server) {
74 util::dh_t dh = DH_new(); 85 util::dh_t dh = DH_new();
75 if(!dh) 86 if(!dh)
76 throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); 87 throw exception_openssl(OPKELE_CP_ "failed to DH_new()");
77 dh->p = util::dec_to_bignum(data::_default_p); 88 dh->p = util::dec_to_bignum(data::_default_p);
78 dh->g = util::dec_to_bignum(data::_default_g); 89 dh->g = util::dec_to_bignum(data::_default_g);
@@ -81,30 +92,28 @@ namespace opkele {
81 string request = 92 string request =
82 "openid.mode=associate" 93 "openid.mode=associate"
83 "&openid.assoc_type=HMAC-SHA1" 94 "&openid.assoc_type=HMAC-SHA1"
84 "&openid.session_type=DH-SHA1" 95 "&openid.session_type=DH-SHA1"
85 "&openid.dh_consumer_public="; 96 "&openid.dh_consumer_public=";
86 request += util::url_encode(util::bignum_to_base64(dh->pub_key)); 97 request += util::url_encode(util::bignum_to_base64(dh->pub_key));
87 curl_t curl = curl_t::easy_init(); 98 curl_pick_t curl = curl_pick_t::easy_init();
88 if(!curl) 99 if(!curl)
89 throw exception_curl(OPKELE_CP_ "failed to initialize curl"); 100 throw exception_curl(OPKELE_CP_ "failed to initialize curl");
90 string response;
91 CURLcode r; 101 CURLcode r;
92 (r=curl.misc_sets()) 102 (r=curl.misc_sets())
93 || (r=curl.easy_setopt(CURLOPT_URL,server.c_str())) 103 || (r=curl.easy_setopt(CURLOPT_URL,server.c_str()))
94 || (r=curl.easy_setopt(CURLOPT_POST,1)) 104 || (r=curl.easy_setopt(CURLOPT_POST,1))
95 || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data())) 105 || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data()))
96 || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length())) 106 || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length()))
97 || (r=curl.easy_setopt(CURLOPT_WRITEFUNCTION,_curl_tostring)) 107 || (r=curl.set_write())
98 || (r=curl.easy_setopt(CURLOPT_WRITEDATA,&response))
99 ; 108 ;
100 if(r) 109 if(r)
101 throw exception_curl(OPKELE_CP_ "failed to set curly options",r); 110 throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
102 if( (r=curl.easy_perform()) ) 111 if( (r=curl.easy_perform()) )
103 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); 112 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
104 params_t p; p.parse_keyvalues(response); 113 params_t p; p.parse_keyvalues(curl.response);
105 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1") 114 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1")
106 throw bad_input(OPKELE_CP_ "unsupported assoc_type"); 115 throw bad_input(OPKELE_CP_ "unsupported assoc_type");
107 string st; 116 string st;
108 if(p.has_param("session_type")) st = p.get_param("session_type"); 117 if(p.has_param("session_type")) st = p.get_param("session_type");
109 if((!st.empty()) && st!="DH-SHA1") 118 if((!st.empty()) && st!="DH-SHA1")
110 throw bad_input(OPKELE_CP_ "unsupported session_type"); 119 throw bad_input(OPKELE_CP_ "unsupported session_type");
@@ -241,30 +250,28 @@ namespace opkele {
241 request += '&'; 250 request += '&';
242 request += i->first; 251 request += i->first;
243 request += '='; 252 request += '=';
244 request += util::url_encode(i->second); 253 request += util::url_encode(i->second);
245 } 254 }
246 } 255 }
247 curl_t curl = curl_t::easy_init(); 256 curl_pick_t curl = curl_pick_t::easy_init();
248 if(!curl) 257 if(!curl)
249 throw exception_curl(OPKELE_CP_ "failed to initialize curl"); 258 throw exception_curl(OPKELE_CP_ "failed to initialize curl");
250 string response;
251 CURLcode r; 259 CURLcode r;
252 (r=curl.misc_sets()) 260 (r=curl.misc_sets())
253 || (r=curl.easy_setopt(CURLOPT_URL,server.c_str())) 261 || (r=curl.easy_setopt(CURLOPT_URL,server.c_str()))
254 || (r=curl.easy_setopt(CURLOPT_POST,1)) 262 || (r=curl.easy_setopt(CURLOPT_POST,1))
255 || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data())) 263 || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data()))
256 || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length())) 264 || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length()))
257 || (r=curl.easy_setopt(CURLOPT_WRITEFUNCTION,_curl_tostring)) 265 || (r=curl.set_write())
258 || (r=curl.easy_setopt(CURLOPT_WRITEDATA,&response))
259 ; 266 ;
260 if(r) 267 if(r)
261 throw exception_curl(OPKELE_CP_ "failed to set curly options",r); 268 throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
262 if( (r=curl.easy_perform()) ) 269 if( (r=curl.easy_perform()) )
263 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); 270 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
264 params_t pp; pp.parse_keyvalues(response); 271 params_t pp; pp.parse_keyvalues(curl.response);
265 if(pp.has_param("invalidate_handle")) 272 if(pp.has_param("invalidate_handle"))
266 invalidate_assoc(server,pp.get_param("invalidate_handle")); 273 invalidate_assoc(server,pp.get_param("invalidate_handle"));
267 if(pp.has_param("is_valid")) { 274 if(pp.has_param("is_valid")) {
268 if(pp.get_param("is_valid")=="true") 275 if(pp.get_param("is_valid")=="true")
269 return; 276 return;
270 }else if(pp.has_param("lifetime")) { 277 }else if(pp.has_param("lifetime")) {
@@ -274,21 +281,20 @@ namespace opkele {
274 throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); 281 throw failed_check_authentication(OPKELE_CP_ "failed to verify response");
275 } 282 }
276 283
277 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { 284 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) {
278 server.erase(); 285 server.erase();
279 delegate.erase(); 286 delegate.erase();
280 curl_t curl = curl_t::easy_init(); 287 curl_pick_t curl = curl_pick_t::easy_init();
281 if(!curl) 288 if(!curl)
282 throw exception_curl(OPKELE_CP_ "failed to initialize curl"); 289 throw exception_curl(OPKELE_CP_ "failed to initialize curl");
283 string html; 290 string& html = curl.response;
284 CURLcode r; 291 CURLcode r;
285 (r=curl.misc_sets()) 292 (r=curl.misc_sets())
286 || (r=curl.easy_setopt(CURLOPT_URL,url.c_str())) 293 || (r=curl.easy_setopt(CURLOPT_URL,url.c_str()))
287 || (r=curl.easy_setopt(CURLOPT_WRITEFUNCTION,_curl_tostring)) 294 || (r=curl.set_write());
288 || (r=curl.easy_setopt(CURLOPT_WRITEDATA,&html))
289 ; 295 ;
290 if(r) 296 if(r)
291 throw exception_curl(OPKELE_CP_ "failed to set curly options",r); 297 throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
292 r = curl.easy_perform(); 298 r = curl.easy_perform();
293 if(r && r!=CURLE_WRITE_ERROR) 299 if(r && r!=CURLE_WRITE_ERROR)
294 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); 300 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);