1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Laravel WuBook. |
5
|
|
|
* |
6
|
|
|
* (c) Filippo Galante <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace IlGala\LaravelWubook\Api; |
13
|
|
|
|
14
|
|
|
use fXmlRpc\Client; |
15
|
|
|
use fXmlRpc\Exception\AbstractTransportException; |
16
|
|
|
use IlGala\WuBook\Exceptions\WuBookException; |
17
|
|
|
use Carbon\Carbon; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This is the WuBook authentication class. |
21
|
|
|
* |
22
|
|
|
* @author Filippo Galante <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class WuBookAuth |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $config; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Illuminate\Cache\Repository |
34
|
|
|
*/ |
35
|
|
|
private $cache; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var fXmlRpc\Client |
39
|
|
|
*/ |
40
|
|
|
private $client; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create a new WuBookAuth Instance. |
44
|
|
|
* |
45
|
|
|
* @param array $config |
46
|
|
|
* @param \Illuminate\Cache\Repository $cache |
47
|
|
|
* @param Client $client |
48
|
|
|
*/ |
49
|
|
|
public function __construct(array $config, Illuminate\Cache\Repository $cache, Client $client) |
50
|
|
|
{ |
51
|
|
|
$this->config = $config; |
52
|
|
|
$this->client = $client; |
|
|
|
|
53
|
|
|
$this->cache = $cache; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Acquire token. If cache_token option is set to true, the package will automatically save it into application cache |
58
|
|
|
* |
59
|
|
|
* http://tdocs.wubook.net/wired/auth.html#acquiring-and-releasing-a-token |
60
|
|
|
* |
61
|
|
|
* |
62
|
|
|
* @return string token |
63
|
|
|
*/ |
64
|
|
|
public function acquire_token() |
65
|
|
|
{ |
66
|
|
|
// Setup request data |
67
|
|
|
$data = [ |
68
|
|
|
$this->config['username'], |
69
|
|
|
$this->config['password'], |
70
|
|
|
$this->config['provider_key'] |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
// Retrieve response |
75
|
|
|
$response = $this->client->call('acquire_token', $data); |
76
|
|
|
|
77
|
|
|
// Check response |
78
|
|
|
if ($response[0] == 0) { |
79
|
|
|
// Success |
80
|
|
|
$token = $response[1]; |
81
|
|
|
|
82
|
|
|
// Setup cache token expiration and max operations |
83
|
|
|
$expires_at = Carbon::now()->addSeconds(3600); |
84
|
|
|
|
85
|
|
|
// Setup cache |
86
|
|
|
if ($this->config['cache_token']) { |
87
|
|
|
$this->cache->put('wubook.token', $token, $expires_at); |
88
|
|
|
$this->cache->put('wubook.token.ops', 0, $expires_at); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $token; |
92
|
|
|
} else { |
93
|
|
|
// Error |
94
|
|
|
throw new WuBookException($response[1], $response[0]); |
95
|
|
|
} |
96
|
|
|
} catch (AbstractTransportException $error) { |
97
|
|
|
throw new WuBookException($error->getMessage(), $error->getCode(), $error); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Token release. |
103
|
|
|
* |
104
|
|
|
* http://tdocs.wubook.net/wired/auth.html#acquiring-and-releasing-a-token |
105
|
|
|
* |
106
|
|
|
* @param string $token |
107
|
|
|
*/ |
108
|
|
|
public function release_token($token) |
109
|
|
|
{ |
110
|
|
|
// Setup request data |
111
|
|
|
$data = [ |
112
|
|
|
$token |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
try { |
116
|
|
|
// Retrieve response |
117
|
|
|
$response = $this->client->call('release_token', $data); |
118
|
|
|
|
119
|
|
|
// Check response |
120
|
|
|
if ($response[0] == 0) { |
121
|
|
|
// Empty cache |
122
|
|
|
$this->cache->forget('wubook.token'); |
123
|
|
|
$this->cache->forget('wubook.token.ops'); |
124
|
|
|
|
125
|
|
|
return true; |
126
|
|
|
} else { |
127
|
|
|
// Error |
128
|
|
|
throw new WuBookException($response[1], $response[0]); |
129
|
|
|
} |
130
|
|
|
} catch (AbstractTransportException $error) { |
131
|
|
|
throw new WuBookException($error->getMessage(), $error->getCode(), $error); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* The is_token_valid() function returns two information. |
137
|
|
|
* If (and only if) the ReturnCode is zero, it means that the token is valid. |
138
|
|
|
* In that case, the return value of the function is an integer and represents the number of times that this token has been used. |
139
|
|
|
* |
140
|
|
|
* The request_new param will not be considered if token is valid. |
141
|
|
|
* |
142
|
|
|
* http://tdocs.wubook.net/wired/auth.html#other-token-tools |
143
|
|
|
* |
144
|
|
|
* @param string $token |
145
|
|
|
* @param boolean $request_new |
146
|
|
|
* @return int|string |
147
|
|
|
* @throws IlGala\WuBook\Exceptions\WuBookException |
148
|
|
|
*/ |
149
|
|
|
public function is_token_valid($token, $request_new = false) |
150
|
|
|
{ |
151
|
|
|
// Setup request data |
152
|
|
|
$data = [ |
153
|
|
|
$token |
154
|
|
|
]; |
155
|
|
|
|
156
|
|
|
try { |
157
|
|
|
// Retrieve response |
158
|
|
|
$response = $this->client->call('is_token_valid', $data); |
159
|
|
|
|
160
|
|
|
if ($response[0] == 0) { |
161
|
|
|
return $response[1]; |
162
|
|
|
} elseif ($request_new) { |
163
|
|
|
return $this->acquire_token(); |
164
|
|
|
} else { |
165
|
|
|
return false; |
|
|
|
|
166
|
|
|
} |
167
|
|
|
} catch (AbstractTransportException $error) { |
168
|
|
|
throw new WuBookException($error->getMessage(), $error->getCode(), $error); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* The provider_info() function is used to return the information WuBook holds about you as Wired Provider. |
174
|
|
|
* In particular, you can check what email we have registered and associated with your Provider Key. |
175
|
|
|
* The return value of this function is a Complex Structure. |
176
|
|
|
* |
177
|
|
|
* http://tdocs.wubook.net/wired/auth.html#other-token-tools |
178
|
|
|
* |
179
|
|
|
* @param string $token |
180
|
|
|
* @return mixed |
181
|
|
|
* @throws IlGala\WuBook\Exceptions\WuBookException |
182
|
|
|
*/ |
183
|
|
|
public function provider_info($token = null) |
184
|
|
|
{ |
185
|
|
|
// Check token |
186
|
|
|
if (empty($token)) { |
187
|
|
|
$token = $this->cache->get('wubook.token'); |
188
|
|
|
|
189
|
|
|
if (empty($token)) { |
190
|
|
|
$token = $this->acquire_token(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// Setup request data |
195
|
|
|
$data = [ |
196
|
|
|
$token |
197
|
|
|
]; |
198
|
|
|
|
199
|
|
|
try { |
200
|
|
|
// Retrieve response |
201
|
|
|
$response = $this->client->call('provider_info', $data); |
202
|
|
|
|
203
|
|
|
if ($response[0] == 0) { |
204
|
|
|
return $response[1]; |
205
|
|
|
} else { |
206
|
|
|
// Error |
207
|
|
|
throw new WuBookException($response[1], $response[0]); |
208
|
|
|
} |
209
|
|
|
} catch (AbstractTransportException $error) { |
210
|
|
|
throw new WuBookException($error->getMessage(), $error->getCode(), $error); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
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..