-rw-r--r-- | include/opkele/oauth.h | 22 | ||||
-rw-r--r-- | include/opkele/oauth/consumer.h | 124 | ||||
-rw-r--r-- | include/opkele/types.h | 18 |
3 files changed, 164 insertions, 0 deletions
diff --git a/include/opkele/oauth.h b/include/opkele/oauth.h new file mode 100644 index 0000000..14d0586 --- a/dev/null +++ b/include/opkele/oauth.h | |||
@@ -0,0 +1,22 @@ | |||
1 | #ifndef __OPKELE_OAUTH_H | ||
2 | #define __OPKELE_OAUTH_H | ||
3 | |||
4 | #include <string> | ||
5 | |||
6 | namespace opkele { | ||
7 | namespace oauth { | ||
8 | using std::string; | ||
9 | |||
10 | struct token_t { | ||
11 | string key; | ||
12 | string secret; | ||
13 | |||
14 | token_t() { } | ||
15 | token_t(const string& k,const string& s) | ||
16 | : key(k), secret(s) { } | ||
17 | }; | ||
18 | |||
19 | } | ||
20 | } | ||
21 | |||
22 | #endif /* __OPKELE_OAUTH_H */ | ||
diff --git a/include/opkele/oauth/consumer.h b/include/opkele/oauth/consumer.h new file mode 100644 index 0000000..1e2784c --- a/dev/null +++ b/include/opkele/oauth/consumer.h | |||
@@ -0,0 +1,124 @@ | |||
1 | #ifndef __OPKELE_OAUTH_CONSUMER_H | ||
2 | #define __OPKELE_OAUTH_CONSUMER_H | ||
3 | |||
4 | #include <string> | ||
5 | #include <opkele/types.h> | ||
6 | #include <opkele/oauth.h> | ||
7 | |||
8 | namespace opkele { | ||
9 | namespace oauth { | ||
10 | using std::string; | ||
11 | |||
12 | enum oauth_method_t { | ||
13 | oauth_auth_header, oauth_post_body, oauth_url_query | ||
14 | }; | ||
15 | |||
16 | struct service_endpoint_t { | ||
17 | string url; | ||
18 | string signature_method; | ||
19 | oauth_method_t oauth_method; | ||
20 | |||
21 | service_endpoint_t(const string& u,const string& sm,oauth_method_t om) | ||
22 | : url(u), signature_method(sm), oauth_method(om) { } | ||
23 | }; | ||
24 | |||
25 | class basic_provider_endpoints { | ||
26 | public: | ||
27 | |||
28 | virtual ~basic_provider_endpoints() { } | ||
29 | |||
30 | virtual const service_endpoint_t& get_request_token_endpoint() const = 0; | ||
31 | virtual const service_endpoint_t& get_authorize_user_endpoint() const = 0; | ||
32 | virtual const service_endpoint_t& get_access_token_endpoint() const = 0; | ||
33 | |||
34 | virtual service_endpoint_t& get_url_endpoint(service_endpoint_t& sep, | ||
35 | const string& url) const = 0; | ||
36 | }; | ||
37 | |||
38 | struct http_request_t { | ||
39 | string authorize_header; | ||
40 | string method; | ||
41 | string url; | ||
42 | string body; | ||
43 | |||
44 | http_request_t(const string& m,const string& u) | ||
45 | : method(m), url(u) { } | ||
46 | }; | ||
47 | |||
48 | class basic_consumer { | ||
49 | public: | ||
50 | token_t consumer_token; | ||
51 | |||
52 | basic_consumer(const token_t& ct) | ||
53 | : consumer_token(ct) { } | ||
54 | virtual ~basic_consumer() { } | ||
55 | |||
56 | virtual const basic_provider_endpoints& get_endpoints() const = 0; | ||
57 | virtual const string allocate_nonce(time_t ts) = 0; | ||
58 | |||
59 | token_t get_request_token(); | ||
60 | const string get_authorize_url(const token_t& rt,const string& callback=""); | ||
61 | token_t get_access_token(const token_t& rt); | ||
62 | |||
63 | void prepare_request( | ||
64 | http_request_t& req, | ||
65 | const basic_fields& qf,const basic_fields& pf, | ||
66 | oauth_method_t om,const string& sm, | ||
67 | const token_t *t=0,const string& realm=""); | ||
68 | void prepare_request( | ||
69 | http_request_t& req, | ||
70 | const basic_fields& qf,const basic_fields& pf, | ||
71 | const service_endpoint_t& sep, | ||
72 | const token_t *t=0,const string& realm=""); | ||
73 | |||
74 | const string signature( | ||
75 | const string& method, | ||
76 | const string& url, | ||
77 | const basic_fields& fields, | ||
78 | const token_t* rt=0); | ||
79 | |||
80 | token_t acquire_token( | ||
81 | const service_endpoint_t& sep, | ||
82 | const token_t* rt=0); | ||
83 | }; | ||
84 | |||
85 | class simple_provider_endpoints : public basic_provider_endpoints { | ||
86 | public: | ||
87 | service_endpoint_t sep_request_token; | ||
88 | service_endpoint_t sep_authorize_user; | ||
89 | service_endpoint_t sep_access_token; | ||
90 | service_endpoint_t sep_generic; | ||
91 | |||
92 | simple_provider_endpoints( | ||
93 | const string& rt,const string& au,const string& at, | ||
94 | const string& sm, | ||
95 | oauth_method_t ams=oauth_post_body, | ||
96 | oauth_method_t amr=oauth_auth_header ) | ||
97 | : sep_request_token(rt,sm,ams), | ||
98 | sep_authorize_user(au,sm,oauth_url_query), | ||
99 | sep_access_token(at,sm,ams), | ||
100 | sep_generic("",sm,amr) { } | ||
101 | |||
102 | const service_endpoint_t& get_request_token_endpoint() const; | ||
103 | const service_endpoint_t& get_authorize_user_endpoint() const; | ||
104 | const service_endpoint_t& get_access_token_endpoint() const; | ||
105 | service_endpoint_t& get_url_endpoint(service_endpoint_t& sep, | ||
106 | const string& url) const; | ||
107 | }; | ||
108 | |||
109 | class simple_consumer : public basic_consumer { | ||
110 | public: | ||
111 | simple_provider_endpoints peps; | ||
112 | |||
113 | simple_consumer(const simple_provider_endpoints& eps, | ||
114 | const token_t& ct) | ||
115 | : basic_consumer(ct), peps(eps) { } | ||
116 | |||
117 | const basic_provider_endpoints& get_endpoints() const; | ||
118 | const string allocate_nonce(time_t ts); | ||
119 | }; | ||
120 | |||
121 | } | ||
122 | } | ||
123 | |||
124 | #endif /* __OPKELE_OAUTH_CONSUMER_H */ | ||
diff --git a/include/opkele/types.h b/include/opkele/types.h index f63bf5d..4471e6a 100644 --- a/include/opkele/types.h +++ b/include/opkele/types.h | |||
@@ -1,243 +1,261 @@ | |||
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_fields { | 121 | class basic_fields { |
122 | public: | 122 | public: |
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_fields() { } | 127 | basic_fields() { } |
128 | virtual ~basic_fields() { } | 128 | virtual ~basic_fields() { } |
129 | basic_fields(const basic_fields& x); | 129 | basic_fields(const basic_fields& x); |
130 | void copy_to(basic_fields& x) const; | 130 | void copy_to(basic_fields& x) const; |
131 | void append_to(basic_fields& x) const; | 131 | void append_to(basic_fields& x) const; |
132 | 132 | ||
133 | virtual bool has_field(const string& n) const = 0; | 133 | virtual bool has_field(const string& n) const = 0; |
134 | virtual const string& get_field(const string& n) const = 0; | 134 | virtual const string& get_field(const string& n) const = 0; |
135 | 135 | ||
136 | virtual fields_iterator fields_begin() const = 0; | 136 | virtual fields_iterator fields_begin() const = 0; |
137 | virtual fields_iterator fields_end() const = 0; | 137 | virtual fields_iterator fields_end() const = 0; |
138 | 138 | ||
139 | virtual string append_query(const string& url,const char *pfx=0) const; | 139 | virtual string append_query(const string& url,const char *pfx=0) const; |
140 | virtual string query_string(const char *pfx=0) const; | 140 | virtual string query_string(const char *pfx=0) const; |
141 | 141 | ||
142 | virtual void reset_fields(); | 142 | virtual void reset_fields(); |
143 | virtual void set_field(const string& n,const string& v); | 143 | virtual void set_field(const string& n,const string& v); |
144 | virtual void reset_field(const string& n); | 144 | virtual void reset_field(const string& n); |
145 | 145 | ||
146 | void from_query(const string& qs); | ||
147 | }; | ||
148 | |||
149 | class fields_t : public basic_fields, public map<string,string> { | ||
150 | public: | ||
151 | fields_t() { } | ||
152 | fields_t(const basic_fields& x) | ||
153 | : basic_fields(x) { } | ||
154 | |||
155 | bool has_field(const string& n) const; | ||
156 | const string& get_field(const string& n) const; | ||
157 | |||
158 | virtual fields_iterator fields_begin() const; | ||
159 | virtual fields_iterator fields_end() const; | ||
160 | |||
161 | virtual void reset_fields(); | ||
162 | virtual void set_field(const string& n,const string& v); | ||
163 | virtual void reset_field(const string& n); | ||
146 | }; | 164 | }; |
147 | 165 | ||
148 | class basic_openid_message : public basic_fields { | 166 | class basic_openid_message : public basic_fields { |
149 | public: | 167 | public: |
150 | 168 | ||
151 | basic_openid_message() { } | 169 | basic_openid_message() { } |
152 | basic_openid_message(const basic_openid_message& x); | 170 | basic_openid_message(const basic_openid_message& x); |
153 | 171 | ||
154 | virtual bool has_ns(const string& uri) const; | 172 | virtual bool has_ns(const string& uri) const; |
155 | virtual string get_ns(const string& uri) const; | 173 | virtual string get_ns(const string& uri) const; |
156 | 174 | ||
157 | virtual string append_query(const string& url,const char *pfx="openid.") const { | 175 | virtual string append_query(const string& url,const char *pfx="openid.") const { |
158 | return basic_fields::append_query(url,pfx); } | 176 | return basic_fields::append_query(url,pfx); } |
159 | virtual string query_string(const char *pfx="openid.") const { | 177 | virtual string query_string(const char *pfx="openid.") const { |
160 | return basic_fields::query_string(pfx); } | 178 | return basic_fields::query_string(pfx); } |
161 | 179 | ||
162 | virtual void from_keyvalues(const string& kv); | 180 | virtual void from_keyvalues(const string& kv); |
163 | virtual void to_keyvalues(ostream& o) const; | 181 | virtual void to_keyvalues(ostream& o) const; |
164 | 182 | ||
165 | virtual void to_htmlhiddens(ostream& o,const char* pfx=0) const; | 183 | virtual void to_htmlhiddens(ostream& o,const char* pfx=0) const; |
166 | 184 | ||
167 | void add_to_signed(const string& fields); | 185 | void add_to_signed(const string& fields); |
168 | string find_ns(const string& uri,const char *pfx) const; | 186 | string find_ns(const string& uri,const char *pfx) const; |
169 | string allocate_ns(const string& uri,const char *pfx); | 187 | string allocate_ns(const string& uri,const char *pfx); |
170 | }; | 188 | }; |
171 | 189 | ||
172 | class openid_message_t : public basic_openid_message, public map<string,string> { | 190 | class openid_message_t : public basic_openid_message, public map<string,string> { |
173 | public: | 191 | public: |
174 | openid_message_t() { } | 192 | openid_message_t() { } |
175 | openid_message_t(const basic_openid_message& x) | 193 | openid_message_t(const basic_openid_message& x) |
176 | : basic_openid_message(x) { } | 194 | : basic_openid_message(x) { } |
177 | 195 | ||
178 | bool has_field(const string& n) const; | 196 | bool has_field(const string& n) const; |
179 | const string& get_field(const string& n) const; | 197 | const string& get_field(const string& n) const; |
180 | virtual fields_iterator fields_begin() const; | 198 | virtual fields_iterator fields_begin() const; |
181 | virtual fields_iterator fields_end() const; | 199 | virtual fields_iterator fields_end() const; |
182 | 200 | ||
183 | void reset_fields(); | 201 | void reset_fields(); |
184 | void set_field(const string& n,const string& v); | 202 | void set_field(const string& n,const string& v); |
185 | void reset_field(const string& n); | 203 | void reset_field(const string& n); |
186 | }; | 204 | }; |
187 | 205 | ||
188 | /** | 206 | /** |
189 | * request/response parameters map | 207 | * request/response parameters map |
190 | */ | 208 | */ |
191 | class params_t : public openid_message_t { | 209 | class params_t : public openid_message_t { |
192 | public: | 210 | public: |
193 | 211 | ||
194 | /** | 212 | /** |
195 | * check whether the parameter is present. | 213 | * check whether the parameter is present. |
196 | * @param n the parameter name | 214 | * @param n the parameter name |
197 | * @return true if yes | 215 | * @return true if yes |
198 | */ | 216 | */ |
199 | bool has_param(const string& n) const { | 217 | bool has_param(const string& n) const { |
200 | return has_field(n); } | 218 | return has_field(n); } |
201 | /** | 219 | /** |
202 | * retrieve the parameter (const version) | 220 | * retrieve the parameter (const version) |
203 | * @param n the parameter name | 221 | * @param n the parameter name |
204 | * @return the parameter value | 222 | * @return the parameter value |
205 | * @throw failed_lookup if there is no such parameter | 223 | * @throw failed_lookup if there is no such parameter |
206 | */ | 224 | */ |
207 | const string& get_param(const string& n) const { | 225 | const string& get_param(const string& n) const { |
208 | return get_field(n); } | 226 | return get_field(n); } |
209 | 227 | ||
210 | /** | 228 | /** |
211 | * parse the OpenID key/value data. | 229 | * parse the OpenID key/value data. |
212 | * @param kv the OpenID key/value data | 230 | * @param kv the OpenID key/value data |
213 | */ | 231 | */ |
214 | void parse_keyvalues(const string& kv) { | 232 | void parse_keyvalues(const string& kv) { |
215 | from_keyvalues(kv); } | 233 | from_keyvalues(kv); } |
216 | 234 | ||
217 | string append_query(const string& url,const char *prefix="openid.") const; | 235 | string append_query(const string& url,const char *prefix="openid.") const; |
218 | 236 | ||
219 | }; | 237 | }; |
220 | 238 | ||
221 | struct openid_endpoint_t { | 239 | struct openid_endpoint_t { |
222 | string uri; | 240 | string uri; |
223 | string claimed_id; | 241 | string claimed_id; |
224 | string local_id; | 242 | string local_id; |
225 | 243 | ||
226 | openid_endpoint_t() { } | 244 | openid_endpoint_t() { } |
227 | openid_endpoint_t(const string& u,const string& cid,const string& lid) | 245 | openid_endpoint_t(const string& u,const string& cid,const string& lid) |
228 | : uri(u), claimed_id(cid), local_id(lid) { } | 246 | : uri(u), claimed_id(cid), local_id(lid) { } |
229 | 247 | ||
230 | bool operator==(const openid_endpoint_t& x) const { | 248 | bool operator==(const openid_endpoint_t& x) const { |
231 | return uri==x.uri && local_id==x.local_id; } | 249 | return uri==x.uri && local_id==x.local_id; } |
232 | bool operator<(const openid_endpoint_t& x) const { | 250 | bool operator<(const openid_endpoint_t& x) const { |
233 | int c; | 251 | int c; |
234 | return (c=strcmp(uri.c_str(),x.uri.c_str())) | 252 | return (c=strcmp(uri.c_str(),x.uri.c_str())) |
235 | ? (c<0) : (strcmp(local_id.c_str(),x.local_id.c_str())<0); } | 253 | ? (c<0) : (strcmp(local_id.c_str(),x.local_id.c_str())<0); } |
236 | }; | 254 | }; |
237 | 255 | ||
238 | typedef util::output_iterator_proxy<openid_endpoint_t> | 256 | typedef util::output_iterator_proxy<openid_endpoint_t> |
239 | openid_endpoint_output_iterator; | 257 | openid_endpoint_output_iterator; |
240 | 258 | ||
241 | } | 259 | } |
242 | 260 | ||
243 | #endif /* __OPKELE_TYPES_H */ | 261 | #endif /* __OPKELE_TYPES_H */ |