summaryrefslogtreecommitdiffabout
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--include/opkele/types.h4
-rw-r--r--lib/discovery.cc6
2 files changed, 10 insertions, 0 deletions
diff --git a/include/opkele/types.h b/include/opkele/types.h
index d959021..4e1415f 100644
--- a/include/opkele/types.h
+++ b/include/opkele/types.h
@@ -16,227 +16,231 @@
namespace opkele {
using std::vector;
using std::string;
using std::map;
using std::ostream;
using std::multimap;
using std::set;
/**
* the OpenID operation mode
*/
typedef enum _mode_t {
mode_associate,
mode_checkid_immediate,
mode_checkid_setup,
mode_check_association
} mode_t;
/**
* the association secret container
*/
class secret_t : public vector<unsigned char> {
public:
/**
* xor the secret and hmac together and encode, using base64
* @param key_d pointer to the message digest
* @param rv reference to the return value
*/
void enxor_to_base64(const unsigned char *key_d,string& rv) const;
/**
* decode base64-encoded secret and xor it with the message digest
* @param key_d pointer to the message digest
* @param b64 base64-encoded secret value
*/
void enxor_from_base64(const unsigned char *key_d,const string& b64);
/**
* plainly encode to base64 representation
* @param rv reference to the return value
*/
void to_base64(string& rv) const;
/**
* decode cleartext secret from base64
* @param b64 base64-encoded representation of the secret value
*/
void from_base64(const string& b64);
};
/**
* Interface to the association.
*/
class association_t {
public:
virtual ~association_t() { }
/**
* retrieve the server with which association was established.
* @return server name
*/
virtual string server() const = 0;
/**
* retrieve the association handle.
* @return handle
*/
virtual string handle() const = 0;
/**
* retrieve the association type.
* @return association type
*/
virtual string assoc_type() const = 0;
/**
* retrieve the association secret.
* @return association secret
*/
virtual secret_t secret() const = 0;
/**
* retrieve the number of seconds the association expires in.
* @return seconds till expiration
*/
virtual int expires_in() const = 0;
/**
* check whether the association is stateless.
* @return true if stateless
*/
virtual bool stateless() const = 0;
/**
* check whether the association is expired.
* @return true if expired
*/
virtual bool is_expired() const = 0;
};
/**
* the shared_ptr<> for association_t object type
*/
typedef tr1mem::shared_ptr<association_t> assoc_t;
/**
* request/response parameters map
*/
class params_t : public map<string,string> {
public:
/**
* check whether the parameter is present.
* @param n the parameter name
* @return true if yes
*/
bool has_param(const string& n) const;
/**
* retrieve the parameter (const version)
* @param n the parameter name
* @return the parameter value
* @throw failed_lookup if there is no such parameter
*/
const string& get_param(const string& n) const;
/**
* retrieve the parameter.
* @param n the parameter name
* @return the parameter value
* @throw failed_lookup if there is no such parameter
*/
string& get_param(const string& n);
/**
* parse the OpenID key/value data.
* @param kv the OpenID key/value data
*/
void parse_keyvalues(const string& kv);
/**
* sign the fields.
* @param secret the secret used for signing
* @param sig reference to the string, containing base64-encoded
* result
* @param slist the comma-separated list of fields to sign
* @param prefix the string to prepend to parameter names
*/
void sign(secret_t secret,string& sig,const string& slist,const char *prefix=0) const;
/**
* append parameters to the URL as a GET-request parameters.
* @param url the base URL
* @param prefix the string to prepend to parameter names
* @return the ready-to-use location
*/
string append_query(const string& url,const char *prefix = "openid.") const;
/**
* make up a query string suitable for use in GET and POST
* requests.
* @param prefix string to prened to parameter names
* @return query string
*/
string query_string(const char *prefix = "openid.") const;
};
/**
* dump the key/value pairs for the parameters to the stream.
* @param o output stream
* @param p the parameters
*/
ostream& operator << (ostream& o,const params_t& p);
namespace xrd {
struct priority_compare {
inline bool operator()(long a,long b) const {
return (a<0) ? false : (b<0) ? false : (a<b);
}
};
template <typename _DT>
class priority_map : public multimap<long,_DT,priority_compare> {
typedef multimap<long,_DT,priority_compare> map_type;
public:
inline _DT& add(long priority,const _DT& d) {
return insert(typename map_type::value_type(priority,d))->second;
}
};
typedef priority_map<string> canonical_ids_t;
typedef priority_map<string> local_ids_t;
typedef set<string> types_t;
typedef priority_map<string> uris_t;
class service_t {
public:
types_t types;
uris_t uris;
local_ids_t local_ids;
+ string provider_id;
void clear() {
types.clear();
uris.clear(); local_ids.clear();
+ provider_id.clear();
}
};
typedef priority_map<service_t> services_t;
class XRD_t {
public:
time_t expires;
canonical_ids_t canonical_ids;
local_ids_t local_ids;
services_t services;
+ string provider_id;
void clear() {
expires = 0;
canonical_ids.clear(); local_ids.clear();
services.clear();
+ provider_id.clear();
}
bool empty() const {
return
canonical_ids.empty()
&& local_ids.empty()
&& services.empty();
}
};
}
}
#endif /* __OPKELE_TYPES_H */
diff --git a/lib/discovery.cc b/lib/discovery.cc
index bc7d6fb..81727c0 100644
--- a/lib/discovery.cc
+++ b/lib/discovery.cc
@@ -47,336 +47,342 @@ namespace opkele {
enum {
xmode_html = 1, xmode_xrd = 2
};
int xmode;
string xrds_location;
string http_content_type;
service_t html_openid1;
service_t html_openid2;
string cdata_buf;
long status_code;
string status_string;
typedef list<string> pt_stack_t;
pt_stack_t pt_stack;
int skipping;
XRD_t *xrd;
service_t *xrd_service;
string* cdata;
idigger_t()
: util::curl_t(easy_init()),
util::expat_t(0),
xri_proxy(XRI_PROXY_URL) {
CURLcode r;
(r=misc_sets())
|| (r=set_write())
|| (r=set_header())
;
if(r)
throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
}
~idigger_t() throw() { }
void discover(idiscovery_t& result,const string& identity) {
result.clear();
string::size_type fsc = identity.find_first_not_of(whitespace);
if(fsc==string::npos)
throw bad_input(OPKELE_CP_ "whtiespace-only identity");
string::size_type lsc = identity.find_last_not_of(whitespace);
assert(lsc!=string::npos);
if(!strncasecmp(identity.c_str()+fsc,"xri://",sizeof("xri://")-1))
fsc += sizeof("xri://")-1;
if((fsc+1)>=lsc)
throw bad_input(OPKELE_CP_ "not a character of importance in identity");
string id(identity,fsc,lsc-fsc+1);
if(strchr(i_leaders,id[0])) {
result.normalized_id = id;
result.xri_identity = true;
/* TODO: further canonicalize xri identity? Like folding case or whatever... */
discover_at(
result,
xri_proxy + util::url_encode(id)+
"?_xrd_r=application/xrd+xml;sep=false", xmode_xrd);
if(status_code!=100)
throw failed_xri_resolution(OPKELE_CP_
"XRI resolution failed with '"+status_string+"' message",status_code);
if(result.xrd.canonical_ids.empty())
throw opkele::failed_discovery(OPKELE_CP_ "No CanonicalID for XRI identity found");
result.canonicalized_id = result.xrd.canonical_ids.begin()->second;
}else{
result.xri_identity = false;
if(id.find("://")==string::npos)
id.insert(0,"http://");
string::size_type fp = id.find('#');
if(fp!=string::npos) {
string::size_type qp = id.find('?');
if(qp==string::npos || qp<fp)
id.erase(fp);
else if(qp>fp)
id.erase(fp,qp-fp);
}
result.normalized_id = util::rfc_3986_normalize_uri(id);
discover_at(result,id,xmode_html|xmode_xrd);
const char * eu = 0;
CURLcode r = easy_getinfo(CURLINFO_EFFECTIVE_URL,&eu);
if(r)
throw exception_curl(OPKELE_CP_ "failed to get CURLINFO_EFFECTIVE_URL",r);
result.canonicalized_id = util::rfc_3986_normalize_uri(eu); /* XXX: strip fragment part? */
if(xrds_location.empty()) {
html2xrd(result.xrd);
}else{
discover_at(result,xrds_location,xmode_xrd);
if(result.xrd.empty())
html2xrd(result.xrd);
}
}
}
void discover_at(idiscovery_t& result,const string& url,int xm) {
CURLcode r = easy_setopt(CURLOPT_URL,url.c_str());
if(r)
throw exception_curl(OPKELE_CP_ "failed to set culry urlie",r);
(*(expat_t*)this) = parser_create_ns();
set_user_data(); set_element_handler();
set_character_data_handler();
http_content_type.clear();
xmode = xm;
if(xmode&xmode_html) {
xrds_location.clear();
html_openid1.clear(); html_openid2.clear();
}
xrd = &result.xrd;
cdata = 0; xrd_service = 0; skipping = 0;
status_code = 100; status_string.clear();
r = easy_perform();
if(r && r!=CURLE_WRITE_ERROR)
throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
parse(0,0,true);
}
void html2xrd(XRD_t& x) {
if(!html_openid1.uris.empty()) {
html_openid1.types.insert(STURI_OPENID11);
x.services.add(-1,html_openid1);
}
if(!html_openid2.uris.empty()) {
html_openid2.types.insert(STURI_OPENID20);
x.services.add(-1,html_openid2);
}
}
size_t write(void *p,size_t s,size_t nm) {
if(skipping<0) return 0;
/* TODO: limit total size */
size_t bytes = s*nm;
parse((const char *)p,bytes,false);
return bytes;
}
size_t header(void *p,size_t s,size_t nm) {
size_t bytes = s*nm;
const char *h = (const char*)p;
const char *colon = (const char*)memchr(p,':',bytes);
const char *space = (const char*)memchr(p,' ',bytes);
if(space && ( (!colon) || space<colon ) ) {
xrds_location.clear(); http_content_type.clear();
}else if(colon) {
const char *hv = ++colon;
int hnl = colon-h;
int rb;
for(rb = bytes-hnl-1;rb>0 && isspace(*hv);++hv,--rb);
while(rb>0 && isspace(hv[rb-1])) --rb;
if(rb) {
if( (hnl>=sizeof(XRDS_HEADER))
&& !strncasecmp(h,XRDS_HEADER":",
sizeof(XRDS_HEADER)) ) {
xrds_location.assign(hv,rb);
}else if( (hnl>=sizeof(CT_HEADER))
&& !strncasecmp(h,CT_HEADER":",
sizeof(CT_HEADER)) ) {
const char *sc = (const char*)memchr(
hv,';',rb);
http_content_type.assign(hv,sc?(sc-hv):rb);
}
}
}
return curl_t::header(p,s,nm);
}
void start_element(const XML_Char *n,const XML_Char **a) {
if(skipping<0) return;
if(skipping) {
if(xmode&xmode_html)
html_start_element(n,a);
++skipping; return;
}
if(pt_stack.empty()) {
if(is_qelement(n,NSURI_XRDS "\tXRDS"))
return;
if(is_qelement(n,NSURI_XRD "\tXRD")) {
assert(xrd);
xrd->clear();
pt_stack.push_back(n);
}else if(xmode&xmode_html) {
html_start_element(n,a);
}else{
skipping = -1;
}
}else{
int pt_s = pt_stack.size();
if(pt_s==1) {
if(is_qelement(n,NSURI_XRD "\tCanonicalID")) {
assert(xrd);
cdata = &(xrd->canonical_ids.add(element_priority(a),string()));
}else if(is_qelement(n,NSURI_XRD "\tLocalID")) {
assert(xrd);
cdata = &(xrd->local_ids.add(element_priority(a),string()));
+ }else if(is_qelement(n,NSURI_XRD "\tProviderID")) {
+ assert(xrd);
+ cdata = &(xrd->provider_id);
}else if(is_qelement(n,NSURI_XRD "\tService")) {
assert(xrd);
xrd_service = &(xrd->services.add(element_priority(a),
service_t()));
pt_stack.push_back(n);
}else if(is_qelement(n,NSURI_XRD "\tStatus")) {
for(;*a;) {
if(!strcasecmp(*(a++),"code")) {
if(sscanf(*(a++),"%ld",&status_code)==1 && status_code!=100) {
cdata = &status_string;
pt_stack.push_back(n);
break;
}
}
}
}else if(is_qelement(n,NSURI_XRD "\tExpires")) {
assert(xrd);
cdata_buf.clear();
cdata = &cdata_buf;
}else if(xmode&xmode_html) {
html_start_element(n,a);
}else{
skipping = 1;
}
}else if(pt_s==2) {
if(is_qelement(pt_stack.back().c_str(), NSURI_XRD "\tService")) {
if(is_qelement(n,NSURI_XRD "\tType")) {
assert(xrd); assert(xrd_service);
cdata_buf.clear();
cdata = &cdata_buf;
}else if(is_qelement(n,NSURI_XRD "\tURI")) {
assert(xrd); assert(xrd_service);
cdata = &(xrd_service->uris.add(element_priority(a),string()));
}else if(is_qelement(n,NSURI_XRD "\tLocalID")
|| is_qelement(n,NSURI_OPENID10 "\tDelegate") ) {
assert(xrd); assert(xrd_service);
cdata = &(xrd_service->local_ids.add(element_priority(a),string()));
+ }else if(is_qelement(n,NSURI_XRD "\tProviderID")) {
+ assert(xrd); assert(xrd_service);
+ cdata = &(xrd_service->provider_id);
}else{
skipping = 1;
}
}else
skipping = 1;
}else if(xmode&xmode_html) {
html_start_element(n,a);
}else{
skipping = 1;
}
}
}
void end_element(const XML_Char *n) {
if(skipping<0) return;
if(skipping) {
--skipping; return;
}
if(is_qelement(n,NSURI_XRD "\tType")) {
assert(xrd); assert(xrd_service); assert(cdata==&cdata_buf);
xrd_service->types.insert(cdata_buf);
}else if(is_qelement(n,NSURI_XRD "\tService")) {
assert(xrd); assert(xrd_service);
assert(!pt_stack.empty());
assert(pt_stack.back()==(NSURI_XRD "\tService"));
pt_stack.pop_back();
xrd_service = 0;
}else if(is_qelement(n,NSURI_XRD "\tStatus")) {
assert(xrd);
if(is_qelement(pt_stack.back().c_str(),n)) {
assert(cdata==&status_string);
pt_stack.pop_back();
if(status_code!=100)
skipping = -1;
}
}else if(is_qelement(n,NSURI_XRD "\tExpires")) {
assert(xrd);
xrd->expires = util::w3c_to_time(cdata_buf);
}else if((xmode&xmode_html) && is_element(n,"head")) {
skipping = -1;
}
cdata = 0;
}
void character_data(const XML_Char *s,int l) {
if(skipping) return;
if(cdata) cdata->append(s,l);
}
void html_start_element(const XML_Char *n,const XML_Char **a) {
if(is_element(n,"meta")) {
bool heq = false;
string l;
for(;*a;a+=2) {
if(!( strcasecmp(a[0],"http-equiv")
|| strcasecmp(a[1],XRDS_HEADER) ))
heq = true;
else if(!strcasecmp(a[0],"content"))
l.assign(a[1]);
}
if(heq)
xrds_location = l;
}else if(is_element(n,"link")) {
string rels;
string href;
for(;*a;a+=2) {
if( !strcasecmp(a[0],"rel") ) {
rels.assign(a[1]);
}else if( !strcasecmp(a[0],"href") ) {
const char *ns = a[1];
for(;*ns && isspace(*ns);++ns);
href.assign(ns);
string::size_type lns=href.find_last_not_of(whitespace);
href.erase(lns+1);
}
}
for(string::size_type ns=rels.find_first_not_of(whitespace);
ns!=string::npos; ns=rels.find_first_not_of(whitespace,ns)) {
string::size_type s = rels.find_first_of(whitespace,ns);
string rel;
if(s==string::npos) {
rel.assign(rels,ns,string::npos);
ns = string::npos;
}else{
rel.assign(rels,ns,s-ns);
ns = s;
}
if(rel=="openid.server")
html_openid1.uris.add(-1,href);
else if(rel=="openid.delegate")
html_openid1.local_ids.add(-1,href);
else if(rel=="openid2.provider")
html_openid2.uris.add(-1,href);
else if(rel=="openid2.local_id")
html_openid2.local_ids.add(-1,href);
}
}else if(is_element(n,"body")) {
skipping = -1;
}
}
};
void idiscover(idiscovery_t& result,const string& identity) {
idigger_t idigger;
idigger.discover(result,identity);
}
}