|
|
|
@@ -1,430 +1,430 @@ |
1 | #include <uuid/uuid.h> |
1 | #include <uuid/uuid.h> |
2 | #include <iostream> |
2 | #include <iostream> |
3 | #include <cassert> |
3 | #include <cassert> |
4 | #include <stdexcept> |
4 | #include <stdexcept> |
5 | #include <string> |
5 | #include <string> |
6 | #include <set> |
6 | #include <set> |
7 | #include <iterator> |
7 | #include <iterator> |
8 | using namespace std; |
8 | using namespace std; |
9 | #include <kingate/exception.h> |
9 | #include <kingate/exception.h> |
10 | #include <kingate/plaincgi.h> |
10 | #include <kingate/plaincgi.h> |
11 | #include <kingate/cgi_gateway.h> |
11 | #include <kingate/cgi_gateway.h> |
12 | #include <opkele/exception.h> |
12 | #include <opkele/exception.h> |
13 | #include <opkele/types.h> |
13 | #include <opkele/types.h> |
14 | #include <opkele/util.h> |
14 | #include <opkele/util.h> |
15 | #include <opkele/uris.h> |
15 | #include <opkele/uris.h> |
16 | #include <opkele/discovery.h> |
16 | #include <opkele/discovery.h> |
17 | #include <opkele/association.h> |
17 | #include <opkele/association.h> |
18 | #include <opkele/sreg.h> |
18 | #include <opkele/sreg.h> |
19 | using namespace opkele; |
19 | using namespace opkele; |
20 | #include <opkele/prequeue_rp.h> |
20 | #include <opkele/prequeue_rp.h> |
21 | #include <opkele/debug.h> |
21 | #include <opkele/debug.h> |
22 | |
22 | |
23 | #include "sqlite.h" |
23 | #include "sqlite.h" |
24 | #include "kingate_openid_message.h" |
24 | #include "kingate_openid_message.h" |
25 | |
25 | |
26 | #undef DUMB_RP |
26 | #undef DUMB_RP |
27 | |
27 | |
28 | #ifdef DUMB_RP |
28 | #ifdef DUMB_RP |
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, |
95 | util::encode_base64(&(secret.front()),secret.size()).c_str() ); |
95 | util::encode_base64(&(secret.front()),secret.size()).c_str() ); |
96 | db.exec(S); |
96 | db.exec(S); |
97 | return opkele::assoc_t(new opkele::association( |
97 | return opkele::assoc_t(new opkele::association( |
98 | OP, handle, type, secret, exp, false )); |
98 | OP, handle, type, secret, exp, false )); |
99 | } |
99 | } |
100 | |
100 | |
101 | opkele::assoc_t find_assoc( |
101 | opkele::assoc_t find_assoc( |
102 | const string& OP) { |
102 | const string& OP) { |
103 | DUMBTHROW; |
103 | DUMBTHROW; |
104 | DOUT_("Looking for an assoc with '" << OP << '\''); |
104 | DOUT_("Looking for an assoc with '" << OP << '\''); |
105 | sqlite3_mem_t<char*> |
105 | sqlite3_mem_t<char*> |
106 | S = sqlite3_mprintf( |
106 | S = sqlite3_mprintf( |
107 | "SELECT" |
107 | "SELECT" |
108 | " a_op,a_handle,a_type,a_secret," |
108 | " a_op,a_handle,a_type,a_secret," |
109 | " strftime('%%s',a_etime) AS a_etime" |
109 | " strftime('%%s',a_etime) AS a_etime" |
110 | " FROM assoc" |
110 | " FROM assoc" |
111 | " WHERE a_op=%Q AND a_itime IS NULL AND NOT a_stateless" |
111 | " WHERE a_op=%Q AND a_itime IS NULL AND NOT a_stateless" |
112 | " AND ( a_etime > datetime('now','-30 seconds') )" |
112 | " AND ( a_etime > datetime('now','-30 seconds') )" |
113 | " LIMIT 1", |
113 | " LIMIT 1", |
114 | OP.c_str()); |
114 | OP.c_str()); |
115 | sqlite3_table_t T; |
115 | sqlite3_table_t T; |
116 | int nr,nc; |
116 | int nr,nc; |
117 | db.get_table(S,T,&nr,&nc); |
117 | db.get_table(S,T,&nr,&nc); |
118 | if(nr<1) |
118 | if(nr<1) |
119 | throw opkele::failed_lookup(OPKELE_CP_ "Couldn't find unexpired handle"); |
119 | throw opkele::failed_lookup(OPKELE_CP_ "Couldn't find unexpired handle"); |
120 | assert(nr==1); |
120 | assert(nr==1); |
121 | assert(nc==5); |
121 | assert(nc==5); |
122 | secret_t secret; |
122 | secret_t secret; |
123 | util::decode_base64(T.get(1,3,nc),secret); |
123 | util::decode_base64(T.get(1,3,nc),secret); |
124 | DOUT_(" found '" << T.get(1,1,nc) << '\''); |
124 | DOUT_(" found '" << T.get(1,1,nc) << '\''); |
125 | return opkele::assoc_t(new opkele::association( |
125 | return opkele::assoc_t(new opkele::association( |
126 | T.get(1,0,nc), T.get(1,1,nc), T.get(1,2,nc), |
126 | T.get(1,0,nc), T.get(1,1,nc), T.get(1,2,nc), |
127 | secret, strtol(T.get(1,4,nc),0,0), false )); |
127 | secret, strtol(T.get(1,4,nc),0,0), false )); |
128 | } |
128 | } |
129 | |
129 | |
130 | opkele::assoc_t retrieve_assoc( |
130 | opkele::assoc_t retrieve_assoc( |
131 | const string& OP,const string& handle) { |
131 | const string& OP,const string& handle) { |
132 | DUMBTHROW; |
132 | DUMBTHROW; |
133 | DOUT_("Retrieving assoc '" << handle << "' with '" << OP << '\''); |
133 | DOUT_("Retrieving assoc '" << handle << "' with '" << OP << '\''); |
134 | sqlite3_mem_t<char*> |
134 | sqlite3_mem_t<char*> |
135 | S = sqlite3_mprintf( |
135 | S = sqlite3_mprintf( |
136 | "SELECT" |
136 | "SELECT" |
137 | " a_op,a_handle,a_type,a_secret," |
137 | " a_op,a_handle,a_type,a_secret," |
138 | " strftime('%%s',a_etime) AS a_etime" |
138 | " strftime('%%s',a_etime) AS a_etime" |
139 | " FROM assoc" |
139 | " FROM assoc" |
140 | " WHERE a_op=%Q AND a_handle=%Q" |
140 | " WHERE a_op=%Q AND a_handle=%Q" |
141 | " AND a_itime IS NULL AND NOT a_stateless" |
141 | " AND a_itime IS NULL AND NOT a_stateless" |
142 | " LIMIT 1", |
142 | " LIMIT 1", |
143 | OP.c_str(),handle.c_str()); |
143 | OP.c_str(),handle.c_str()); |
144 | sqlite3_table_t T; |
144 | sqlite3_table_t T; |
145 | int nr,nc; |
145 | int nr,nc; |
146 | db.get_table(S,T,&nr,&nc); |
146 | db.get_table(S,T,&nr,&nc); |
147 | if(nr<1) |
147 | if(nr<1) |
148 | throw opkele::failed_lookup(OPKELE_CP_ "couldn't retrieve valid association"); |
148 | throw opkele::failed_lookup(OPKELE_CP_ "couldn't retrieve valid association"); |
149 | assert(nr==1); assert(nc==5); |
149 | assert(nr==1); assert(nc==5); |
150 | secret_t secret; util::decode_base64(T.get(1,3,nc),secret); |
150 | secret_t secret; util::decode_base64(T.get(1,3,nc),secret); |
151 | DOUT_(" found. type=" << T.get(1,2,nc) << '\''); |
151 | DOUT_(" found. type=" << T.get(1,2,nc) << '\''); |
152 | return opkele::assoc_t(new opkele::association( |
152 | return opkele::assoc_t(new opkele::association( |
153 | T.get(1,0,nc), T.get(1,1,nc), T.get(1,2,nc), |
153 | T.get(1,0,nc), T.get(1,1,nc), T.get(1,2,nc), |
154 | secret, strtol(T.get(1,4,nc),0,0), false )); |
154 | secret, strtol(T.get(1,4,nc),0,0), false )); |
155 | } |
155 | } |
156 | |
156 | |
157 | void invalidate_assoc( |
157 | void invalidate_assoc( |
158 | const string& OP,const string& handle) { |
158 | const string& OP,const string& handle) { |
159 | DUMBTHROW; |
159 | DUMBTHROW; |
160 | DOUT_("Invalidating assoc '" << handle << "' with '" << OP << '\''); |
160 | DOUT_("Invalidating assoc '" << handle << "' with '" << OP << '\''); |
161 | sqlite3_mem_t<char*> |
161 | sqlite3_mem_t<char*> |
162 | S = sqlite3_mprintf( |
162 | S = sqlite3_mprintf( |
163 | "UPDATE assoc SET a_itime=datetime('now')" |
163 | "UPDATE assoc SET a_itime=datetime('now')" |
164 | " WHERE a_op=%Q AND a_handle=%Q", |
164 | " WHERE a_op=%Q AND a_handle=%Q", |
165 | OP.c_str(), handle.c_str() ); |
165 | OP.c_str(), handle.c_str() ); |
166 | db.exec(S); |
166 | db.exec(S); |
167 | } |
167 | } |
168 | |
168 | |
169 | void check_nonce(const string& OP,const string& nonce) { |
169 | void check_nonce(const string& OP,const string& nonce) { |
170 | DOUT_("Checking nonce '" << nonce << "' from '" << OP << '\''); |
170 | DOUT_("Checking nonce '" << nonce << "' from '" << OP << '\''); |
171 | sqlite3_mem_t<char*> |
171 | sqlite3_mem_t<char*> |
172 | S = sqlite3_mprintf( |
172 | S = sqlite3_mprintf( |
173 | "SELECT 1 FROM nonces WHERE n_op=%Q AND n_once=%Q", |
173 | "SELECT 1 FROM nonces WHERE n_op=%Q AND n_once=%Q", |
174 | OP.c_str(), nonce.c_str()); |
174 | OP.c_str(), nonce.c_str()); |
175 | sqlite3_table_t T; |
175 | sqlite3_table_t T; |
176 | int nr,nc; |
176 | int nr,nc; |
177 | db.get_table(S,T,&nr,&nc); |
177 | db.get_table(S,T,&nr,&nc); |
178 | if(nr) |
178 | if(nr) |
179 | throw opkele::id_res_bad_nonce(OPKELE_CP_ "already seen that nonce"); |
179 | throw opkele::id_res_bad_nonce(OPKELE_CP_ "already seen that nonce"); |
180 | sqlite3_mem_t<char*> |
180 | sqlite3_mem_t<char*> |
181 | SS = sqlite3_mprintf( |
181 | SS = sqlite3_mprintf( |
182 | "INSERT INTO nonces (n_op,n_once) VALUES (%Q,%Q)", |
182 | "INSERT INTO nonces (n_op,n_once) VALUES (%Q,%Q)", |
183 | OP.c_str(), nonce.c_str()); |
183 | OP.c_str(), nonce.c_str()); |
184 | db.exec(SS); |
184 | db.exec(SS); |
185 | } |
185 | } |
186 | |
186 | |
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; |
251 | mutable string _cid; |
252 | mutable string _nid; |
252 | mutable string _nid; |
253 | |
253 | |
254 | void set_claimed_id(const string& cid) { |
254 | void set_claimed_id(const string& cid) { |
255 | assert(as_id>=0); |
255 | assert(as_id>=0); |
256 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
256 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
257 | "UPDATE auth_sessions" |
257 | "UPDATE auth_sessions" |
258 | " SET as_claimed_id=%Q" |
258 | " SET as_claimed_id=%Q" |
259 | " WHERE hts_id=%Q and as_id=%ld", |
259 | " WHERE hts_id=%Q and as_id=%ld", |
260 | cid.c_str(), |
260 | cid.c_str(), |
261 | htc.get_value().c_str(),as_id); |
261 | htc.get_value().c_str(),as_id); |
262 | db.exec(S); |
262 | db.exec(S); |
263 | _cid = cid; |
263 | _cid = cid; |
264 | } |
264 | } |
265 | const string get_claimed_id() const { |
265 | const string get_claimed_id() const { |
266 | assert(as_id>=0); |
266 | assert(as_id>=0); |
267 | if(_cid.empty()) { |
267 | if(_cid.empty()) { |
268 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
268 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
269 | "SELECT as_claimed_id" |
269 | "SELECT as_claimed_id" |
270 | " FROM" |
270 | " FROM" |
271 | " auth_sessions" |
271 | " auth_sessions" |
272 | " WHERE" |
272 | " WHERE" |
273 | " hts_id=%Q AND as_id=%ld", |
273 | " hts_id=%Q AND as_id=%ld", |
274 | htc.get_value().c_str(),as_id); |
274 | htc.get_value().c_str(),as_id); |
275 | sqlite3_table_t T; int nr,nc; |
275 | sqlite3_table_t T; int nr,nc; |
276 | db.get_table(S,T,&nr,&nc); |
276 | db.get_table(S,T,&nr,&nc); |
277 | assert(nr==1); assert(nc==1); |
277 | assert(nr==1); assert(nc==1); |
278 | _cid = T.get(1,0,nc); |
278 | _cid = T.get(1,0,nc); |
279 | } |
279 | } |
280 | return _cid; |
280 | return _cid; |
281 | } |
281 | } |
282 | void set_normalized_id(const string& nid) { |
282 | void set_normalized_id(const string& nid) { |
283 | assert(as_id>=0); |
283 | assert(as_id>=0); |
284 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
284 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
285 | "UPDATE auth_sessions" |
285 | "UPDATE auth_sessions" |
286 | " SET as_normalized_id=%Q" |
286 | " SET as_normalized_id=%Q" |
287 | " WHERE hts_id=%Q and as_id=%ld", |
287 | " WHERE hts_id=%Q and as_id=%ld", |
288 | nid.c_str(), |
288 | nid.c_str(), |
289 | htc.get_value().c_str(),as_id); |
289 | htc.get_value().c_str(),as_id); |
290 | db.exec(S); |
290 | db.exec(S); |
291 | _nid = nid; |
291 | _nid = nid; |
292 | } |
292 | } |
293 | const string get_normalized_id() const { |
293 | const string get_normalized_id() const { |
294 | assert(as_id>=0); |
294 | assert(as_id>=0); |
295 | if(_nid.empty()) { |
295 | if(_nid.empty()) { |
296 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
296 | sqlite3_mem_t<char*> S = sqlite3_mprintf( |
297 | "SELECT as_normalized_id" |
297 | "SELECT as_normalized_id" |
298 | " FROM" |
298 | " FROM" |
299 | " auth_sessions" |
299 | " auth_sessions" |
300 | " WHERE" |
300 | " WHERE" |
301 | " hts_id=%Q AND as_id=%ld", |
301 | " hts_id=%Q AND as_id=%ld", |
302 | htc.get_value().c_str(),as_id); |
302 | htc.get_value().c_str(),as_id); |
303 | sqlite3_table_t T; int nr,nc; |
303 | sqlite3_table_t T; int nr,nc; |
304 | db.get_table(S,T,&nr,&nc); |
304 | db.get_table(S,T,&nr,&nc); |
305 | assert(nr==1); assert(nc==1); |
305 | assert(nr==1); assert(nc==1); |
306 | _nid = T.get(1,0,nc); |
306 | _nid = T.get(1,0,nc); |
307 | } |
307 | } |
308 | return _nid; |
308 | return _nid; |
309 | } |
309 | } |
310 | |
310 | |
311 | const string get_this_url() const { |
311 | const string get_this_url() const { |
312 | bool s = gw.has_meta("SSL_PROTOCOL_VERSION"); |
312 | bool s = gw.has_meta("SSL_PROTOCOL_VERSION"); |
313 | string rv = s?"https://":"http://"; |
313 | string rv = s?"https://":"http://"; |
314 | rv += gw.http_request_header("Host"); |
314 | rv += gw.http_request_header("Host"); |
315 | const string& port = gw.get_meta("SERVER_PORT"); |
315 | const string& port = gw.get_meta("SERVER_PORT"); |
316 | if( port!=(s?"443":"80") ) { |
316 | if( port!=(s?"443":"80") ) { |
317 | rv += ':'; rv += port; |
317 | rv += ':'; rv += port; |
318 | } |
318 | } |
319 | rv += gw.get_meta("REQUEST_URI"); |
319 | rv += gw.get_meta("REQUEST_URI"); |
320 | return rv; |
320 | return rv; |
321 | } |
321 | } |
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) { |
387 | cout << *i << '=' << om.get_field(*i) << endl; |
387 | cout << *i << '=' << om.get_field(*i) << endl; |
388 | } |
388 | } |
389 | cout << endl |
389 | cout << endl |
390 | << "SREG fields: " << sreg.has_fields << endl; |
390 | << "SREG fields: " << sreg.has_fields << endl; |
391 | }else{ |
391 | }else{ |
392 | cout << |
392 | cout << |
393 | "Content-type: text/html\n\n" |
393 | "Content-type: text/html\n\n" |
394 | |
394 | |
395 | "<html>" |
395 | "<html>" |
396 | "<head><title>test RP</title></head>" |
396 | "<head><title>test RP</title></head>" |
397 | "<body>" |
397 | "<body>" |
398 | "<form action='' method='post'>" |
398 | "<form action='' method='post'>" |
399 | "<input type='hidden' name='op' value='initiate' />" |
399 | "<input type='hidden' name='op' value='initiate' />" |
400 | "<input type='text' name='openid_identity'/>" |
400 | "<input type='text' name='openid_identity'/>" |
401 | "<input type='submit' name='submit' value='submit' />" |
401 | "<input type='submit' name='submit' value='submit' />" |
402 | "</form>" |
402 | "</form>" |
403 | "<br/><br/>" |
403 | "<br/><br/>" |
404 | "<a href='?op=initiate&openid_identity=www.myopenid.com&dummy=" << time(0) << "'>login with myopenid.com account</a>" |
404 | "<a href='?op=initiate&openid_identity=www.myopenid.com&dummy=" << time(0) << "'>login with myopenid.com account</a>" |
405 | "<br/>" |
405 | "<br/>" |
406 | "</body" |
406 | "</body" |
407 | "</html>" |
407 | "</html>" |
408 | ; |
408 | ; |
409 | } |
409 | } |
410 | #ifdef OPKELE_HAVE_KONFORKA |
410 | #ifdef OPKELE_HAVE_KONFORKA |
411 | }catch(konforka::exception& e) { |
411 | }catch(konforka::exception& e) { |
412 | #else |
412 | #else |
413 | }catch(std::exception& e){ |
413 | }catch(std::exception& e){ |
414 | #endif |
414 | #endif |
415 | DOUT_("Oops: " << e.what()); |
415 | DOUT_("Oops: " << e.what()); |
416 | cout << "Content-Type: text/plain\n\n" |
416 | cout << "Content-Type: text/plain\n\n" |
417 | "Exception:\n" |
417 | "Exception:\n" |
418 | " what: " << e.what() << endl; |
418 | " what: " << e.what() << endl; |
419 | #ifdef OPKELE_HAVE_KONFORKA |
419 | #ifdef OPKELE_HAVE_KONFORKA |
420 | cout << " where: " << e.where() << endl; |
420 | cout << " where: " << e.where() << endl; |
421 | if(!e._seen.empty()) { |
421 | if(!e._seen.empty()) { |
422 | cout << " seen:" << endl; |
422 | cout << " seen:" << endl; |
423 | for(list<konforka::code_point>::const_iterator |
423 | for(list<konforka::code_point>::const_iterator |
424 | i=e._seen.begin();i!=e._seen.end();++i) { |
424 | i=e._seen.begin();i!=e._seen.end();++i) { |
425 | cout << " " << i->c_str() << endl; |
425 | cout << " " << i->c_str() << endl; |
426 | } |
426 | } |
427 | } |
427 | } |
428 | #endif |
428 | #endif |
429 | } |
429 | } |
430 | } |
430 | } |
|