summaryrefslogtreecommitdiffabout
path: root/lib/consumer.cc
Side-by-side diff
Diffstat (limited to 'lib/consumer.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/consumer.cc1
1 files changed, 1 insertions, 0 deletions
diff --git a/lib/consumer.cc b/lib/consumer.cc
index ebda262..801496e 100644
--- a/lib/consumer.cc
+++ b/lib/consumer.cc
@@ -1,100 +1,101 @@
#include <algorithm>
#include <cassert>
#include <cstring>
#include <opkele/util.h>
+#include <opkele/util-internal.h>
#include <opkele/curl.h>
#include <opkele/exception.h>
#include <opkele/data.h>
#include <opkele/consumer.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <iostream>
#include "config.h"
#include <pcre.h>
namespace opkele {
using namespace std;
using util::curl_t;
using util::curl_pick_t;
class pcre_matches_t {
public:
int *_ov;
int _s;
pcre_matches_t() : _ov(0), _s(0) { }
pcre_matches_t(int s) : _ov(0), _s(s) {
if(_s&1) ++_s;
_s += _s>>1;
_ov = new int[_s];
}
~pcre_matches_t() throw() { if(_ov) delete[] _ov; }
int begin(int i) const { return _ov[i<<1]; }
int end(int i) const { return _ov[(i<<1)+1]; }
int length(int i) const { int t=i<<1; return _ov[t+1]-_ov[t]; }
};
class pcre_t {
public:
pcre *_p;
pcre_t() : _p(0) { }
pcre_t(pcre *p) : _p(p) { }
pcre_t(const char *re,int opts) : _p(0) {
static const char *errptr; static int erroffset;
_p = pcre_compile(re,opts,&errptr,&erroffset,NULL);
if(!_p)
throw internal_error(OPKELE_CP_ string("Failed to compile regexp: ")+errptr);
}
~pcre_t() throw() { if(_p) (*pcre_free)(_p); }
pcre_t& operator=(pcre *p) { if(_p) (*pcre_free)(_p); _p=p; return *this; }
operator const pcre*(void) const { return _p; }
operator pcre*(void) { return _p; }
int exec(const string& s,pcre_matches_t& m) {
if(!_p)
throw internal_error(OPKELE_CP_ "Trying to execute absent regexp");
return pcre_exec(_p,NULL,s.c_str(),s.length(),0,0,m._ov,m._s);
}
};
assoc_t consumer_t::associate(const string& server) {
util::dh_t dh = DH_new();
if(!dh)
throw exception_openssl(OPKELE_CP_ "failed to DH_new()");
dh->p = util::dec_to_bignum(data::_default_p);
dh->g = util::dec_to_bignum(data::_default_g);
if(!DH_generate_key(dh))
throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()");
string request =
"openid.mode=associate"
"&openid.assoc_type=HMAC-SHA1"
"&openid.session_type=DH-SHA1"
"&openid.dh_consumer_public=";
request += util::url_encode(util::bignum_to_base64(dh->pub_key));
curl_pick_t curl = curl_pick_t::easy_init();
if(!curl)
throw exception_curl(OPKELE_CP_ "failed to initialize curl");
CURLcode r;
(r=curl.misc_sets())
|| (r=curl.easy_setopt(CURLOPT_URL,server.c_str()))
|| (r=curl.easy_setopt(CURLOPT_POST,1))
|| (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data()))
|| (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length()))
|| (r=curl.set_write())
;
if(r)
throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
if( (r=curl.easy_perform()) )
throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
params_t p; p.parse_keyvalues(curl.response);
if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1")
throw bad_input(OPKELE_CP_ "unsupported assoc_type");
string st;
if(p.has_param("session_type")) st = p.get_param("session_type");
if((!st.empty()) && st!="DH-SHA1")