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