1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
/* 
  Author: Pontus Östlund <https://profiles.google.com/poppanator> 
 
  Permission to copy, modify, and distribute this source for any legal 
  purpose granted as long as my name is still attached to it. More 
  specifically, the GPL, LGPL and MPL licenses apply to this software. 
*/ 
 
//! OAuth client class 
//! 
//! @note 
//!  This class is of no use by it self. It's intended to be inherited by 
//!  classes that uses OAuth authorization. 
 
#include "oauth.h" 
 
import "."; 
 
//#define assert_cache() if (!cache) return UNDEFINED 
 
//! The consumer obejct 
protected Consumer consumer; 
 
//! The token object 
protected Token token; 
 
//! The endpoint to send request for a request token 
protected string request_token_url; 
 
//! The endpoint to send request for an access token 
protected string access_token_url; 
 
//! The enpoint to redirect to when authorize an application 
protected string user_auth_url; 
 
// ! Data cache object 
//protected DataCache cache; 
 
//! Create a new @[Client]. 
//! 
//! @note 
//!  This class must be inherited 
protected void create(Consumer _consumer, Token _token) 
{ 
  consumer = _consumer; 
  token    = _token; 
} 
 
//! Returns the @[consumer] 
Consumer get_consumer() 
{ 
  return consumer; 
} 
 
//! Returns the @[token] 
Token get_token() 
{ 
  return token; 
} 
 
//! Set the Token 
//! 
//! decl set_token(Token token) 
//! decl set_token(string token_key, string token_secret) 
//! 
//! @param key 
//!  Either a @[Token()] object or a token key 
//! @param secret 
//!  The token secret if @[key] is a token key 
void set_token(Token|string key, void|string secret) 
{ 
  token = stringp(key) ? Token(key, secret) : key; 
} 
 
//! Returns the url for requesting a request token 
string get_request_token_url() 
{ 
  return request_token_url; 
} 
 
//! Returns the url for requesting an access token 
string get_access_token_url() 
{ 
  return access_token_url; 
} 
 
//! Returns the url for authorizing an application 
string get_user_auth_url() 
{ 
  return user_auth_url; 
} 
 
#if 0 
//! Generates a cache key 
string get_cache_key(string key) 
{ 
  return MD5(consumer->secret + key); 
} 
 
//! Tries to find the entry @[key] in the cache 
mixed get_cache(string key) 
{ 
  assert_cache(); 
  return cache->get(get_cache_key(key)); 
} 
 
//! Add to cache 
//! 
//! @param key 
//! @param value 
//! @param maxlife 
void set_cache(string key, string value, void|int maxlife) 
{ 
  assert_cache(); 
  cache->set(get_cache_key(key), value, maxlife); 
} 
 
//! Delete the entry with key @[key] from the cache 
void delete_cache(string key) 
{ 
  assert_cache(); 
  cache->delete(get_cache_key(key)); 
} 
 
//! Returns the cache object 
DataCache get_cache_obj() 
{ 
  assert_cache(); 
  return cache; 
} 
 
//! Abstract class for cache handling 
class DataCache 
{ 
  protected mixed cache; 
 
  protected void create() {} 
 
  //! Return cache item with key @[key] 
  //! 
  //! @param key 
  mixed get(string key); 
 
  //! Set cache. 
  //! 
  //! @param key 
  //! @param value 
  //! @param maxlife 
  //!  Number of seconds the cache should live 
  void set(string key, mixed value, void|int maxlife); 
 
  //! Delete item with key @[key] from the cache 
  //! 
  //! @param key 
  void delete(string key); 
} 
#endif