-rw-r--r-- | test/RP.cc | 110 | ||||
-rw-r--r-- | test/kingate_openid_message.h | 108 |
2 files changed, 109 insertions, 109 deletions
@@ -8,150 +8,42 @@ | |||
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 | 25 | ||
25 | #undef DUMB_RP | 26 | #undef DUMB_RP |
26 | 27 | ||
27 | #ifdef DUMB_RP | 28 | #ifdef DUMB_RP |
28 | # 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") |
29 | #else | 30 | #else |
30 | # define DUMBTHROW (void)0 | 31 | # define DUMBTHROW (void)0 |
31 | #endif | 32 | #endif |
32 | 33 | ||
33 | template<typename IT> | ||
34 | class join_iterator : public iterator< | ||
35 | input_iterator_tag,typename IT::value_type, | ||
36 | void,typename IT::pointer,typename IT::reference> { | ||
37 | public: | ||
38 | typedef pair<IT,IT> range_t; | ||
39 | typedef list<range_t> ranges_t; | ||
40 | ranges_t ranges; | ||
41 | |||
42 | join_iterator() { } | ||
43 | |||
44 | bool cleanup() { | ||
45 | bool rv = false; | ||
46 | while(!(ranges.empty() || ranges.front().first!=ranges.front().second)) { | ||
47 | ranges.pop_front(); rv = true; | ||
48 | } | ||
49 | return rv; | ||
50 | } | ||
51 | |||
52 | join_iterator<IT>& add_range(const IT& b,const IT& e) { | ||
53 | ranges.push_back(typename ranges_t::value_type(b,e)); | ||
54 | cleanup(); | ||
55 | return *this; | ||
56 | } | ||
57 | |||
58 | bool operator==(const join_iterator<IT>& x) const { | ||
59 | return ranges==x.ranges; } | ||
60 | bool operator!=(const join_iterator<IT>& x) const { | ||
61 | return ranges!=x.ranges; } | ||
62 | |||
63 | typename IT::reference operator*() const { | ||
64 | assert(!ranges.empty()); | ||
65 | assert(ranges.front().first!=ranges.front().second); | ||
66 | return *ranges.front().first; } | ||
67 | typename IT::pointer operator->() const { | ||
68 | assert(!ranges.empty()); | ||
69 | assert(ranges.front().first!=ranges.front().second); | ||
70 | return ranges.front().first.operator->(); } | ||
71 | |||
72 | join_iterator<IT>& operator++() { | ||
73 | cleanup(); | ||
74 | if(ranges.empty()) return *this; | ||
75 | do { | ||
76 | ++ranges.front().first; | ||
77 | }while(cleanup() && !ranges.empty()); | ||
78 | return *this; | ||
79 | } | ||
80 | join_iterator<IT> operator++(int) { | ||
81 | join_iterator<IT> rv(*this); | ||
82 | ++(*this); return rv; } | ||
83 | }; | ||
84 | |||
85 | template<typename IT> | ||
86 | class cut_prefix_filterator : public opkele::util::basic_filterator<IT> { | ||
87 | public: | ||
88 | string pfx; | ||
89 | mutable string tmp; | ||
90 | |||
91 | cut_prefix_filterator() { } | ||
92 | cut_prefix_filterator(const IT& bi,const IT&ei,const string& pfx) | ||
93 | : opkele::util::basic_filterator<IT>(bi,ei), pfx(pfx) { | ||
94 | this->prepare(); | ||
95 | } | ||
96 | |||
97 | bool is_interesting() const { | ||
98 | return pfx.length()==0 || !strncmp(this->it->c_str(),pfx.c_str(),pfx.length()); | ||
99 | } | ||
100 | |||
101 | typename IT::reference operator*() const { | ||
102 | assert(!this->empty); | ||
103 | tmp = *this->it; tmp.erase(0,pfx.length()); | ||
104 | return tmp; } | ||
105 | typename IT::pointer operator->() const { | ||
106 | assert(!this->empty); | ||
107 | return &this->operator*(); } | ||
108 | }; | ||
109 | |||
110 | class kingate_openid_message_t : public opkele::basic_openid_message { | ||
111 | typedef join_iterator<kingate::cgi_gateway::params_t::const_iterator> jitterator; | ||
112 | typedef opkele::util::map_keys_iterator< | ||
113 | jitterator, | ||
114 | fields_iterator::value_type, | ||
115 | fields_iterator::reference, | ||
116 | fields_iterator::pointer> keys_iterator; | ||
117 | typedef cut_prefix_filterator<keys_iterator> pfilterator; | ||
118 | public: | ||
119 | const kingate::cgi_gateway& gw; | ||
120 | |||
121 | kingate_openid_message_t(const kingate::cgi_gateway& g) : gw(g) { } | ||
122 | |||
123 | bool has_field(const string& n) const { | ||
124 | return gw.has_param("openid."+n); } | ||
125 | const string& get_field(const string& n) const { | ||
126 | return gw.get_param("openid."+n); } | ||
127 | |||
128 | fields_iterator fields_begin() const { | ||
129 | return | ||
130 | pfilterator( keys_iterator( | ||
131 | jitterator() | ||
132 | .add_range( gw.get.begin(), gw.get.end() ) | ||
133 | .add_range( gw.post.begin(), gw.post.end() ), | ||
134 | jitterator() | ||
135 | ), keys_iterator(), "openid." ); | ||
136 | } | ||
137 | fields_iterator fields_end() const { | ||
138 | return pfilterator(); | ||
139 | } | ||
140 | }; | ||
141 | |||
142 | class rpdb_t : public sqlite3_t { | 34 | class rpdb_t : public sqlite3_t { |
143 | public: | 35 | public: |
144 | rpdb_t() | 36 | rpdb_t() |
145 | : sqlite3_t("/tmp/RP.db") { | 37 | : sqlite3_t("/tmp/RP.db") { |
146 | assert(_D); | 38 | assert(_D); |
147 | char **resp; int nrow,ncol; char *errm; | 39 | char **resp; int nrow,ncol; char *errm; |
148 | if(sqlite3_get_table( | 40 | if(sqlite3_get_table( |
149 | _D,"SELECT a_op FROM assoc LIMIT 0", | 41 | _D,"SELECT a_op FROM assoc LIMIT 0", |
150 | &resp,&nrow,&ncol,&errm)!=SQLITE_OK) { | 42 | &resp,&nrow,&ncol,&errm)!=SQLITE_OK) { |
151 | extern const char *__RP_db_bootstrap; | 43 | extern const char *__RP_db_bootstrap; |
152 | DOUT_("Bootstrapping DB"); | 44 | DOUT_("Bootstrapping DB"); |
153 | 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) |
154 | 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); |
155 | }else | 47 | }else |
156 | sqlite3_free_table(resp); | 48 | sqlite3_free_table(resp); |
157 | 49 | ||
diff --git a/test/kingate_openid_message.h b/test/kingate_openid_message.h new file mode 100644 index 0000000..b3a2c9d --- a/dev/null +++ b/test/kingate_openid_message.h | |||
@@ -0,0 +1,108 @@ | |||
1 | template<typename IT> | ||
2 | class join_iterator : public iterator< | ||
3 | input_iterator_tag,typename IT::value_type, | ||
4 | void,typename IT::pointer,typename IT::reference> { | ||
5 | public: | ||
6 | typedef pair<IT,IT> range_t; | ||
7 | typedef list<range_t> ranges_t; | ||
8 | ranges_t ranges; | ||
9 | |||
10 | join_iterator() { } | ||
11 | |||
12 | bool cleanup() { | ||
13 | bool rv = false; | ||
14 | while(!(ranges.empty() || ranges.front().first!=ranges.front().second)) { | ||
15 | ranges.pop_front(); rv = true; | ||
16 | } | ||
17 | return rv; | ||
18 | } | ||
19 | |||
20 | join_iterator<IT>& add_range(const IT& b,const IT& e) { | ||
21 | ranges.push_back(typename ranges_t::value_type(b,e)); | ||
22 | cleanup(); | ||
23 | return *this; | ||
24 | } | ||
25 | |||
26 | bool operator==(const join_iterator<IT>& x) const { | ||
27 | return ranges==x.ranges; } | ||
28 | bool operator!=(const join_iterator<IT>& x) const { | ||
29 | return ranges!=x.ranges; } | ||
30 | |||
31 | typename IT::reference operator*() const { | ||
32 | assert(!ranges.empty()); | ||
33 | assert(ranges.front().first!=ranges.front().second); | ||
34 | return *ranges.front().first; } | ||
35 | typename IT::pointer operator->() const { | ||
36 | assert(!ranges.empty()); | ||
37 | assert(ranges.front().first!=ranges.front().second); | ||
38 | return ranges.front().first.operator->(); } | ||
39 | |||
40 | join_iterator<IT>& operator++() { | ||
41 | cleanup(); | ||
42 | if(ranges.empty()) return *this; | ||
43 | do { | ||
44 | ++ranges.front().first; | ||
45 | }while(cleanup() && !ranges.empty()); | ||
46 | return *this; | ||
47 | } | ||
48 | join_iterator<IT> operator++(int) { | ||
49 | join_iterator<IT> rv(*this); | ||
50 | ++(*this); return rv; } | ||
51 | }; | ||
52 | |||
53 | template<typename IT> | ||
54 | class cut_prefix_filterator : public opkele::util::basic_filterator<IT> { | ||
55 | public: | ||
56 | string pfx; | ||
57 | mutable string tmp; | ||
58 | |||
59 | cut_prefix_filterator() { } | ||
60 | cut_prefix_filterator(const IT& bi,const IT&ei,const string& pfx) | ||
61 | : opkele::util::basic_filterator<IT>(bi,ei), pfx(pfx) { | ||
62 | this->prepare(); | ||
63 | } | ||
64 | |||
65 | bool is_interesting() const { | ||
66 | return pfx.length()==0 || !strncmp(this->it->c_str(),pfx.c_str(),pfx.length()); | ||
67 | } | ||
68 | |||
69 | typename IT::reference operator*() const { | ||
70 | assert(!this->empty); | ||
71 | tmp = *this->it; tmp.erase(0,pfx.length()); | ||
72 | return tmp; } | ||
73 | typename IT::pointer operator->() const { | ||
74 | assert(!this->empty); | ||
75 | return &this->operator*(); } | ||
76 | }; | ||
77 | |||
78 | class kingate_openid_message_t : public opkele::basic_openid_message { | ||
79 | typedef join_iterator<kingate::cgi_gateway::params_t::const_iterator> jitterator; | ||
80 | typedef opkele::util::map_keys_iterator< | ||
81 | jitterator, | ||
82 | fields_iterator::value_type, | ||
83 | fields_iterator::reference, | ||
84 | fields_iterator::pointer> keys_iterator; | ||
85 | typedef cut_prefix_filterator<keys_iterator> pfilterator; | ||
86 | public: | ||
87 | const kingate::cgi_gateway& gw; | ||
88 | |||
89 | kingate_openid_message_t(const kingate::cgi_gateway& g) : gw(g) { } | ||
90 | |||
91 | bool has_field(const string& n) const { | ||
92 | return gw.has_param("openid."+n); } | ||
93 | const string& get_field(const string& n) const { | ||
94 | return gw.get_param("openid."+n); } | ||
95 | |||
96 | fields_iterator fields_begin() const { | ||
97 | return | ||
98 | pfilterator( keys_iterator( | ||
99 | jitterator() | ||
100 | .add_range( gw.get.begin(), gw.get.end() ) | ||
101 | .add_range( gw.post.begin(), gw.post.end() ), | ||
102 | jitterator() | ||
103 | ), keys_iterator(), "openid." ); | ||
104 | } | ||
105 | fields_iterator fields_end() const { | ||
106 | return pfilterator(); | ||
107 | } | ||
108 | }; | ||