author | Michael Krelin <hacker@klever.net> | 2008-03-03 15:57:15 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-03-03 15:57:15 (UTC) |
commit | c28479399ef0fedeb6bf14ec665bb4c427654356 (patch) (unidiff) | |
tree | ff1dfe84a54f717772bdb6233a5cbfedb57788d1 | |
parent | 0182b9dee269f1a8f3fc0794bfdf4a143fa1b5be (diff) | |
download | libopkele-c28479399ef0fedeb6bf14ec665bb4c427654356.zip libopkele-c28479399ef0fedeb6bf14ec665bb4c427654356.tar.gz libopkele-c28479399ef0fedeb6bf14ec665bb4c427654356.tar.bz2 |
introduced base_message class as a base for basic_openid_message
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | include/opkele/types.h | 42 | ||||
-rw-r--r-- | lib/Makefile.am | 2 | ||||
-rw-r--r-- | lib/message.cc (renamed from lib/openid_message.cc) | 94 |
3 files changed, 73 insertions, 65 deletions
diff --git a/include/opkele/types.h b/include/opkele/types.h index ffb9afb..64f165c 100644 --- a/include/opkele/types.h +++ b/include/opkele/types.h | |||
@@ -1,234 +1,248 @@ | |||
1 | #ifndef __OPKELE_TYPES_H | 1 | #ifndef __OPKELE_TYPES_H |
2 | #define __OPKELE_TYPES_H | 2 | #define __OPKELE_TYPES_H |
3 | 3 | ||
4 | /** | 4 | /** |
5 | * @file | 5 | * @file |
6 | * @brief various types declarations | 6 | * @brief various types declarations |
7 | */ | 7 | */ |
8 | 8 | ||
9 | #include <cstring> | 9 | #include <cstring> |
10 | #include <ostream> | 10 | #include <ostream> |
11 | #include <vector> | 11 | #include <vector> |
12 | #include <string> | 12 | #include <string> |
13 | #include <map> | 13 | #include <map> |
14 | #include <set> | 14 | #include <set> |
15 | #include <list> | 15 | #include <list> |
16 | #include <opkele/iterator.h> | 16 | #include <opkele/iterator.h> |
17 | #include <opkele/tr1-mem.h> | 17 | #include <opkele/tr1-mem.h> |
18 | 18 | ||
19 | namespace opkele { | 19 | namespace opkele { |
20 | using std::vector; | 20 | using std::vector; |
21 | using std::string; | 21 | using std::string; |
22 | using std::map; | 22 | using std::map; |
23 | using std::ostream; | 23 | using std::ostream; |
24 | using std::multimap; | 24 | using std::multimap; |
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_openid_message { | 121 | class basic_message { |
122 | public: | 122 | public: |
123 | typedef list<string> fields_t; | 123 | typedef list<string> fields_t; |
124 | typedef util::forward_iterator_proxy< | 124 | typedef util::forward_iterator_proxy< |
125 | string,const string&,const string* | 125 | string,const string&,const string* |
126 | > fields_iterator; | 126 | > fields_iterator; |
127 | 127 | ||
128 | basic_openid_message() { } | 128 | basic_message() { } |
129 | virtual ~basic_openid_message() { } | 129 | virtual ~basic_message() { } |
130 | basic_openid_message(const basic_openid_message& x); | 130 | basic_message(const basic_message& x); |
131 | void copy_to(basic_openid_message& x) const; | 131 | void copy_to(basic_message& x) const; |
132 | void append_to(basic_openid_message& x) const; | 132 | void append_to(basic_message& x) const; |
133 | 133 | ||
134 | virtual bool has_field(const string& n) const = 0; | 134 | virtual bool has_field(const string& n) const = 0; |
135 | virtual const string& get_field(const string& n) const = 0; | 135 | virtual const string& get_field(const string& n) const = 0; |
136 | 136 | ||
137 | virtual bool has_ns(const string& uri) const; | ||
138 | virtual string get_ns(const string& uri) const; | ||
139 | |||
140 | virtual fields_iterator fields_begin() const = 0; | 137 | virtual fields_iterator fields_begin() const = 0; |
141 | virtual fields_iterator fields_end() const = 0; | 138 | virtual fields_iterator fields_end() const = 0; |
142 | 139 | ||
143 | virtual string append_query(const string& url,const char *pfx="openid.") const; | 140 | virtual string append_query(const string& url,const char *pfx=0) const; |
144 | virtual string query_string(const char *pfx="openid.") const; | 141 | virtual string query_string(const char *pfx=0) const; |
145 | |||
146 | 142 | ||
147 | virtual void reset_fields(); | 143 | virtual void reset_fields(); |
148 | virtual void set_field(const string& n,const string& v); | 144 | virtual void set_field(const string& n,const string& v); |
149 | virtual void reset_field(const string& n); | 145 | virtual void reset_field(const string& n); |
150 | 146 | ||
147 | }; | ||
148 | |||
149 | class basic_openid_message : public basic_message { | ||
150 | public: | ||
151 | typedef list<string> fields_t; | ||
152 | typedef util::forward_iterator_proxy< | ||
153 | string,const string&,const string* | ||
154 | > fields_iterator; | ||
155 | |||
156 | basic_openid_message() { } | ||
157 | basic_openid_message(const basic_openid_message& x); | ||
158 | |||
159 | virtual bool has_ns(const string& uri) const; | ||
160 | virtual string get_ns(const string& uri) const; | ||
161 | |||
162 | virtual string append_query(const string& url,const char *pfx="openid.") const { | ||
163 | return basic_message::append_query(url,pfx); } | ||
164 | virtual string query_string(const char *pfx="openid.") const { | ||
165 | return basic_message::query_string(pfx); } | ||
166 | |||
151 | virtual void from_keyvalues(const string& kv); | 167 | virtual void from_keyvalues(const string& kv); |
152 | virtual void to_keyvalues(ostream& o) const; | 168 | virtual void to_keyvalues(ostream& o) const; |
153 | 169 | ||
154 | virtual void to_htmlhiddens(ostream& o,const char* pfx=0) const; | 170 | virtual void to_htmlhiddens(ostream& o,const char* pfx=0) const; |
155 | 171 | ||
156 | void add_to_signed(const string& fields); | 172 | void add_to_signed(const string& fields); |
157 | string find_ns(const string& uri,const char *pfx) const; | 173 | string find_ns(const string& uri,const char *pfx) const; |
158 | string allocate_ns(const string& uri,const char *pfx); | 174 | string allocate_ns(const string& uri,const char *pfx); |
159 | }; | 175 | }; |
160 | 176 | ||
161 | class openid_message_t : public basic_openid_message, public map<string,string> { | 177 | class openid_message_t : public basic_openid_message, public map<string,string> { |
162 | public: | 178 | public: |
163 | openid_message_t() { } | 179 | openid_message_t() { } |
164 | openid_message_t(const basic_openid_message& x) | 180 | openid_message_t(const basic_openid_message& x) |
165 | : basic_openid_message(x) { } | 181 | : basic_openid_message(x) { } |
166 | 182 | ||
167 | void copy_to(basic_openid_message& x) const; | ||
168 | |||
169 | bool has_field(const string& n) const; | 183 | bool has_field(const string& n) const; |
170 | const string& get_field(const string& n) const; | 184 | const string& get_field(const string& n) const; |
171 | virtual fields_iterator fields_begin() const; | 185 | virtual fields_iterator fields_begin() const; |
172 | virtual fields_iterator fields_end() const; | 186 | virtual fields_iterator fields_end() const; |
173 | 187 | ||
174 | void reset_fields(); | 188 | void reset_fields(); |
175 | void set_field(const string& n,const string& v); | 189 | void set_field(const string& n,const string& v); |
176 | void reset_field(const string& n); | 190 | void reset_field(const string& n); |
177 | }; | 191 | }; |
178 | 192 | ||
179 | /** | 193 | /** |
180 | * request/response parameters map | 194 | * request/response parameters map |
181 | */ | 195 | */ |
182 | class params_t : public openid_message_t { | 196 | class params_t : public openid_message_t { |
183 | public: | 197 | public: |
184 | 198 | ||
185 | /** | 199 | /** |
186 | * check whether the parameter is present. | 200 | * check whether the parameter is present. |
187 | * @param n the parameter name | 201 | * @param n the parameter name |
188 | * @return true if yes | 202 | * @return true if yes |
189 | */ | 203 | */ |
190 | bool has_param(const string& n) const { | 204 | bool has_param(const string& n) const { |
191 | return has_field(n); } | 205 | return has_field(n); } |
192 | /** | 206 | /** |
193 | * retrieve the parameter (const version) | 207 | * retrieve the parameter (const version) |
194 | * @param n the parameter name | 208 | * @param n the parameter name |
195 | * @return the parameter value | 209 | * @return the parameter value |
196 | * @throw failed_lookup if there is no such parameter | 210 | * @throw failed_lookup if there is no such parameter |
197 | */ | 211 | */ |
198 | const string& get_param(const string& n) const { | 212 | const string& get_param(const string& n) const { |
199 | return get_field(n); } | 213 | return get_field(n); } |
200 | 214 | ||
201 | /** | 215 | /** |
202 | * parse the OpenID key/value data. | 216 | * parse the OpenID key/value data. |
203 | * @param kv the OpenID key/value data | 217 | * @param kv the OpenID key/value data |
204 | */ | 218 | */ |
205 | void parse_keyvalues(const string& kv) { | 219 | void parse_keyvalues(const string& kv) { |
206 | from_keyvalues(kv); } | 220 | from_keyvalues(kv); } |
207 | 221 | ||
208 | string append_query(const string& url,const char *prefix="openid.") const; | 222 | string append_query(const string& url,const char *prefix="openid.") const; |
209 | 223 | ||
210 | }; | 224 | }; |
211 | 225 | ||
212 | struct openid_endpoint_t { | 226 | struct openid_endpoint_t { |
213 | string uri; | 227 | string uri; |
214 | string claimed_id; | 228 | string claimed_id; |
215 | string local_id; | 229 | string local_id; |
216 | 230 | ||
217 | openid_endpoint_t() { } | 231 | openid_endpoint_t() { } |
218 | openid_endpoint_t(const string& u,const string& cid,const string& lid) | 232 | openid_endpoint_t(const string& u,const string& cid,const string& lid) |
219 | : uri(u), claimed_id(cid), local_id(lid) { } | 233 | : uri(u), claimed_id(cid), local_id(lid) { } |
220 | 234 | ||
221 | bool operator==(const openid_endpoint_t& x) const { | 235 | bool operator==(const openid_endpoint_t& x) const { |
222 | return uri==x.uri && local_id==x.local_id; } | 236 | return uri==x.uri && local_id==x.local_id; } |
223 | bool operator<(const openid_endpoint_t& x) const { | 237 | bool operator<(const openid_endpoint_t& x) const { |
224 | int c; | 238 | int c; |
225 | return (c=strcmp(uri.c_str(),x.uri.c_str())) | 239 | return (c=strcmp(uri.c_str(),x.uri.c_str())) |
226 | ? (c<0) : (strcmp(local_id.c_str(),x.local_id.c_str())<0); } | 240 | ? (c<0) : (strcmp(local_id.c_str(),x.local_id.c_str())<0); } |
227 | }; | 241 | }; |
228 | 242 | ||
229 | typedef util::output_iterator_proxy<openid_endpoint_t> | 243 | typedef util::output_iterator_proxy<openid_endpoint_t> |
230 | openid_endpoint_output_iterator; | 244 | openid_endpoint_output_iterator; |
231 | 245 | ||
232 | } | 246 | } |
233 | 247 | ||
234 | #endif /* __OPKELE_TYPES_H */ | 248 | #endif /* __OPKELE_TYPES_H */ |
diff --git a/lib/Makefile.am b/lib/Makefile.am index e8bfbf5..9b25b42 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 | openid_message.cc \ | 31 | 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/openid_message.cc b/lib/message.cc index e244f43..78f20f4 100644 --- a/lib/openid_message.cc +++ b/lib/message.cc | |||
@@ -1,276 +1,270 @@ | |||
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> { | 13 | struct __om_copier : public unary_function<const string&,void> { |
14 | public: | 14 | public: |
15 | const basic_openid_message& from; | 15 | const basic_message& from; |
16 | basic_openid_message& to; | 16 | basic_message& to; |
17 | 17 | ||
18 | __om_copier(basic_openid_message& t,const basic_openid_message& f) | 18 | __om_copier(basic_message& t,const basic_message& f) |
19 | : from(f), to(t) { } | 19 | : from(f), to(t) { } |
20 | 20 | ||
21 | result_type operator()(argument_type f) { | 21 | result_type operator()(argument_type f) { |
22 | to.set_field(f,from.get_field(f)); } | 22 | to.set_field(f,from.get_field(f)); } |
23 | }; | 23 | }; |
24 | 24 | ||
25 | basic_openid_message::basic_openid_message(const basic_openid_message& x) { | 25 | basic_message::basic_message(const basic_message& x) { |
26 | x.copy_to(*this); | 26 | x.copy_to(*this); |
27 | } | 27 | } |
28 | void basic_openid_message::copy_to(basic_openid_message& x) const { | 28 | void basic_message::copy_to(basic_message& x) const { |
29 | x.reset_fields(); | 29 | x.reset_fields(); |
30 | for_each(fields_begin(),fields_end(), | 30 | for_each(fields_begin(),fields_end(), |
31 | __om_copier(x,*this) ); | 31 | __om_copier(x,*this) ); |
32 | } | 32 | } |
33 | void basic_openid_message::append_to(basic_openid_message& x) const { | 33 | void basic_message::append_to(basic_message& x) const { |
34 | for_each(fields_begin(),fields_end(), | 34 | for_each(fields_begin(),fields_end(), |
35 | __om_copier(x,*this) ); | 35 | __om_copier(x,*this) ); |
36 | } | 36 | } |
37 | 37 | ||
38 | struct __om_ns_finder : public unary_function<const string&,bool> { | ||
39 | public: | ||
40 | const basic_openid_message& om; | ||
41 | const string& uri; | ||
42 | |||
43 | __om_ns_finder(const basic_openid_message& m, | ||
44 | const string& u) : om(m), uri(u) { } | ||
45 | |||
46 | result_type operator()(argument_type f) { | ||
47 | return | ||
48 | (!strncmp(f.c_str(),"ns.",sizeof("ns.")-1)) | ||
49 | && om.get_field(f)==uri ; | ||
50 | } | ||
51 | }; | ||
52 | |||
53 | bool basic_openid_message::has_ns(const string& uri) const { | ||
54 | fields_iterator ei = fields_end(); | ||
55 | fields_iterator i = find_if(fields_begin(),fields_end(), | ||
56 | __om_ns_finder(*this,uri)); | ||
57 | return !(i==ei); | ||
58 | } | ||
59 | string basic_openid_message::get_ns(const string& uri) const { | ||
60 | fields_iterator ei = fields_end(); | ||
61 | fields_iterator i = find_if(fields_begin(),fields_end(), | ||
62 | __om_ns_finder(*this,uri)); | ||
63 | if(i==ei) | ||
64 | throw failed_lookup(OPKELE_CP_ string("failed to find namespace ")+uri); | ||
65 | return i->substr(3); | ||
66 | } | ||
67 | |||
68 | struct __om_query_builder : public unary_function<const string&,void> { | 38 | struct __om_query_builder : public unary_function<const string&,void> { |
69 | public: | 39 | public: |
70 | const basic_openid_message& om; | 40 | const basic_message& om; |
71 | bool first; | 41 | bool first; |
72 | string& rv; | 42 | string& rv; |
73 | const char *pfx; | 43 | const char *pfx; |
74 | 44 | ||
75 | __om_query_builder(const char *p,string& r,const basic_openid_message& m) | 45 | __om_query_builder(const char *p,string& r,const basic_message& m) |
76 | : om(m), first(true), rv(r), pfx(p) { | 46 | : om(m), first(true), rv(r), pfx(p) { |
77 | for_each(om.fields_begin(),om.fields_end(),*this); | 47 | for_each(om.fields_begin(),om.fields_end(),*this); |
78 | } | 48 | } |
79 | __om_query_builder(const char *p,string& r,const basic_openid_message& m,const string& u) | 49 | __om_query_builder(const char *p,string& r,const basic_message& m,const string& u) |
80 | : om(m), first(true), rv(r), pfx(p) { | 50 | : om(m), first(true), rv(r), pfx(p) { |
81 | rv = u; | 51 | rv = u; |
82 | if(rv.find('?')==string::npos) | 52 | if(rv.find('?')==string::npos) |
83 | rv += '?'; | 53 | rv += '?'; |
84 | else | 54 | else |
85 | first = false; | 55 | first = false; |
86 | for_each(om.fields_begin(),om.fields_end(),*this); | 56 | for_each(om.fields_begin(),om.fields_end(),*this); |
87 | } | 57 | } |
88 | 58 | ||
89 | result_type operator()(argument_type f) { | 59 | result_type operator()(argument_type f) { |
90 | if(first) | 60 | if(first) |
91 | first = false; | 61 | first = false; |
92 | else | 62 | else |
93 | rv += '&'; | 63 | rv += '&'; |
94 | if(pfx) rv += pfx; | 64 | if(pfx) rv += pfx; |
95 | rv+= f; | 65 | rv+= f; |
96 | rv += '='; | 66 | rv += '='; |
97 | rv += util::url_encode(om.get_field(f)); | 67 | rv += util::url_encode(om.get_field(f)); |
98 | } | 68 | } |
99 | }; | 69 | }; |
100 | 70 | ||
101 | string basic_openid_message::append_query(const string& url,const char *pfx) const { | 71 | string basic_message::append_query(const string& url,const char *pfx) const { |
102 | string rv; | 72 | string rv; |
103 | return __om_query_builder(pfx,rv,*this,url).rv; | 73 | return __om_query_builder(pfx,rv,*this,url).rv; |
104 | } | 74 | } |
105 | string basic_openid_message::query_string(const char *pfx) const { | 75 | string basic_message::query_string(const char *pfx) const { |
106 | string rv; | 76 | string rv; |
107 | return __om_query_builder(pfx,rv,*this).rv; | 77 | return __om_query_builder(pfx,rv,*this).rv; |
108 | } | 78 | } |
109 | 79 | ||
110 | void basic_openid_message::reset_fields() { | 80 | void basic_message::reset_fields() { |
111 | throw not_implemented(OPKELE_CP_ "reset_fields() not implemented"); | 81 | throw not_implemented(OPKELE_CP_ "reset_fields() not implemented"); |
112 | } | 82 | } |
113 | void basic_openid_message::set_field(const string&,const string&) { | 83 | void basic_message::set_field(const string&,const string&) { |
114 | throw not_implemented(OPKELE_CP_ "set_field() not implemented"); | 84 | throw not_implemented(OPKELE_CP_ "set_field() not implemented"); |
115 | } | 85 | } |
116 | void basic_openid_message::reset_field(const string&) { | 86 | void basic_message::reset_field(const string&) { |
117 | throw not_implemented(OPKELE_CP_ "reset_field() not implemented"); | 87 | throw not_implemented(OPKELE_CP_ "reset_field() not implemented"); |
118 | } | 88 | } |
119 | 89 | ||
90 | struct __om_ns_finder : public unary_function<const string&,bool> { | ||
91 | public: | ||
92 | const basic_openid_message& om; | ||
93 | const string& uri; | ||
94 | |||
95 | __om_ns_finder(const basic_openid_message& m, | ||
96 | const string& u) : om(m), uri(u) { } | ||
97 | |||
98 | result_type operator()(argument_type f) { | ||
99 | return | ||
100 | (!strncmp(f.c_str(),"ns.",sizeof("ns.")-1)) | ||
101 | && om.get_field(f)==uri ; | ||
102 | } | ||
103 | }; | ||
104 | |||
105 | bool basic_openid_message::has_ns(const string& uri) const { | ||
106 | fields_iterator ei = fields_end(); | ||
107 | fields_iterator i = find_if(fields_begin(),fields_end(), | ||
108 | __om_ns_finder(*this,uri)); | ||
109 | return !(i==ei); | ||
110 | } | ||
111 | string basic_openid_message::get_ns(const string& uri) const { | ||
112 | fields_iterator ei = fields_end(); | ||
113 | fields_iterator i = find_if(fields_begin(),fields_end(), | ||
114 | __om_ns_finder(*this,uri)); | ||
115 | if(i==ei) | ||
116 | throw failed_lookup(OPKELE_CP_ string("failed to find namespace ")+uri); | ||
117 | return i->substr(3); | ||
118 | } | ||
119 | |||
120 | void basic_openid_message::from_keyvalues(const string& kv) { | 120 | void basic_openid_message::from_keyvalues(const string& kv) { |
121 | reset_fields(); | 121 | reset_fields(); |
122 | string::size_type p = 0; | 122 | string::size_type p = 0; |
123 | while(true) { | 123 | while(true) { |
124 | string::size_type co = kv.find(':',p); | 124 | string::size_type co = kv.find(':',p); |
125 | if(co==string::npos) | 125 | if(co==string::npos) |
126 | break; | 126 | break; |
127 | #ifndef POSTELS_LAW | 127 | #ifndef POSTELS_LAW |
128 | string::size_type nl = kv.find('\n',co+1); | 128 | string::size_type nl = kv.find('\n',co+1); |
129 | if(nl==string::npos) | 129 | if(nl==string::npos) |
130 | throw bad_input(OPKELE_CP_ "malformed input"); | 130 | throw bad_input(OPKELE_CP_ "malformed input"); |
131 | if(nl>co) | 131 | if(nl>co) |
132 | insert(value_type(kv.substr(p,co-p),kv.substr(co+1,nl-co-1))); | 132 | insert(value_type(kv.substr(p,co-p),kv.substr(co+1,nl-co-1))); |
133 | p = nl+1; | 133 | p = nl+1; |
134 | #else /* POSTELS_LAW */ | 134 | #else /* POSTELS_LAW */ |
135 | string::size_type lb = kv.find_first_of("\r\n",co+1); | 135 | string::size_type lb = kv.find_first_of("\r\n",co+1); |
136 | if(lb==string::npos) { | 136 | if(lb==string::npos) { |
137 | set_field(kv.substr(p,co-p),kv.substr(co+1)); | 137 | set_field(kv.substr(p,co-p),kv.substr(co+1)); |
138 | break; | 138 | break; |
139 | } | 139 | } |
140 | if(lb>co) | 140 | if(lb>co) |
141 | set_field(kv.substr(p,co-p),kv.substr(co+1,lb-co-1)); | 141 | 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); | 142 | string::size_type nolb = kv.find_first_not_of("\r\n",lb); |
143 | if(nolb==string::npos) | 143 | if(nolb==string::npos) |
144 | break; | 144 | break; |
145 | p = nolb; | 145 | p = nolb; |
146 | #endif /* POSTELS_LAW */ | 146 | #endif /* POSTELS_LAW */ |
147 | } | 147 | } |
148 | } | 148 | } |
149 | 149 | ||
150 | struct __om_kv_outputter : public unary_function<const string&,void> { | 150 | struct __om_kv_outputter : public unary_function<const string&,void> { |
151 | public: | 151 | public: |
152 | const basic_openid_message& om; | 152 | const basic_openid_message& om; |
153 | ostream& os; | 153 | ostream& os; |
154 | 154 | ||
155 | __om_kv_outputter(const basic_openid_message& m,ostream& s) | 155 | __om_kv_outputter(const basic_openid_message& m,ostream& s) |
156 | : om(m), os(s) { } | 156 | : om(m), os(s) { } |
157 | 157 | ||
158 | result_type operator()(argument_type f) { | 158 | result_type operator()(argument_type f) { |
159 | os << f << ':' << om.get_field(f) << '\n'; | 159 | os << f << ':' << om.get_field(f) << '\n'; |
160 | } | 160 | } |
161 | }; | 161 | }; |
162 | 162 | ||
163 | void basic_openid_message::to_keyvalues(ostream& o) const { | 163 | void basic_openid_message::to_keyvalues(ostream& o) const { |
164 | for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o)); | 164 | for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o)); |
165 | } | 165 | } |
166 | 166 | ||
167 | struct __om_html_outputter : public unary_function<const string&,void> { | 167 | struct __om_html_outputter : public unary_function<const string&,void> { |
168 | public: | 168 | public: |
169 | const basic_openid_message& om; | 169 | const basic_openid_message& om; |
170 | ostream& os; | 170 | ostream& os; |
171 | const char *pfx; | 171 | const char *pfx; |
172 | 172 | ||
173 | __om_html_outputter(const basic_openid_message& m,ostream& s,const char *p=0) | 173 | __om_html_outputter(const basic_openid_message& m,ostream& s,const char *p=0) |
174 | : om(m), os(s), pfx(p) { } | 174 | : om(m), os(s), pfx(p) { } |
175 | 175 | ||
176 | result_type operator()(argument_type f) { | 176 | result_type operator()(argument_type f) { |
177 | os << | 177 | os << |
178 | "<input type=\"hidden\"" | 178 | "<input type=\"hidden\"" |
179 | " name=\""; | 179 | " name=\""; |
180 | if(pfx) | 180 | if(pfx) |
181 | os << util::attr_escape(pfx); | 181 | os << util::attr_escape(pfx); |
182 | os << util::attr_escape(f) << "\"" | 182 | os << util::attr_escape(f) << "\"" |
183 | " value=\"" << util::attr_escape(om.get_field(f)) << "\" />"; | 183 | " value=\"" << util::attr_escape(om.get_field(f)) << "\" />"; |
184 | } | 184 | } |
185 | }; | 185 | }; |
186 | 186 | ||
187 | void basic_openid_message::to_htmlhiddens(ostream& o,const char* pfx) const { | 187 | 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)); | 188 | for_each(fields_begin(),fields_end(),__om_html_outputter(*this,o,pfx)); |
189 | } | 189 | } |
190 | 190 | ||
191 | void basic_openid_message::add_to_signed(const string& fields) { | 191 | void basic_openid_message::add_to_signed(const string& fields) { |
192 | string::size_type fnc = fields.find_first_not_of(","); | 192 | string::size_type fnc = fields.find_first_not_of(","); |
193 | if(fnc==string::npos) | 193 | if(fnc==string::npos) |
194 | throw bad_input(OPKELE_CP_ "Trying to add nothing in particular to the list of signed fields"); | 194 | throw bad_input(OPKELE_CP_ "Trying to add nothing in particular to the list of signed fields"); |
195 | string signeds; | 195 | string signeds; |
196 | try { | 196 | try { |
197 | signeds = get_field("signed"); | 197 | signeds = get_field("signed"); |
198 | string::size_type lnc = signeds.find_last_not_of(","); | 198 | string::size_type lnc = signeds.find_last_not_of(","); |
199 | if(lnc==string::npos) | 199 | if(lnc==string::npos) |
200 | signeds.assign(fields,fnc,fields.size()-fnc); | 200 | signeds.assign(fields,fnc,fields.size()-fnc); |
201 | else{ | 201 | else{ |
202 | string::size_type ss = signeds.size(); | 202 | string::size_type ss = signeds.size(); |
203 | if(lnc==(ss-1)) { | 203 | if(lnc==(ss-1)) { |
204 | signeds+= ','; | 204 | signeds+= ','; |
205 | signeds.append(fields,fnc,fields.size()-fnc); | 205 | signeds.append(fields,fnc,fields.size()-fnc); |
206 | }else{ | 206 | }else{ |
207 | if(lnc<(ss-2)) | 207 | if(lnc<(ss-2)) |
208 | signeds.replace(lnc+2,ss-lnc-2, | 208 | signeds.replace(lnc+2,ss-lnc-2, |
209 | fields,fnc,fields.size()-fnc); | 209 | fields,fnc,fields.size()-fnc); |
210 | else | 210 | else |
211 | signeds.append(fields,fnc,fields.size()-fnc); | 211 | signeds.append(fields,fnc,fields.size()-fnc); |
212 | } | 212 | } |
213 | } | 213 | } |
214 | }catch(failed_lookup&) { | 214 | }catch(failed_lookup&) { |
215 | signeds.assign(fields,fnc,fields.size()-fnc); | 215 | signeds.assign(fields,fnc,fields.size()-fnc); |
216 | } | 216 | } |
217 | set_field("signed",signeds); | 217 | set_field("signed",signeds); |
218 | } | 218 | } |
219 | 219 | ||
220 | string basic_openid_message::find_ns(const string& uri,const char *pfx) const { | 220 | string basic_openid_message::find_ns(const string& uri,const char *pfx) const { |
221 | try { | 221 | try { |
222 | return get_ns(uri); | 222 | return get_ns(uri); |
223 | }catch(failed_lookup&) { | 223 | }catch(failed_lookup&) { |
224 | return pfx; | 224 | return pfx; |
225 | } | 225 | } |
226 | } | 226 | } |
227 | string basic_openid_message::allocate_ns(const string& uri,const char *pfx) { | 227 | string basic_openid_message::allocate_ns(const string& uri,const char *pfx) { |
228 | if(!has_field("ns")) | 228 | if(!has_field("ns")) |
229 | return pfx; | 229 | return pfx; |
230 | if(has_ns(uri)) | 230 | if(has_ns(uri)) |
231 | throw bad_input(OPKELE_CP_ "OpenID message already contains namespace"); | 231 | throw bad_input(OPKELE_CP_ "OpenID message already contains namespace"); |
232 | string rv = pfx; | 232 | string rv = pfx; |
233 | if(has_field("ns."+rv)) { | 233 | if(has_field("ns."+rv)) { |
234 | string::reference c=rv[rv.length()]; | 234 | string::reference c=rv[rv.length()]; |
235 | for(c='a';c<='z' && has_field("ns."+rv);++c); | 235 | for(c='a';c<='z' && has_field("ns."+rv);++c); |
236 | if(c=='z') | 236 | if(c=='z') |
237 | throw exception(OPKELE_CP_ "Failed to allocate namespace"); | 237 | throw exception(OPKELE_CP_ "Failed to allocate namespace"); |
238 | } | 238 | } |
239 | set_field("ns."+rv,uri); | 239 | set_field("ns."+rv,uri); |
240 | return rv; | 240 | return rv; |
241 | } | 241 | } |
242 | 242 | ||
243 | void openid_message_t::copy_to(basic_openid_message& x) const { | ||
244 | x.reset_fields(); | ||
245 | for(const_iterator i=begin();i!=end();++i) | ||
246 | x.set_field(i->first,i->second); | ||
247 | } | ||
248 | |||
249 | bool openid_message_t::has_field(const string& n) const { | 243 | bool openid_message_t::has_field(const string& n) const { |
250 | return find(n)!=end(); | 244 | return find(n)!=end(); |
251 | } | 245 | } |
252 | const string& openid_message_t::get_field(const string& n) const { | 246 | const string& openid_message_t::get_field(const string& n) const { |
253 | const_iterator i=find(n); | 247 | const_iterator i=find(n); |
254 | if(i==end()) | 248 | if(i==end()) |
255 | throw failed_lookup(OPKELE_CP_ n+": no such field"); | 249 | throw failed_lookup(OPKELE_CP_ n+": no such field"); |
256 | return i->second; | 250 | return i->second; |
257 | } | 251 | } |
258 | 252 | ||
259 | openid_message_t::fields_iterator openid_message_t::fields_begin() const { | 253 | openid_message_t::fields_iterator openid_message_t::fields_begin() const { |
260 | return util::map_keys_iterator<const_iterator,string,const string&,const string*>(begin(),end()); | 254 | return util::map_keys_iterator<const_iterator,string,const string&,const string*>(begin(),end()); |
261 | } | 255 | } |
262 | openid_message_t::fields_iterator openid_message_t::fields_end() const { | 256 | openid_message_t::fields_iterator openid_message_t::fields_end() const { |
263 | return util::map_keys_iterator<const_iterator,string,const string&,const string*>(end(),end()); | 257 | return util::map_keys_iterator<const_iterator,string,const string&,const string*>(end(),end()); |
264 | } | 258 | } |
265 | 259 | ||
266 | void openid_message_t::reset_fields() { | 260 | void openid_message_t::reset_fields() { |
267 | clear(); | 261 | clear(); |
268 | } | 262 | } |
269 | void openid_message_t::set_field(const string& n,const string& v) { | 263 | void openid_message_t::set_field(const string& n,const string& v) { |
270 | (*this)[n]=v; | 264 | (*this)[n]=v; |
271 | } | 265 | } |
272 | void openid_message_t::reset_field(const string& n) { | 266 | void openid_message_t::reset_field(const string& n) { |
273 | erase(n); | 267 | erase(n); |
274 | } | 268 | } |
275 | 269 | ||
276 | } | 270 | } |