1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Axsor\PhpIPAM\Http\Requests; |
4
|
|
|
|
5
|
|
|
use Axsor\PhpIPAM\Exceptions\BadCredentialsException; |
6
|
|
|
use Axsor\PhpIPAM\Facades\PhpIPAM; |
7
|
|
|
use Illuminate\Support\Facades\Cache; |
8
|
|
|
|
9
|
|
|
class Connector |
10
|
|
|
{ |
11
|
|
|
const SUCCESS_CODE = 200; |
12
|
|
|
const UNAUTHORIZED_CODE = 401; |
13
|
|
|
|
14
|
|
|
private $config; |
15
|
|
|
|
16
|
|
|
private $headers; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
$this->config = PhpIPAM::getConfig(); |
|
|
|
|
21
|
|
|
|
22
|
|
|
$this->configHeaders(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function get($uri) |
26
|
|
|
{ |
27
|
|
|
return $this->call('GET', $uri); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function post($uri, $payload) |
31
|
|
|
{ |
32
|
|
|
return $this->call('POST', $uri, $payload); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function patch($uri, $payload) |
36
|
|
|
{ |
37
|
|
|
return $this->call('PATCH', $uri, $payload); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function delete($uri) |
41
|
|
|
{ |
42
|
|
|
return $this->call('DELETE', $uri); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function call($method, $uri, $payload = [], $firstTry = false) |
46
|
|
|
{ |
47
|
|
|
try { |
48
|
|
|
$response = PhpIPAM::getClient()->$method($this->config['url'].'/'.$this->config['app'].'/'.$uri, [ |
|
|
|
|
49
|
|
|
'headers' => $this->headers, |
50
|
|
|
'json' => $payload, |
51
|
|
|
]); |
52
|
|
|
|
53
|
|
|
return json_decode($response->getBody()->getContents(), true); |
54
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
55
|
|
|
if (! $firstTry && $e->getCode() == self::UNAUTHORIZED_CODE) { |
56
|
|
|
Cache::forget(PhpIPAM::getCacheKey()); |
|
|
|
|
57
|
|
|
$this->configHeaders(); |
58
|
|
|
|
59
|
|
|
return $this->call($method, $uri, $payload, true); |
60
|
|
|
} else { |
61
|
|
|
throw $e; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function configHeaders() |
67
|
|
|
{ |
68
|
|
|
$cachedData = null; |
69
|
|
|
|
70
|
|
|
if (Cache::has(PhpIPAM::getCacheKey())) { |
71
|
|
|
$cachedData = Cache::get(PhpIPAM::getCacheKey()); |
72
|
|
|
|
73
|
|
|
if ($cachedData['expires'] <= date('Y-m-d h:i:s')) { |
74
|
|
|
$cachedData = null; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (! $cachedData) { |
79
|
|
|
$token = $this->login(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->headers = [ |
83
|
|
|
'token' => $cachedData ? $cachedData['token'] : $token, |
|
|
|
|
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Tries to login into PhpIPAM and stores API token into laravel Cache. |
89
|
|
|
* |
90
|
|
|
* @return mixed Token to connect to PhpIPAM |
91
|
|
|
* @throws \Axsor\PhpIPAM\Exceptions\BadCredentialsException |
92
|
|
|
*/ |
93
|
|
|
private function login() |
94
|
|
|
{ |
95
|
|
|
$response = PhpIPAM::getClient()->post($this->config['url'].'/'.$this->config['app'].'/user/', [ |
96
|
|
|
'auth' => [ |
97
|
|
|
$this->config['user'], |
98
|
|
|
$this->config['pass'], |
99
|
|
|
], |
100
|
|
|
'headers' => [ |
101
|
|
|
'token' => $this->config['token'], |
102
|
|
|
], |
103
|
|
|
'json' => [], |
104
|
|
|
]); |
105
|
|
|
|
106
|
|
|
if ($response->getStatusCode() != self::SUCCESS_CODE) { |
107
|
|
|
throw new BadCredentialsException(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$payload = json_decode($response->getBody()->getContents(), true)['data']; |
111
|
|
|
|
112
|
|
|
Cache::set(PhpIPAM::getCacheKey(), $payload); |
113
|
|
|
|
114
|
|
|
return $payload['token']; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|