-rw-r--r-- | include/Makefile.am | 1 | ||||
-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 |
4 files changed, 165 insertions, 0 deletions
diff --git a/include/Makefile.am b/include/Makefile.am index f842bb9..b41e6cc 100644 --- a/include/Makefile.am +++ b/include/Makefile.am | |||
@@ -22,2 +22,3 @@ nobase_include_HEADERS = \ | |||
22 | opkele/util.h \ | 22 | opkele/util.h \ |
23 | opkele/oauth.h opkele/oauth/consumer.h \ | ||
23 | ${NODIST_HEADERS_} | 24 | ${NODIST_HEADERS_} |
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 | |||
@@ -145,2 +145,20 @@ namespace opkele { | |||
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 | }; |