1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace E2Consult\DNBApiClient; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
|
|
|
6
|
|
|
use GuzzleHttp\Psr7\Request; |
7
|
|
|
use Aws\Signature\SignatureV4; |
8
|
|
|
use Aws\Credentials\Credentials; |
9
|
|
|
use Illuminate\Support\Facades\Cache; |
|
|
|
|
10
|
|
|
|
11
|
|
|
class JwtToken |
12
|
|
|
{ |
13
|
|
|
private $api_key; |
14
|
|
|
private $credentials; |
15
|
|
|
|
16
|
|
|
protected $cache_key; |
17
|
|
|
protected $customerId; |
18
|
|
|
|
19
|
|
|
protected $token; |
20
|
|
|
protected $signer; |
21
|
|
|
protected $duration = 600; |
22
|
|
|
|
23
|
|
|
public function __construct(Credentials $credentials, $api_key, $customerId) |
24
|
|
|
{ |
25
|
|
|
$this->credentials = $credentials; |
26
|
|
|
$this->api_key = $api_key; |
27
|
|
|
$this->customerId = $customerId; |
28
|
|
|
|
29
|
|
|
$this->signer = new SignatureV4( |
30
|
|
|
config('services.dnb.servcice') ?? 'execute-api', |
|
|
|
|
31
|
|
|
config('services.dnb.region') ?? 'eu-west-1' |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
$this->cache_key = 'services.dnb.token.'.$this->customerId; |
35
|
|
|
$this->token = null; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getTokenKey() |
39
|
|
|
{ |
40
|
|
|
return $this->cache_key; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getJwtToken() |
44
|
|
|
{ |
45
|
|
|
if (is_null($this->token)) { |
46
|
|
|
if (is_null($this->token = Cache::get($this->cache_key))) { |
47
|
|
|
$this->token = $this->getFreshToken(true); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->token->jwtToken; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getFreshToken($cache = true) |
55
|
|
|
{ |
56
|
|
|
$signedrequest = $this->signer->signRequest( |
57
|
|
|
new Request('GET', config('services.dnb.endpoint').'/token?customerId='.json_encode([ |
|
|
|
|
58
|
|
|
'type' => 'SSN', |
59
|
|
|
'value' => $this->customerId |
60
|
|
|
]), [ |
61
|
|
|
'Accept' => 'application/json', |
62
|
|
|
'Content-Type' => 'application/json', |
63
|
|
|
'x-api-key' => $this->api_key |
64
|
|
|
] |
65
|
|
|
), |
66
|
|
|
$this->credentials |
67
|
|
|
); |
68
|
|
|
$response = (new Client)->send($signedrequest); |
69
|
|
|
|
70
|
|
|
return tap(collect(json_decode($response->getBody())->tokenInfo)->first(), function ($token) use ($cache) { |
|
|
|
|
71
|
|
|
if ($cache) { |
72
|
|
|
Cache::put($this->cache_key, $token, $this->duration); |
73
|
|
|
} |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: