summaryrefslogtreecommitdiffabout
authorMichael Krelin <hacker@klever.net>2008-01-21 19:51:23 (UTC)
committer Michael Krelin <hacker@klever.net>2008-01-21 19:51:23 (UTC)
commit2a116bce75236e46946bb9564790c26c6a59b9a4 (patch) (unidiff)
tree27b047f773ea2f37d080c8c9c8f583d345649856
parenteee78b62ac9c4aed629fc47eb2af6615ca17d2e2 (diff)
downloadlibopkele-2a116bce75236e46946bb9564790c26c6a59b9a4.zip
libopkele-2a116bce75236e46946bb9564790c26c6a59b9a4.tar.gz
libopkele-2a116bce75236e46946bb9564790c26c6a59b9a4.tar.bz2
Implement URI construction handling append='qxri' xrd:URI attribute.
This is the right thing to do and may come in handy assisting OPs in improving UI. Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--include/opkele/discovery.h10
-rw-r--r--lib/discovery.cc29
2 files changed, 31 insertions, 8 deletions
diff --git a/include/opkele/discovery.h b/include/opkele/discovery.h
index ab4b9d9..677d7bb 100644
--- a/include/opkele/discovery.h
+++ b/include/opkele/discovery.h
@@ -22,33 +22,41 @@ namespace opkele {
22 public: 22 public:
23 23
24 inline _DT& add(long priority,const _DT& d) { 24 inline _DT& add(long priority,const _DT& d) {
25 return insert(typename map_type::value_type(priority,d))->second; 25 return insert(typename map_type::value_type(priority,d))->second;
26 } 26 }
27 27
28 bool has_value(const _DT& d) const { 28 bool has_value(const _DT& d) const {
29 for(typename map_type::const_iterator i=this->begin();i!=this->end();++i) 29 for(typename map_type::const_iterator i=this->begin();i!=this->end();++i)
30 if(i->second==d) return true; 30 if(i->second==d) return true;
31 return false; 31 return false;
32 } 32 }
33 }; 33 };
34 34
35 typedef priority_map<string> canonical_ids_t; 35 typedef priority_map<string> canonical_ids_t;
36 typedef priority_map<string> local_ids_t; 36 typedef priority_map<string> local_ids_t;
37 typedef set<string> types_t; 37 typedef set<string> types_t;
38 typedef priority_map<string> uris_t; 38 struct uri_t {
39 string uri;
40 string append;
41
42 uri_t() { }
43 uri_t(const string& u) : uri(u) { }
44 uri_t(const string& u,const string& a) : uri(u), append(a) { }
45 };
46 typedef priority_map<uri_t> uris_t;
39 47
40 class service_t { 48 class service_t {
41 public: 49 public:
42 types_t types; 50 types_t types;
43 uris_t uris; 51 uris_t uris;
44 local_ids_t local_ids; 52 local_ids_t local_ids;
45 string provider_id; 53 string provider_id;
46 54
47 void clear() { 55 void clear() {
48 types.clear(); 56 types.clear();
49 uris.clear(); local_ids.clear(); 57 uris.clear(); local_ids.clear();
50 provider_id.clear(); 58 provider_id.clear();
51 } 59 }
52 }; 60 };
53 typedef priority_map<service_t> services_t; 61 typedef priority_map<service_t> services_t;
54 62
diff --git a/lib/discovery.cc b/lib/discovery.cc
index 93409f4..6e20654 100644
--- a/lib/discovery.cc
+++ b/lib/discovery.cc
@@ -46,32 +46,41 @@ namespace opkele {
46 if(!strcasecmp(n,en)) return true; 46 if(!strcasecmp(n,en)) return true;
47 int nl = strlen(n), enl = strlen(en); 47 int nl = strlen(n), enl = strlen(en);
48 if( (nl>=(enl+1)) && n[nl-enl-1]=='\t' 48 if( (nl>=(enl+1)) && n[nl-enl-1]=='\t'
49 && !strcasecmp(&n[nl-enl],en) ) 49 && !strcasecmp(&n[nl-enl],en) )
50 return true; 50 return true;
51 return false; 51 return false;
52 } 52 }
53 53
54 static long element_priority(const XML_Char **a) { 54 static long element_priority(const XML_Char **a) {
55 for(;*a;++a) 55 for(;*a;++a)
56 if(!strcasecmp(*(a++),"priority")) { 56 if(!strcasecmp(*(a++),"priority")) {
57 long rv; 57 long rv;
58 return (sscanf(*a,"%ld",&rv)==1)?rv:-1; 58 return (sscanf(*a,"%ld",&rv)==1)?rv:-1;
59 } 59 }
60 return -1; 60 return -1;
61 } 61 }
62 /* TODO: ideally all attributes should be
63 * retrieved in one run */
64 static const char *element_attr(const XML_Char **a, const char *at) {
65 for(;*a;++a)
66 if(!strcasecmp(*(a++),at)) {
67 return *a;
68 }
69 return 0;
70 }
62 71
63 class idigger_t : public util::curl_t, public util::expat_t { 72 class idigger_t : public util::curl_t, public util::expat_t {
64 public: 73 public:
65 string xri_proxy; 74 string xri_proxy;
66 75
67 enum { 76 enum {
68 xmode_html = 1, xmode_xrd = 2, xmode_cid = 4 77 xmode_html = 1, xmode_xrd = 2, xmode_cid = 4
69 }; 78 };
70 int xmode; 79 int xmode;
71 80
72 string xrds_location; 81 string xrds_location;
73 string http_content_type; 82 string http_content_type;
74 service_t html_openid1; 83 service_t html_openid1;
75 service_t html_openid2; 84 service_t html_openid2;
76 string cdata_buf; 85 string cdata_buf;
77 long status_code; 86 long status_code;
@@ -105,34 +114,33 @@ namespace opkele {
105 string rv; 114 string rv;
106 idiscovery_t idis; 115 idiscovery_t idis;
107 string::size_type fsc = identity.find_first_not_of(whitespace); 116 string::size_type fsc = identity.find_first_not_of(whitespace);
108 if(fsc==string::npos) 117 if(fsc==string::npos)
109 throw bad_input(OPKELE_CP_ "whitespace-only identity"); 118 throw bad_input(OPKELE_CP_ "whitespace-only identity");
110 string::size_type lsc = identity.find_last_not_of(whitespace); 119 string::size_type lsc = identity.find_last_not_of(whitespace);
111 assert(lsc!=string::npos); 120 assert(lsc!=string::npos);
112 if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1)) 121 if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1))
113 fsc += sizeof("xri://")-1; 122 fsc += sizeof("xri://")-1;
114 if((fsc+1)>=lsc) 123 if((fsc+1)>=lsc)
115 throw bad_input(OPKELE_CP_ "not a character of importance in identity"); 124 throw bad_input(OPKELE_CP_ "not a character of importance in identity");
116 string id(identity,fsc,lsc-fsc+1); 125 string id(identity,fsc,lsc-fsc+1);
117 idis.clear(); 126 idis.clear();
118 if(strchr(i_leaders,id[0])) { 127 if(strchr(i_leaders,id[0])) {
119 /* TODO: further normalize xri identity? Like folding case 128 /* TODO: further normalize xri identity? Like folding case
120 * or whatever... */ 129 * or whatever... */
121 rv = idis.normalized_id = id; 130 rv = id;
122 idis.xri_identity = true;
123 set<string> cids; 131 set<string> cids;
124 for(const struct service_type_t *st=service_types; 132 for(const struct service_type_t *st=service_types;
125 st<&service_types[sizeof(service_types)/sizeof(*service_types)];++st) { 133 st<&service_types[sizeof(service_types)/sizeof(*service_types)];++st) {
126 idis.clear(); 134 idis.clear();
127 discover_at( idis, 135 discover_at( idis,
128 xri_proxy + util::url_encode(id)+ 136 xri_proxy + util::url_encode(id)+
129 "?_xrd_t="+util::url_encode(st->uri)+ 137 "?_xrd_t="+util::url_encode(st->uri)+
130 "&_xrd_r=application/xrd%2Bxml" 138 "&_xrd_r=application/xrd%2Bxml"
131 ";sep=true;refs=true", 139 ";sep=true;refs=true",
132 xmode_xrd ); 140 xmode_xrd );
133 if(status_code==241) continue; 141 if(status_code==241) continue;
134 if(status_code!=100) 142 if(status_code!=100)
135 throw failed_xri_resolution(OPKELE_CP_ 143 throw failed_xri_resolution(OPKELE_CP_
136 "XRI resolution failed with '"+status_string+"' message" 144 "XRI resolution failed with '"+status_string+"' message"
137 ", while looking for SEP with type '"+st->uri+"'", status_code); 145 ", while looking for SEP with type '"+st->uri+"'", status_code);
138 if(idis.xrd.canonical_ids.empty()) 146 if(idis.xrd.canonical_ids.empty())
@@ -142,32 +150,33 @@ namespace opkele {
142 cids.insert(cid); 150 cids.insert(cid);
143 idis.clear(); 151 idis.clear();
144 discover_at( idis, 152 discover_at( idis,
145 xri_proxy + util::url_encode(id)+ 153 xri_proxy + util::url_encode(id)+
146 "?_xrd_t="+util::url_encode(st->uri)+ 154 "?_xrd_t="+util::url_encode(st->uri)+
147 "&_xrd_r=application/xrd%2Bxml" 155 "&_xrd_r=application/xrd%2Bxml"
148 ";sep=true;refs=true", 156 ";sep=true;refs=true",
149 xmode_xrd ); 157 xmode_xrd );
150 if(status_code==241) continue; 158 if(status_code==241) continue;
151 if(status_code!=100) 159 if(status_code!=100)
152 throw failed_xri_resolution(OPKELE_CP_ 160 throw failed_xri_resolution(OPKELE_CP_
153 "XRI resolution failed with '"+status_string+"' message" 161 "XRI resolution failed with '"+status_string+"' message"
154 ", while looking for SEP with type '"+st->uri+"'" 162 ", while looking for SEP with type '"+st->uri+"'"
155 " on canonical id", status_code); 163 " on canonical id", status_code);
156 } 164 }
157 idis.canonicalized_id = cid; 165 idis.canonicalized_id = cid;
166 idis.normalized_id = rv; idis.xri_identity = true;
158 queue_endpoints(oi,idis,st); 167 queue_endpoints(oi,idis,st);
159 } 168 }
160 }else{ 169 }else{
161 idis.xri_identity = false; 170 idis.xri_identity = false;
162 if(id.find("://")==string::npos) 171 if(id.find("://")==string::npos)
163 id.insert(0,"http://"); 172 id.insert(0,"http://");
164 string::size_type fp = id.find('#'); 173 string::size_type fp = id.find('#');
165 if(fp!=string::npos) { 174 if(fp!=string::npos) {
166 string::size_type qp = id.find('?'); 175 string::size_type qp = id.find('?');
167 if(qp==string::npos || qp<fp) 176 if(qp==string::npos || qp<fp)
168 id.erase(fp); 177 id.erase(fp);
169 else if(qp>fp) 178 else if(qp>fp)
170 id.erase(fp,qp-fp); 179 id.erase(fp,qp-fp);
171 } 180 }
172 rv = idis.normalized_id = util::rfc_3986_normalize_uri(id); 181 rv = idis.normalized_id = util::rfc_3986_normalize_uri(id);
173 discover_at(idis,id,xmode_html|xmode_xrd); 182 discover_at(idis,id,xmode_html|xmode_xrd);
@@ -182,33 +191,32 @@ namespace opkele {
182 idis.clear(); 191 idis.clear();
183 idis.canonicalized_id = cid; 192 idis.canonicalized_id = cid;
184 discover_at(idis,xrds_location,xmode_xrd); 193 discover_at(idis,xrds_location,xmode_xrd);
185 if(idis.xrd.empty()) 194 if(idis.xrd.empty())
186 html2xrd(oi,idis); 195 html2xrd(oi,idis);
187 else{ 196 else{
188 for(const service_type_t *st=service_types; 197 for(const service_type_t *st=service_types;
189 st<&service_types[sizeof(service_types)/sizeof(*service_types)];++st) 198 st<&service_types[sizeof(service_types)/sizeof(*service_types)];++st)
190 queue_endpoints(oi,idis,st); 199 queue_endpoints(oi,idis,st);
191 } 200 }
192 } 201 }
193 } 202 }
194 return rv; 203 return rv;
195 } 204 }
196 205
197 void discover_at(idiscovery_t& idis,const string& url,int xm) { 206 void discover_at(idiscovery_t& idis,const string& url,int xm) {
198 DOUT_("Doing discovery at " << url);
199 CURLcode r = easy_setopt(CURLOPT_URL,url.c_str()); 207 CURLcode r = easy_setopt(CURLOPT_URL,url.c_str());
200 if(r) 208 if(r)
201 throw exception_curl(OPKELE_CP_ "failed to set culry urlie",r); 209 throw exception_curl(OPKELE_CP_ "failed to set culry urlie",r);
202 210
203 http_content_type.clear(); 211 http_content_type.clear();
204 xmode = xm; 212 xmode = xm;
205 prepare_to_parse(); 213 prepare_to_parse();
206 if(xmode&xmode_html) { 214 if(xmode&xmode_html) {
207 xrds_location.clear(); 215 xrds_location.clear();
208 save_html.clear(); 216 save_html.clear();
209 save_html.reserve(max_html); 217 save_html.reserve(max_html);
210 } 218 }
211 xrd = &idis.xrd; 219 xrd = &idis.xrd;
212 220
213 r = easy_perform(); 221 r = easy_perform();
214 if(r && r!=CURLE_WRITE_ERROR) 222 if(r && r!=CURLE_WRITE_ERROR)
@@ -377,33 +385,35 @@ namespace opkele {
377 assert(xrd); 385 assert(xrd);
378 cdata_buf.clear(); 386 cdata_buf.clear();
379 cdata = &cdata_buf; 387 cdata = &cdata_buf;
380 }else if(xmode&xmode_html) { 388 }else if(xmode&xmode_html) {
381 html_start_element(n,a); 389 html_start_element(n,a);
382 }else{ 390 }else{
383 skipping = 1; 391 skipping = 1;
384 } 392 }
385 }else if(pt_s==2) { 393 }else if(pt_s==2) {
386 if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) { 394 if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) {
387 if(is_qelement(n,NSURI_XRD "\tType")) { 395 if(is_qelement(n,NSURI_XRD "\tType")) {
388 assert(xrd); assert(xrd_service); 396 assert(xrd); assert(xrd_service);
389 cdata_buf.clear(); 397 cdata_buf.clear();
390 cdata = &cdata_buf; 398 cdata = &cdata_buf;
391 }else if(is_qelement(n,NSURI_XRD "\tURI")) { 399 }else if(is_qelement(n,NSURI_XRD "\tURI")) {
392 assert(xrd); assert(xrd_service); 400 assert(xrd); assert(xrd_service);
393 cdata = &(xrd_service->uris.add(element_priority(a),string())); 401 const char *append = element_attr(a,"append");
402 xrd::uri_t& uri = xrd_service->uris.add(element_priority(a),xrd::uri_t("",append?append:""));
403 cdata = &uri.uri;
394 }else if(is_qelement(n,NSURI_XRD "\tLocalID") 404 }else if(is_qelement(n,NSURI_XRD "\tLocalID")
395 || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) { 405 || is_qelement(n,NSURI_OPENID10 "\tDelegate") ) {
396 assert(xrd); assert(xrd_service); 406 assert(xrd); assert(xrd_service);
397 cdata = &(xrd_service->local_ids.add(element_priority(a),string())); 407 cdata = &(xrd_service->local_ids.add(element_priority(a),string()));
398 }else if(is_qelement(n,NSURI_XRD "\tProviderID")) { 408 }else if(is_qelement(n,NSURI_XRD "\tProviderID")) {
399 assert(xrd); assert(xrd_service); 409 assert(xrd); assert(xrd_service);
400 cdata = &(xrd_service->provider_id); 410 cdata = &(xrd_service->provider_id);
401 }else{ 411 }else{
402 skipping = 1; 412 skipping = 1;
403 } 413 }
404 }else 414 }else
405 skipping = 1; 415 skipping = 1;
406 }else if(xmode&xmode_html) { 416 }else if(xmode&xmode_html) {
407 html_start_element(n,a); 417 html_start_element(n,a);
408 }else{ 418 }else{
409 skipping = 1; 419 skipping = 1;
@@ -471,56 +481,61 @@ namespace opkele {
471 string::size_type lns=href.find_last_not_of(whitespace); 481 string::size_type lns=href.find_last_not_of(whitespace);
472 href.erase(lns+1); 482 href.erase(lns+1);
473 } 483 }
474 } 484 }
475 for(string::size_type ns=rels.find_first_not_of(whitespace); 485 for(string::size_type ns=rels.find_first_not_of(whitespace);
476 ns!=string::npos; ns=rels.find_first_not_of(whitespace,ns)) { 486 ns!=string::npos; ns=rels.find_first_not_of(whitespace,ns)) {
477 string::size_type s = rels.find_first_of(whitespace,ns); 487 string::size_type s = rels.find_first_of(whitespace,ns);
478 string rel; 488 string rel;
479 if(s==string::npos) { 489 if(s==string::npos) {
480 rel.assign(rels,ns,string::npos); 490 rel.assign(rels,ns,string::npos);
481 ns = string::npos; 491 ns = string::npos;
482 }else{ 492 }else{
483 rel.assign(rels,ns,s-ns); 493 rel.assign(rels,ns,s-ns);
484 ns = s; 494 ns = s;
485 } 495 }
486 if(rel=="openid.server") 496 if(rel=="openid.server")
487 html_openid1.uris.add(-1,href); 497 html_openid1.uris.add(-1,xrd::uri_t(href));
488 else if(rel=="openid.delegate") 498 else if(rel=="openid.delegate")
489 html_openid1.local_ids.add(-1,href); 499 html_openid1.local_ids.add(-1,href);
490 else if(rel=="openid2.provider") 500 else if(rel=="openid2.provider")
491 html_openid2.uris.add(-1,href); 501 html_openid2.uris.add(-1,xrd::uri_t(href));
492 else if(rel=="openid2.local_id") 502 else if(rel=="openid2.local_id")
493 html_openid2.local_ids.add(-1,href); 503 html_openid2.local_ids.add(-1,href);
494 } 504 }
495 }else if(is_element(n,"body")) { 505 }else if(is_element(n,"body")) {
496 skipping = -1; 506 skipping = -1;
497 } 507 }
498 } 508 }
499 509
500 void queue_endpoints(endpoint_discovery_iterator& oi, 510 void queue_endpoints(endpoint_discovery_iterator& oi,
501 const idiscovery_t &id, 511 const idiscovery_t &id,
502 const service_type_t *st) { 512 const service_type_t *st) {
503 openid_endpoint_t ep; 513 openid_endpoint_t ep;
504 ep.claimed_id = id.canonicalized_id; 514 ep.claimed_id = id.canonicalized_id;
505 for(xrd::services_t::const_iterator isvc=id.xrd.services.begin(); 515 for(xrd::services_t::const_iterator isvc=id.xrd.services.begin();
506 isvc!=id.xrd.services.end(); ++isvc) { 516 isvc!=id.xrd.services.end(); ++isvc) {
507 const xrd::service_t svc = isvc->second; 517 const xrd::service_t svc = isvc->second;
508 if(svc.types.find(st->uri)==svc.types.end()) continue; 518 if(svc.types.find(st->uri)==svc.types.end()) continue;
509 for(xrd::uris_t::const_iterator iu=svc.uris.begin();iu!=svc.uris.end();++iu) { 519 for(xrd::uris_t::const_iterator iu=svc.uris.begin();iu!=svc.uris.end();++iu) {
510 ep.uri = iu->second; 520 ep.uri = iu->second.uri;
521 if(id.xri_identity) {
522 if(iu->second.append=="qxri") {
523 ep.uri += id.normalized_id;
524 } /* TODO: else handle other append attribute values */
525 }
511 if(st->forceid) { 526 if(st->forceid) {
512 ep.local_id = ep.claimed_id = st->forceid; 527 ep.local_id = ep.claimed_id = st->forceid;
513 *(oi++) = ep; 528 *(oi++) = ep;
514 }else{ 529 }else{
515 if(svc.local_ids.empty()) { 530 if(svc.local_ids.empty()) {
516 ep.local_id = ep.claimed_id; 531 ep.local_id = ep.claimed_id;
517 *(oi++) = ep; 532 *(oi++) = ep;
518 }else{ 533 }else{
519 for(xrd::local_ids_t::const_iterator ilid=svc.local_ids.begin(); 534 for(xrd::local_ids_t::const_iterator ilid=svc.local_ids.begin();
520 ilid!=svc.local_ids.end(); ++ilid) { 535 ilid!=svc.local_ids.end(); ++ilid) {
521 ep.local_id = ilid->second; 536 ep.local_id = ilid->second;
522 *(oi++) = ep; 537 *(oi++) = ep;
523 } 538 }
524 } 539 }
525 } 540 }
526 } 541 }