1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\Auth0Bundle\Factory; |
4
|
|
|
|
5
|
|
|
use Auth0\SDK\API\Authentication; |
6
|
|
|
use Auth0\SDK\API\Management; |
7
|
|
|
use Auth0\SDK\Exception\CoreException; |
8
|
|
|
use Http\Client\HttpClient; |
9
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
|
12
|
|
|
class ManagementFactory |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var CacheItemPoolInterface|null |
16
|
|
|
*/ |
17
|
|
|
private $cacheItemPool; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Authentication |
21
|
|
|
*/ |
22
|
|
|
private $authentication; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $domain; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var HttpClient|null |
31
|
|
|
*/ |
32
|
|
|
private $httpClient; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var LoggerInterface|null |
36
|
|
|
*/ |
37
|
|
|
private $logger; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* ManagementFactory constructor. |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
Authentication $authentication, |
44
|
|
|
string $domain, |
45
|
|
|
?CacheItemPoolInterface $cacheItemPool, |
46
|
|
|
?HttpClient $httpClient, |
47
|
|
|
?LoggerInterface $logger |
48
|
|
|
) { |
49
|
|
|
$this->cacheItemPool = $cacheItemPool; |
50
|
|
|
$this->authentication = $authentication; |
51
|
|
|
$this->domain = $domain; |
52
|
|
|
$this->httpClient = $httpClient; |
53
|
|
|
$this->logger = $logger; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function create(): Management |
57
|
|
|
{ |
58
|
|
|
if ($this->cacheItemPool) { |
59
|
|
|
$accessToken = $this->getCachedAccessToken(); |
60
|
|
|
} else { |
61
|
|
|
$accessToken = $this->getUncachedAccessToken(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return new Management($accessToken, $this->domain, $this->httpClient); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function getCachedAccessToken(): string |
68
|
|
|
{ |
69
|
|
|
$item = $this->cacheItemPool->getItem('auth0_management_access_token'); |
|
|
|
|
70
|
|
|
|
71
|
|
|
if (!$item->isHit()) { |
72
|
|
|
$token = $this->getTokenStruct(); |
73
|
|
|
|
74
|
|
|
$item->set($token['access_token']); |
75
|
|
|
$item->expiresAfter((int) $token['expires_in']); |
76
|
|
|
$this->cacheItemPool->save($item); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $item->get(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function getUncachedAccessToken(): string |
83
|
|
|
{ |
84
|
|
|
if ($this->logger) { |
85
|
|
|
$this->logger->warning('Using the Auth0 management API without using an access token cache. This increases the number of API requests.'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->getTokenStruct()['access_token']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @throws CoreException |
93
|
|
|
*/ |
94
|
|
|
protected function getTokenStruct(): array |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
|
|
$token = $this->authentication->clientCredentials([ |
98
|
|
|
'audience' => sprintf('https://%s/api/v2/', $this->domain), |
99
|
|
|
]); |
100
|
|
|
} catch (\Auth0\SDK\Exception\ApiException $e) { |
101
|
|
|
throw $e; |
102
|
|
|
} catch (\Exception $e) { |
103
|
|
|
throw new CoreException('Could not get access token', 0, $e); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (isset($token['error'])) { |
107
|
|
|
throw new CoreException($token['error_description']); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (empty($token['access_token'])) { |
111
|
|
|
throw new CoreException('Could not get access token'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($this->logger) { |
115
|
|
|
$this->logger->debug(sprintf('Got new access token for Auth0 management API. Scope: "%s" ', isset($token['scope']) ? $token['scope'] : 'no scope')); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $token; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.