summaryrefslogtreecommitdiffabout
path: root/lib/consumer.cc
Unidiff
Diffstat (limited to 'lib/consumer.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/consumer.cc5
1 files changed, 2 insertions, 3 deletions
diff --git a/lib/consumer.cc b/lib/consumer.cc
index df95b64..7f31d52 100644
--- a/lib/consumer.cc
+++ b/lib/consumer.cc
@@ -297,102 +297,101 @@ namespace opkele {
297 invalidate_assoc(server,pp.get_param("invalidate_handle")); 297 invalidate_assoc(server,pp.get_param("invalidate_handle"));
298 if(pp.has_param("is_valid")) { 298 if(pp.has_param("is_valid")) {
299 if(pp.get_param("is_valid")=="true") 299 if(pp.get_param("is_valid")=="true")
300 return; 300 return;
301 }else if(pp.has_param("lifetime")) { 301 }else if(pp.has_param("lifetime")) {
302 if(util::string_to_long(pp.get_param("lifetime"))) 302 if(util::string_to_long(pp.get_param("lifetime")))
303 return; 303 return;
304 } 304 }
305 throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); 305 throw failed_check_authentication(OPKELE_CP_ "failed to verify response");
306 } 306 }
307 307
308 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { 308 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) {
309 server.erase(); 309 server.erase();
310 delegate.erase(); 310 delegate.erase();
311 curl_t curl = curl_easy_init(); 311 curl_t curl = curl_easy_init();
312 if(!curl) 312 if(!curl)
313 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 313 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
314 string html; 314 string html;
315 CURLcode r; 315 CURLcode r;
316 (r=curl_misc_sets(curl)) 316 (r=curl_misc_sets(curl))
317 || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str())) 317 || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str()))
318 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) 318 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
319 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html)) 319 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html))
320 ; 320 ;
321 if(r) 321 if(r)
322 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 322 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
323 r = curl_easy_perform(curl); 323 r = curl_easy_perform(curl);
324 if(r && r!=CURLE_WRITE_ERROR) 324 if(r && r!=CURLE_WRITE_ERROR)
325 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 325 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
326 static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>", 326 static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>",
327 *re_lre = "<\\s*link\\b([^>]+)>", 327 *re_lre = "<\\s*link\\b([^>]+)>",
328 *re_rre = "\\brel\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]", 328 *re_rre = "\\brel\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]",
329 *re_hre = "\\bhref\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]"; 329 *re_hre = "\\bhref\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]";
330 pcre_matches_t m1(3), m2(3); 330 pcre_matches_t m1(3), m2(3);
331 pcre_t bre(re_bre,PCRE_CASELESS); 331 pcre_t bre(re_bre,PCRE_CASELESS);
332 if(bre.exec(html,m1)>0) 332 if(bre.exec(html,m1)>0)
333 html.erase(m1.begin(0)); 333 html.erase(m1.begin(0));
334 pcre_t hdre(re_hdre,PCRE_CASELESS); 334 pcre_t hdre(re_hdre,PCRE_CASELESS);
335 if(hdre.exec(html,m1)<=0) 335 if(hdre.exec(html,m1)<=0)
336 throw bad_input(OPKELE_CP_ "failed to find <head>"); 336 throw bad_input(OPKELE_CP_ "failed to find <head>");
337 html.erase(0,m1.end(0)+1); 337 html.erase(0,m1.end(0)+1);
338 pcre_t lre(re_lre,PCRE_CASELESS), rre(re_rre,PCRE_CASELESS), hre(re_hre,PCRE_CASELESS); 338 pcre_t lre(re_lre,PCRE_CASELESS), rre(re_rre,PCRE_CASELESS), hre(re_hre,PCRE_CASELESS);
339 while(lre.exec(html,m1)>=2) { 339 while(lre.exec(html,m1)>=2) {
340 string attrs(html,m1.begin(1),m1.length(1)); 340 string attrs(html,m1.begin(1),m1.length(1));
341 html.erase(0,m1.end(0)+1); 341 html.erase(0,m1.end(0)+1);
342 if(!( rre.exec(attrs,m1)>=2 && hre.exec(attrs,m2)>=2 )) 342 if(!( rre.exec(attrs,m1)>=2 && hre.exec(attrs,m2)>=2 ))
343 continue; 343 continue;
344 string rel(attrs,m1.begin(1),m1.length(1)); 344 string rel(attrs,m1.begin(1),m1.length(1));
345 string href(attrs,m2.begin(1),m2.length(1));
346 if(rel=="openid.server") { 345 if(rel=="openid.server") {
347 server = href; 346 server.assign(attrs,m2.begin(1),m2.length(1));
348 if(!delegate.empty()) break; 347 if(!delegate.empty()) break;
349 }else if(rel=="openid.delegate") { 348 }else if(rel=="openid.delegate") {
350 delegate = href; 349 delegate.assign(attrs,m2.begin(1),m2.length(1));
351 if(!server.empty()) break; 350 if(!server.empty()) break;
352 } 351 }
353 } 352 }
354 if(server.empty()) 353 if(server.empty())
355 throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration"); 354 throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration");
356 } 355 }
357 356
358 assoc_t consumer_t::find_assoc(const string& server) { 357 assoc_t consumer_t::find_assoc(const string& server) {
359 throw failed_lookup(OPKELE_CP_ "no find_assoc() provided"); 358 throw failed_lookup(OPKELE_CP_ "no find_assoc() provided");
360 } 359 }
361 360
362 string consumer_t::normalize(const string& url) { 361 string consumer_t::normalize(const string& url) {
363 string rv = url; 362 string rv = url;
364 // strip leading and trailing spaces 363 // strip leading and trailing spaces
365 string::size_type i = rv.find_first_not_of(" \t\r\n"); 364 string::size_type i = rv.find_first_not_of(" \t\r\n");
366 if(i==string::npos) 365 if(i==string::npos)
367 throw bad_input(OPKELE_CP_ "empty URL"); 366 throw bad_input(OPKELE_CP_ "empty URL");
368 if(i) 367 if(i)
369 rv.erase(0,i); 368 rv.erase(0,i);
370 i = rv.find_last_not_of(" \t\r\n"); 369 i = rv.find_last_not_of(" \t\r\n");
371 assert(i!=string::npos); 370 assert(i!=string::npos);
372 if(i<(rv.length()-1)) 371 if(i<(rv.length()-1))
373 rv.erase(i+1); 372 rv.erase(i+1);
374 // add missing http:// 373 // add missing http://
375 i = rv.find("://"); 374 i = rv.find("://");
376 if(i==string::npos) { // primitive. but do we need more? 375 if(i==string::npos) { // primitive. but do we need more?
377 rv.insert(0,"http://"); 376 rv.insert(0,"http://");
378 i = sizeof("http://")-1; 377 i = sizeof("http://")-1;
379 }else{ 378 }else{
380 i += sizeof("://")-1; 379 i += sizeof("://")-1;
381 } 380 }
382 string::size_type qm = rv.find('?',i); 381 string::size_type qm = rv.find('?',i);
383 string::size_type sl = rv.find('/',i); 382 string::size_type sl = rv.find('/',i);
384 if(qm!=string::npos) { 383 if(qm!=string::npos) {
385 if(sl==string::npos || sl>qm) 384 if(sl==string::npos || sl>qm)
386 rv.insert(qm,1,'/'); 385 rv.insert(qm,1,'/');
387 }else{ 386 }else{
388 if(sl==string::npos) 387 if(sl==string::npos)
389 rv += '/'; 388 rv += '/';
390 } 389 }
391 return rv; 390 return rv;
392 } 391 }
393 392
394 string consumer_t::canonicalize(const string& url) { 393 string consumer_t::canonicalize(const string& url) {
395 string rv = normalize(url); 394 string rv = normalize(url);
396 curl_t curl = curl_easy_init(); 395 curl_t curl = curl_easy_init();
397 if(!curl) 396 if(!curl)
398 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 397 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");