|
|
|
@@ -187,129 +187,128 @@ class example_rp_t : public opkele::prequeue_RP { |
187 | /* Session perisistent store */ |
187 | /* Session perisistent store */ |
188 | |
188 | |
189 | void begin_queueing() { |
189 | void begin_queueing() { |
190 | assert(as_id>=0); |
190 | assert(as_id>=0); |
191 | DOUT_("Resetting queue for session '" << htc.get_value() << "'/" << as_id); |
191 | DOUT_("Resetting queue for session '" << htc.get_value() << "'/" << as_id); |
192 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
192 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
193 | "DELETE FROM endpoints_queue" |
193 | "DELETE FROM endpoints_queue" |
194 | " WHERE as_id=%ld", |
194 | " WHERE as_id=%ld", |
195 | as_id); |
195 | as_id); |
196 | db.exec(S); |
196 | db.exec(S); |
197 | } |
197 | } |
198 | |
198 | |
199 | void queue_endpoint(const opkele::openid_endpoint_t& ep) { |
199 | void queue_endpoint(const opkele::openid_endpoint_t& ep) { |
200 | assert(as_id>=0); |
200 | assert(as_id>=0); |
201 | DOUT_("Queueing endpoint " << ep.claimed_id << " : " << ep.local_id << " @ " << ep.uri); |
201 | DOUT_("Queueing endpoint " << ep.claimed_id << " : " << ep.local_id << " @ " << ep.uri); |
202 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
202 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
203 | "INSERT INTO endpoints_queue" |
203 | "INSERT INTO endpoints_queue" |
204 | " (as_id,eq_ctime,eq_ordinal,eq_uri,eq_claimed_id,eq_local_id)" |
204 | " (as_id,eq_ctime,eq_ordinal,eq_uri,eq_claimed_id,eq_local_id)" |
205 | " VALUES (%ld,strftime('%%s','now'),%d,%Q,%Q,%Q)", |
205 | " VALUES (%ld,strftime('%%s','now'),%d,%Q,%Q,%Q)", |
206 | as_id,ordinal++, |
206 | as_id,ordinal++, |
207 | ep.uri.c_str(),ep.claimed_id.c_str(),ep.local_id.c_str()); |
207 | ep.uri.c_str(),ep.claimed_id.c_str(),ep.local_id.c_str()); |
208 | db.exec(S); |
208 | db.exec(S); |
209 | } |
209 | } |
210 | |
210 | |
211 | mutable openid_endpoint_t eqtop; |
211 | mutable openid_endpoint_t eqtop; |
212 | mutable bool have_eqtop; |
212 | mutable bool have_eqtop; |
213 | |
213 | |
214 | const openid_endpoint_t& get_endpoint() const { |
214 | const openid_endpoint_t& get_endpoint() const { |
215 | assert(as_id>=0); |
215 | assert(as_id>=0); |
216 | if(!have_eqtop) { |
216 | if(!have_eqtop) { |
217 | sqlite3_mem_t<char*> |
217 | sqlite3_mem_t<char*> |
218 | S = sqlite3_mprintf( |
218 | S = sqlite3_mprintf( |
219 | "SELECT" |
219 | "SELECT" |
220 | " eq_uri, eq_claimed_id, eq_local_id" |
220 | " eq_uri, eq_claimed_id, eq_local_id" |
221 | " FROM endpoints_queue" |
221 | " FROM endpoints_queue" |
222 | " JOIN auth_sessions USING(as_id)" |
222 | " JOIN auth_sessions USING(as_id)" |
223 | " WHERE hts_id=%Q AND as_id=%ld" |
223 | " WHERE hts_id=%Q AND as_id=%ld" |
224 | " ORDER BY eq_ctime,eq_ordinal" |
224 | " ORDER BY eq_ctime,eq_ordinal" |
225 | " LIMIT 1",htc.get_value().c_str(),as_id); |
225 | " LIMIT 1",htc.get_value().c_str(),as_id); |
226 | sqlite3_table_t T; int nr,nc; |
226 | sqlite3_table_t T; int nr,nc; |
227 | db.get_table(S,T,&nr,&nc); |
227 | db.get_table(S,T,&nr,&nc); |
228 | if(nr<1) |
228 | if(nr<1) |
229 | throw opkele::exception(OPKELE_CP_ "No more endpoints queued"); |
229 | throw opkele::exception(OPKELE_CP_ "No more endpoints queued"); |
230 | assert(nr==1); assert(nc==3); |
230 | assert(nr==1); assert(nc==3); |
231 | eqtop.uri = T.get(1,0,nc); |
231 | eqtop.uri = T.get(1,0,nc); |
232 | eqtop.claimed_id = T.get(1,1,nc); |
232 | eqtop.claimed_id = T.get(1,1,nc); |
233 | eqtop.local_id = T.get(1,2,nc); |
233 | eqtop.local_id = T.get(1,2,nc); |
234 | have_eqtop = true; |
234 | have_eqtop = true; |
235 | } |
235 | } |
236 | return eqtop; |
236 | return eqtop; |
237 | } |
237 | } |
238 | |
238 | |
239 | void next_endpoint() { |
239 | void next_endpoint() { |
240 | assert(as_id>=0); |
240 | assert(as_id>=0); |
241 | get_endpoint(); |
241 | get_endpoint(); |
242 | have_eqtop = false; |
242 | have_eqtop = false; |
243 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
243 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
244 | "DELETE FROM endpoints_queue" |
244 | "DELETE FROM endpoints_queue" |
245 | " WHERE as_id=%ld AND eq_uri=%Q AND eq_local_id=%Q", |
245 | " WHERE as_id=%ld AND eq_uri=%Q AND eq_local_id=%Q", |
246 | htc.get_value().c_str(),as_id, |
246 | htc.get_value().c_str(),as_id, |
247 | eqtop.uri.c_str()); |
247 | eqtop.uri.c_str()); |
248 | db.exec(S); |
248 | db.exec(S); |
249 | } |
249 | } |
250 | |
250 | |
251 | mutable string _cid; |
| |
252 | mutable string _nid; |
251 | mutable string _nid; |
253 | |
252 | |
254 | void set_normalized_id(const string& nid) { |
253 | void set_normalized_id(const string& nid) { |
255 | assert(as_id>=0); |
254 | assert(as_id>=0); |
256 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
255 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
257 | "UPDATE auth_sessions" |
256 | "UPDATE auth_sessions" |
258 | " SET as_normalized_id=%Q" |
257 | " SET as_normalized_id=%Q" |
259 | " WHERE hts_id=%Q and as_id=%ld", |
258 | " WHERE hts_id=%Q and as_id=%ld", |
260 | nid.c_str(), |
259 | nid.c_str(), |
261 | htc.get_value().c_str(),as_id); |
260 | htc.get_value().c_str(),as_id); |
262 | db.exec(S); |
261 | db.exec(S); |
263 | _nid = nid; |
262 | _nid = nid; |
264 | } |
263 | } |
265 | const string get_normalized_id() const { |
264 | const string get_normalized_id() const { |
266 | assert(as_id>=0); |
265 | assert(as_id>=0); |
267 | if(_nid.empty()) { |
266 | if(_nid.empty()) { |
268 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
267 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
269 | "SELECT as_normalized_id" |
268 | "SELECT as_normalized_id" |
270 | " FROM" |
269 | " FROM" |
271 | " auth_sessions" |
270 | " auth_sessions" |
272 | " WHERE" |
271 | " WHERE" |
273 | " hts_id=%Q AND as_id=%ld", |
272 | " hts_id=%Q AND as_id=%ld", |
274 | htc.get_value().c_str(),as_id); |
273 | htc.get_value().c_str(),as_id); |
275 | sqlite3_table_t T; int nr,nc; |
274 | sqlite3_table_t T; int nr,nc; |
276 | db.get_table(S,T,&nr,&nc); |
275 | db.get_table(S,T,&nr,&nc); |
277 | assert(nr==1); assert(nc==1); |
276 | assert(nr==1); assert(nc==1); |
278 | _nid = T.get(1,0,nc); |
277 | _nid = T.get(1,0,nc); |
279 | } |
278 | } |
280 | return _nid; |
279 | return _nid; |
281 | } |
280 | } |
282 | |
281 | |
283 | const string get_this_url() const { |
282 | const string get_this_url() const { |
284 | bool s = gw.has_meta("SSL_PROTOCOL_VERSION"); |
283 | bool s = gw.has_meta("SSL_PROTOCOL_VERSION"); |
285 | string rv = s?"https://":"http://"; |
284 | string rv = s?"https://":"http://"; |
286 | rv += gw.http_request_header("Host"); |
285 | rv += gw.http_request_header("Host"); |
287 | const string& port = gw.get_meta("SERVER_PORT"); |
286 | const string& port = gw.get_meta("SERVER_PORT"); |
288 | if( port!=(s?"443":"80") ) { |
287 | if( port!=(s?"443":"80") ) { |
289 | rv += ':'; rv += port; |
288 | rv += ':'; rv += port; |
290 | } |
289 | } |
291 | rv += gw.get_meta("REQUEST_URI"); |
290 | rv += gw.get_meta("REQUEST_URI"); |
292 | return rv; |
291 | return rv; |
293 | } |
292 | } |
294 | |
293 | |
295 | void initiate(const string& usi) { |
294 | void initiate(const string& usi) { |
296 | allocate_asid(); |
295 | allocate_asid(); |
297 | prequeue_RP::initiate(usi); |
296 | prequeue_RP::initiate(usi); |
298 | } |
297 | } |
299 | |
298 | |
300 | string get_self_url() const { |
299 | string get_self_url() const { |
301 | string rv = get_this_url(); |
300 | string rv = get_this_url(); |
302 | string::size_type q = rv.find('?'); |
301 | string::size_type q = rv.find('?'); |
303 | if(q!=string::npos) |
302 | if(q!=string::npos) |
304 | rv.erase(q); |
303 | rv.erase(q); |
305 | return rv; |
304 | return rv; |
306 | } |
305 | } |
307 | |
306 | |
308 | void allocate_asid() { |
307 | void allocate_asid() { |
309 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
308 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
310 | "INSERT INTO auth_sessions (hts_id)" |
309 | "INSERT INTO auth_sessions (hts_id)" |
311 | " VALUES (%Q)", |
310 | " VALUES (%Q)", |
312 | htc.get_value().c_str()); |
311 | htc.get_value().c_str()); |
313 | db.exec(S); |
312 | db.exec(S); |
314 | as_id = sqlite3_last_insert_rowid(db); |
313 | as_id = sqlite3_last_insert_rowid(db); |
315 | DOUT_("Allocated authentication session id "<<as_id); |
314 | DOUT_("Allocated authentication session id "<<as_id); |
|