1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JincorTech\AuthClient; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
7
|
|
|
use GuzzleHttp\Exception\ServerException; |
8
|
|
|
use JincorTech\AuthClient\Exception\AccessTokenNotFound; |
9
|
|
|
use JincorTech\AuthClient\Exception\DecodedSectionNotFound; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class AuthClient. |
13
|
|
|
*/ |
14
|
|
|
class AuthClient implements AuthServiceInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Http Client. |
18
|
|
|
* |
19
|
|
|
* @var ClientInterface |
20
|
|
|
*/ |
21
|
|
|
private $httpClient; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* AuthClient constructor. |
25
|
|
|
* |
26
|
|
|
* @param ClientInterface $httpClient |
27
|
|
|
*/ |
28
|
|
|
public function __construct(ClientInterface $httpClient) |
29
|
|
|
{ |
30
|
|
|
$this->httpClient = $httpClient; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $email |
35
|
|
|
* @param string $password |
36
|
|
|
* @return TenantRegistrationResult |
37
|
|
|
* @throws GuzzleException |
38
|
|
|
* @throws ServerException |
39
|
|
|
*/ |
40
|
|
View Code Duplication |
public function registerTenant(string $email, string $password): TenantRegistrationResult |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$response = $this->httpClient->request( |
43
|
|
|
'POST', '/tenant', [ |
44
|
|
|
'json' => [ |
45
|
|
|
'email' => $email, |
46
|
|
|
'password' => $password, |
47
|
|
|
], |
48
|
|
|
] |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
return new TenantRegistrationResult(json_decode($response->getBody()->getContents(), true)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $email |
56
|
|
|
* @param string $password |
57
|
|
|
* @return string |
58
|
|
|
* @throws AccessTokenNotFound |
59
|
|
|
* @throws GuzzleException |
60
|
|
|
* @throws ServerException |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function loginTenant(string $email, string $password): string |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$response = $this->httpClient->request( |
65
|
|
|
'POST', '/tenant/login', [ |
66
|
|
|
'json' => [ |
67
|
|
|
'email' => $email, |
68
|
|
|
'password' => $password, |
69
|
|
|
], |
70
|
|
|
] |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$result = json_decode($response->getBody()->getContents(), true); |
74
|
|
|
if (array_key_exists('accessToken', $result)) { |
75
|
|
|
$token = $result['accessToken']; |
76
|
|
|
} else { |
77
|
|
|
throw new AccessTokenNotFound(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $token; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $tenantToken |
85
|
|
|
* @return TenantTokenVerificationResult |
86
|
|
|
* @throws GuzzleException |
87
|
|
|
* @throws DecodedSectionNotFound |
88
|
|
|
* @throws ServerException |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function verifyTenantToken(string $tenantToken): TenantTokenVerificationResult |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$response = $this->httpClient->request( |
93
|
|
|
'POST', '/tenant/verify', [ |
94
|
|
|
'json' => [ |
95
|
|
|
'token' => $tenantToken, |
96
|
|
|
], |
97
|
|
|
] |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$responseData = json_decode($response->getBody()->getContents(), true); |
101
|
|
|
if (! array_key_exists('decoded', $responseData)) { |
102
|
|
|
throw new DecodedSectionNotFound('Decoded section not found'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$params = $responseData['decoded']; |
106
|
|
|
|
107
|
|
|
return new TenantTokenVerificationResult($params); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $tenantToken |
112
|
|
|
* @void |
113
|
|
|
* @throws GuzzleException |
114
|
|
|
* @throws ServerException |
115
|
|
|
*/ |
116
|
|
|
public function logoutTenant(string $tenantToken) |
117
|
|
|
{ |
118
|
|
|
$this->httpClient->request( |
119
|
|
|
'POST', '/tenant/logout', [ |
120
|
|
|
'json' => [ |
121
|
|
|
'token' => $tenantToken, |
122
|
|
|
], |
123
|
|
|
] |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param array $data containing the necessary params |
129
|
|
|
* $data = [ |
130
|
|
|
* 'email' => (string) email. Required. |
131
|
|
|
* 'login' => (string) login. Required. |
132
|
|
|
* 'password' => (string) password. Required. |
133
|
|
|
* 'sub' => (string) sub. Required. |
134
|
|
|
* 'scope' => (array) scope. Optional. |
135
|
|
|
* ] |
136
|
|
|
* @param string $tenantToken |
137
|
|
|
* @return UserRegistrationResult |
138
|
|
|
* @throws GuzzleException |
139
|
|
|
* @throws ServerException |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
public function createUser(array $data, string $tenantToken): UserRegistrationResult |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$response = $this->httpClient->request( |
144
|
|
|
'POST', |
145
|
|
|
'/user', |
146
|
|
|
[ |
147
|
|
|
'json' => $data, |
148
|
|
|
'headers' => [ |
149
|
|
|
'Authorization' => 'Bearer '.$tenantToken, |
150
|
|
|
], |
151
|
|
|
] |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
return new UserRegistrationResult(json_decode($response->getBody()->getContents(), true)); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param array $data containing the necessary params |
159
|
|
|
* $data = [ |
160
|
|
|
* 'login' => (string) login. Required. |
161
|
|
|
* 'password' => (string) password. Required. |
162
|
|
|
* 'deviceId' => (string) device ID. Required. |
163
|
|
|
* ] |
164
|
|
|
* @param string $tenantToken |
165
|
|
|
* @return string |
166
|
|
|
* @throws GuzzleException |
167
|
|
|
* @throws AccessTokenNotFound |
168
|
|
|
* @throws ServerException |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public function loginUser(array $data, string $tenantToken): string |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
$response = $this->httpClient->request( |
173
|
|
|
'POST', |
174
|
|
|
'/auth', |
175
|
|
|
[ |
176
|
|
|
'json' => $data, |
177
|
|
|
'headers' => [ |
178
|
|
|
'Authorization' => 'Bearer '.$tenantToken, |
179
|
|
|
], |
180
|
|
|
] |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
$result = json_decode($response->getBody()->getContents(), true); |
184
|
|
|
|
185
|
|
|
if (array_key_exists('accessToken', $result)) { |
186
|
|
|
$token = $result['accessToken']; |
187
|
|
|
} else { |
188
|
|
|
throw new AccessTokenNotFound(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $token; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $userToken |
196
|
|
|
* @param string $tenantToken |
197
|
|
|
* @return UserTokenVerificationResult |
198
|
|
|
* @throws GuzzleException |
199
|
|
|
* @throws DecodedSectionNotFound |
200
|
|
|
* @throws ServerException |
201
|
|
|
*/ |
202
|
|
|
public function verifyUserToken(string $userToken, string $tenantToken): UserTokenVerificationResult |
203
|
|
|
{ |
204
|
|
|
$response = $this->httpClient->request( |
205
|
|
|
'POST', |
206
|
|
|
'/auth/verify', |
207
|
|
|
[ |
208
|
|
|
'json' => [ |
209
|
|
|
'token' => $userToken, |
210
|
|
|
], |
211
|
|
|
'headers' => [ |
212
|
|
|
'Authorization' => 'Bearer '.$tenantToken, |
213
|
|
|
], |
214
|
|
|
] |
215
|
|
|
); |
216
|
|
|
|
217
|
|
|
$responseData = json_decode($response->getBody()->getContents(), true); |
218
|
|
|
if (! array_key_exists('decoded', $responseData)) { |
219
|
|
|
throw new DecodedSectionNotFound('Decoded section not found.'); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$params = $responseData['decoded']; |
223
|
|
|
|
224
|
|
|
return new UserTokenVerificationResult($params); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param string $userToken |
229
|
|
|
* @param string $tenantToken |
230
|
|
|
* @void |
231
|
|
|
* @throws GuzzleException |
232
|
|
|
* @throws ServerException |
233
|
|
|
*/ |
234
|
|
|
public function logoutUser(string $userToken, string $tenantToken) |
235
|
|
|
{ |
236
|
|
|
$this->httpClient->request( |
237
|
|
|
'POST', |
238
|
|
|
'/auth/logout', |
239
|
|
|
[ |
240
|
|
|
'json' => [ |
241
|
|
|
'token' => $userToken, |
242
|
|
|
], |
243
|
|
|
'headers' => [ |
244
|
|
|
'Authorization' => 'Bearer '.$tenantToken, |
245
|
|
|
], |
246
|
|
|
] |
247
|
|
|
); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param string $login |
252
|
|
|
* @param string $tenantToken |
253
|
|
|
* @void |
254
|
|
|
* @throws GuzzleException |
255
|
|
|
* @throws ServerException |
256
|
|
|
*/ |
257
|
|
|
public function deleteUser(string $login, string $tenantToken) |
258
|
|
|
{ |
259
|
|
|
$this->httpClient->request( |
260
|
|
|
'DELETE', |
261
|
|
|
'/user/'.$login, |
262
|
|
|
[ |
263
|
|
|
'headers' => [ |
264
|
|
|
'Authorization' => 'Bearer '.$tenantToken, |
265
|
|
|
], |
266
|
|
|
] |
267
|
|
|
); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.