summaryrefslogtreecommitdiffabout
authorMichael Krelin <hacker@klever.net>2007-12-16 22:52:51 (UTC)
committer Michael Krelin <hacker@klever.net>2007-12-17 12:09:16 (UTC)
commit24a6f9e3525b76ed1aa787f66b003b01b767af39 (patch) (side-by-side diff)
tree87741582a2a673205b11ac0386725c0b9d8377f9
parentd4e05a6fe00ebd64546637e5873759d4e8d1b010 (diff)
downloadlibopkele-24a6f9e3525b76ed1aa787f66b003b01b767af39.zip
libopkele-24a6f9e3525b76ed1aa787f66b003b01b767af39.tar.gz
libopkele-24a6f9e3525b76ed1aa787f66b003b01b767af39.tar.bz2
made secret_t non-constant size and added params_t::query_string function
1. removed checking that secret_t is exactly 20 bytes, because its size is not so constant anymore. 2. added a query_string function that produces a query string suitable for use in GET and POST requests. I'm making use of it when performing direct request when establishing associations. Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--include/opkele/types.h18
-rw-r--r--lib/params.cc13
-rw-r--r--lib/secret.cc12
3 files changed, 30 insertions, 13 deletions
diff --git a/include/opkele/types.h b/include/opkele/types.h
index 520618d..ca07df5 100644
--- a/include/opkele/types.h
+++ b/include/opkele/types.h
@@ -19,58 +19,58 @@ namespace opkele {
using std::map;
using std::ostream;
using std::auto_ptr;
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_sha1 pointer to the sha1 digest
+ * @param key_d pointer to the message digest
* @param rv reference to the return value
*/
- void enxor_to_base64(const unsigned char *key_sha1,string& rv) const;
+ void enxor_to_base64(const unsigned char *key_d,string& rv) const;
/**
- * decode base64-encoded secret and xor it with the sha1 digest
- * @param key_sha1 pointer to the message digest
+ * 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_sha1,const string& b64);
+ 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
*/
@@ -140,48 +140,56 @@ namespace opkele {
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;
diff --git a/lib/params.cc b/lib/params.cc
index ea86d3a..7a572c1 100644
--- a/lib/params.cc
+++ b/lib/params.cc
@@ -78,31 +78,44 @@ namespace opkele {
0,&md_len);
sig = util::encode_base64(md,md_len);
}
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;
}
+ string params_t::query_string(const char *prefix) const {
+ string rv;
+ for(const_iterator i=begin();i!=end();++i) {
+ if(!rv.empty())
+ rv += '&';
+ 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;
}
}
diff --git a/lib/secret.cc b/lib/secret.cc
index 632a2ca..d538890 100644
--- a/lib/secret.cc
+++ b/lib/secret.cc
@@ -1,49 +1,45 @@
#include <algorithm>
#include <functional>
#include <opkele/types.h>
#include <opkele/exception.h>
#include <opkele/util.h>
namespace opkele {
using namespace std;
template<class __a1,class __a2,class __r>
struct bitwise_xor : public binary_function<__a1,__a2,__r> {
__r operator() (const __a1& a1,const __a2& a2) const {
return a1^a2;
}
};
- void secret_t::enxor_to_base64(const unsigned char *key_sha1,string& rv) const {
- if(size()!=20)
- throw bad_input(OPKELE_CP_ "wrong secret size");
+ void secret_t::enxor_to_base64(const unsigned char *key_d,string& rv) const {
vector<unsigned char> tmp;
transform(
begin(), end(),
- key_sha1,
+ key_d,
back_insert_iterator<vector<unsigned char> >(tmp),
bitwise_xor<unsigned char,unsigned char,unsigned char>() );
rv = util::encode_base64(&(tmp.front()),tmp.size());
}
- void secret_t::enxor_from_base64(const unsigned char *key_sha1,const string& b64) {
+ void secret_t::enxor_from_base64(const unsigned char *key_d,const string& b64) {
clear();
util::decode_base64(b64,*this);
transform(
begin(), end(),
- key_sha1,
+ key_d,
begin(),
bitwise_xor<unsigned char,unsigned char,unsigned char>() );
}
void secret_t::to_base64(string& rv) const {
- if(size()!=20)
- throw bad_input(OPKELE_CP_ "wrong secret size");
rv = util::encode_base64(&(front()),size());
}
void secret_t::from_base64(const string& b64) {
util::decode_base64(b64,*this);
}
}