summaryrefslogtreecommitdiffabout
path: root/lib/consumer.cc
Unidiff
Diffstat (limited to 'lib/consumer.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/consumer.cc16
1 files changed, 2 insertions, 14 deletions
diff --git a/lib/consumer.cc b/lib/consumer.cc
index b215aa8..331b1e9 100644
--- a/lib/consumer.cc
+++ b/lib/consumer.cc
@@ -1,352 +1,340 @@
1#include <algorithm> 1#include <algorithm>
2#include <cassert>
2#include <opkele/util.h> 3#include <opkele/util.h>
3#include <opkele/exception.h> 4#include <opkele/exception.h>
4#include <opkele/data.h> 5#include <opkele/data.h>
5#include <opkele/consumer.h> 6#include <opkele/consumer.h>
6#include <openssl/sha.h> 7#include <openssl/sha.h>
7#include <openssl/hmac.h> 8#include <openssl/hmac.h>
8#include <mimetic/mimetic.h>
9#include <curl/curl.h> 9#include <curl/curl.h>
10#include <pcre++.h> 10#include <pcre++.h>
11 11
12#include <iostream> 12#include <iostream>
13 13
14/* silly mimetic */
15#undef PACKAGE
16#undef PACKAGE_BUGREPORT
17#undef PACKAGE_NAME
18#undef PACKAGE_STRING
19#undef PACKAGE_TARNAME
20#undef PACKAGE_VERSION
21#undef VERSION
22
23#include "config.h" 14#include "config.h"
24 15
25namespace opkele { 16namespace opkele {
26 using namespace std; 17 using namespace std;
27 18
28 class curl_t { 19 class curl_t {
29 public: 20 public:
30 CURL *_c; 21 CURL *_c;
31 22
32 curl_t() : _c(0) { } 23 curl_t() : _c(0) { }
33 curl_t(CURL *c) : _c(c) { } 24 curl_t(CURL *c) : _c(c) { }
34 ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); } 25 ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); }
35 26
36 curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; } 27 curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; }
37 28
38 operator const CURL*(void) const { return _c; } 29 operator const CURL*(void) const { return _c; }
39 operator CURL*(void) { return _c; } 30 operator CURL*(void) { return _c; }
40 }; 31 };
41 32
42 static CURLcode curl_misc_sets(CURL* c) { 33 static CURLcode curl_misc_sets(CURL* c) {
43 CURLcode r; 34 CURLcode r;
44 (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1)) 35 (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1))
45 || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5)) 36 || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5))
46 || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120)) 37 || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120))
47 || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1)) 38 || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1))
48 || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_VERSION)) 39 || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_VERSION))
49 || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20)) 40 || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20))
50 ; 41 ;
51 return r; 42 return r;
52 } 43 }
53 44
54 static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) { 45 static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) {
55 string *str = (string*)stream; 46 string *str = (string*)stream;
56 size_t bytes = size*nmemb; 47 size_t bytes = size*nmemb;
57 size_t get = min(16384-str->length(),bytes); 48 size_t get = min(16384-str->length(),bytes);
58 str->append((const char*)ptr,get); 49 str->append((const char*)ptr,get);
59 return get; 50 return get;
60 } 51 }
61 52
62 assoc_t consumer_t::associate(const string& server) { 53 assoc_t consumer_t::associate(const string& server) {
63 util::dh_t dh = DH_new(); 54 util::dh_t dh = DH_new();
64 if(!dh) 55 if(!dh)
65 throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); 56 throw exception_openssl(OPKELE_CP_ "failed to DH_new()");
66 dh->p = util::dec_to_bignum(data::_default_p); 57 dh->p = util::dec_to_bignum(data::_default_p);
67 dh->g = util::dec_to_bignum(data::_default_g); 58 dh->g = util::dec_to_bignum(data::_default_g);
68 if(!DH_generate_key(dh)) 59 if(!DH_generate_key(dh))
69 throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); 60 throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()");
70 string request = 61 string request =
71 "openid.mode=associate" 62 "openid.mode=associate"
72 "&openid.assoc_type=HMAC-SHA1" 63 "&openid.assoc_type=HMAC-SHA1"
73 "&openid.session_type=DH-SHA1" 64 "&openid.session_type=DH-SHA1"
74 "&openid.dh_consumer_public="; 65 "&openid.dh_consumer_public=";
75 request += util::url_encode(util::bignum_to_base64(dh->pub_key)); 66 request += util::url_encode(util::bignum_to_base64(dh->pub_key));
76 curl_t curl = curl_easy_init(); 67 curl_t curl = curl_easy_init();
77 if(!curl) 68 if(!curl)
78 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 69 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
79 string response; 70 string response;
80 CURLcode r; 71 CURLcode r;
81 (r=curl_misc_sets(curl)) 72 (r=curl_misc_sets(curl))
82 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) 73 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str()))
83 || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) 74 || (r=curl_easy_setopt(curl,CURLOPT_POST,1))
84 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) 75 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data()))
85 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) 76 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length()))
86 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) 77 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
87 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) 78 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response))
88 ; 79 ;
89 if(r) 80 if(r)
90 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 81 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
91 if(r=curl_easy_perform(curl)) 82 if(r=curl_easy_perform(curl))
92 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 83 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
93 params_t p; p.parse_keyvalues(response); 84 params_t p; p.parse_keyvalues(response);
94 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1") 85 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1")
95 throw bad_input(OPKELE_CP_ "unsupported assoc_type"); 86 throw bad_input(OPKELE_CP_ "unsupported assoc_type");
96 string st; 87 string st;
97 if(p.has_param("session_type")) st = p.get_param("session_type"); 88 if(p.has_param("session_type")) st = p.get_param("session_type");
98 if((!st.empty()) && st!="DH-SHA1") 89 if((!st.empty()) && st!="DH-SHA1")
99 throw bad_input(OPKELE_CP_ "unsupported session_type"); 90 throw bad_input(OPKELE_CP_ "unsupported session_type");
100 secret_t secret; 91 secret_t secret;
101 if(st.empty()) { 92 if(st.empty()) {
102 secret.from_base64(p.get_param("mac_key")); 93 secret.from_base64(p.get_param("mac_key"));
103 }else{ 94 }else{
104 util::bignum_t s_pub = util::base64_to_bignum(p.get_param("dh_server_public")); 95 util::bignum_t s_pub = util::base64_to_bignum(p.get_param("dh_server_public"));
105 vector<unsigned char> ck(DH_size(dh)); 96 vector<unsigned char> ck(DH_size(dh));
106 int cklen = DH_compute_key(&(ck.front()),s_pub,dh); 97 int cklen = DH_compute_key(&(ck.front()),s_pub,dh);
107 if(cklen<0) 98 if(cklen<0)
108 throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()"); 99 throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()");
109 ck.resize(cklen); 100 ck.resize(cklen);
110 // OpenID algorithm requires extra zero in case of set bit here 101 // OpenID algorithm requires extra zero in case of set bit here
111 if(ck[0]&0x80) ck.insert(ck.begin(),1,0); 102 if(ck[0]&0x80) ck.insert(ck.begin(),1,0);
112 unsigned char key_sha1[SHA_DIGEST_LENGTH]; 103 unsigned char key_sha1[SHA_DIGEST_LENGTH];
113 SHA1(&(ck.front()),ck.size(),key_sha1); 104 SHA1(&(ck.front()),ck.size(),key_sha1);
114 secret.enxor_from_base64(key_sha1,p.get_param("enc_mac_key")); 105 secret.enxor_from_base64(key_sha1,p.get_param("enc_mac_key"));
115 } 106 }
116 int expires_in = 0; 107 int expires_in = 0;
117 if(p.has_param("expires_in")) { 108 if(p.has_param("expires_in")) {
118 expires_in = util::string_to_long(p.get_param("expires_in")); 109 expires_in = util::string_to_long(p.get_param("expires_in"));
119 }else if(p.has_param("issued") && p.has_param("expiry")) { 110 }else if(p.has_param("issued") && p.has_param("expiry")) {
120 expires_in = util::w3c_to_time(p.get_param("expiry"))-util::w3c_to_time(p.get_param("issued")); 111 expires_in = util::w3c_to_time(p.get_param("expiry"))-util::w3c_to_time(p.get_param("issued"));
121 }else 112 }else
122 throw bad_input(OPKELE_CP_ "no expiration information"); 113 throw bad_input(OPKELE_CP_ "no expiration information");
123 return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in); 114 return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in);
124 } 115 }
125 116
126 string consumer_t::checkid_immediate(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 117 string consumer_t::checkid_immediate(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
127 return checkid_(mode_checkid_immediate,identity,return_to,trust_root,ext); 118 return checkid_(mode_checkid_immediate,identity,return_to,trust_root,ext);
128 } 119 }
129 string consumer_t::checkid_setup(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 120 string consumer_t::checkid_setup(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
130 return checkid_(mode_checkid_setup,identity,return_to,trust_root,ext); 121 return checkid_(mode_checkid_setup,identity,return_to,trust_root,ext);
131 } 122 }
132 string consumer_t::checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 123 string consumer_t::checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
133 params_t p; 124 params_t p;
134 if(mode==mode_checkid_immediate) 125 if(mode==mode_checkid_immediate)
135 p["mode"]="checkid_immediate"; 126 p["mode"]="checkid_immediate";
136 else if(mode==mode_checkid_setup) 127 else if(mode==mode_checkid_setup)
137 p["mode"]="checkid_setup"; 128 p["mode"]="checkid_setup";
138 else 129 else
139 throw bad_input(OPKELE_CP_ "unknown checkid_* mode"); 130 throw bad_input(OPKELE_CP_ "unknown checkid_* mode");
140 string iurl = canonicalize(identity); 131 string iurl = canonicalize(identity);
141 string server, delegate; 132 string server, delegate;
142 retrieve_links(iurl,server,delegate); 133 retrieve_links(iurl,server,delegate);
143 p["identity"] = delegate.empty()?iurl:delegate; 134 p["identity"] = delegate.empty()?iurl:delegate;
144 if(!trust_root.empty()) 135 if(!trust_root.empty())
145 p["trust_root"] = trust_root; 136 p["trust_root"] = trust_root;
146 p["return_to"] = return_to; 137 p["return_to"] = return_to;
147 try { 138 try {
148 try { 139 try {
149 string ah = find_assoc(server)->handle(); 140 string ah = find_assoc(server)->handle();
150 p["assoc_handle"] = ah; 141 p["assoc_handle"] = ah;
151 }catch(failed_lookup& fl) { 142 }catch(failed_lookup& fl) {
152 string ah = associate(server)->handle(); 143 string ah = associate(server)->handle();
153 p["assoc_handle"] = ah; 144 p["assoc_handle"] = ah;
154 } 145 }
155 }catch(exception& e) { } 146 }catch(exception& e) { }
156 if(ext) ext->checkid_hook(p,identity); 147 if(ext) ext->checkid_hook(p,identity);
157 return p.append_query(server); 148 return p.append_query(server);
158 } 149 }
159 150
160 void consumer_t::id_res(const params_t& pin,const string& identity,extension_t *ext) { 151 void consumer_t::id_res(const params_t& pin,const string& identity,extension_t *ext) {
161 if(pin.has_param("openid.user_setup_url")) 152 if(pin.has_param("openid.user_setup_url"))
162 throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url")); 153 throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url"));
163 string server,delegate; 154 string server,delegate;
164 retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate); 155 retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate);
165 params_t ps; 156 params_t ps;
166 try { 157 try {
167 assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle")); 158 assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle"));
168 const string& sigenc = pin.get_param("openid.sig"); 159 const string& sigenc = pin.get_param("openid.sig");
169 mimetic::Base64::Decoder b;
170 vector<unsigned char> sig; 160 vector<unsigned char> sig;
171 mimetic::decode( 161 util::decode_base64(sigenc,sig);
172 sigenc.begin(),sigenc.end(), b,
173 back_insert_iterator<vector<unsigned char> >(sig) );
174 const string& slist = pin.get_param("openid.signed"); 162 const string& slist = pin.get_param("openid.signed");
175 string kv; 163 string kv;
176 string::size_type p = 0; 164 string::size_type p = 0;
177 while(true) { 165 while(true) {
178 string::size_type co = slist.find(',',p); 166 string::size_type co = slist.find(',',p);
179 string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); 167 string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p);
180 kv += f; 168 kv += f;
181 kv += ':'; 169 kv += ':';
182 f.insert(0,"openid."); 170 f.insert(0,"openid.");
183 kv += pin.get_param(f); 171 kv += pin.get_param(f);
184 kv += '\n'; 172 kv += '\n';
185 if(ext) ps[f.substr(sizeof("openid.")-1)] = pin.get_param(f); 173 if(ext) ps[f.substr(sizeof("openid.")-1)] = pin.get_param(f);
186 if(co==string::npos) 174 if(co==string::npos)
187 break; 175 break;
188 p = co+1; 176 p = co+1;
189 } 177 }
190 secret_t secret = assoc->secret(); 178 secret_t secret = assoc->secret();
191 unsigned int md_len = 0; 179 unsigned int md_len = 0;
192 unsigned char *md = HMAC( 180 unsigned char *md = HMAC(
193 EVP_sha1(), 181 EVP_sha1(),
194 &(secret.front()),secret.size(), 182 &(secret.front()),secret.size(),
195 (const unsigned char *)kv.data(),kv.length(), 183 (const unsigned char *)kv.data(),kv.length(),
196 0,&md_len); 184 0,&md_len);
197 if(sig.size()!=md_len || memcmp(&(sig.front()),md,md_len)) 185 if(sig.size()!=md_len || memcmp(&(sig.front()),md,md_len))
198 throw id_res_mismatch(OPKELE_CP_ "signature mismatch"); 186 throw id_res_mismatch(OPKELE_CP_ "signature mismatch");
199 }catch(failed_lookup& e) { /* XXX: more specific? */ 187 }catch(failed_lookup& e) { /* XXX: more specific? */
200 const string& slist = pin.get_param("openid.signed"); 188 const string& slist = pin.get_param("openid.signed");
201 string::size_type pp = 0; 189 string::size_type pp = 0;
202 params_t p; 190 params_t p;
203 while(true) { 191 while(true) {
204 string::size_type co = slist.find(',',pp); 192 string::size_type co = slist.find(',',pp);
205 string f = "openid."; 193 string f = "openid.";
206 f += (co==string::npos)?slist.substr(pp):slist.substr(pp,co-pp); 194 f += (co==string::npos)?slist.substr(pp):slist.substr(pp,co-pp);
207 p[f] = pin.get_param(f); 195 p[f] = pin.get_param(f);
208 if(co==string::npos) 196 if(co==string::npos)
209 break; 197 break;
210 pp = co+1; 198 pp = co+1;
211 } 199 }
212 p["openid.assoc_handle"] = pin.get_param("openid.assoc_handle"); 200 p["openid.assoc_handle"] = pin.get_param("openid.assoc_handle");
213 p["openid.sig"] = pin.get_param("openid.sig"); 201 p["openid.sig"] = pin.get_param("openid.sig");
214 p["openid.signed"] = pin.get_param("openid.signed"); 202 p["openid.signed"] = pin.get_param("openid.signed");
215 try { 203 try {
216 string ih = pin.get_param("openid.invalidate_handle"); 204 string ih = pin.get_param("openid.invalidate_handle");
217 p["openid.invalidate_handle"] = ih; 205 p["openid.invalidate_handle"] = ih;
218 }catch(failed_lookup& fl) { } 206 }catch(failed_lookup& fl) { }
219 try { 207 try {
220 check_authentication(server,p); 208 check_authentication(server,p);
221 }catch(failed_check_authentication& fca) { 209 }catch(failed_check_authentication& fca) {
222 throw id_res_failed(OPKELE_CP_ "failed to check_authentication()"); 210 throw id_res_failed(OPKELE_CP_ "failed to check_authentication()");
223 } 211 }
224 } 212 }
225 if(ext) ext->id_res_hook(pin,ps,identity); 213 if(ext) ext->id_res_hook(pin,ps,identity);
226 } 214 }
227 215
228 void consumer_t::check_authentication(const string& server,const params_t& p) { 216 void consumer_t::check_authentication(const string& server,const params_t& p) {
229 string request = "openid.mode=check_authentication"; 217 string request = "openid.mode=check_authentication";
230 for(params_t::const_iterator i=p.begin();i!=p.end();++i) { 218 for(params_t::const_iterator i=p.begin();i!=p.end();++i) {
231 if(i->first!="openid.mode") { 219 if(i->first!="openid.mode") {
232 request += '&'; 220 request += '&';
233 request += i->first; 221 request += i->first;
234 request += '='; 222 request += '=';
235 request += util::url_encode(i->second); 223 request += util::url_encode(i->second);
236 } 224 }
237 } 225 }
238 curl_t curl = curl_easy_init(); 226 curl_t curl = curl_easy_init();
239 if(!curl) 227 if(!curl)
240 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 228 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
241 string response; 229 string response;
242 CURLcode r; 230 CURLcode r;
243 (r=curl_misc_sets(curl)) 231 (r=curl_misc_sets(curl))
244 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) 232 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str()))
245 || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) 233 || (r=curl_easy_setopt(curl,CURLOPT_POST,1))
246 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) 234 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data()))
247 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) 235 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length()))
248 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) 236 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
249 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) 237 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response))
250 ; 238 ;
251 if(r) 239 if(r)
252 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 240 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
253 if(r=curl_easy_perform(curl)) 241 if(r=curl_easy_perform(curl))
254 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 242 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
255 params_t pp; pp.parse_keyvalues(response); 243 params_t pp; pp.parse_keyvalues(response);
256 if(pp.has_param("invalidate_handle")) 244 if(pp.has_param("invalidate_handle"))
257 invalidate_assoc(server,pp.get_param("invalidate_handle")); 245 invalidate_assoc(server,pp.get_param("invalidate_handle"));
258 if(pp.has_param("is_valid")) { 246 if(pp.has_param("is_valid")) {
259 if(pp.get_param("is_valid")=="true") 247 if(pp.get_param("is_valid")=="true")
260 return; 248 return;
261 }else if(pp.has_param("lifetime")) { 249 }else if(pp.has_param("lifetime")) {
262 if(util::string_to_long(pp.get_param("lifetime"))) 250 if(util::string_to_long(pp.get_param("lifetime")))
263 return; 251 return;
264 } 252 }
265 throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); 253 throw failed_check_authentication(OPKELE_CP_ "failed to verify response");
266 } 254 }
267 255
268 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { 256 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) {
269 server.erase(); 257 server.erase();
270 delegate.erase(); 258 delegate.erase();
271 curl_t curl = curl_easy_init(); 259 curl_t curl = curl_easy_init();
272 if(!curl) 260 if(!curl)
273 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 261 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
274 string html; 262 string html;
275 CURLcode r; 263 CURLcode r;
276 (r=curl_misc_sets(curl)) 264 (r=curl_misc_sets(curl))
277 || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str())) 265 || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str()))
278 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) 266 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
279 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html)) 267 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html))
280 ; 268 ;
281 if(r) 269 if(r)
282 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 270 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
283 r = curl_easy_perform(curl); 271 r = curl_easy_perform(curl);
284 if(r && r!=CURLE_WRITE_ERROR) 272 if(r && r!=CURLE_WRITE_ERROR)
285 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 273 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
286 pcrepp::Pcre bre("<body\\b",PCRE_CASELESS); 274 pcrepp::Pcre bre("<body\\b",PCRE_CASELESS);
287 // strip out everything past body 275 // strip out everything past body
288 if(bre.search(html)) 276 if(bre.search(html))
289 html.erase(bre.get_match_start()); 277 html.erase(bre.get_match_start());
290 pcrepp::Pcre hdre("<head[^>]*>",PCRE_CASELESS); 278 pcrepp::Pcre hdre("<head[^>]*>",PCRE_CASELESS);
291 if(!hdre.search(html)) 279 if(!hdre.search(html))
292 throw bad_input(OPKELE_CP_ "failed to find head"); 280 throw bad_input(OPKELE_CP_ "failed to find head");
293 html.erase(0,hdre.get_match_end()+1); 281 html.erase(0,hdre.get_match_end()+1);
294 pcrepp::Pcre lre("<link\\b([^>]+)>",PCRE_CASELESS), 282 pcrepp::Pcre lre("<link\\b([^>]+)>",PCRE_CASELESS),
295 rre("\\brel=['\"]([^'\"]+)['\"]",PCRE_CASELESS), 283 rre("\\brel=['\"]([^'\"]+)['\"]",PCRE_CASELESS),
296 hre("\\bhref=['\"]([^'\"]+)['\"]",PCRE_CASELESS); 284 hre("\\bhref=['\"]([^'\"]+)['\"]",PCRE_CASELESS);
297 while(lre.search(html)) { 285 while(lre.search(html)) {
298 string attrs = lre[0]; 286 string attrs = lre[0];
299 html.erase(0,lre.get_match_end()+1); 287 html.erase(0,lre.get_match_end()+1);
300 if(!(rre.search(attrs)&&hre.search(attrs))) 288 if(!(rre.search(attrs)&&hre.search(attrs)))
301 continue; 289 continue;
302 if(rre[0]=="openid.server") { 290 if(rre[0]=="openid.server") {
303 server = hre[0]; 291 server = hre[0];
304 if(!delegate.empty()) 292 if(!delegate.empty())
305 break; 293 break;
306 }else if(rre[0]=="openid.delegate") { 294 }else if(rre[0]=="openid.delegate") {
307 delegate = hre[0]; 295 delegate = hre[0];
308 if(!server.empty()) 296 if(!server.empty())
309 break; 297 break;
310 } 298 }
311 } 299 }
312 if(server.empty()) 300 if(server.empty())
313 throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration"); 301 throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration");
314 } 302 }
315 303
316 assoc_t consumer_t::find_assoc(const string& server) { 304 assoc_t consumer_t::find_assoc(const string& server) {
317 throw failed_lookup(OPKELE_CP_ "no find_assoc() provided"); 305 throw failed_lookup(OPKELE_CP_ "no find_assoc() provided");
318 } 306 }
319 307
320 string consumer_t::canonicalize(const string& url) { 308 string consumer_t::canonicalize(const string& url) {
321 string rv = url; 309 string rv = url;
322 // strip leading and trailing spaces 310 // strip leading and trailing spaces
323 string::size_type i = rv.find_first_not_of(" \t\r\n"); 311 string::size_type i = rv.find_first_not_of(" \t\r\n");
324 if(i==string::npos) 312 if(i==string::npos)
325 throw bad_input(OPKELE_CP_ "empty URL"); 313 throw bad_input(OPKELE_CP_ "empty URL");
326 if(i) 314 if(i)
327 rv.erase(0,i); 315 rv.erase(0,i);
328 i = rv.find_last_not_of(" \t\r\n"); 316 i = rv.find_last_not_of(" \t\r\n");
329 assert(i!=string::npos); 317 assert(i!=string::npos);
330 if(i<(rv.length()-1)) 318 if(i<(rv.length()-1))
331 rv.erase(i+1); 319 rv.erase(i+1);
332 // add missing http:// 320 // add missing http://
333 i = rv.find("://"); 321 i = rv.find("://");
334 if(i==string::npos) { // primitive. but do we need more? 322 if(i==string::npos) { // primitive. but do we need more?
335 rv.insert(0,"http://"); 323 rv.insert(0,"http://");
336 i = sizeof("http://")-1; 324 i = sizeof("http://")-1;
337 }else{ 325 }else{
338 i += sizeof("://")-1; 326 i += sizeof("://")-1;
339 } 327 }
340 string::size_type qm = rv.find('?',i); 328 string::size_type qm = rv.find('?',i);
341 string::size_type sl = rv.find('/',i); 329 string::size_type sl = rv.find('/',i);
342 if(qm!=string::npos) { 330 if(qm!=string::npos) {
343 if(sl==string::npos || sl>qm) 331 if(sl==string::npos || sl>qm)
344 rv.insert(qm,1,'/'); 332 rv.insert(qm,1,'/');
345 }else{ 333 }else{
346 if(sl==string::npos) 334 if(sl==string::npos)
347 rv += '/'; 335 rv += '/';
348 } 336 }
349 return rv; 337 return rv;
350 } 338 }
351 339
352} 340}