|
|
|
@@ -29,66 +29,66 @@ using namespace opkele; |
29 | # define DUMBTHROW throw opkele::dumb_RP(OPKELE_CP_ "This RP is dumb") |
29 | # define DUMBTHROW throw opkele::dumb_RP(OPKELE_CP_ "This RP is dumb") |
30 | #else |
30 | #else |
31 | # define DUMBTHROW (void)0 |
31 | # define DUMBTHROW (void)0 |
32 | #endif |
32 | #endif |
33 | |
33 | |
34 | class rpdb_t : public sqlite3_t { |
34 | class rpdb_t : public sqlite3_t { |
35 | public: |
35 | public: |
36 | rpdb_t() |
36 | rpdb_t() |
37 | : sqlite3_t("/tmp/RP.db") { |
37 | : sqlite3_t("/tmp/RP.db") { |
38 | assert(_D); |
38 | assert(_D); |
39 | char **resp; int nrow,ncol; char *errm; |
39 | char **resp; int nrow,ncol; char *errm; |
40 | if(sqlite3_get_table( |
40 | if(sqlite3_get_table( |
41 | _D,"SELECT a_op FROM assoc LIMIT 0", |
41 | _D,"SELECT a_op FROM assoc LIMIT 0", |
42 | &resp,&nrow,&ncol,&errm)!=SQLITE_OK) { |
42 | &resp,&nrow,&ncol,&errm)!=SQLITE_OK) { |
43 | extern const char *__RP_db_bootstrap; |
43 | extern const char *__RP_db_bootstrap; |
44 | DOUT_("Bootstrapping DB"); |
44 | DOUT_("Bootstrapping DB"); |
45 | if(sqlite3_exec(_D,__RP_db_bootstrap,NULL,NULL,&errm)!=SQLITE_OK) |
45 | if(sqlite3_exec(_D,__RP_db_bootstrap,NULL,NULL,&errm)!=SQLITE_OK) |
46 | throw opkele::exception(OPKELE_CP_ string("Failed to bootstrap SQLite database: ")+errm); |
46 | throw opkele::exception(OPKELE_CP_ string("Failed to bootstrap SQLite database: ")+errm); |
47 | }else |
47 | }else |
48 | sqlite3_free_table(resp); |
48 | sqlite3_free_table(resp); |
49 | |
49 | |
50 | } |
50 | } |
51 | }; |
51 | }; |
52 | |
52 | |
53 | class example_rp_t : public opkele::prequeue_RP { |
53 | class example_rp_t : public opkele::prequeue_RP { |
54 | public: |
54 | public: |
55 | mutable rpdb_t db; |
55 | mutable rpdb_t db; |
56 | kingate::cookie htc; |
56 | kingate::cookie htc; |
57 | long as_id; |
57 | long as_id; |
58 | int ordinal; |
58 | int ordinal; |
59 | kingate::cgi_gateway& gw; |
59 | kingate::cgi_gateway& gw; |
60 | |
60 | |
61 | example_rp_t(kingate::cgi_gateway& gw) |
61 | example_rp_t(kingate::cgi_gateway& g) |
62 | : ordinal(0), have_eqtop(false), gw(gw), as_id(-1) { |
62 | : as_id(-1), ordinal(0), gw(g), have_eqtop(false) { |
63 | try { |
63 | try { |
64 | htc = gw.cookies.get_cookie("ht_session"); |
64 | htc = gw.cookies.get_cookie("ht_session"); |
65 | as_id = opkele::util::string_to_long(gw.get_param("asid")); |
65 | as_id = opkele::util::string_to_long(gw.get_param("asid")); |
66 | }catch(kingate::exception_notfound& kenf) { |
66 | }catch(kingate::exception_notfound& kenf) { |
67 | uuid_t uuid; uuid_generate(uuid); |
67 | uuid_t uuid; uuid_generate(uuid); |
68 | htc = kingate::cookie("ht_session",util::encode_base64(uuid,sizeof(uuid))); |
68 | htc = kingate::cookie("ht_session",util::encode_base64(uuid,sizeof(uuid))); |
69 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
69 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
70 | "INSERT INTO ht_sessions (hts_id) VALUES (%Q)", |
70 | "INSERT INTO ht_sessions (hts_id) VALUES (%Q)", |
71 | htc.get_value().c_str()); |
71 | htc.get_value().c_str()); |
72 | db.exec(S); |
72 | db.exec(S); |
73 | } |
73 | } |
74 | } |
74 | } |
75 | |
75 | |
76 | /* Global persistent store */ |
76 | /* Global persistent store */ |
77 | |
77 | |
78 | opkele::assoc_t store_assoc( |
78 | opkele::assoc_t store_assoc( |
79 | const string& OP,const string& handle, |
79 | const string& OP,const string& handle, |
80 | const string& type,const secret_t& secret, |
80 | const string& type,const secret_t& secret, |
81 | int expires_in) { |
81 | int expires_in) { |
82 | DUMBTHROW; |
82 | DUMBTHROW; |
83 | DOUT_("Storing '" << handle << "' assoc with '" << OP << "'"); |
83 | DOUT_("Storing '" << handle << "' assoc with '" << OP << "'"); |
84 | time_t exp = time(0)+expires_in; |
84 | time_t exp = time(0)+expires_in; |
85 | sqlite3_mem_t<char*> |
85 | sqlite3_mem_t<char*> |
86 | S = sqlite3_mprintf( |
86 | S = sqlite3_mprintf( |
87 | "INSERT INTO assoc" |
87 | "INSERT INTO assoc" |
88 | " (a_op,a_handle,a_type,a_ctime,a_etime,a_secret)" |
88 | " (a_op,a_handle,a_type,a_ctime,a_etime,a_secret)" |
89 | " VALUES (" |
89 | " VALUES (" |
90 | " %Q,%Q,%Q," |
90 | " %Q,%Q,%Q," |
91 | " datetime('now'), datetime('now','+%d seconds')," |
91 | " datetime('now'), datetime('now','+%d seconds')," |
92 | " %Q" |
92 | " %Q" |
93 | " );", OP.c_str(), handle.c_str(), type.c_str(), |
93 | " );", OP.c_str(), handle.c_str(), type.c_str(), |
94 | expires_in, |
94 | expires_in, |
@@ -322,65 +322,65 @@ class example_rp_t : public opkele::prequeue_RP { |
322 | |
322 | |
323 | void initiate(const string& usi) { |
323 | void initiate(const string& usi) { |
324 | allocate_asid(); |
324 | allocate_asid(); |
325 | prequeue_RP::initiate(usi); |
325 | prequeue_RP::initiate(usi); |
326 | } |
326 | } |
327 | |
327 | |
328 | string get_self_url() const { |
328 | string get_self_url() const { |
329 | string rv = get_this_url(); |
329 | string rv = get_this_url(); |
330 | string::size_type q = rv.find('?'); |
330 | string::size_type q = rv.find('?'); |
331 | if(q!=string::npos) |
331 | if(q!=string::npos) |
332 | rv.erase(q); |
332 | rv.erase(q); |
333 | return rv; |
333 | return rv; |
334 | } |
334 | } |
335 | |
335 | |
336 | void allocate_asid() { |
336 | void allocate_asid() { |
337 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
337 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
338 | "INSERT INTO auth_sessions (hts_id)" |
338 | "INSERT INTO auth_sessions (hts_id)" |
339 | " VALUES (%Q)", |
339 | " VALUES (%Q)", |
340 | htc.get_value().c_str()); |
340 | htc.get_value().c_str()); |
341 | db.exec(S); |
341 | db.exec(S); |
342 | as_id = sqlite3_last_insert_rowid(db); |
342 | as_id = sqlite3_last_insert_rowid(db); |
343 | DOUT_("Allocated authentication session id "<<as_id); |
343 | DOUT_("Allocated authentication session id "<<as_id); |
344 | assert(as_id>=0); |
344 | assert(as_id>=0); |
345 | } |
345 | } |
346 | |
346 | |
347 | #ifdef DUMB_RP |
347 | #ifdef DUMB_RP |
348 | virtual assoc_t associate(const string& OP) { |
348 | virtual assoc_t associate(const string& OP) { |
349 | DUMBTHROW; |
349 | DUMBTHROW; |
350 | } |
350 | } |
351 | #endif |
351 | #endif |
352 | }; |
352 | }; |
353 | |
353 | |
354 | int main(int argc,char *argv[]) { |
354 | int main(int,char **) { |
355 | try { |
355 | try { |
356 | kingate::plaincgi_interface ci; |
356 | kingate::plaincgi_interface ci; |
357 | kingate::cgi_gateway gw(ci); |
357 | kingate::cgi_gateway gw(ci); |
358 | string op; |
358 | string op; |
359 | try { op = gw.get_param("op"); }catch(kingate::exception_notfound&) { } |
359 | try { op = gw.get_param("op"); }catch(kingate::exception_notfound&) { } |
360 | if(op=="initiate") { |
360 | if(op=="initiate") { |
361 | example_rp_t rp(gw); |
361 | example_rp_t rp(gw); |
362 | string usi = gw.get_param("openid_identity"); |
362 | string usi = gw.get_param("openid_identity"); |
363 | rp.initiate(usi); |
363 | rp.initiate(usi); |
364 | opkele::sreg_t sreg(opkele::sreg_t::fields_NONE,opkele::sreg_t::fields_ALL); |
364 | opkele::sreg_t sreg(opkele::sreg_t::fields_NONE,opkele::sreg_t::fields_ALL); |
365 | opkele::openid_message_t cm; |
365 | opkele::openid_message_t cm; |
366 | string loc; |
366 | string loc; |
367 | cout << |
367 | cout << |
368 | "Set-Cookie: " << rp.htc.set_cookie_header() << "\n" |
368 | "Set-Cookie: " << rp.htc.set_cookie_header() << "\n" |
369 | "Status: 302 Going to OP\n" |
369 | "Status: 302 Going to OP\n" |
370 | "Location: " << ( |
370 | "Location: " << ( |
371 | loc = rp.checkid_(cm,opkele::mode_checkid_setup, |
371 | loc = rp.checkid_(cm,opkele::mode_checkid_setup, |
372 | rp.get_self_url()+ |
372 | rp.get_self_url()+ |
373 | "?op=confirm&asid="+opkele::util::long_to_string(rp.as_id), |
373 | "?op=confirm&asid="+opkele::util::long_to_string(rp.as_id), |
374 | rp.get_self_url(),&sreg).append_query(rp.get_endpoint().uri) |
374 | rp.get_self_url(),&sreg).append_query(rp.get_endpoint().uri) |
375 | ) |
375 | ) |
376 | << "\n\n"; |
376 | << "\n\n"; |
377 | DOUT_("Going to " << loc); |
377 | DOUT_("Going to " << loc); |
378 | }else if(op=="confirm") { |
378 | }else if(op=="confirm") { |
379 | kingate_openid_message_t om(gw); |
379 | kingate_openid_message_t om(gw); |
380 | example_rp_t rp(gw); |
380 | example_rp_t rp(gw); |
381 | opkele::sreg_t sreg(opkele::sreg_t::fields_NONE,opkele::sreg_t::fields_ALL); |
381 | opkele::sreg_t sreg(opkele::sreg_t::fields_NONE,opkele::sreg_t::fields_ALL); |
382 | rp.id_res(om,&sreg); |
382 | rp.id_res(om,&sreg); |
383 | cout << |
383 | cout << |
384 | "Content-Type: text/plain\n\n"; |
384 | "Content-Type: text/plain\n\n"; |
385 | for(opkele::basic_openid_message::fields_iterator i=om.fields_begin(); |
385 | for(opkele::basic_openid_message::fields_iterator i=om.fields_begin(); |
386 | i!=om.fields_end();++i) { |
386 | i!=om.fields_end();++i) { |
|