summaryrefslogtreecommitdiffabout
path: root/lib/consumer.cc
Unidiff
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 f9212ea..d578546 100644
--- a/lib/consumer.cc
+++ b/lib/consumer.cc
@@ -1,439 +1,440 @@
1#include <algorithm> 1#include <algorithm>
2#include <cassert> 2#include <cassert>
3#include <cstring>
3#include <opkele/util.h> 4#include <opkele/util.h>
4#include <opkele/exception.h> 5#include <opkele/exception.h>
5#include <opkele/data.h> 6#include <opkele/data.h>
6#include <opkele/consumer.h> 7#include <opkele/consumer.h>
7#include <openssl/sha.h> 8#include <openssl/sha.h>
8#include <openssl/hmac.h> 9#include <openssl/hmac.h>
9#include <curl/curl.h> 10#include <curl/curl.h>
10 11
11#include <iostream> 12#include <iostream>
12 13
13#include "config.h" 14#include "config.h"
14 15
15#include <pcre.h> 16#include <pcre.h>
16 17
17namespace opkele { 18namespace opkele {
18 using namespace std; 19 using namespace std;
19 20
20 class pcre_matches_t { 21 class pcre_matches_t {
21 public: 22 public:
22 int *_ov; 23 int *_ov;
23 int _s; 24 int _s;
24 25
25 pcre_matches_t() : _ov(0), _s(0) { } 26 pcre_matches_t() : _ov(0), _s(0) { }
26 pcre_matches_t(int s) : _ov(0), _s(s) { 27 pcre_matches_t(int s) : _ov(0), _s(s) {
27 if(_s&1) ++_s; 28 if(_s&1) ++_s;
28 _s += _s>>1; 29 _s += _s>>1;
29 _ov = new int[_s]; 30 _ov = new int[_s];
30 } 31 }
31 ~pcre_matches_t() throw() { if(_ov) delete[] _ov; } 32 ~pcre_matches_t() throw() { if(_ov) delete[] _ov; }
32 33
33 int begin(int i) const { return _ov[i<<1]; } 34 int begin(int i) const { return _ov[i<<1]; }
34 int end(int i) const { return _ov[(i<<1)+1]; } 35 int end(int i) const { return _ov[(i<<1)+1]; }
35 int length(int i) const { int t=i<<1; return _ov[t+1]-_ov[t]; } 36 int length(int i) const { int t=i<<1; return _ov[t+1]-_ov[t]; }
36 }; 37 };
37 38
38 class pcre_t { 39 class pcre_t {
39 public: 40 public:
40 pcre *_p; 41 pcre *_p;
41 42
42 pcre_t() : _p(0) { } 43 pcre_t() : _p(0) { }
43 pcre_t(pcre *p) : _p(p) { } 44 pcre_t(pcre *p) : _p(p) { }
44 pcre_t(const char *re,int opts) : _p(0) { 45 pcre_t(const char *re,int opts) : _p(0) {
45 static const char *errptr; static int erroffset; 46 static const char *errptr; static int erroffset;
46 _p = pcre_compile(re,opts,&errptr,&erroffset,NULL); 47 _p = pcre_compile(re,opts,&errptr,&erroffset,NULL);
47 if(!_p) 48 if(!_p)
48 throw internal_error(OPKELE_CP_ string("Failed to compile regexp: ")+errptr); 49 throw internal_error(OPKELE_CP_ string("Failed to compile regexp: ")+errptr);
49 } 50 }
50 ~pcre_t() throw() { if(_p) (*pcre_free)(_p); } 51 ~pcre_t() throw() { if(_p) (*pcre_free)(_p); }
51 52
52 pcre_t& operator=(pcre *p) { if(_p) (*pcre_free)(_p); _p=p; return *this; } 53 pcre_t& operator=(pcre *p) { if(_p) (*pcre_free)(_p); _p=p; return *this; }
53 54
54 operator const pcre*(void) const { return _p; } 55 operator const pcre*(void) const { return _p; }
55 operator pcre*(void) { return _p; } 56 operator pcre*(void) { return _p; }
56 57
57 int exec(const string& s,pcre_matches_t& m) { 58 int exec(const string& s,pcre_matches_t& m) {
58 if(!_p) 59 if(!_p)
59 throw internal_error(OPKELE_CP_ "Trying to execute absent regexp"); 60 throw internal_error(OPKELE_CP_ "Trying to execute absent regexp");
60 return pcre_exec(_p,NULL,s.c_str(),s.length(),0,0,m._ov,m._s); 61 return pcre_exec(_p,NULL,s.c_str(),s.length(),0,0,m._ov,m._s);
61 } 62 }
62 }; 63 };
63 64
64 class curl_t { 65 class curl_t {
65 public: 66 public:
66 CURL *_c; 67 CURL *_c;
67 68
68 curl_t() : _c(0) { } 69 curl_t() : _c(0) { }
69 curl_t(CURL *c) : _c(c) { } 70 curl_t(CURL *c) : _c(c) { }
70 ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); } 71 ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); }
71 72
72 curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; } 73 curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; }
73 74
74 operator const CURL*(void) const { return _c; } 75 operator const CURL*(void) const { return _c; }
75 operator CURL*(void) { return _c; } 76 operator CURL*(void) { return _c; }
76 }; 77 };
77 78
78 static CURLcode curl_misc_sets(CURL* c) { 79 static CURLcode curl_misc_sets(CURL* c) {
79 CURLcode r; 80 CURLcode r;
80 (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1)) 81 (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1))
81 || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5)) 82 || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5))
82 || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120)) 83 || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120))
83 || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1)) 84 || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1))
84 || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_SRC_VERSION)) 85 || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_SRC_VERSION))
85 || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20)) 86 || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20))
86 #ifdefDISABLE_CURL_SSL_VERIFYHOST 87 #ifdefDISABLE_CURL_SSL_VERIFYHOST
87 || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYHOST,0)) 88 || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYHOST,0))
88#endif 89#endif
89 #ifdefDISABLE_CURL_SSL_VERIFYPEER 90 #ifdefDISABLE_CURL_SSL_VERIFYPEER
90 || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYPEER,0)) 91 || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYPEER,0))
91#endif 92#endif
92 ; 93 ;
93 return r; 94 return r;
94 } 95 }
95 96
96 static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) { 97 static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) {
97 string *str = (string*)stream; 98 string *str = (string*)stream;
98 size_t bytes = size*nmemb; 99 size_t bytes = size*nmemb;
99 size_t get = min(16384-str->length(),bytes); 100 size_t get = min(16384-str->length(),bytes);
100 str->append((const char*)ptr,get); 101 str->append((const char*)ptr,get);
101 return get; 102 return get;
102 } 103 }
103 104
104 assoc_t consumer_t::associate(const string& server) { 105 assoc_t consumer_t::associate(const string& server) {
105 util::dh_t dh = DH_new(); 106 util::dh_t dh = DH_new();
106 if(!dh) 107 if(!dh)
107 throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); 108 throw exception_openssl(OPKELE_CP_ "failed to DH_new()");
108 dh->p = util::dec_to_bignum(data::_default_p); 109 dh->p = util::dec_to_bignum(data::_default_p);
109 dh->g = util::dec_to_bignum(data::_default_g); 110 dh->g = util::dec_to_bignum(data::_default_g);
110 if(!DH_generate_key(dh)) 111 if(!DH_generate_key(dh))
111 throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); 112 throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()");
112 string request = 113 string request =
113 "openid.mode=associate" 114 "openid.mode=associate"
114 "&openid.assoc_type=HMAC-SHA1" 115 "&openid.assoc_type=HMAC-SHA1"
115 "&openid.session_type=DH-SHA1" 116 "&openid.session_type=DH-SHA1"
116 "&openid.dh_consumer_public="; 117 "&openid.dh_consumer_public=";
117 request += util::url_encode(util::bignum_to_base64(dh->pub_key)); 118 request += util::url_encode(util::bignum_to_base64(dh->pub_key));
118 curl_t curl = curl_easy_init(); 119 curl_t curl = curl_easy_init();
119 if(!curl) 120 if(!curl)
120 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 121 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
121 string response; 122 string response;
122 CURLcode r; 123 CURLcode r;
123 (r=curl_misc_sets(curl)) 124 (r=curl_misc_sets(curl))
124 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) 125 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str()))
125 || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) 126 || (r=curl_easy_setopt(curl,CURLOPT_POST,1))
126 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) 127 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data()))
127 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) 128 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length()))
128 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) 129 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
129 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) 130 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response))
130 ; 131 ;
131 if(r) 132 if(r)
132 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 133 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
133 if( (r=curl_easy_perform(curl)) ) 134 if( (r=curl_easy_perform(curl)) )
134 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 135 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
135 params_t p; p.parse_keyvalues(response); 136 params_t p; p.parse_keyvalues(response);
136 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1") 137 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1")
137 throw bad_input(OPKELE_CP_ "unsupported assoc_type"); 138 throw bad_input(OPKELE_CP_ "unsupported assoc_type");
138 string st; 139 string st;
139 if(p.has_param("session_type")) st = p.get_param("session_type"); 140 if(p.has_param("session_type")) st = p.get_param("session_type");
140 if((!st.empty()) && st!="DH-SHA1") 141 if((!st.empty()) && st!="DH-SHA1")
141 throw bad_input(OPKELE_CP_ "unsupported session_type"); 142 throw bad_input(OPKELE_CP_ "unsupported session_type");
142 secret_t secret; 143 secret_t secret;
143 if(st.empty()) { 144 if(st.empty()) {
144 secret.from_base64(p.get_param("mac_key")); 145 secret.from_base64(p.get_param("mac_key"));
145 }else{ 146 }else{
146 util::bignum_t s_pub = util::base64_to_bignum(p.get_param("dh_server_public")); 147 util::bignum_t s_pub = util::base64_to_bignum(p.get_param("dh_server_public"));
147 vector<unsigned char> ck(DH_size(dh)+1); 148 vector<unsigned char> ck(DH_size(dh)+1);
148 unsigned char *ckptr = &(ck.front())+1; 149 unsigned char *ckptr = &(ck.front())+1;
149 int cklen = DH_compute_key(ckptr,s_pub,dh); 150 int cklen = DH_compute_key(ckptr,s_pub,dh);
150 if(cklen<0) 151 if(cklen<0)
151 throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()"); 152 throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()");
152 if(cklen && (*ckptr)&0x80) { 153 if(cklen && (*ckptr)&0x80) {
153 (*(--ckptr)) = 0; ++cklen; 154 (*(--ckptr)) = 0; ++cklen;
154 } 155 }
155 unsigned char key_sha1[SHA_DIGEST_LENGTH]; 156 unsigned char key_sha1[SHA_DIGEST_LENGTH];
156 SHA1(ckptr,cklen,key_sha1); 157 SHA1(ckptr,cklen,key_sha1);
157 secret.enxor_from_base64(key_sha1,p.get_param("enc_mac_key")); 158 secret.enxor_from_base64(key_sha1,p.get_param("enc_mac_key"));
158 } 159 }
159 int expires_in = 0; 160 int expires_in = 0;
160 if(p.has_param("expires_in")) { 161 if(p.has_param("expires_in")) {
161 expires_in = util::string_to_long(p.get_param("expires_in")); 162 expires_in = util::string_to_long(p.get_param("expires_in"));
162 }else if(p.has_param("issued") && p.has_param("expiry")) { 163 }else if(p.has_param("issued") && p.has_param("expiry")) {
163 expires_in = util::w3c_to_time(p.get_param("expiry"))-util::w3c_to_time(p.get_param("issued")); 164 expires_in = util::w3c_to_time(p.get_param("expiry"))-util::w3c_to_time(p.get_param("issued"));
164 }else 165 }else
165 throw bad_input(OPKELE_CP_ "no expiration information"); 166 throw bad_input(OPKELE_CP_ "no expiration information");
166 return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in); 167 return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in);
167 } 168 }
168 169
169 string consumer_t::checkid_immediate(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 170 string consumer_t::checkid_immediate(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
170 return checkid_(mode_checkid_immediate,identity,return_to,trust_root,ext); 171 return checkid_(mode_checkid_immediate,identity,return_to,trust_root,ext);
171 } 172 }
172 string consumer_t::checkid_setup(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 173 string consumer_t::checkid_setup(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
173 return checkid_(mode_checkid_setup,identity,return_to,trust_root,ext); 174 return checkid_(mode_checkid_setup,identity,return_to,trust_root,ext);
174 } 175 }
175 string consumer_t::checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 176 string consumer_t::checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
176 params_t p; 177 params_t p;
177 if(mode==mode_checkid_immediate) 178 if(mode==mode_checkid_immediate)
178 p["mode"]="checkid_immediate"; 179 p["mode"]="checkid_immediate";
179 else if(mode==mode_checkid_setup) 180 else if(mode==mode_checkid_setup)
180 p["mode"]="checkid_setup"; 181 p["mode"]="checkid_setup";
181 else 182 else
182 throw bad_input(OPKELE_CP_ "unknown checkid_* mode"); 183 throw bad_input(OPKELE_CP_ "unknown checkid_* mode");
183 string iurl = canonicalize(identity); 184 string iurl = canonicalize(identity);
184 string server, delegate; 185 string server, delegate;
185 retrieve_links(iurl,server,delegate); 186 retrieve_links(iurl,server,delegate);
186 p["identity"] = delegate.empty()?iurl:delegate; 187 p["identity"] = delegate.empty()?iurl:delegate;
187 if(!trust_root.empty()) 188 if(!trust_root.empty())
188 p["trust_root"] = trust_root; 189 p["trust_root"] = trust_root;
189 p["return_to"] = return_to; 190 p["return_to"] = return_to;
190 try { 191 try {
191 string ah = find_assoc(server)->handle(); 192 string ah = find_assoc(server)->handle();
192 p["assoc_handle"] = ah; 193 p["assoc_handle"] = ah;
193 }catch(failed_lookup& fl) { 194 }catch(failed_lookup& fl) {
194 string ah = associate(server)->handle(); 195 string ah = associate(server)->handle();
195 p["assoc_handle"] = ah; 196 p["assoc_handle"] = ah;
196 } 197 }
197 if(ext) ext->checkid_hook(p,identity); 198 if(ext) ext->checkid_hook(p,identity);
198 return p.append_query(server); 199 return p.append_query(server);
199 } 200 }
200 201
201 void consumer_t::id_res(const params_t& pin,const string& identity,extension_t *ext) { 202 void consumer_t::id_res(const params_t& pin,const string& identity,extension_t *ext) {
202 if(pin.has_param("openid.user_setup_url")) 203 if(pin.has_param("openid.user_setup_url"))
203 throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url")); 204 throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url"));
204 string server,delegate; 205 string server,delegate;
205 retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate); 206 retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate);
206 params_t ps; 207 params_t ps;
207 try { 208 try {
208 assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle")); 209 assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle"));
209 if(assoc->is_expired()) /* TODO: or should I throw some other exception to force programmer fix his implementation? */ 210 if(assoc->is_expired()) /* TODO: or should I throw some other exception to force programmer fix his implementation? */
210 throw failed_lookup(OPKELE_CP_ "retrieve_assoc() has returned expired handle"); 211 throw failed_lookup(OPKELE_CP_ "retrieve_assoc() has returned expired handle");
211 const string& sigenc = pin.get_param("openid.sig"); 212 const string& sigenc = pin.get_param("openid.sig");
212 vector<unsigned char> sig; 213 vector<unsigned char> sig;
213 util::decode_base64(sigenc,sig); 214 util::decode_base64(sigenc,sig);
214 const string& slist = pin.get_param("openid.signed"); 215 const string& slist = pin.get_param("openid.signed");
215 string kv; 216 string kv;
216 string::size_type p = 0; 217 string::size_type p = 0;
217 while(true) { 218 while(true) {
218 string::size_type co = slist.find(',',p); 219 string::size_type co = slist.find(',',p);
219 string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); 220 string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p);
220 kv += f; 221 kv += f;
221 kv += ':'; 222 kv += ':';
222 f.insert(0,"openid."); 223 f.insert(0,"openid.");
223 kv += pin.get_param(f); 224 kv += pin.get_param(f);
224 kv += '\n'; 225 kv += '\n';
225 if(ext) ps[f.substr(sizeof("openid.")-1)] = pin.get_param(f); 226 if(ext) ps[f.substr(sizeof("openid.")-1)] = pin.get_param(f);
226 if(co==string::npos) 227 if(co==string::npos)
227 break; 228 break;
228 p = co+1; 229 p = co+1;
229 } 230 }
230 secret_t secret = assoc->secret(); 231 secret_t secret = assoc->secret();
231 unsigned int md_len = 0; 232 unsigned int md_len = 0;
232 unsigned char *md = HMAC( 233 unsigned char *md = HMAC(
233 EVP_sha1(), 234 EVP_sha1(),
234 &(secret.front()),secret.size(), 235 &(secret.front()),secret.size(),
235 (const unsigned char *)kv.data(),kv.length(), 236 (const unsigned char *)kv.data(),kv.length(),
236 0,&md_len); 237 0,&md_len);
237 if(sig.size()!=md_len || memcmp(&(sig.front()),md,md_len)) 238 if(sig.size()!=md_len || memcmp(&(sig.front()),md,md_len))
238 throw id_res_mismatch(OPKELE_CP_ "signature mismatch"); 239 throw id_res_mismatch(OPKELE_CP_ "signature mismatch");
239 }catch(failed_lookup& e) { /* XXX: more specific? */ 240 }catch(failed_lookup& e) { /* XXX: more specific? */
240 const string& slist = pin.get_param("openid.signed"); 241 const string& slist = pin.get_param("openid.signed");
241 string::size_type pp = 0; 242 string::size_type pp = 0;
242 params_t p; 243 params_t p;
243 while(true) { 244 while(true) {
244 string::size_type co = slist.find(',',pp); 245 string::size_type co = slist.find(',',pp);
245 string f = "openid."; 246 string f = "openid.";
246 f += (co==string::npos)?slist.substr(pp):slist.substr(pp,co-pp); 247 f += (co==string::npos)?slist.substr(pp):slist.substr(pp,co-pp);
247 p[f] = pin.get_param(f); 248 p[f] = pin.get_param(f);
248 if(co==string::npos) 249 if(co==string::npos)
249 break; 250 break;
250 pp = co+1; 251 pp = co+1;
251 } 252 }
252 p["openid.assoc_handle"] = pin.get_param("openid.assoc_handle"); 253 p["openid.assoc_handle"] = pin.get_param("openid.assoc_handle");
253 p["openid.sig"] = pin.get_param("openid.sig"); 254 p["openid.sig"] = pin.get_param("openid.sig");
254 p["openid.signed"] = pin.get_param("openid.signed"); 255 p["openid.signed"] = pin.get_param("openid.signed");
255 try { 256 try {
256 string ih = pin.get_param("openid.invalidate_handle"); 257 string ih = pin.get_param("openid.invalidate_handle");
257 p["openid.invalidate_handle"] = ih; 258 p["openid.invalidate_handle"] = ih;
258 }catch(failed_lookup& fl) { } 259 }catch(failed_lookup& fl) { }
259 try { 260 try {
260 check_authentication(server,p); 261 check_authentication(server,p);
261 }catch(failed_check_authentication& fca) { 262 }catch(failed_check_authentication& fca) {
262 throw id_res_failed(OPKELE_CP_ "failed to check_authentication()"); 263 throw id_res_failed(OPKELE_CP_ "failed to check_authentication()");
263 } 264 }
264 } 265 }
265 if(ext) ext->id_res_hook(pin,ps,identity); 266 if(ext) ext->id_res_hook(pin,ps,identity);
266 } 267 }
267 268
268 void consumer_t::check_authentication(const string& server,const params_t& p) { 269 void consumer_t::check_authentication(const string& server,const params_t& p) {
269 string request = "openid.mode=check_authentication"; 270 string request = "openid.mode=check_authentication";
270 for(params_t::const_iterator i=p.begin();i!=p.end();++i) { 271 for(params_t::const_iterator i=p.begin();i!=p.end();++i) {
271 if(i->first!="openid.mode") { 272 if(i->first!="openid.mode") {
272 request += '&'; 273 request += '&';
273 request += i->first; 274 request += i->first;
274 request += '='; 275 request += '=';
275 request += util::url_encode(i->second); 276 request += util::url_encode(i->second);
276 } 277 }
277 } 278 }
278 curl_t curl = curl_easy_init(); 279 curl_t curl = curl_easy_init();
279 if(!curl) 280 if(!curl)
280 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 281 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
281 string response; 282 string response;
282 CURLcode r; 283 CURLcode r;
283 (r=curl_misc_sets(curl)) 284 (r=curl_misc_sets(curl))
284 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) 285 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str()))
285 || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) 286 || (r=curl_easy_setopt(curl,CURLOPT_POST,1))
286 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) 287 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data()))
287 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) 288 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length()))
288 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) 289 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
289 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) 290 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response))
290 ; 291 ;
291 if(r) 292 if(r)
292 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 293 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
293 if( (r=curl_easy_perform(curl)) ) 294 if( (r=curl_easy_perform(curl)) )
294 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 295 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
295 params_t pp; pp.parse_keyvalues(response); 296 params_t pp; pp.parse_keyvalues(response);
296 if(pp.has_param("invalidate_handle")) 297 if(pp.has_param("invalidate_handle"))
297 invalidate_assoc(server,pp.get_param("invalidate_handle")); 298 invalidate_assoc(server,pp.get_param("invalidate_handle"));
298 if(pp.has_param("is_valid")) { 299 if(pp.has_param("is_valid")) {
299 if(pp.get_param("is_valid")=="true") 300 if(pp.get_param("is_valid")=="true")
300 return; 301 return;
301 }else if(pp.has_param("lifetime")) { 302 }else if(pp.has_param("lifetime")) {
302 if(util::string_to_long(pp.get_param("lifetime"))) 303 if(util::string_to_long(pp.get_param("lifetime")))
303 return; 304 return;
304 } 305 }
305 throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); 306 throw failed_check_authentication(OPKELE_CP_ "failed to verify response");
306 } 307 }
307 308
308 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { 309 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) {
309 server.erase(); 310 server.erase();
310 delegate.erase(); 311 delegate.erase();
311 curl_t curl = curl_easy_init(); 312 curl_t curl = curl_easy_init();
312 if(!curl) 313 if(!curl)
313 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 314 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
314 string html; 315 string html;
315 CURLcode r; 316 CURLcode r;
316 (r=curl_misc_sets(curl)) 317 (r=curl_misc_sets(curl))
317 || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str())) 318 || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str()))
318 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) 319 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
319 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html)) 320 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html))
320 ; 321 ;
321 if(r) 322 if(r)
322 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 323 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
323 r = curl_easy_perform(curl); 324 r = curl_easy_perform(curl);
324 if(r && r!=CURLE_WRITE_ERROR) 325 if(r && r!=CURLE_WRITE_ERROR)
325 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 326 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
326 static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>", 327 static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>",
327 *re_lre = "<\\s*link\\b([^>]+)>", 328 *re_lre = "<\\s*link\\b([^>]+)>",
328 *re_rre = "\\brel\\s*=\\s*['\"]([^'\"]+)['\"]", 329 *re_rre = "\\brel\\s*=\\s*['\"]([^'\"]+)['\"]",
329 *re_hre = "\\bhref\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]"; 330 *re_hre = "\\bhref\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]";
330 pcre_matches_t m1(3), m2(3); 331 pcre_matches_t m1(3), m2(3);
331 pcre_t bre(re_bre,PCRE_CASELESS); 332 pcre_t bre(re_bre,PCRE_CASELESS);
332 if(bre.exec(html,m1)>0) 333 if(bre.exec(html,m1)>0)
333 html.erase(m1.begin(0)); 334 html.erase(m1.begin(0));
334 pcre_t hdre(re_hdre,PCRE_CASELESS); 335 pcre_t hdre(re_hdre,PCRE_CASELESS);
335 if(hdre.exec(html,m1)<=0) 336 if(hdre.exec(html,m1)<=0)
336 throw bad_input(OPKELE_CP_ "failed to find <head>"); 337 throw bad_input(OPKELE_CP_ "failed to find <head>");
337 html.erase(0,m1.end(0)+1); 338 html.erase(0,m1.end(0)+1);
338 pcre_t lre(re_lre,PCRE_CASELESS), rre(re_rre,PCRE_CASELESS), hre(re_hre,PCRE_CASELESS); 339 pcre_t lre(re_lre,PCRE_CASELESS), rre(re_rre,PCRE_CASELESS), hre(re_hre,PCRE_CASELESS);
339 bool gotit = false; 340 bool gotit = false;
340 while( (!gotit) && lre.exec(html,m1)>=2 ) { 341 while( (!gotit) && lre.exec(html,m1)>=2 ) {
341 static const char *whitespace = " \t"; 342 static const char *whitespace = " \t";
342 string attrs(html,m1.begin(1),m1.length(1)); 343 string attrs(html,m1.begin(1),m1.length(1));
343 html.erase(0,m1.end(0)+1); 344 html.erase(0,m1.end(0)+1);
344 if(!( rre.exec(attrs,m1)>=2 && hre.exec(attrs,m2)>=2 )) 345 if(!( rre.exec(attrs,m1)>=2 && hre.exec(attrs,m2)>=2 ))
345 continue; 346 continue;
346 string rels(attrs,m1.begin(1),m1.length(1)); 347 string rels(attrs,m1.begin(1),m1.length(1));
347 for(string::size_type ns = rels.find_first_not_of(whitespace); 348 for(string::size_type ns = rels.find_first_not_of(whitespace);
348 ns!=string::npos; 349 ns!=string::npos;
349 ns=rels.find_first_not_of(whitespace,ns)) { 350 ns=rels.find_first_not_of(whitespace,ns)) {
350 string::size_type s = rels.find_first_of(whitespace,ns); 351 string::size_type s = rels.find_first_of(whitespace,ns);
351 string rel; 352 string rel;
352 if(s==string::npos) { 353 if(s==string::npos) {
353 rel.assign(rels,ns,string::npos); 354 rel.assign(rels,ns,string::npos);
354 ns=string::npos; 355 ns=string::npos;
355 }else{ 356 }else{
356 rel.assign(rels,ns,s-ns); 357 rel.assign(rels,ns,s-ns);
357 ns=s; 358 ns=s;
358 } 359 }
359 if(rel=="openid.server") { 360 if(rel=="openid.server") {
360 server.assign(attrs,m2.begin(1),m2.length(1)); 361 server.assign(attrs,m2.begin(1),m2.length(1));
361 if(!delegate.empty()) { 362 if(!delegate.empty()) {
362 gotit = true; 363 gotit = true;
363 break; 364 break;
364 } 365 }
365 }else if(rel=="openid.delegate") { 366 }else if(rel=="openid.delegate") {
366 delegate.assign(attrs,m2.begin(1),m2.length(1)); 367 delegate.assign(attrs,m2.begin(1),m2.length(1));
367 if(!server.empty()) { 368 if(!server.empty()) {
368 gotit = true; 369 gotit = true;
369 break; 370 break;
370 } 371 }
371 } 372 }
372 if(ns==string::npos) break; 373 if(ns==string::npos) break;
373 } 374 }
374 } 375 }
375 if(server.empty()) 376 if(server.empty())
376 throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration"); 377 throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration");
377 } 378 }
378 379
379 assoc_t consumer_t::find_assoc(const string& server) { 380 assoc_t consumer_t::find_assoc(const string& server) {
380 throw failed_lookup(OPKELE_CP_ "no find_assoc() provided"); 381 throw failed_lookup(OPKELE_CP_ "no find_assoc() provided");
381 } 382 }
382 383
383 string consumer_t::normalize(const string& url) { 384 string consumer_t::normalize(const string& url) {
384 string rv = url; 385 string rv = url;
385 // strip leading and trailing spaces 386 // strip leading and trailing spaces
386 string::size_type i = rv.find_first_not_of(" \t\r\n"); 387 string::size_type i = rv.find_first_not_of(" \t\r\n");
387 if(i==string::npos) 388 if(i==string::npos)
388 throw bad_input(OPKELE_CP_ "empty URL"); 389 throw bad_input(OPKELE_CP_ "empty URL");
389 if(i) 390 if(i)
390 rv.erase(0,i); 391 rv.erase(0,i);
391 i = rv.find_last_not_of(" \t\r\n"); 392 i = rv.find_last_not_of(" \t\r\n");
392 assert(i!=string::npos); 393 assert(i!=string::npos);
393 if(i<(rv.length()-1)) 394 if(i<(rv.length()-1))
394 rv.erase(i+1); 395 rv.erase(i+1);
395 // add missing http:// 396 // add missing http://
396 i = rv.find("://"); 397 i = rv.find("://");
397 if(i==string::npos) { // primitive. but do we need more? 398 if(i==string::npos) { // primitive. but do we need more?
398 rv.insert(0,"http://"); 399 rv.insert(0,"http://");
399 i = sizeof("http://")-1; 400 i = sizeof("http://")-1;
400 }else{ 401 }else{
401 i += sizeof("://")-1; 402 i += sizeof("://")-1;
402 } 403 }
403 string::size_type qm = rv.find('?',i); 404 string::size_type qm = rv.find('?',i);
404 string::size_type sl = rv.find('/',i); 405 string::size_type sl = rv.find('/',i);
405 if(qm!=string::npos) { 406 if(qm!=string::npos) {
406 if(sl==string::npos || sl>qm) 407 if(sl==string::npos || sl>qm)
407 rv.insert(qm,1,'/'); 408 rv.insert(qm,1,'/');
408 }else{ 409 }else{
409 if(sl==string::npos) 410 if(sl==string::npos)
410 rv += '/'; 411 rv += '/';
411 } 412 }
412 return rv; 413 return rv;
413 } 414 }
414 415
415 string consumer_t::canonicalize(const string& url) { 416 string consumer_t::canonicalize(const string& url) {
416 string rv = normalize(url); 417 string rv = normalize(url);
417 curl_t curl = curl_easy_init(); 418 curl_t curl = curl_easy_init();
418 if(!curl) 419 if(!curl)
419 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 420 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
420 string html; 421 string html;
421 CURLcode r; 422 CURLcode r;
422 (r=curl_misc_sets(curl)) 423 (r=curl_misc_sets(curl))
423 || (r=curl_easy_setopt(curl,CURLOPT_URL,rv.c_str())) 424 || (r=curl_easy_setopt(curl,CURLOPT_URL,rv.c_str()))
424 || (r=curl_easy_setopt(curl,CURLOPT_NOBODY,1)) 425 || (r=curl_easy_setopt(curl,CURLOPT_NOBODY,1))
425 ; 426 ;
426 if(r) 427 if(r)
427 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 428 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
428 r = curl_easy_perform(curl); 429 r = curl_easy_perform(curl);
429 if(r) 430 if(r)
430 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 431 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
431 const char *eu = 0; 432 const char *eu = 0;
432 r = curl_easy_getinfo(curl,CURLINFO_EFFECTIVE_URL,&eu); 433 r = curl_easy_getinfo(curl,CURLINFO_EFFECTIVE_URL,&eu);
433 if(r) 434 if(r)
434 throw exception_curl(OPKELE_CP_ "failed to curl_easy_getinfo(..CURLINFO_EFFECTIVE_URL..)",r); 435 throw exception_curl(OPKELE_CP_ "failed to curl_easy_getinfo(..CURLINFO_EFFECTIVE_URL..)",r);
435 rv = eu; 436 rv = eu;
436 return normalize(rv); 437 return normalize(rv);
437 } 438 }
438 439
439} 440}