summaryrefslogtreecommitdiffabout
path: root/include/opkele/types.h
Unidiff
Diffstat (limited to 'include/opkele/types.h') (more/less context) (ignore whitespace changes)
-rw-r--r--include/opkele/types.h171
1 files changed, 66 insertions, 105 deletions
diff --git a/include/opkele/types.h b/include/opkele/types.h
index de44a5c..d5ad258 100644
--- a/include/opkele/types.h
+++ b/include/opkele/types.h
@@ -1,46 +1,51 @@
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 <ostream> 9#include <ostream>
10#include <vector> 10#include <vector>
11#include <string> 11#include <string>
12#include <map> 12#include <map>
13#include <set> 13#include <set>
14#include <list>
15#include <opkele/iterator.h>
14#include <opkele/tr1-mem.h> 16#include <opkele/tr1-mem.h>
15 17
16namespace opkele { 18namespace opkele {
17 using std::vector; 19 using std::vector;
18 using std::string; 20 using std::string;
19 using std::map; 21 using std::map;
20 using std::ostream; 22 using std::ostream;
21 using std::multimap; 23 using std::multimap;
22 using std::set; 24 using std::set;
25 using std::list;
26 using std::iterator;
27 using std::forward_iterator_tag;
23 28
24 /** 29 /**
25 * the OpenID operation mode 30 * the OpenID operation mode
26 */ 31 */
27 typedef enum _mode_t { 32 typedef enum _mode_t {
28 mode_associate, 33 mode_associate,
29 mode_checkid_immediate, 34 mode_checkid_immediate,
30 mode_checkid_setup, 35 mode_checkid_setup,
31 mode_check_association 36 mode_check_association
32 } mode_t; 37 } mode_t;
33 38
34 /** 39 /**
35 * the association secret container 40 * the association secret container
36 */ 41 */
37 class secret_t : public vector<unsigned char> { 42 class secret_t : public vector<unsigned char> {
38 public: 43 public:
39 44
40 /** 45 /**
41 * xor the secret and hmac together and encode, using base64 46 * xor the secret and hmac together and encode, using base64
42 * @param key_d pointer to the message digest 47 * @param key_d pointer to the message digest
43 * @param rv reference to the return value 48 * @param rv reference to the return value
44 */ 49 */
45 void enxor_to_base64(const unsigned char *key_d,string& rv) const; 50 void enxor_to_base64(const unsigned char *key_d,string& rv) const;
46 /** 51 /**
@@ -90,157 +95,113 @@ namespace opkele {
90 */ 95 */
91 virtual secret_t secret() const = 0; 96 virtual secret_t secret() const = 0;
92 /** 97 /**
93 * retrieve the number of seconds the association expires in. 98 * retrieve the number of seconds the association expires in.
94 * @return seconds till expiration 99 * @return seconds till expiration
95 */ 100 */
96 virtual int expires_in() const = 0; 101 virtual int expires_in() const = 0;
97 /** 102 /**
98 * check whether the association is stateless. 103 * check whether the association is stateless.
99 * @return true if stateless 104 * @return true if stateless
100 */ 105 */
101 virtual bool stateless() const = 0; 106 virtual bool stateless() const = 0;
102 /** 107 /**
103 * check whether the association is expired. 108 * check whether the association is expired.
104 * @return true if expired 109 * @return true if expired
105 */ 110 */
106 virtual bool is_expired() const = 0; 111 virtual bool is_expired() const = 0;
107 }; 112 };
108 113
109 /** 114 /**
110 * the shared_ptr<> for association_t object type 115 * the shared_ptr<> for association_t object type
111 */ 116 */
112 typedef tr1mem::shared_ptr<association_t> assoc_t; 117 typedef tr1mem::shared_ptr<association_t> assoc_t;
113 118
119 class basic_openid_message {
120 public:
121 typedef list<string> fields_t;
122 typedef util::forward_iterator_proxy<
123 string,const string&,const string*
124 > fields_iterator;
125
126 basic_openid_message() { }
127 basic_openid_message(const basic_openid_message& x);
128 void copy_to(basic_openid_message& x) const;
129
130 virtual bool has_field(const string& n) const = 0;
131 virtual const string& get_field(const string& n) const = 0;
132
133 virtual bool has_ns(const string& uri) const;
134 virtual string get_ns(const string& uri) const;
135
136 virtual fields_iterator fields_begin() const = 0;
137 virtual fields_iterator fields_end() const = 0;
138
139 virtual string append_query(const string& url) const;
140 virtual string query_string() const;
141
142
143 virtual void reset_fields();
144 virtual void set_field(const string& n,const string& v);
145 virtual void reset_field(const string& n);
146
147 virtual void from_keyvalues(const string& kv);
148
149 void add_to_signed(const string& fields);
150 string find_ns(const string& uri,const char *pfx) const;
151 string allocate_ns(const string& uri,const char *pfx);
152 };
153
154 class openid_message_t : public basic_openid_message, public map<string,string> {
155 public:
156 openid_message_t() { }
157 openid_message_t(const basic_openid_message& x)
158 : basic_openid_message(x) { }
159
160 void copy_to(basic_openid_message& x) const;
161
162 bool has_field(const string& n) const;
163 const string& get_field(const string& n) const;
164 virtual fields_iterator fields_begin() const;
165 virtual fields_iterator fields_end() const;
166
167 void reset_fields();
168 void set_field(const string& n,const string& v);
169 void reset_field(const string& n);
170 };
171
114 /** 172 /**
115 * request/response parameters map 173 * request/response parameters map
116 */ 174 */
117 class params_t : public map<string,string> { 175 class params_t : public openid_message_t {
118 public: 176 public:
119 177
120 /** 178 /**
121 * check whether the parameter is present. 179 * check whether the parameter is present.
122 * @param n the parameter name 180 * @param n the parameter name
123 * @return true if yes 181 * @return true if yes
124 */ 182 */
125 bool has_param(const string& n) const; 183 bool has_param(const string& n) const {
184 return has_field(n); }
126 /** 185 /**
127 * retrieve the parameter (const version) 186 * retrieve the parameter (const version)
128 * @param n the parameter name 187 * @param n the parameter name
129 * @return the parameter value 188 * @return the parameter value
130 * @throw failed_lookup if there is no such parameter 189 * @throw failed_lookup if there is no such parameter
131 */ 190 */
132 const string& get_param(const string& n) const; 191 const string& get_param(const string& n) const {
133 /** 192 return get_field(n); }
134 * retrieve the parameter.
135 * @param n the parameter name
136 * @return the parameter value
137 * @throw failed_lookup if there is no such parameter
138 */
139 string& get_param(const string& n);
140 193
141 /** 194 /**
142 * parse the OpenID key/value data. 195 * parse the OpenID key/value data.
143 * @param kv the OpenID key/value data 196 * @param kv the OpenID key/value data
144 */ 197 */
145 void parse_keyvalues(const string& kv); 198 void parse_keyvalues(const string& kv) {
146 /** 199 from_keyvalues(kv); }
147 * sign the fields.
148 * @param secret the secret used for signing
149 * @param sig reference to the string, containing base64-encoded
150 * result
151 * @param slist the comma-separated list of fields to sign
152 * @param prefix the string to prepend to parameter names
153 */
154 void sign(secret_t secret,string& sig,const string& slist,const char *prefix=0) const;
155 200
156 /** 201 string append_query(const string& url,const char *prefix="openid.") const;
157 * append parameters to the URL as a GET-request parameters.
158 * @param url the base URL
159 * @param prefix the string to prepend to parameter names
160 * @return the ready-to-use location
161 */
162 string append_query(const string& url,const char *prefix = "openid.") const;
163 202
164 /**
165 * make up a query string suitable for use in GET and POST
166 * requests.
167 * @param prefix string to prened to parameter names
168 * @return query string
169 */
170 string query_string(const char *prefix = "openid.") const;
171 }; 203 };
172 204
173 /**
174 * dump the key/value pairs for the parameters to the stream.
175 * @param o output stream
176 * @param p the parameters
177 */
178 ostream& operator << (ostream& o,const params_t& p);
179
180 namespace xrd {
181
182 struct priority_compare {
183 inline bool operator()(long a,long b) const {
184 return (a<0) ? false : (b<0) ? true : (a<b);
185 }
186 };
187
188 template <typename _DT>
189 class priority_map : public multimap<long,_DT,priority_compare> {
190 typedef multimap<long,_DT,priority_compare> map_type;
191 public:
192
193 inline _DT& add(long priority,const _DT& d) {
194 return insert(typename map_type::value_type(priority,d))->second;
195 }
196 };
197
198 typedef priority_map<string> canonical_ids_t;
199 typedef priority_map<string> local_ids_t;
200 typedef set<string> types_t;
201 typedef priority_map<string> uris_t;
202
203 class service_t {
204 public:
205 types_t types;
206 uris_t uris;
207 local_ids_t local_ids;
208 string provider_id;
209
210 void clear() {
211 types.clear();
212 uris.clear(); local_ids.clear();
213 provider_id.clear();
214 }
215 };
216 typedef priority_map<service_t> services_t;
217
218 class XRD_t {
219 public:
220 time_t expires;
221
222 canonical_ids_t canonical_ids;
223 local_ids_t local_ids;
224 services_t services;
225 string provider_id;
226
227 void clear() {
228 expires = 0;
229 canonical_ids.clear(); local_ids.clear();
230 services.clear();
231 provider_id.clear();
232 }
233 bool empty() const {
234 return
235 canonical_ids.empty()
236 && local_ids.empty()
237 && services.empty();
238 }
239
240 };
241
242 }
243
244} 205}
245 206
246#endif /* __OPKELE_TYPES_H */ 207#endif /* __OPKELE_TYPES_H */