author | Michael Krelin <hacker@klever.net> | 2008-02-02 10:49:52 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-02-02 10:49:52 (UTC) |
commit | f29ad6501686e5f43b536258b86d12683c711f02 (patch) (unidiff) | |
tree | 79ce9cf3f75449846bdf556550eaa927b20074be | |
parent | 61e6da06804f98d0cbb9d27eeb335351b3e05d4d (diff) | |
download | libopkele-f29ad6501686e5f43b536258b86d12683c711f02.zip libopkele-f29ad6501686e5f43b536258b86d12683c711f02.tar.gz libopkele-f29ad6501686e5f43b536258b86d12683c711f02.tar.bz2 |
generate html form hiddens from the openid message
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | include/opkele/types.h | 2 | ||||
-rw-r--r-- | lib/openid_message.cc | 20 |
2 files changed, 22 insertions, 0 deletions
diff --git a/include/opkele/types.h b/include/opkele/types.h index 842a722..5d39a5c 100644 --- a/include/opkele/types.h +++ b/include/opkele/types.h | |||
@@ -103,96 +103,98 @@ namespace opkele { | |||
103 | /** | 103 | /** |
104 | * check whether the association is stateless. | 104 | * check whether the association is stateless. |
105 | * @return true if stateless | 105 | * @return true if stateless |
106 | */ | 106 | */ |
107 | virtual bool stateless() const = 0; | 107 | virtual bool stateless() const = 0; |
108 | /** | 108 | /** |
109 | * check whether the association is expired. | 109 | * check whether the association is expired. |
110 | * @return true if expired | 110 | * @return true if expired |
111 | */ | 111 | */ |
112 | virtual bool is_expired() const = 0; | 112 | virtual bool is_expired() const = 0; |
113 | }; | 113 | }; |
114 | 114 | ||
115 | /** | 115 | /** |
116 | * the shared_ptr<> for association_t object type | 116 | * the shared_ptr<> for association_t object type |
117 | */ | 117 | */ |
118 | typedef tr1mem::shared_ptr<association_t> assoc_t; | 118 | typedef tr1mem::shared_ptr<association_t> assoc_t; |
119 | 119 | ||
120 | class basic_openid_message { | 120 | class basic_openid_message { |
121 | public: | 121 | public: |
122 | typedef list<string> fields_t; | 122 | typedef list<string> fields_t; |
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_openid_message() { } | 127 | basic_openid_message() { } |
128 | basic_openid_message(const basic_openid_message& x); | 128 | basic_openid_message(const basic_openid_message& x); |
129 | void copy_to(basic_openid_message& x) const; | 129 | void copy_to(basic_openid_message& x) const; |
130 | 130 | ||
131 | virtual bool has_field(const string& n) const = 0; | 131 | virtual bool has_field(const string& n) const = 0; |
132 | virtual const string& get_field(const string& n) const = 0; | 132 | virtual const string& get_field(const string& n) const = 0; |
133 | 133 | ||
134 | virtual bool has_ns(const string& uri) const; | 134 | virtual bool has_ns(const string& uri) const; |
135 | virtual string get_ns(const string& uri) const; | 135 | virtual string get_ns(const string& uri) const; |
136 | 136 | ||
137 | virtual fields_iterator fields_begin() const = 0; | 137 | virtual fields_iterator fields_begin() const = 0; |
138 | virtual fields_iterator fields_end() const = 0; | 138 | virtual fields_iterator fields_end() const = 0; |
139 | 139 | ||
140 | virtual string append_query(const string& url) const; | 140 | virtual string append_query(const string& url) const; |
141 | virtual string query_string() const; | 141 | virtual string query_string() const; |
142 | 142 | ||
143 | 143 | ||
144 | virtual void reset_fields(); | 144 | virtual void reset_fields(); |
145 | virtual void set_field(const string& n,const string& v); | 145 | virtual void set_field(const string& n,const string& v); |
146 | virtual void reset_field(const string& n); | 146 | virtual void reset_field(const string& n); |
147 | 147 | ||
148 | virtual void from_keyvalues(const string& kv); | 148 | virtual void from_keyvalues(const string& kv); |
149 | virtual void to_keyvalues(ostream& o) const; | 149 | virtual void to_keyvalues(ostream& o) const; |
150 | 150 | ||
151 | virtual void to_htmlhiddens(ostream& o) const; | ||
152 | |||
151 | void add_to_signed(const string& fields); | 153 | void add_to_signed(const string& fields); |
152 | string find_ns(const string& uri,const char *pfx) const; | 154 | string find_ns(const string& uri,const char *pfx) const; |
153 | string allocate_ns(const string& uri,const char *pfx); | 155 | string allocate_ns(const string& uri,const char *pfx); |
154 | }; | 156 | }; |
155 | 157 | ||
156 | class openid_message_t : public basic_openid_message, public map<string,string> { | 158 | class openid_message_t : public basic_openid_message, public map<string,string> { |
157 | public: | 159 | public: |
158 | openid_message_t() { } | 160 | openid_message_t() { } |
159 | openid_message_t(const basic_openid_message& x) | 161 | openid_message_t(const basic_openid_message& x) |
160 | : basic_openid_message(x) { } | 162 | : basic_openid_message(x) { } |
161 | 163 | ||
162 | void copy_to(basic_openid_message& x) const; | 164 | void copy_to(basic_openid_message& x) const; |
163 | 165 | ||
164 | bool has_field(const string& n) const; | 166 | bool has_field(const string& n) const; |
165 | const string& get_field(const string& n) const; | 167 | const string& get_field(const string& n) const; |
166 | virtual fields_iterator fields_begin() const; | 168 | virtual fields_iterator fields_begin() const; |
167 | virtual fields_iterator fields_end() const; | 169 | virtual fields_iterator fields_end() const; |
168 | 170 | ||
169 | void reset_fields(); | 171 | void reset_fields(); |
170 | void set_field(const string& n,const string& v); | 172 | void set_field(const string& n,const string& v); |
171 | void reset_field(const string& n); | 173 | void reset_field(const string& n); |
172 | }; | 174 | }; |
173 | 175 | ||
174 | /** | 176 | /** |
175 | * request/response parameters map | 177 | * request/response parameters map |
176 | */ | 178 | */ |
177 | class params_t : public openid_message_t { | 179 | class params_t : public openid_message_t { |
178 | public: | 180 | public: |
179 | 181 | ||
180 | /** | 182 | /** |
181 | * check whether the parameter is present. | 183 | * check whether the parameter is present. |
182 | * @param n the parameter name | 184 | * @param n the parameter name |
183 | * @return true if yes | 185 | * @return true if yes |
184 | */ | 186 | */ |
185 | bool has_param(const string& n) const { | 187 | bool has_param(const string& n) const { |
186 | return has_field(n); } | 188 | return has_field(n); } |
187 | /** | 189 | /** |
188 | * retrieve the parameter (const version) | 190 | * retrieve the parameter (const version) |
189 | * @param n the parameter name | 191 | * @param n the parameter name |
190 | * @return the parameter value | 192 | * @return the parameter value |
191 | * @throw failed_lookup if there is no such parameter | 193 | * @throw failed_lookup if there is no such parameter |
192 | */ | 194 | */ |
193 | const string& get_param(const string& n) const { | 195 | const string& get_param(const string& n) const { |
194 | return get_field(n); } | 196 | return get_field(n); } |
195 | 197 | ||
196 | /** | 198 | /** |
197 | * parse the OpenID key/value data. | 199 | * parse the OpenID key/value data. |
198 | * @param kv the OpenID key/value data | 200 | * @param kv the OpenID key/value data |
diff --git a/lib/openid_message.cc b/lib/openid_message.cc index 905ea95..fdb4b04 100644 --- a/lib/openid_message.cc +++ b/lib/openid_message.cc | |||
@@ -114,96 +114,116 @@ namespace opkele { | |||
114 | 114 | ||
115 | void basic_openid_message::from_keyvalues(const string& kv) { | 115 | void basic_openid_message::from_keyvalues(const string& kv) { |
116 | reset_fields(); | 116 | reset_fields(); |
117 | string::size_type p = 0; | 117 | string::size_type p = 0; |
118 | while(true) { | 118 | while(true) { |
119 | string::size_type co = kv.find(':',p); | 119 | string::size_type co = kv.find(':',p); |
120 | if(co==string::npos) | 120 | if(co==string::npos) |
121 | break; | 121 | break; |
122 | #ifndef POSTELS_LAW | 122 | #ifndef POSTELS_LAW |
123 | string::size_type nl = kv.find('\n',co+1); | 123 | string::size_type nl = kv.find('\n',co+1); |
124 | if(nl==string::npos) | 124 | if(nl==string::npos) |
125 | throw bad_input(OPKELE_CP_ "malformed input"); | 125 | throw bad_input(OPKELE_CP_ "malformed input"); |
126 | if(nl>co) | 126 | if(nl>co) |
127 | insert(value_type(kv.substr(p,co-p),kv.substr(co+1,nl-co-1))); | 127 | insert(value_type(kv.substr(p,co-p),kv.substr(co+1,nl-co-1))); |
128 | p = nl+1; | 128 | p = nl+1; |
129 | #else /* POSTELS_LAW */ | 129 | #else /* POSTELS_LAW */ |
130 | string::size_type lb = kv.find_first_of("\r\n",co+1); | 130 | string::size_type lb = kv.find_first_of("\r\n",co+1); |
131 | if(lb==string::npos) { | 131 | if(lb==string::npos) { |
132 | set_field(kv.substr(p,co-p),kv.substr(co+1)); | 132 | set_field(kv.substr(p,co-p),kv.substr(co+1)); |
133 | break; | 133 | break; |
134 | } | 134 | } |
135 | if(lb>co) | 135 | if(lb>co) |
136 | set_field(kv.substr(p,co-p),kv.substr(co+1,lb-co-1)); | 136 | set_field(kv.substr(p,co-p),kv.substr(co+1,lb-co-1)); |
137 | string::size_type nolb = kv.find_first_not_of("\r\n",lb); | 137 | string::size_type nolb = kv.find_first_not_of("\r\n",lb); |
138 | if(nolb==string::npos) | 138 | if(nolb==string::npos) |
139 | break; | 139 | break; |
140 | p = nolb; | 140 | p = nolb; |
141 | #endif /* POSTELS_LAW */ | 141 | #endif /* POSTELS_LAW */ |
142 | } | 142 | } |
143 | } | 143 | } |
144 | 144 | ||
145 | struct __om_kv_outputter : public unary_function<const string&,void> { | 145 | struct __om_kv_outputter : public unary_function<const string&,void> { |
146 | public: | 146 | public: |
147 | const basic_openid_message& om; | 147 | const basic_openid_message& om; |
148 | ostream& os; | 148 | ostream& os; |
149 | 149 | ||
150 | __om_kv_outputter(const basic_openid_message& om,ostream& os) | 150 | __om_kv_outputter(const basic_openid_message& om,ostream& os) |
151 | : om(om), os(os) { } | 151 | : om(om), os(os) { } |
152 | 152 | ||
153 | result_type operator()(argument_type f) { | 153 | result_type operator()(argument_type f) { |
154 | os << f << ':' << om.get_field(f) << '\n'; | 154 | os << f << ':' << om.get_field(f) << '\n'; |
155 | } | 155 | } |
156 | }; | 156 | }; |
157 | 157 | ||
158 | void basic_openid_message::to_keyvalues(ostream& o) const { | 158 | void basic_openid_message::to_keyvalues(ostream& o) const { |
159 | for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o)); | 159 | for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o)); |
160 | } | 160 | } |
161 | 161 | ||
162 | struct __om_html_outputter : public unary_function<const string&,void> { | ||
163 | public: | ||
164 | const basic_openid_message& om; | ||
165 | ostream& os; | ||
166 | |||
167 | __om_html_outputter(const basic_openid_message& om,ostream& os) | ||
168 | : om(om), os(os) { } | ||
169 | |||
170 | result_type operator()(argument_type f) { | ||
171 | os << | ||
172 | "<input type=\"hidden\"" | ||
173 | " name=\"" << util::attr_escape(f) << "\"" | ||
174 | " value=\"" << util::attr_escape(om.get_field(f)) << "\" />"; | ||
175 | } | ||
176 | }; | ||
177 | |||
178 | void basic_openid_message::to_htmlhiddens(ostream& o) const { | ||
179 | for_each(fields_begin(),fields_end(),__om_html_outputter(*this,o)); | ||
180 | } | ||
181 | |||
162 | void basic_openid_message::add_to_signed(const string& fields) { | 182 | void basic_openid_message::add_to_signed(const string& fields) { |
163 | string::size_type fnc = fields.find_first_not_of(","); | 183 | string::size_type fnc = fields.find_first_not_of(","); |
164 | if(fnc==string::npos) | 184 | if(fnc==string::npos) |
165 | throw bad_input(OPKELE_CP_ "Trying to add nothing in particular to the list of signed fields"); | 185 | throw bad_input(OPKELE_CP_ "Trying to add nothing in particular to the list of signed fields"); |
166 | string signeds; | 186 | string signeds; |
167 | try { | 187 | try { |
168 | signeds = get_field("signed"); | 188 | signeds = get_field("signed"); |
169 | string::size_type lnc = signeds.find_last_not_of(","); | 189 | string::size_type lnc = signeds.find_last_not_of(","); |
170 | if(lnc==string::npos) | 190 | if(lnc==string::npos) |
171 | signeds.assign(fields,fnc,fields.size()-fnc); | 191 | signeds.assign(fields,fnc,fields.size()-fnc); |
172 | else{ | 192 | else{ |
173 | string::size_type ss = signeds.size(); | 193 | string::size_type ss = signeds.size(); |
174 | if(lnc==(ss-1)) { | 194 | if(lnc==(ss-1)) { |
175 | signeds+= ','; | 195 | signeds+= ','; |
176 | signeds.append(fields,fnc,fields.size()-fnc); | 196 | signeds.append(fields,fnc,fields.size()-fnc); |
177 | }else{ | 197 | }else{ |
178 | if(lnc<(ss-2)) | 198 | if(lnc<(ss-2)) |
179 | signeds.replace(lnc+2,ss-lnc-2, | 199 | signeds.replace(lnc+2,ss-lnc-2, |
180 | fields,fnc,fields.size()-fnc); | 200 | fields,fnc,fields.size()-fnc); |
181 | else | 201 | else |
182 | signeds.append(fields,fnc,fields.size()-fnc); | 202 | signeds.append(fields,fnc,fields.size()-fnc); |
183 | } | 203 | } |
184 | } | 204 | } |
185 | }catch(failed_lookup&) { | 205 | }catch(failed_lookup&) { |
186 | signeds.assign(fields,fnc,fields.size()-fnc); | 206 | signeds.assign(fields,fnc,fields.size()-fnc); |
187 | } | 207 | } |
188 | set_field("signed",signeds); | 208 | set_field("signed",signeds); |
189 | } | 209 | } |
190 | 210 | ||
191 | string basic_openid_message::find_ns(const string& uri,const char *pfx) const { | 211 | string basic_openid_message::find_ns(const string& uri,const char *pfx) const { |
192 | if(has_field("ns")) | 212 | if(has_field("ns")) |
193 | return get_ns(uri); | 213 | return get_ns(uri); |
194 | return pfx; | 214 | return pfx; |
195 | } | 215 | } |
196 | string basic_openid_message::allocate_ns(const string& uri,const char *pfx) { | 216 | string basic_openid_message::allocate_ns(const string& uri,const char *pfx) { |
197 | if(!has_field("ns")) | 217 | if(!has_field("ns")) |
198 | return pfx; | 218 | return pfx; |
199 | if(has_ns(uri)) | 219 | if(has_ns(uri)) |
200 | throw bad_input(OPKELE_CP_ "OpenID message already contains namespace"); | 220 | throw bad_input(OPKELE_CP_ "OpenID message already contains namespace"); |
201 | string rv = pfx; | 221 | string rv = pfx; |
202 | if(has_field("ns."+rv)) { | 222 | if(has_field("ns."+rv)) { |
203 | string::reference c=rv[rv.length()]; | 223 | string::reference c=rv[rv.length()]; |
204 | for(c='a';c<='z' && has_field("ns."+rv);++c); | 224 | for(c='a';c<='z' && has_field("ns."+rv);++c); |
205 | if(c=='z') | 225 | if(c=='z') |
206 | throw exception(OPKELE_CP_ "Failed to allocate namespace"); | 226 | throw exception(OPKELE_CP_ "Failed to allocate namespace"); |
207 | } | 227 | } |
208 | set_field("ns."+rv,uri); | 228 | set_field("ns."+rv,uri); |
209 | return rv; | 229 | return rv; |