summaryrefslogtreecommitdiffabout
path: root/lib/params.cc
authorMichael Krelin <hacker@klever.net>2005-07-19 13:08:32 (UTC)
committer Michael Krelin <hacker@klever.net>2005-07-19 13:08:32 (UTC)
commit4c82851dd5d5644a89d4f269079bf901f763ee33 (patch) (side-by-side diff)
treeb64c8b3c9a1be88e2a9c3f762272e0b4509ba7d9 /lib/params.cc
parent907343b0c973eb295bec8795902a6d49744e9174 (diff)
downloadlibopkele-4c82851dd5d5644a89d4f269079bf901f763ee33.zip
libopkele-4c82851dd5d5644a89d4f269079bf901f763ee33.tar.gz
libopkele-4c82851dd5d5644a89d4f269079bf901f763ee33.tar.bz2
initial commit of libopkele - OpenID support library
Diffstat (limited to 'lib/params.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/params.cc96
1 files changed, 96 insertions, 0 deletions
diff --git a/lib/params.cc b/lib/params.cc
new file mode 100644
index 0000000..14f1a53
--- a/dev/null
+++ b/lib/params.cc
@@ -0,0 +1,96 @@
+#include <opkele/types.h>
+#include <opkele/exception.h>
+#include <opkele/util.h>
+#include <openssl/sha.h>
+#include <openssl/hmac.h>
+#include <mimetic/mimetic.h>
+
+namespace opkele {
+ using namespace std;
+
+ bool params_t::has_param(const string& n) const {
+ return find(n)!=end();
+ }
+ const string& params_t::get_param(const string& n) const {
+ const_iterator i = find(n);
+ if(i==end())
+ throw failed_lookup(OPKELE_CP_ n+": no such parameter");
+ return i->second;
+ }
+ string& params_t::get_param(const string& n) {
+ iterator i = find(n);
+ if(i==end())
+ throw failed_lookup(OPKELE_CP_ n+": no such parameter");
+ return i->second;
+ }
+
+ void params_t::parse_keyvalues(const string& kv) {
+ clear();
+ string::size_type p = 0;
+ while(true) {
+ string::size_type co = kv.find(':',p);
+ if(co==string::npos)
+ break;
+ string::size_type nl = kv.find('\n',co+1);
+ if(nl==string::npos)
+ throw bad_input(OPKELE_CP_ "malformed input");
+ insert(value_type(kv.substr(p,co-p),kv.substr(co+1,nl-co-1)));
+ p = nl+1;
+ }
+ }
+
+ void params_t::sign(secret_t secret,string& sig,const string& slist,const char *prefix) const {
+ 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 += ':';
+ if(prefix) f.insert(0,prefix);
+ kv += get_param(f);
+ kv += '\n';
+ if(co==string::npos)
+ break;
+ p = co+1;
+ }
+ unsigned int md_len = 0;
+ unsigned char *md = HMAC(
+ EVP_sha1(),
+ &(secret.front()),secret.size(),
+ (const unsigned char *)kv.data(),kv.length(),
+ 0,&md_len);
+ mimetic::Base64::Encoder b(0);
+ sig.erase();
+ mimetic::encode(
+ md,md+md_len, b,
+ back_insert_iterator<string>(sig) );
+ }
+
+ string params_t::append_query(const string& url,const char *prefix) const {
+ string rv = url;
+ bool p = true;
+ if(rv.find('?')==string::npos) {
+ rv += '?';
+ p = false;
+ }
+ for(const_iterator i=begin();i!=end();++i) {
+ if(p)
+ rv += '&';
+ else
+ p = true;
+ rv += prefix;
+ rv += i->first;
+ rv += '=';
+ rv += util::url_encode(i->second);
+ }
+ return rv;
+ }
+
+ ostream& operator << (ostream& o,const params_t& p) {
+ for(params_t::const_iterator i=p.begin();i!=p.end();++i)
+ o << i->first << ':' << i->second << '\n';
+ return o;
+ }
+
+}