summaryrefslogtreecommitdiffabout
path: root/lib/openid_message.cc
authorMichael Krelin <hacker@klever.net>2008-03-03 15:57:15 (UTC)
committer Michael Krelin <hacker@klever.net>2008-03-03 15:57:15 (UTC)
commitc28479399ef0fedeb6bf14ec665bb4c427654356 (patch) (unidiff)
treeff1dfe84a54f717772bdb6233a5cbfedb57788d1 /lib/openid_message.cc
parent0182b9dee269f1a8f3fc0794bfdf4a143fa1b5be (diff)
downloadlibopkele-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>
Diffstat (limited to 'lib/openid_message.cc') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/openid_message.cc276
1 files changed, 0 insertions, 276 deletions
diff --git a/lib/openid_message.cc b/lib/openid_message.cc
deleted file mode 100644
index e244f43..0000000
--- a/lib/openid_message.cc
+++ b/dev/null
@@ -1,276 +0,0 @@
1#include <cassert>
2#include <opkele/types.h>
3#include <opkele/exception.h>
4#include <opkele/util.h>
5#include <opkele/debug.h>
6
7#include "config.h"
8
9namespace opkele {
10 using std::input_iterator_tag;
11 using std::unary_function;
12
13 struct __om_copier : public unary_function<const string&,void> {
14 public:
15 const basic_openid_message& from;
16 basic_openid_message& to;
17
18 __om_copier(basic_openid_message& t,const basic_openid_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_openid_message::basic_openid_message(const basic_openid_message& x) {
26 x.copy_to(*this);
27 }
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 {
34 for_each(fields_begin(),fields_end(),
35 __om_copier(x,*this) );
36 }
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> {
69 public:
70 const basic_openid_message& om;
71 bool first;
72 string& rv;
73 const char *pfx;
74
75 __om_query_builder(const char *p,string& r,const basic_openid_message& m)
76 : om(m), first(true), rv(r), pfx(p) {
77 for_each(om.fields_begin(),om.fields_end(),*this);
78 }
79 __om_query_builder(const char *p,string& r,const basic_openid_message& m,const string& u)
80 : om(m), first(true), rv(r), pfx(p) {
81 rv = u;
82 if(rv.find('?')==string::npos)
83 rv += '?';
84 else
85 first = false;
86 for_each(om.fields_begin(),om.fields_end(),*this);
87 }
88
89 result_type operator()(argument_type f) {
90 if(first)
91 first = false;
92 else
93 rv += '&';
94 if(pfx) rv += pfx;
95 rv+= f;
96 rv += '=';
97 rv += util::url_encode(om.get_field(f));
98 }
99 };
100
101 string basic_openid_message::append_query(const string& url,const char *pfx) const {
102 string rv;
103 return __om_query_builder(pfx,rv,*this,url).rv;
104 }
105 string basic_openid_message::query_string(const char *pfx) const {
106 string rv;
107 return __om_query_builder(pfx,rv,*this).rv;
108 }
109
110 void basic_openid_message::reset_fields() {
111 throw not_implemented(OPKELE_CP_ "reset_fields() not implemented");
112 }
113 void basic_openid_message::set_field(const string&,const string&) {
114 throw not_implemented(OPKELE_CP_ "set_field() not implemented");
115 }
116 void basic_openid_message::reset_field(const string&) {
117 throw not_implemented(OPKELE_CP_ "reset_field() not implemented");
118 }
119
120 void basic_openid_message::from_keyvalues(const string& kv) {
121 reset_fields();
122 string::size_type p = 0;
123 while(true) {
124 string::size_type co = kv.find(':',p);
125 if(co==string::npos)
126 break;
127#ifndef POSTELS_LAW
128 string::size_type nl = kv.find('\n',co+1);
129 if(nl==string::npos)
130 throw bad_input(OPKELE_CP_ "malformed input");
131 if(nl>co)
132 insert(value_type(kv.substr(p,co-p),kv.substr(co+1,nl-co-1)));
133 p = nl+1;
134#else /* POSTELS_LAW */
135 string::size_type lb = kv.find_first_of("\r\n",co+1);
136 if(lb==string::npos) {
137 set_field(kv.substr(p,co-p),kv.substr(co+1));
138 break;
139 }
140 if(lb>co)
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);
143 if(nolb==string::npos)
144 break;
145 p = nolb;
146#endif /* POSTELS_LAW */
147 }
148 }
149
150 struct __om_kv_outputter : public unary_function<const string&,void> {
151 public:
152 const basic_openid_message& om;
153 ostream& os;
154
155 __om_kv_outputter(const basic_openid_message& m,ostream& s)
156 : om(m), os(s) { }
157
158 result_type operator()(argument_type f) {
159 os << f << ':' << om.get_field(f) << '\n';
160 }
161 };
162
163 void basic_openid_message::to_keyvalues(ostream& o) const {
164 for_each(fields_begin(),fields_end(),__om_kv_outputter(*this,o));
165 }
166
167 struct __om_html_outputter : public unary_function<const string&,void> {
168 public:
169 const basic_openid_message& om;
170 ostream& os;
171 const char *pfx;
172
173 __om_html_outputter(const basic_openid_message& m,ostream& s,const char *p=0)
174 : om(m), os(s), pfx(p) { }
175
176 result_type operator()(argument_type f) {
177 os <<
178 "<input type=\"hidden\""
179 " name=\"";
180 if(pfx)
181 os << util::attr_escape(pfx);
182 os << util::attr_escape(f) << "\""
183 " value=\"" << util::attr_escape(om.get_field(f)) << "\" />";
184 }
185 };
186
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));
189 }
190
191 void basic_openid_message::add_to_signed(const string& fields) {
192 string::size_type fnc = fields.find_first_not_of(",");
193 if(fnc==string::npos)
194 throw bad_input(OPKELE_CP_ "Trying to add nothing in particular to the list of signed fields");
195 string signeds;
196 try {
197 signeds = get_field("signed");
198 string::size_type lnc = signeds.find_last_not_of(",");
199 if(lnc==string::npos)
200 signeds.assign(fields,fnc,fields.size()-fnc);
201 else{
202 string::size_type ss = signeds.size();
203 if(lnc==(ss-1)) {
204 signeds+= ',';
205 signeds.append(fields,fnc,fields.size()-fnc);
206 }else{
207 if(lnc<(ss-2))
208 signeds.replace(lnc+2,ss-lnc-2,
209 fields,fnc,fields.size()-fnc);
210 else
211 signeds.append(fields,fnc,fields.size()-fnc);
212 }
213 }
214 }catch(failed_lookup&) {
215 signeds.assign(fields,fnc,fields.size()-fnc);
216 }
217 set_field("signed",signeds);
218 }
219
220 string basic_openid_message::find_ns(const string& uri,const char *pfx) const {
221 try {
222 return get_ns(uri);
223 }catch(failed_lookup&) {
224 return pfx;
225 }
226 }
227 string basic_openid_message::allocate_ns(const string& uri,const char *pfx) {
228 if(!has_field("ns"))
229 return pfx;
230 if(has_ns(uri))
231 throw bad_input(OPKELE_CP_ "OpenID message already contains namespace");
232 string rv = pfx;
233 if(has_field("ns."+rv)) {
234 string::reference c=rv[rv.length()];
235 for(c='a';c<='z' && has_field("ns."+rv);++c);
236 if(c=='z')
237 throw exception(OPKELE_CP_ "Failed to allocate namespace");
238 }
239 set_field("ns."+rv,uri);
240 return rv;
241 }
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 {
250 return find(n)!=end();
251 }
252 const string& openid_message_t::get_field(const string& n) const {
253 const_iterator i=find(n);
254 if(i==end())
255 throw failed_lookup(OPKELE_CP_ n+": no such field");
256 return i->second;
257 }
258
259 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());
261 }
262 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());
264 }
265
266 void openid_message_t::reset_fields() {
267 clear();
268 }
269 void openid_message_t::set_field(const string& n,const string& v) {
270 (*this)[n]=v;
271 }
272 void openid_message_t::reset_field(const string& n) {
273 erase(n);
274 }
275
276}