1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (C) 2015 Frederic France <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* \file htdocs/includes/OAuth/Common/Storage/DoliStorage.php |
21
|
|
|
* \ingroup oauth |
22
|
|
|
* \brief Dolibarr token storage class |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OAuth\Common\Storage; |
26
|
|
|
|
27
|
|
|
use OAuth\Common\Token\TokenInterface; |
28
|
|
|
use OAuth\Common\Storage\Exception\TokenNotFoundException; |
29
|
|
|
use OAuth\Common\Storage\Exception\AuthorizationStateNotFoundException; |
30
|
|
|
use DoliDB; |
31
|
|
|
|
32
|
|
|
class DoliStorage implements TokenStorageInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var DoliDB Database handler |
36
|
|
|
*/ |
37
|
|
|
protected $db; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var object|TokenInterface |
41
|
|
|
*/ |
42
|
|
|
protected $tokens; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string Error code (or message) |
46
|
|
|
*/ |
47
|
|
|
public $error; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string[] Several error codes (or messages) |
51
|
|
|
*/ |
52
|
|
|
public $errors = array(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var int userid |
56
|
|
|
*/ |
57
|
|
|
public $userid; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var Conf |
61
|
|
|
*/ |
62
|
|
|
private $conf; |
63
|
|
|
|
64
|
|
|
private $key; |
65
|
|
|
|
66
|
|
|
private $stateKey; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param DoliDB $db Database object |
70
|
|
|
* @param Conf $conf Conf object |
71
|
|
|
* @param string $userid userid of user |
72
|
|
|
*/ |
73
|
|
|
public function __construct(DoliDB $db, $conf, $userid = 0) |
74
|
|
|
{ |
75
|
|
|
$this->db = $db; |
76
|
|
|
$this->conf = $conf; |
77
|
|
|
$this->tokens = array(); |
|
|
|
|
78
|
|
|
$this->states = array(); |
|
|
|
|
79
|
|
|
$this->userid = $userid; |
80
|
|
|
//$this->key = $key; |
81
|
|
|
//$this->stateKey = $stateKey; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
public function retrieveAccessToken($service) |
88
|
|
|
{ |
89
|
|
|
if ($this->hasAccessToken($service)) { |
90
|
|
|
return $this->tokens[$service]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
throw new TokenNotFoundException('Token not found in db, are you sure you stored it?'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritDoc} |
98
|
|
|
*/ |
99
|
|
|
public function storeAccessToken($service, TokenInterface $token) |
100
|
|
|
{ |
101
|
|
|
//var_dump("storeAccessToken"); |
102
|
|
|
//var_dump($token); |
103
|
|
|
dol_syslog("storeAccessToken"); |
104
|
|
|
|
105
|
|
|
$serializedToken = serialize($token); |
106
|
|
|
$this->tokens[$service] = $token; |
107
|
|
|
|
108
|
|
|
if (!is_array($this->tokens)) { |
109
|
|
|
$this->tokens = array(); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."oauth_token"; |
112
|
|
|
$sql .= " WHERE service='".$this->db->escape($service)."' AND entity=".(int) $this->conf->entity; |
113
|
|
|
$sql .= " AND fk_user=".(int) $this->userid; |
114
|
|
|
$resql = $this->db->query($sql); |
115
|
|
|
if (! $resql) { |
116
|
|
|
dol_print_error($this->db); |
117
|
|
|
} |
118
|
|
|
$obj = $this->db->fetch_array($resql); |
119
|
|
|
if ($obj) { |
120
|
|
|
// update |
121
|
|
|
$sql = "UPDATE ".MAIN_DB_PREFIX."oauth_token"; |
122
|
|
|
$sql.= " SET token='".$this->db->escape($serializedToken)."'"; |
123
|
|
|
$sql.= " WHERE rowid='".$obj['rowid']."'"; |
124
|
|
|
|
125
|
|
|
$resql = $this->db->query($sql); |
|
|
|
|
126
|
|
|
} else { |
127
|
|
|
// save |
128
|
|
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."oauth_token (service, token, fk_user, entity)"; |
129
|
|
|
$sql.= " VALUES ('".$this->db->escape($service)."', '".$this->db->escape($serializedToken)."', ".(int) $this->userid.", ".(int) $this->conf->entity.")"; |
130
|
|
|
|
131
|
|
|
$resql = $this->db->query($sql); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
//print $sql; |
134
|
|
|
|
135
|
|
|
// allow chaining |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritDoc} |
141
|
|
|
*/ |
142
|
|
|
public function hasAccessToken($service) |
143
|
|
|
{ |
144
|
|
|
// get from db |
145
|
|
|
dol_syslog("hasAccessToken service=".$service); |
146
|
|
|
$sql = "SELECT token FROM ".MAIN_DB_PREFIX."oauth_token"; |
147
|
|
|
$sql .= " WHERE service='".$this->db->escape($service)."'"; |
148
|
|
|
$sql .= " AND fk_user=".(int) $this->userid." AND entity=".(int) $this->conf->entity; |
149
|
|
|
|
150
|
|
|
$resql = $this->db->query($sql); |
151
|
|
|
if (! $resql) { |
152
|
|
|
dol_print_error($this->db); |
153
|
|
|
} |
154
|
|
|
$result = $this->db->fetch_array($resql); |
155
|
|
|
$token = unserialize($result['token']); |
156
|
|
|
|
157
|
|
|
$this->tokens[$service] = $token; |
158
|
|
|
|
159
|
|
|
return is_array($this->tokens) |
160
|
|
|
&& isset($this->tokens[$service]) |
161
|
|
|
&& $this->tokens[$service] instanceof TokenInterface; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritDoc} |
166
|
|
|
*/ |
167
|
|
|
public function clearToken($service) |
168
|
|
|
{ |
169
|
|
|
// TODO |
170
|
|
|
// get previously saved tokens |
171
|
|
|
//$tokens = $this->retrieveAccessToken($service); |
172
|
|
|
|
173
|
|
|
//if (is_array($tokens) && array_key_exists($service, $tokens)) { |
174
|
|
|
// unset($tokens[$service]); |
175
|
|
|
|
176
|
|
|
$sql = "DELETE FROM ".MAIN_DB_PREFIX."oauth_token"; |
177
|
|
|
$sql.= " WHERE service='".$this->db->escape($service)."'"; |
178
|
|
|
$sql .= " AND fk_user=".(int) $this->userid." AND entity=".(int) $this->conf->entity; |
179
|
|
|
$resql = $this->db->query($sql); |
|
|
|
|
180
|
|
|
//} |
181
|
|
|
|
182
|
|
|
// allow chaining |
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritDoc} |
188
|
|
|
*/ |
189
|
|
|
public function clearAllTokens() |
190
|
|
|
{ |
191
|
|
|
// TODO |
192
|
|
|
$this->conf->remove($this->key); |
193
|
|
|
|
194
|
|
|
// allow chaining |
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritDoc} |
200
|
|
|
*/ |
201
|
|
|
public function retrieveAuthorizationState($service) |
202
|
|
|
{ |
203
|
|
|
if ($this->hasAuthorizationState($service)) { |
204
|
|
|
return $this->states[$service]; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
throw new AuthorizationStateNotFoundException('State not found in db, are you sure you stored it?'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* {@inheritDoc} |
212
|
|
|
*/ |
213
|
|
|
public function storeAuthorizationState($service, $state) |
214
|
|
|
{ |
215
|
|
|
// TODO save or update |
216
|
|
|
|
217
|
|
|
$states = array(); |
218
|
|
|
|
219
|
|
|
$states[$service] = $state; |
220
|
|
|
$this->states[$service] = $state; |
221
|
|
|
|
222
|
|
|
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."oauth_state"; |
223
|
|
|
$sql.= " WHERE service='".$this->db->escape($service); |
224
|
|
|
$sql .= " AND fk_user=".(int) $this->userid." AND entity=".(int) $this->conf->entity; |
225
|
|
|
$resql = $this->db->query($sql); |
226
|
|
|
if (! $resql) { |
227
|
|
|
dol_print_error($this->db); |
228
|
|
|
} |
229
|
|
|
$obj = $this->db->fetch_array($resql); |
230
|
|
|
if ($obj) { |
231
|
|
|
// update |
232
|
|
|
$sql = "UPDATE ".MAIN_DB_PREFIX."oauth_state"; |
233
|
|
|
$sql.= " SET state='".$this->db->escape($state)."'"; |
234
|
|
|
$sql.= " WHERE rowid='".$obj['rowid']."'"; |
235
|
|
|
$resql = $this->db->query($sql); |
|
|
|
|
236
|
|
|
} else { |
237
|
|
|
// save |
238
|
|
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."oauth_state (service, state, fk_user, entity)"; |
239
|
|
|
$sql.= " VALUES ('".$this->db->escape($service)."', '".$this->db->escape($state)."', ".(int) $this->userid.", ".(int) $this->conf->entity.")"; |
240
|
|
|
$resql = $this->db->query($sql); |
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
// allow chaining |
244
|
|
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* {@inheritDoc} |
249
|
|
|
*/ |
250
|
|
|
public function hasAuthorizationState($service) |
251
|
|
|
{ |
252
|
|
|
// get state from db |
253
|
|
|
dol_syslog("get state from db"); |
254
|
|
|
$sql = "SELECT state FROM ".MAIN_DB_PREFIX."oauth_state"; |
255
|
|
|
$sql.= " WHERE service='".$this->db->escape($service)."'"; |
256
|
|
|
$sql .= " AND fk_user=".(int) $this->userid." AND entity=".(int) $this->conf->entity; |
257
|
|
|
$resql = $this->db->query($sql); |
258
|
|
|
$result = $this->db->fetch_array($resql); |
259
|
|
|
$states[$service] = $result['state']; |
|
|
|
|
260
|
|
|
$this->states[$service] = $states[$service]; |
261
|
|
|
|
262
|
|
|
return is_array($states) |
263
|
|
|
&& isset($states[$service]) |
264
|
|
|
&& null !== $states[$service]; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* {@inheritDoc} |
269
|
|
|
*/ |
270
|
|
|
public function clearAuthorizationState($service) |
271
|
|
|
{ |
272
|
|
|
// TODO |
273
|
|
|
// get previously saved tokens |
274
|
|
|
//$states = $this->conf->get($this->stateKey); |
275
|
|
|
|
276
|
|
|
if (is_array($states) && array_key_exists($service, $states)) { |
|
|
|
|
277
|
|
|
unset($states[$service]); |
278
|
|
|
|
279
|
|
|
// Replace the stored tokens array |
280
|
|
|
//$this->conf->set($this->stateKey, $states); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
// allow chaining |
284
|
|
|
return $this; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* {@inheritDoc} |
289
|
|
|
*/ |
290
|
|
|
public function clearAllAuthorizationStates() |
291
|
|
|
{ |
292
|
|
|
// TODO |
293
|
|
|
//$this->conf->remove($this->stateKey); |
294
|
|
|
|
295
|
|
|
// allow chaining |
296
|
|
|
return $this; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..