1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spinen\ConnectWise\Api; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Token |
10
|
|
|
* |
11
|
|
|
* @package Spinen\ConnectWise\Api |
12
|
|
|
*/ |
13
|
|
|
class Token |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Company Id to scope calls to |
17
|
|
|
* |
18
|
|
|
* @var string|null |
19
|
|
|
*/ |
20
|
|
|
protected $company_id = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Member Id making calls |
24
|
|
|
* |
25
|
|
|
* @var string|null |
26
|
|
|
*/ |
27
|
|
|
protected $member_id = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Expiration timestamp of token |
31
|
|
|
* |
32
|
|
|
* @var Carbon|null |
33
|
|
|
*/ |
34
|
|
|
protected $expiration = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Password for the user that is making the calls |
38
|
|
|
* |
39
|
|
|
* This is really the private key from the user impersonation token. |
40
|
|
|
* |
41
|
|
|
* @var string|null |
42
|
|
|
*/ |
43
|
|
|
protected $password = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* User that is making the calls |
47
|
|
|
* |
48
|
|
|
* This is really the public key from the user impersonation token. |
49
|
|
|
* |
50
|
|
|
* @var string|null |
51
|
|
|
*/ |
52
|
|
|
protected $username = null; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Path to the endpoint that returns tokens |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
7 |
|
protected function endpoint() |
60
|
|
|
{ |
61
|
7 |
|
return 'system/members/' . $this->member_id . '/tokens'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Call CW & get temp tokens for the user |
66
|
|
|
* |
67
|
|
|
* @param Client $client |
68
|
|
|
*/ |
69
|
7 |
|
public function fetch(Client $client) |
70
|
|
|
{ |
71
|
7 |
|
$this->parse($client->post($this->endpoint())); |
72
|
7 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Expose the company id |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
1 |
|
public function getCompanyId() |
80
|
|
|
{ |
81
|
1 |
|
return $this->company_id; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Expose the expiration of the token |
86
|
|
|
* |
87
|
|
|
* @return Carbon|null |
88
|
|
|
*/ |
89
|
2 |
|
public function getExpiration() |
90
|
|
|
{ |
91
|
2 |
|
return $this->expiration; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Expose the member id |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
1 |
|
public function getMemberId() |
100
|
|
|
{ |
101
|
1 |
|
return $this->member_id; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Expose the password |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
2 |
|
public function getPassword() |
110
|
|
|
{ |
111
|
2 |
|
return $this->password; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* The name for the basic auth needs the company_id "+" member_id |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
3 |
|
public function getUsername() |
120
|
|
|
{ |
121
|
3 |
|
return $this->company_id . '+' . $this->username; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Check to see if tokens have expired |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
3 |
|
public function isExpired() |
130
|
|
|
{ |
131
|
3 |
|
return !is_null($this->expiration) && |
132
|
2 |
|
Carbon::now() |
133
|
3 |
|
->gte($this->expiration); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Check to see if the token is for the integrator |
138
|
|
|
* |
139
|
|
|
* @param $username |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
1 |
|
public function isForUser($username) |
144
|
|
|
{ |
145
|
1 |
|
return $username === $this->username; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Check to see if tokens have been setup |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
3 |
|
public function isLoaded() |
154
|
|
|
{ |
155
|
3 |
|
return $this->password && $this->username; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Determine if the token needs to be refreshed |
160
|
|
|
* |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
1 |
|
public function needsRefreshing() |
164
|
|
|
{ |
165
|
1 |
|
return $this->isExpired() || !$this->isLoaded(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Parse the tokens into properties |
170
|
|
|
* |
171
|
|
|
* @param Arrayable|array $token |
172
|
|
|
*/ |
173
|
7 |
|
protected function parse($token) |
174
|
|
|
{ |
175
|
|
|
// TODO: Put guard here to make sure that we got a token. |
176
|
7 |
|
$this->setExpiration($token['expiration']); |
177
|
7 |
|
$this->password = $token['privateKey']; |
178
|
7 |
|
$this->username = $token['publicKey']; |
179
|
7 |
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Erase the token & get new one |
183
|
|
|
* |
184
|
|
|
* @param Client $client |
185
|
|
|
*/ |
186
|
1 |
|
public function refresh(Client $client) |
187
|
|
|
{ |
188
|
1 |
|
$this->username = $client->getIntegrator(); |
189
|
1 |
|
$this->password = $client->getPassword(); |
190
|
|
|
|
191
|
1 |
|
$this->fetch($client); |
192
|
1 |
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Setter for the company id |
196
|
|
|
* |
197
|
|
|
* @param $company_id |
198
|
|
|
* |
199
|
|
|
* @return $this |
200
|
|
|
*/ |
201
|
4 |
|
public function setCompanyId($company_id) |
202
|
|
|
{ |
203
|
4 |
|
$this->company_id = $company_id; |
204
|
|
|
|
205
|
4 |
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Setter for the member id |
210
|
|
|
* |
211
|
|
|
* @param $member_id |
212
|
|
|
* |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
2 |
|
public function setMemberId($member_id) |
216
|
|
|
{ |
217
|
2 |
|
$this->member_id = $member_id; |
218
|
|
|
|
219
|
2 |
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Setter for the expiration timestamp |
224
|
|
|
* |
225
|
|
|
* Make sure to cast it to a Carbon object |
226
|
|
|
* |
227
|
|
|
* @param $expiration |
228
|
|
|
* |
229
|
|
|
* @return $this |
230
|
|
|
*/ |
231
|
7 |
|
protected function setExpiration($expiration) |
232
|
|
|
{ |
233
|
7 |
|
$this->expiration = Carbon::parse($expiration); |
234
|
|
|
|
235
|
7 |
|
return $this; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|