Diffstat (limited to 'include/opkele/oauth/consumer.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | include/opkele/oauth/consumer.h | 124 |
1 files changed, 124 insertions, 0 deletions
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 */ | ||