author | Michael Krelin <hacker@klever.net> | 2008-03-03 17:16:32 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-03-03 17:16:32 (UTC) |
commit | ecb6a585d1fc3705836dc896fe348b970101e8d3 (patch) (unidiff) | |
tree | 2ae11b4109988ab63093c041f8d5925794d51323 | |
parent | 374985b5317d559b561d7f557034661e314f5605 (diff) | |
download | libopkele-ecb6a585d1fc3705836dc896fe348b970101e8d3.zip libopkele-ecb6a585d1fc3705836dc896fe348b970101e8d3.tar.gz libopkele-ecb6a585d1fc3705836dc896fe348b970101e8d3.tar.bz2 |
renamed basic_message to basic_fields
since oauth fieldsets aren't really messages
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | include/opkele/types.h | 22 | ||||
-rw-r--r-- | lib/Makefile.am | 2 | ||||
-rw-r--r-- | lib/fields.cc | 86 | ||||
-rw-r--r-- | lib/message.cc | 78 |
4 files changed, 97 insertions, 91 deletions
diff --git a/include/opkele/types.h b/include/opkele/types.h index bf50e2b..f63bf5d 100644 --- a/include/opkele/types.h +++ b/include/opkele/types.h | |||
@@ -25,223 +25,219 @@ namespace opkele { | |||
25 | using std::set; | 25 | using std::set; |
26 | using std::list; | 26 | using std::list; |
27 | using std::iterator; | 27 | using std::iterator; |
28 | using std::forward_iterator_tag; | 28 | using std::forward_iterator_tag; |
29 | 29 | ||
30 | /** | 30 | /** |
31 | * the OpenID operation mode | 31 | * the OpenID operation mode |
32 | */ | 32 | */ |
33 | typedef enum _mode_t { | 33 | typedef enum _mode_t { |
34 | mode_unknown = 0, | 34 | mode_unknown = 0, |
35 | mode_associate, | 35 | mode_associate, |
36 | mode_checkid_immediate, | 36 | mode_checkid_immediate, |
37 | mode_checkid_setup, | 37 | mode_checkid_setup, |
38 | mode_check_association | 38 | mode_check_association |
39 | } mode_t; | 39 | } mode_t; |
40 | 40 | ||
41 | /** | 41 | /** |
42 | * the association secret container | 42 | * the association secret container |
43 | */ | 43 | */ |
44 | class secret_t : public vector<unsigned char> { | 44 | class secret_t : public vector<unsigned char> { |
45 | public: | 45 | public: |
46 | 46 | ||
47 | /** | 47 | /** |
48 | * xor the secret and hmac together and encode, using base64 | 48 | * xor the secret and hmac together and encode, using base64 |
49 | * @param key_d pointer to the message digest | 49 | * @param key_d pointer to the message digest |
50 | * @param rv reference to the return value | 50 | * @param rv reference to the return value |
51 | */ | 51 | */ |
52 | void enxor_to_base64(const unsigned char *key_d,string& rv) const; | 52 | void enxor_to_base64(const unsigned char *key_d,string& rv) const; |
53 | /** | 53 | /** |
54 | * decode base64-encoded secret and xor it with the message digest | 54 | * decode base64-encoded secret and xor it with the message digest |
55 | * @param key_d pointer to the message digest | 55 | * @param key_d pointer to the message digest |
56 | * @param b64 base64-encoded secret value | 56 | * @param b64 base64-encoded secret value |
57 | */ | 57 | */ |
58 | void enxor_from_base64(const unsigned char *key_d,const string& b64); | 58 | void enxor_from_base64(const unsigned char *key_d,const string& b64); |
59 | /** | 59 | /** |
60 | * plainly encode to base64 representation | 60 | * plainly encode to base64 representation |
61 | * @param rv reference to the return value | 61 | * @param rv reference to the return value |
62 | */ | 62 | */ |
63 | void to_base64(string& rv) const; | 63 | void to_base64(string& rv) const; |
64 | /** | 64 | /** |
65 | * decode cleartext secret from base64 | 65 | * decode cleartext secret from base64 |
66 | * @param b64 base64-encoded representation of the secret value | 66 | * @param b64 base64-encoded representation of the secret value |
67 | */ | 67 | */ |
68 | void from_base64(const string& b64); | 68 | void from_base64(const string& b64); |
69 | }; | 69 | }; |
70 | 70 | ||
71 | /** | 71 | /** |
72 | * Interface to the association. | 72 | * Interface to the association. |
73 | */ | 73 | */ |
74 | class association_t { | 74 | class association_t { |
75 | public: | 75 | public: |
76 | 76 | ||
77 | virtual ~association_t() { } | 77 | virtual ~association_t() { } |
78 | 78 | ||
79 | /** | 79 | /** |
80 | * retrieve the server with which association was established. | 80 | * retrieve the server with which association was established. |
81 | * @return server name | 81 | * @return server name |
82 | */ | 82 | */ |
83 | virtual string server() const = 0; | 83 | virtual string server() const = 0; |
84 | /** | 84 | /** |
85 | * retrieve the association handle. | 85 | * retrieve the association handle. |
86 | * @return handle | 86 | * @return handle |
87 | */ | 87 | */ |
88 | virtual string handle() const = 0; | 88 | virtual string handle() const = 0; |
89 | /** | 89 | /** |
90 | * retrieve the association type. | 90 | * retrieve the association type. |
91 | * @return association type | 91 | * @return association type |
92 | */ | 92 | */ |
93 | virtual string assoc_type() const = 0; | 93 | virtual string assoc_type() const = 0; |
94 | /** | 94 | /** |
95 | * retrieve the association secret. | 95 | * retrieve the association secret. |
96 | * @return association secret | 96 | * @return association secret |
97 | */ | 97 | */ |
98 | virtual secret_t secret() const = 0; | 98 | virtual secret_t secret() const = 0; |
99 | /** | 99 | /** |
100 | * retrieve the number of seconds the association expires in. | 100 | * retrieve the number of seconds the association expires in. |
101 | * @return seconds till expiration | 101 | * @return seconds till expiration |
102 | */ | 102 | */ |
103 | virtual int expires_in() const = 0; | 103 | virtual int expires_in() const = 0; |
104 | /** | 104 | /** |
105 | * check whether the association is stateless. | 105 | * check whether the association is stateless. |
106 | * @return true if stateless | 106 | * @return true if stateless |
107 | */ | 107 | */ |
108 | virtual bool stateless() const = 0; | 108 | virtual bool stateless() const = 0; |
109 | /** | 109 | /** |
110 | * check whether the association is expired. | 110 | * check whether the association is expired. |
111 | * @return true if expired | 111 | * @return true if expired |
112 | */ | 112 | */ |
113 | virtual bool is_expired() const = 0; | 113 | virtual bool is_expired() const = 0; |
114 | }; | 114 | }; |
115 | 115 | ||
116 | /** | 116 | /** |
117 | * the shared_ptr<> for association_t object type | 117 | * the shared_ptr<> for association_t object type |
118 | */ | 118 | */ |
119 | typedef tr1mem::shared_ptr<association_t> assoc_t; | 119 | typedef tr1mem::shared_ptr<association_t> assoc_t; |
120 | 120 | ||
121 | class basic_message { | 121 | class basic_fields { |
122 | public: | 122 | public: |
123 | typedef util::forward_iterator_proxy< | 123 | typedef util::forward_iterator_proxy< |
124 | string,const string&,const string* | 124 | string,const string&,const string* |
125 | > fields_iterator; | 125 | > fields_iterator; |
126 | 126 | ||
127 | basic_message() { } | 127 | basic_fields() { } |
128 | virtual ~basic_message() { } | 128 | virtual ~basic_fields() { } |
129 | basic_message(const basic_message& x); | 129 | basic_fields(const basic_fields& x); |
130 | void copy_to(basic_message& x) const; | 130 | void copy_to(basic_fields& x) const; |
131 | void append_to(basic_message& x) const; | 131 | void append_to(basic_fields& x) const; |
132 | 132 | ||
133 | virtual bool has_field(const string& n) const = 0; | 133 | virtual bool has_field(const string& n) const = 0; |
134 | virtual const string& get_field(const string& n) const = 0; | 134 | virtual const string& get_field(const string& n) const = 0; |
135 | 135 | ||
136 | virtual fields_iterator fields_begin() const = 0; | 136 | virtual fields_iterator fields_begin() const = 0; |
137 | virtual fields_iterator fields_end() const = 0; | 137 | virtual fields_iterator fields_end() const = 0; |
138 | 138 | ||
139 | virtual string append_query(const string& url,const char *pfx=0) const; | 139 | virtual string append_query(const string& url,const char *pfx=0) const; |
140 | virtual string query_string(const char *pfx=0) const; | 140 | virtual string query_string(const char *pfx=0) const; |
141 | 141 | ||
142 | virtual void reset_fields(); | 142 | virtual void reset_fields(); |
143 | virtual void set_field(const string& n,const string& v); | 143 | virtual void set_field(const string& n,const string& v); |
144 | virtual void reset_field(const string& n); | 144 | virtual void reset_field(const string& n); |
145 | 145 | ||
146 | }; | 146 | }; |
147 | 147 | ||
148 | class basic_openid_message : public basic_message { | 148 | class basic_openid_message : public basic_fields { |
149 | public: | 149 | public: |
150 | typedef list<string> fields_t; | ||
151 | typedef util::forward_iterator_proxy< | ||
152 | string,const string&,const string* | ||
153 | > fields_iterator; | ||
154 | 150 | ||
155 | basic_openid_message() { } | 151 | basic_openid_message() { } |
156 | basic_openid_message(const basic_openid_message& x); | 152 | basic_openid_message(const basic_openid_message& x); |
157 | 153 | ||
158 | virtual bool has_ns(const string& uri) const; | 154 | virtual bool has_ns(const string& uri) const; |
159 | virtual string get_ns(const string& uri) const; | 155 | virtual string get_ns(const string& uri) const; |
160 | 156 | ||
161 | virtual string append_query(const string& url,const char *pfx="openid.") const { | 157 | virtual string append_query(const string& url,const char *pfx="openid.") const { |
162 | return basic_message::append_query(url,pfx); } | 158 | return basic_fields::append_query(url,pfx); } |
163 | virtual string query_string(const char *pfx="openid.") const { | 159 | virtual string query_string(const char *pfx="openid.") const { |
164 | return basic_message::query_string(pfx); } | 160 | return basic_fields::query_string(pfx); } |
165 | 161 | ||
166 | virtual void from_keyvalues(const string& kv); | 162 | virtual void from_keyvalues(const string& kv); |
167 | virtual void to_keyvalues(ostream& o) const; | 163 | virtual void to_keyvalues(ostream& o) const; |
168 | 164 | ||
169 | virtual void to_htmlhiddens(ostream& o,const char* pfx=0) const; | 165 | virtual void to_htmlhiddens(ostream& o,const char* pfx=0) const; |
170 | 166 | ||
171 | void add_to_signed(const string& fields); | 167 | void add_to_signed(const string& fields); |
172 | string find_ns(const string& uri,const char *pfx) const; | 168 | string find_ns(const string& uri,const char *pfx) const; |
173 | string allocate_ns(const string& uri,const char *pfx); | 169 | string allocate_ns(const string& uri,const char *pfx); |
174 | }; | 170 | }; |
175 | 171 | ||
176 | class openid_message_t : public basic_openid_message, public map<string,string> { | 172 | class openid_message_t : public basic_openid_message, public map<string,string> { |
177 | public: | 173 | public: |
178 | openid_message_t() { } | 174 | openid_message_t() { } |
179 | openid_message_t(const basic_openid_message& x) | 175 | openid_message_t(const basic_openid_message& x) |
180 | : basic_openid_message(x) { } | 176 | : basic_openid_message(x) { } |
181 | 177 | ||
182 | bool has_field(const string& n) const; | 178 | bool has_field(const string& n) const; |
183 | const string& get_field(const string& n) const; | 179 | const string& get_field(const string& n) const; |
184 | virtual fields_iterator fields_begin() const; | 180 | virtual fields_iterator fields_begin() const; |
185 | virtual fields_iterator fields_end() const; | 181 | virtual fields_iterator fields_end() const; |
186 | 182 | ||
187 | void reset_fields(); | 183 | void reset_fields(); |
188 | void set_field(const string& n,const string& v); | 184 | void set_field(const string& n,const string& v); |
189 | void reset_field(const string& n); | 185 | void reset_field(const string& n); |
190 | }; | 186 | }; |
191 | 187 | ||
192 | /** | 188 | /** |
193 | * request/response parameters map | 189 | * request/response parameters map |
194 | */ | 190 | */ |
195 | class params_t : public openid_message_t { | 191 | class params_t : public openid_message_t { |
196 | public: | 192 | public: |
197 | 193 | ||
198 | /** | 194 | /** |
199 | * check whether the parameter is present. | 195 | * check whether the parameter is present. |
200 | * @param n the parameter name | 196 | * @param n the parameter name |
201 | * @return true if yes | 197 | * @return true if yes |
202 | */ | 198 | */ |
203 | bool has_param(const string& n) const { | 199 | bool has_param(const string& n) const { |
204 | return has_field(n); } | 200 | return has_field(n); } |
205 | /** | 201 | /** |
206 | * retrieve the parameter (const version) | 202 | * retrieve the parameter (const version) |
207 | * @param n the parameter name | 203 | * @param n the parameter name |
208 | * @return the parameter value | 204 | * @return the parameter value |
209 | * @throw failed_lookup if there is no such parameter | 205 | * @throw failed_lookup if there is no such parameter |
210 | */ | 206 | */ |
211 | const string& get_param(const string& n) const { | 207 | const string& get_param(const string& n) const { |
212 | return get_field(n); } | 208 | return get_field(n); } |
213 | 209 | ||
214 | /** | 210 | /** |
215 | * parse the OpenID key/value data. | 211 | * parse the OpenID key/value data. |
216 | * @param kv the OpenID key/value data | 212 | * @param kv the OpenID key/value data |
217 | */ | 213 | */ |
218 | void parse_keyvalues(const string& kv) { | 214 | void parse_keyvalues(const string& kv) { |
219 | from_keyvalues(kv); } | 215 | from_keyvalues(kv); } |
220 | 216 | ||
221 | string append_query(const string& url,const char *prefix="openid.") const; | 217 | string append_query(const string& url,const char *prefix="openid.") const; |
222 | 218 | ||
223 | }; | 219 | }; |
224 | 220 | ||
225 | struct openid_endpoint_t { | 221 | struct openid_endpoint_t { |
226 | string uri; | 222 | string uri; |
227 | string claimed_id; | 223 | string claimed_id; |
228 | string local_id; | 224 | string local_id; |
229 | 225 | ||
230 | openid_endpoint_t() { } | 226 | openid_endpoint_t() { } |
231 | openid_endpoint_t(const string& u,const string& cid,const string& lid) | 227 | openid_endpoint_t(const string& u,const string& cid,const string& lid) |
232 | : uri(u), claimed_id(cid), local_id(lid) { } | 228 | : uri(u), claimed_id(cid), local_id(lid) { } |
233 | 229 | ||
234 | bool operator==(const openid_endpoint_t& x) const { | 230 | bool operator==(const openid_endpoint_t& x) const { |
235 | return uri==x.uri && local_id==x.local_id; } | 231 | return uri==x.uri && local_id==x.local_id; } |
236 | bool operator<(const openid_endpoint_t& x) const { | 232 | bool operator<(const openid_endpoint_t& x) const { |
237 | int c; | 233 | int c; |
238 | return (c=strcmp(uri.c_str(),x.uri.c_str())) | 234 | return (c=strcmp(uri.c_str(),x.uri.c_str())) |
239 | ? (c<0) : (strcmp(local_id.c_str(),x.local_id.c_str())<0); } | 235 | ? (c<0) : (strcmp(local_id.c_str(),x.local_id.c_str())<0); } |
240 | }; | 236 | }; |
241 | 237 | ||
242 | typedef util::output_iterator_proxy<openid_endpoint_t> | 238 | typedef util::output_iterator_proxy<openid_endpoint_t> |
243 | openid_endpoint_output_iterator; | 239 | openid_endpoint_output_iterator; |
244 | 240 | ||
245 | } | 241 | } |
246 | 242 | ||
247 | #endif /* __OPKELE_TYPES_H */ | 243 | #endif /* __OPKELE_TYPES_H */ |
diff --git a/lib/Makefile.am b/lib/Makefile.am index 9b25b42..20d15b8 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am | |||
@@ -1,34 +1,34 @@ | |||
1 | lib_LTLIBRARIES = libopkele.la | 1 | lib_LTLIBRARIES = libopkele.la |
2 | 2 | ||
3 | AM_CPPFLAGS = ${CPPFLAGS_DEBUG} | 3 | AM_CPPFLAGS = ${CPPFLAGS_DEBUG} |
4 | DEFAULT_INCLUDES = -I${top_builddir} | 4 | DEFAULT_INCLUDES = -I${top_builddir} |
5 | INCLUDES = \ | 5 | INCLUDES = \ |
6 | -I${top_builddir}/include/ -I${top_srcdir}/include/ \ | 6 | -I${top_builddir}/include/ -I${top_srcdir}/include/ \ |
7 | ${KONFORKA_CFLAGS} \ | 7 | ${KONFORKA_CFLAGS} \ |
8 | ${OPENSSL_CFLAGS} \ | 8 | ${OPENSSL_CFLAGS} \ |
9 | ${LIBCURL_CPPFLAGS} \ | 9 | ${LIBCURL_CPPFLAGS} \ |
10 | ${PCRE_CFLAGS} ${EXPAT_CFLAGS} ${TIDY_CFLAGS} | 10 | ${PCRE_CFLAGS} ${EXPAT_CFLAGS} ${TIDY_CFLAGS} |
11 | libopkele_la_LIBADD = \ | 11 | libopkele_la_LIBADD = \ |
12 | ${LIBCURL} \ | 12 | ${LIBCURL} \ |
13 | ${PCRE_LIBS} ${EXPAT_LIBS} \ | 13 | ${PCRE_LIBS} ${EXPAT_LIBS} \ |
14 | ${OPENSSL_LIBS} \ | 14 | ${OPENSSL_LIBS} \ |
15 | ${KONFORKA_LIBS} ${TIDY_LIBS} | 15 | ${KONFORKA_LIBS} ${TIDY_LIBS} |
16 | 16 | ||
17 | libopkele_la_SOURCES = \ | 17 | libopkele_la_SOURCES = \ |
18 | params.cc \ | 18 | params.cc \ |
19 | util.cc \ | 19 | util.cc \ |
20 | server.cc \ | 20 | server.cc \ |
21 | secret.cc \ | 21 | secret.cc \ |
22 | data.cc \ | 22 | data.cc \ |
23 | consumer.cc \ | 23 | consumer.cc \ |
24 | exception.cc \ | 24 | exception.cc \ |
25 | extension.cc \ | 25 | extension.cc \ |
26 | sreg.cc \ | 26 | sreg.cc \ |
27 | extension_chain.cc \ | 27 | extension_chain.cc \ |
28 | curl.cc expat.cc \ | 28 | curl.cc expat.cc \ |
29 | discovery.cc \ | 29 | discovery.cc \ |
30 | basic_rp.cc prequeue_rp.cc \ | 30 | basic_rp.cc prequeue_rp.cc \ |
31 | message.cc \ | 31 | fields.cc message.cc \ |
32 | basic_op.cc verify_op.cc | 32 | basic_op.cc verify_op.cc |
33 | libopkele_la_LDFLAGS = \ | 33 | libopkele_la_LDFLAGS = \ |
34 | -version-info 2:0:0 | 34 | -version-info 2:0:0 |
diff --git a/lib/fields.cc b/lib/fields.cc new file mode 100644 index 0000000..d494098 --- a/dev/null +++ b/lib/fields.cc | |||
@@ -0,0 +1,86 @@ | |||
1 | #include <opkele/types.h> | ||
2 | #include <opkele/exception.h> | ||
3 | #include <opkele/util.h> | ||
4 | |||
5 | namespace opkele { | ||
6 | using std::unary_function; | ||
7 | |||
8 | struct __om_copier : public unary_function<const string&,void> { | ||
9 | public: | ||
10 | const basic_fields& from; | ||
11 | basic_fields& to; | ||
12 | |||
13 | __om_copier(basic_fields& t,const basic_fields& f) | ||
14 | : from(f), to(t) { } | ||
15 | |||
16 | result_type operator()(argument_type f) { | ||
17 | to.set_field(f,from.get_field(f)); } | ||
18 | }; | ||
19 | |||
20 | basic_fields::basic_fields(const basic_fields& x) { | ||
21 | x.copy_to(*this); | ||
22 | } | ||
23 | void basic_fields::copy_to(basic_fields& x) const { | ||
24 | x.reset_fields(); | ||
25 | for_each(fields_begin(),fields_end(), | ||
26 | __om_copier(x,*this) ); | ||
27 | } | ||
28 | void basic_fields::append_to(basic_fields& x) const { | ||
29 | for_each(fields_begin(),fields_end(), | ||
30 | __om_copier(x,*this) ); | ||
31 | } | ||
32 | |||
33 | struct __om_query_builder : public unary_function<const string&,void> { | ||
34 | public: | ||
35 | const basic_fields& om; | ||
36 | bool first; | ||
37 | string& rv; | ||
38 | const char *pfx; | ||
39 | |||
40 | __om_query_builder(const char *p,string& r,const basic_fields& m) | ||
41 | : om(m), first(true), rv(r), pfx(p) { | ||
42 | for_each(om.fields_begin(),om.fields_end(),*this); | ||
43 | } | ||
44 | __om_query_builder(const char *p,string& r,const basic_fields& m,const string& u) | ||
45 | : om(m), first(true), rv(r), pfx(p) { | ||
46 | rv = u; | ||
47 | if(rv.find('?')==string::npos) | ||
48 | rv += '?'; | ||
49 | else | ||
50 | first = false; | ||
51 | for_each(om.fields_begin(),om.fields_end(),*this); | ||
52 | } | ||
53 | |||
54 | result_type operator()(argument_type f) { | ||
55 | if(first) | ||
56 | first = false; | ||
57 | else | ||
58 | rv += '&'; | ||
59 | if(pfx) rv += pfx; | ||
60 | rv+= f; | ||
61 | rv += '='; | ||
62 | rv += util::url_encode(om.get_field(f)); | ||
63 | } | ||
64 | }; | ||
65 | |||
66 | string basic_fields::append_query(const string& url,const char *pfx) const { | ||
67 | string rv; | ||
68 | return __om_query_builder(pfx,rv,*this,url).rv; | ||
69 | } | ||
70 | string basic_fields::query_string(const char *pfx) const { | ||
71 | string rv; | ||
72 | return __om_query_builder(pfx,rv,*this).rv; | ||
73 | } | ||
74 | |||
75 | void basic_fields::reset_fields() { | ||
76 | throw not_implemented(OPKELE_CP_ "reset_fields() not implemented"); | ||
77 | } | ||
78 | void basic_fields::set_field(const string&,const string&) { | ||
79 | throw not_implemented(OPKELE_CP_ "set_field() not implemented"); | ||
80 | } | ||
81 | void basic_fields::reset_field(const string&) { | ||
82 | throw not_implemented(OPKELE_CP_ "reset_field() not implemented"); | ||
83 | } | ||
84 | |||
85 | |||
86 | } | ||
diff --git a/lib/message.cc b/lib/message.cc index 78f20f4..b2324b7 100644 --- a/lib/message.cc +++ b/lib/message.cc | |||
@@ -1,228 +1,152 @@ | |||
1 | #include <cassert> | 1 | #include <cassert> |
2 | #include <opkele/types.h> | 2 | #include <opkele/types.h> |
3 | #include <opkele/exception.h> | 3 | #include <opkele/exception.h> |
4 | #include <opkele/util.h> | 4 | #include <opkele/util.h> |
5 | #include <opkele/debug.h> | 5 | #include <opkele/debug.h> |
6 | 6 | ||
7 | #include "config.h" | 7 | #include "config.h" |
8 | 8 | ||
9 | namespace opkele { | 9 | namespace opkele { |
10 | using std::input_iterator_tag; | 10 | using std::input_iterator_tag; |
11 | using std::unary_function; | 11 | using std::unary_function; |
12 | 12 | ||
13 | struct __om_copier : public unary_function<const string&,void> { | ||
14 | public: | ||
15 | const basic_message& from; | ||
16 | basic_message& to; | ||
17 | |||
18 | __om_copier(basic_message& t,const basic_message& f) | ||
19 | : from(f), to(t) { } | ||
20 | |||
21 | result_type operator()(argument_type f) { | ||
22 | to.set_field(f,from.get_field(f)); } | ||
23 | }; | ||
24 | |||
25 | basic_message::basic_message(const basic_message& x) { | ||
26 | x.copy_to(*this); | ||
27 | } | ||
28 | void basic_message::copy_to(basic_message& x) const { | ||
29 | x.reset_fields(); | ||
30 | for_each(fields_begin(),fields_end(), | ||
31 | __om_copier(x,*this) ); | ||
32 | } | ||
33 | void basic_message::append_to(basic_message& x) const { | ||
34 | for_each(fields_begin(),fields_end(), | ||
35 | __om_copier(x,*this) ); | ||
36 | } | ||
37 | |||
38 | struct __om_query_builder : public unary_function<const string&,void> { | ||
39 | public: | ||
40 | const basic_message& om; | ||
41 | bool first; | ||
42 | string& rv; | ||
43 | const char *pfx; | ||
44 | |||
45 | __om_query_builder(const char *p,string& r,const basic_message& m) | ||
46 | : om(m), first(true), rv(r), pfx(p) { | ||
47 | for_each(om.fields_begin(),om.fields_end(),*this); | ||
48 | } | ||
49 | __om_query_builder(const char *p,string& r,const basic_message& m,const string& u) | ||
50 | : om(m), first(true), rv(r), pfx(p) { | ||
51 | rv = u; | ||
52 | if(rv.find('?')==string::npos) | ||
53 | rv += '?'; | ||
54 | else | ||
55 | first = false; | ||
56 | for_each(om.fields_begin(),om.fields_end(),*this); | ||
57 | } | ||
58 | |||
59 | result_type operator()(argument_type f) { | ||
60 | if(first) | ||
61 | first = false; | ||
62 | else | ||
63 | rv += '&'; | ||
64 | if(pfx) rv += pfx; | ||
65 | rv+= f; | ||
66 | rv += '='; | ||
67 | rv += util::url_encode(om.get_field(f)); | ||
68 | } | ||
69 | }; | ||
70 | |||
71 | string basic_message::append_query(const string& url,const char *pfx) const { | ||
72 | string rv; | ||
73 | return __om_query_builder(pfx,rv,*this,url).rv; | ||
74 | } | ||
75 | string basic_message::query_string(const char *pfx) const { | ||
76 | string rv; | ||
77 | return __om_query_builder(pfx,rv,*this).rv; | ||
78 | } | ||
79 | |||
80 | void basic_message::reset_fields() { | ||
81 | throw not_implemented(OPKELE_CP_ "reset_fields() not implemented"); | ||
82 | } | ||
83 | void basic_message::set_field(const string&,const string&) { | ||
84 | throw not_implemented(OPKELE_CP_ "set_field() not implemented"); | ||
85 | } | ||
86 | void basic_message::reset_field(const string&) { | ||
87 | throw not_implemented(OPKELE_CP_ "reset_field() not implemented"); | ||
88 | } | ||
89 | 13 | ||
90 | struct __om_ns_finder : public unary_function<const string&,bool> { | 14 | struct __om_ns_finder : public unary_function<const string&,bool> { |
91 | public: | 15 | public: |
92 | const basic_openid_message& om; | 16 | const basic_openid_message& om; |
93 | const string& uri; | 17 | const string& uri; |
94 | 18 | ||
95 | __om_ns_finder(const basic_openid_message& m, | 19 | __om_ns_finder(const basic_openid_message& m, |
96 | const string& u) : om(m), uri(u) { } | 20 | const string& u) : om(m), uri(u) { } |
97 | 21 | ||
98 | result_type operator()(argument_type f) { | 22 | result_type operator()(argument_type f) { |
99 | return | 23 | return |
100 | (!strncmp(f.c_str(),"ns.",sizeof("ns.")-1)) | 24 | (!strncmp(f.c_str(),"ns.",sizeof("ns.")-1)) |
101 | && om.get_field(f)==uri ; | 25 | && om.get_field(f)==uri ; |
102 | } | 26 | } |
103 | }; | 27 | }; |
104 | 28 | ||
105 | bool basic_openid_message::has_ns(const string& uri) const { | 29 | bool basic_openid_message::has_ns(const string& uri) const { |
106 | fields_iterator ei = fields_end(); | 30 | fields_iterator ei = fields_end(); |
107 | fields_iterator i = find_if(fields_begin(),fields_end(), | 31 | fields_iterator i = find_if(fields_begin(),fields_end(), |
108 | __om_ns_finder(*this,uri)); | 32 | __om_ns_finder(*this,uri)); |
109 | return !(i==ei); | 33 | return !(i==ei); |
110 | } | 34 | } |
111 | string basic_openid_message::get_ns(const string& uri) const { | 35 | string basic_openid_message::get_ns(const string& uri) const { |
112 | fields_iterator ei = fields_end(); | 36 | fields_iterator ei = fields_end(); |
113 | fields_iterator i = find_if(fields_begin(),fields_end(), | 37 | fields_iterator i = find_if(fields_begin(),fields_end(), |
114 | __om_ns_finder(*this,uri)); | 38 | __om_ns_finder(*this,uri)); |
115 | if(i==ei) | 39 | if(i==ei) |
116 | throw failed_lookup(OPKELE_CP_ string("failed to find namespace ")+uri); | 40 | throw failed_lookup(OPKELE_CP_ string("failed to find namespace ")+uri); |
117 | return i->substr(3); | 41 | return i->substr(3); |
118 | } | 42 | } |
119 | 43 | ||
120 | void basic_openid_message::from_keyvalues(const string& kv) { | 44 | void basic_openid_message::from_keyvalues(const string& kv) { |
121 | reset_fields(); | 45 | reset_fields(); |
122 | string::size_type p = 0; | 46 | string::size_type p = 0; |
123 | while(true) { | 47 | while(true) { |
124 | string::size_type co = kv.find(':',p); | 48 | string::size_type co = kv.find(':',p); |
125 | if(co==string::npos) | 49 | if(co==string::npos) |
126 | break; | 50 | break; |
127 | #ifndef POSTELS_LAW | 51 | #ifndef POSTELS_LAW |
128 | string::size_type nl = kv.find('\n',co+1); | 52 | string::size_type nl = kv.find('\n',co+1); |
129 | if(nl==string::npos) | 53 | if(nl==string::npos) |
130 | throw bad_input(OPKELE_CP_ "malformed input"); | 54 | throw bad_input(OPKELE_CP_ "malformed input"); |
131 | if(nl>co) | 55 | if(nl>co) |
132 | insert(value_type(kv.substr(p,co-p),kv.substr(co+1,nl-co-1))); | 56 | set_field(kv.substr(p,co-p),kv.substr(co+1,nl-co-1)); |
133 | p = nl+1; | 57 | p = nl+1; |
134 | #else /* POSTELS_LAW */ | 58 | #else /* POSTELS_LAW */ |
135 | string::size_type lb = kv.find_first_of("\r\n",co+1); | 59 | string::size_type lb = kv.find_first_of("\r\n",co+1); |
136 | if(lb==string::npos) { | 60 | if(lb==string::npos) { |
137 | set_field(kv.substr(p,co-p),kv.substr(co+1)); | 61 | set_field(kv.substr(p,co-p),kv.substr(co+1)); |
138 | break; | 62 | break; |
139 | } | 63 | } |
140 | if(lb>co) | 64 | if(lb>co) |
141 | set_field(kv.substr(p,co-p),kv.substr(co+1,lb-co-1)); | 65 | set_field(kv.substr(p,co-p),kv.substr(co+1,lb-co-1)); |
142 | string::size_type nolb = kv.find_first_not_of("\r\n",lb); | 66 | string::size_type nolb = kv.find_first_not_of("\r\n",lb); |
143 | if(nolb==string::npos) | 67 | if(nolb==string::npos) |
144 | break; | 68 | break; |
145 | p = nolb; | 69 | p = nolb; |
146 | #endif /* POSTELS_LAW */ | 70 | #endif /* POSTELS_LAW */ |
147 | } | 71 | } |
148 | } | 72 | } |
149 | 73 | ||
150 | struct __om_kv_outputter : public unary_function<const string&,void> { | 74 | struct __om_kv_outputter : public unary_function<const string&,void> { |
151 | public: | 75 | public: |
152 | const basic_openid_message& om; | 76 | const basic_openid_message& om; |
153 | ostream& os; | 77 | ostream& os; |
154 | 78 | ||
155 | __om_kv_outputter(const basic_openid_message& m,ostream& s) | 79 | __om_kv_outputter(const basic_openid_message& m,ostream& s) |
156 | : om(m), os(s) { } | 80 | : om(m), os(s) { } |
157 | 81 | ||
158 | result_type operator()(argument_type f) { | 82 | result_type operator()(argument_type f) { |
159 | os << f << ':' << om.get_field(f) << '\n'; | 83 | os << f << ':' << om.get_field(f) << '\n'; |
160 | } | 84 | } |
161 | }; | 85 | }; |
162 | 86 | ||
163 | void basic_openid_message::to_keyvalues(ostream& o) const { | 87 | void basic_openid_message::to_keyvalues(ostream& o) const { |
164 | for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o)); | 88 | for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o)); |
165 | } | 89 | } |
166 | 90 | ||
167 | struct __om_html_outputter : public unary_function<const string&,void> { | 91 | struct __om_html_outputter : public unary_function<const string&,void> { |
168 | public: | 92 | public: |
169 | const basic_openid_message& om; | 93 | const basic_openid_message& om; |
170 | ostream& os; | 94 | ostream& os; |
171 | const char *pfx; | 95 | const char *pfx; |
172 | 96 | ||
173 | __om_html_outputter(const basic_openid_message& m,ostream& s,const char *p=0) | 97 | __om_html_outputter(const basic_openid_message& m,ostream& s,const char *p=0) |
174 | : om(m), os(s), pfx(p) { } | 98 | : om(m), os(s), pfx(p) { } |
175 | 99 | ||
176 | result_type operator()(argument_type f) { | 100 | result_type operator()(argument_type f) { |
177 | os << | 101 | os << |
178 | "<input type=\"hidden\"" | 102 | "<input type=\"hidden\"" |
179 | " name=\""; | 103 | " name=\""; |
180 | if(pfx) | 104 | if(pfx) |
181 | os << util::attr_escape(pfx); | 105 | os << util::attr_escape(pfx); |
182 | os << util::attr_escape(f) << "\"" | 106 | os << util::attr_escape(f) << "\"" |
183 | " value=\"" << util::attr_escape(om.get_field(f)) << "\" />"; | 107 | " value=\"" << util::attr_escape(om.get_field(f)) << "\" />"; |
184 | } | 108 | } |
185 | }; | 109 | }; |
186 | 110 | ||
187 | void basic_openid_message::to_htmlhiddens(ostream& o,const char* pfx) const { | 111 | void basic_openid_message::to_htmlhiddens(ostream& o,const char* pfx) const { |
188 | for_each(fields_begin(),fields_end(),__om_html_outputter(*this,o,pfx)); | 112 | for_each(fields_begin(),fields_end(),__om_html_outputter(*this,o,pfx)); |
189 | } | 113 | } |
190 | 114 | ||
191 | void basic_openid_message::add_to_signed(const string& fields) { | 115 | void basic_openid_message::add_to_signed(const string& fields) { |
192 | string::size_type fnc = fields.find_first_not_of(","); | 116 | string::size_type fnc = fields.find_first_not_of(","); |
193 | if(fnc==string::npos) | 117 | if(fnc==string::npos) |
194 | throw bad_input(OPKELE_CP_ "Trying to add nothing in particular to the list of signed fields"); | 118 | throw bad_input(OPKELE_CP_ "Trying to add nothing in particular to the list of signed fields"); |
195 | string signeds; | 119 | string signeds; |
196 | try { | 120 | try { |
197 | signeds = get_field("signed"); | 121 | signeds = get_field("signed"); |
198 | string::size_type lnc = signeds.find_last_not_of(","); | 122 | string::size_type lnc = signeds.find_last_not_of(","); |
199 | if(lnc==string::npos) | 123 | if(lnc==string::npos) |
200 | signeds.assign(fields,fnc,fields.size()-fnc); | 124 | signeds.assign(fields,fnc,fields.size()-fnc); |
201 | else{ | 125 | else{ |
202 | string::size_type ss = signeds.size(); | 126 | string::size_type ss = signeds.size(); |
203 | if(lnc==(ss-1)) { | 127 | if(lnc==(ss-1)) { |
204 | signeds+= ','; | 128 | signeds+= ','; |
205 | signeds.append(fields,fnc,fields.size()-fnc); | 129 | signeds.append(fields,fnc,fields.size()-fnc); |
206 | }else{ | 130 | }else{ |
207 | if(lnc<(ss-2)) | 131 | if(lnc<(ss-2)) |
208 | signeds.replace(lnc+2,ss-lnc-2, | 132 | signeds.replace(lnc+2,ss-lnc-2, |
209 | fields,fnc,fields.size()-fnc); | 133 | fields,fnc,fields.size()-fnc); |
210 | else | 134 | else |
211 | signeds.append(fields,fnc,fields.size()-fnc); | 135 | signeds.append(fields,fnc,fields.size()-fnc); |
212 | } | 136 | } |
213 | } | 137 | } |
214 | }catch(failed_lookup&) { | 138 | }catch(failed_lookup&) { |
215 | signeds.assign(fields,fnc,fields.size()-fnc); | 139 | signeds.assign(fields,fnc,fields.size()-fnc); |
216 | } | 140 | } |
217 | set_field("signed",signeds); | 141 | set_field("signed",signeds); |
218 | } | 142 | } |
219 | 143 | ||
220 | string basic_openid_message::find_ns(const string& uri,const char *pfx) const { | 144 | string basic_openid_message::find_ns(const string& uri,const char *pfx) const { |
221 | try { | 145 | try { |
222 | return get_ns(uri); | 146 | return get_ns(uri); |
223 | }catch(failed_lookup&) { | 147 | }catch(failed_lookup&) { |
224 | return pfx; | 148 | return pfx; |
225 | } | 149 | } |
226 | } | 150 | } |
227 | string basic_openid_message::allocate_ns(const string& uri,const char *pfx) { | 151 | string basic_openid_message::allocate_ns(const string& uri,const char *pfx) { |
228 | if(!has_field("ns")) | 152 | if(!has_field("ns")) |