summaryrefslogtreecommitdiffabout
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--include/opkele/consumer.h2
-rw-r--r--include/opkele/util.h1
-rw-r--r--lib/consumer.cc29
-rw-r--r--lib/util.cc25
4 files changed, 29 insertions, 28 deletions
diff --git a/include/opkele/consumer.h b/include/opkele/consumer.h
index b9c29bd..3c0ed5f 100644
--- a/include/opkele/consumer.h
+++ b/include/opkele/consumer.h
@@ -107,29 +107,31 @@ namespace opkele {
* @throw exception in case of error
*/
string checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root="");
/**
* verify the id_res response
* @param pin the response parameters
* @param identity the identity being checked (if not specified, extracted
* from the openid.identity parameter
* @throw id_res_mismatch in case of signature
* mismatch
* @throw id_res_setup in case of
* openid.user_setup_url failure (supposedly
* checkid_immediate only)
* @throw id_res_failed in case of failure
* @throw exception in case of other failures
*/
void id_res(const params_t& pin,const string& identity="");
/**
* perform a check_authentication request.
* @param server the OpenID server
* @param p request parameters
*/
void check_authentication(const string& server,const params_t& p);
+ static string canonicalize(const string& url);
+
};
}
#endif /* __OPKELE_CONSUMER_H */
diff --git a/include/opkele/util.h b/include/opkele/util.h
index fbbef93..5372498 100644
--- a/include/opkele/util.h
+++ b/include/opkele/util.h
@@ -27,34 +27,33 @@ namespace opkele {
class dh_t {
public:
DH *_dh;
dh_t() : _dh(0) { }
dh_t(DH *dh) : _dh(dh) { }
~dh_t() throw() { if(_dh) DH_free(_dh); }
dh_t& operator=(DH *dh) { if(_dh) DH_free(_dh); _dh = dh; return *this; }
operator const DH*(void) const { return _dh; }
operator DH*(void) { return _dh; }
DH* operator->() { return _dh; }
const DH* operator->() const { return _dh; }
};
BIGNUM *base64_to_bignum(const string& b64);
BIGNUM *dec_to_bignum(const string& dec);
string bignum_to_base64(const BIGNUM *bn);
string time_to_w3c(time_t t);
time_t w3c_to_time(const string& w);
- string canonicalize_url(const string& url);
string url_encode(const string& str);
string long_to_string(long l);
long string_to_long(const string& s);
}
}
#endif /* __OPKELE_UTIL_H */
diff --git a/lib/consumer.cc b/lib/consumer.cc
index bd76b61..cbe0769 100644
--- a/lib/consumer.cc
+++ b/lib/consumer.cc
@@ -116,72 +116,72 @@ namespace opkele {
int expires_in = 0;
if(p.has_param("expires_in")) {
expires_in = util::string_to_long(p.get_param("expires_in"));
}else if(p.has_param("issued") && p.has_param("expiry")) {
expires_in = util::w3c_to_time(p.get_param("expiry"))-util::w3c_to_time(p.get_param("issued"));
}else
throw bad_input(OPKELE_CP_ "no expiration information");
return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in);
}
string consumer_t::checkid_immediate(const string& identity,const string& return_to,const string& trust_root) {
return checkid_(mode_checkid_immediate,identity,return_to,trust_root);
}
string consumer_t::checkid_setup(const string& identity,const string& return_to,const string& trust_root) {
return checkid_(mode_checkid_setup,identity,return_to,trust_root);
}
string consumer_t::checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root) {
params_t p;
if(mode==mode_checkid_immediate)
p["mode"]="checkid_immediate";
else if(mode==mode_checkid_setup)
p["mode"]="checkid_setup";
else
throw bad_input(OPKELE_CP_ "unknown checkid_* mode");
- string iurl = util::canonicalize_url(identity);
+ string iurl = canonicalize(identity);
string server, delegate;
retrieve_links(iurl,server,delegate);
p["identity"] = delegate.empty()?iurl:delegate;
if(!trust_root.empty())
p["trust_root"] = trust_root;
p["return_to"] = return_to;
try {
try {
string ah = find_assoc(server)->handle();
p["assoc_handle"] = ah;
}catch(failed_lookup& fl) {
string ah = associate(server)->handle();
p["assoc_handle"] = ah;
}
}catch(exception& e) { }
return p.append_query(server);
}
void consumer_t::id_res(const params_t& pin,const string& identity) {
if(pin.has_param("openid.user_setup_url"))
throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url"));
string server,delegate;
- retrieve_links(identity.empty()?pin.get_param("openid.identity"):util::canonicalize_url(identity),server,delegate);
+ retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate);
try {
assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle"));
const string& sigenc = pin.get_param("openid.sig");
mimetic::Base64::Decoder b;
vector<unsigned char> sig;
mimetic::decode(
sigenc.begin(),sigenc.end(), b,
back_insert_iterator<vector<unsigned char> >(sig) );
const string& slist = pin.get_param("openid.signed");
string kv;
string::size_type p = 0;
while(true) {
string::size_type co = slist.find(',',p);
string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p);
kv += f;
kv += ':';
f.insert(0,"openid.");
kv += pin.get_param(f);
kv += '\n';
if(co==string::npos)
break;
p = co+1;
}
secret_t secret = assoc->secret();
@@ -292,25 +292,50 @@ namespace opkele {
hre("\\bhref=['\"]([^'\"]+)['\"]",PCRE_CASELESS);
while(lre.search(html)) {
string attrs = lre[0];
html.erase(0,lre.get_match_end()+1);
if(!(rre.search(attrs)&&hre.search(attrs)))
continue;
if(rre[0]=="openid.server") {
server = hre[0];
if(!delegate.empty())
break;
}else if(rre[0]=="openid.delegate") {
delegate = hre[0];
if(!server.empty())
break;
}
}
if(server.empty())
throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration");
}
assoc_t consumer_t::find_assoc(const string& server) {
throw failed_lookup(OPKELE_CP_ "no find_assoc() provided");
}
+ string consumer_t::canonicalize(const string& url) {
+ string rv = url;
+ // strip leading and trailing spaces
+ string::size_type i = rv.find_first_not_of(" \t\r\n");
+ if(i==string::npos)
+ throw bad_input(OPKELE_CP_ "empty URL");
+ if(i)
+ rv.erase(0,i);
+ i = rv.find_last_not_of(" \t\r\n");
+ assert(i!=string::npos);
+ if(i<(rv.length()-1))
+ rv.erase(i+1);
+ // add missing http://
+ i = rv.find("://");
+ if(i==string::npos) { // primitive. but do we need more?
+ rv.insert(0,"http://");
+ i = sizeof("http://")-1;
+ }else{
+ i += sizeof("://")-1;
+ }
+ if(rv.find('/',i)==string::npos)
+ rv += '/';
+ return rv;
+ }
+
}
diff --git a/lib/util.cc b/lib/util.cc
index 1e7335c..d78b5e0 100644
--- a/lib/util.cc
+++ b/lib/util.cc
@@ -62,73 +62,48 @@ namespace opkele {
time_t w3c_to_time(const string& w) {
struct tm tm_t;
memset(&tm_t,0,sizeof(tm_t));
if(
sscanf(
w.c_str(),
"%04d-%02d-%02dT%02d:%02d:%02dZ",
&tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday,
&tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec
) != 6 )
throw failed_conversion(OPKELE_CP_ "failed to sscanf()");
tm_t.tm_mon--;
tm_t.tm_year-=1900;
time_t rv = mktime(&tm_t);
if(rv==(time_t)-1)
throw failed_conversion(OPKELE_CP_ "failed to mktime()");
return rv;
}
/*
*
*/
- string canonicalize_url(const string& url) {
- string rv = url;
- // strip leading and trailing spaces
- string::size_type i = rv.find_first_not_of(" \t\r\n");
- if(i==string::npos)
- throw bad_input(OPKELE_CP_ "empty URL");
- if(i)
- rv.erase(0,i);
- i = rv.find_last_not_of(" \t\r\n");
- assert(i!=string::npos);
- if(i<(rv.length()-1))
- rv.erase(i+1);
- // add missing http://
- i = rv.find("://");
- if(i==string::npos) { // primitive. but do we need more?
- rv.insert(0,"http://");
- i = sizeof("http://")-1;
- }else{
- i += sizeof("://")-1;
- }
- if(rv.find('/',i)==string::npos)
- rv += '/';
- return rv;
- }
-
string url_encode(const string& str) {
char * t = curl_escape(str.c_str(),str.length());
if(!t)
throw failed_conversion(OPKELE_CP_ "failed to curl_escape()");
string rv(t);
curl_free(t);
return rv;
}
string long_to_string(long l) {
char rv[32];
int r=snprintf(rv,sizeof(rv),"%ld",l);
if(r<0 || r>=sizeof(rv))
throw failed_conversion(OPKELE_CP_ "failed to snprintf()");
return rv;
}
long string_to_long(const string& s) {
char *endptr = 0;
long rv = strtol(s.c_str(),&endptr,10);
if((!endptr) || endptr==s.c_str())
throw failed_conversion(OPKELE_CP_ "failed to strtol()");
return rv;
}