|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Eljam\GuzzleJwt\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Eljam\GuzzleJwt\JwtToken; |
|
6
|
|
|
use Eljam\GuzzleJwt\Strategy\Auth\AuthStrategyInterface; |
|
7
|
|
|
use GuzzleHttp\ClientInterface; |
|
8
|
|
|
use GuzzleHttp\request; |
|
9
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Guillaume Cavana <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class JwtManager |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* $client Guzzle Client. |
|
18
|
|
|
* |
|
19
|
|
|
* @var ClientInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $client; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* $auth Authentication Strategy. |
|
25
|
|
|
* |
|
26
|
|
|
* @var AuthStrategyInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $auth; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* $options. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $options; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* $token. |
|
39
|
|
|
* |
|
40
|
|
|
* @var JwtToken |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $token; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param ClientInterface $client |
|
48
|
|
|
* @param AuthStrategyInterface $auth |
|
49
|
|
|
* @param array $options |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct( |
|
52
|
|
|
ClientInterface $client, |
|
53
|
|
|
AuthStrategyInterface $auth, |
|
54
|
|
|
array $options = [] |
|
55
|
|
|
) { |
|
56
|
|
|
$this->client = $client; |
|
57
|
|
|
$this->auth = $auth; |
|
58
|
|
|
|
|
59
|
|
|
$resolver = new OptionsResolver(); |
|
60
|
|
|
$resolver->setDefaults([ |
|
61
|
|
|
'token_url' => '/token', |
|
62
|
|
|
'timeout' => 1, |
|
63
|
|
|
'token_key' => 'token', |
|
64
|
|
|
]); |
|
65
|
|
|
|
|
66
|
|
|
$resolver->setRequired(['token_url', 'timeout']); |
|
67
|
|
|
|
|
68
|
|
|
$this->options = $resolver->resolve($options); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* getToken. |
|
73
|
|
|
* |
|
74
|
|
|
* @return JwtToken |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getJwtToken() |
|
77
|
|
|
{ |
|
78
|
|
|
if ($this->token && $this->token->isValid()) { |
|
79
|
|
|
return $this->token; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$url = $this->options['token_url']; |
|
83
|
|
|
|
|
84
|
|
|
$requestOptions = array_merge( |
|
85
|
|
|
$this->getDefaultHeaders(), |
|
86
|
|
|
$this->auth->getRequestOptions() |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
$response = $this->client->request('POST', $url, $requestOptions); |
|
90
|
|
|
$body = json_decode($response->getBody(), true); |
|
91
|
|
|
|
|
92
|
|
|
if (isset($body['expires_in'])) { |
|
93
|
|
|
$expiration = new \DateTime('now + ' . $body['expires_in'] . ' seconds'); |
|
94
|
|
|
} else { |
|
95
|
|
|
$expiration = null; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->token = new JwtToken($body[$this->options['token_key']], $expiration); |
|
99
|
|
|
|
|
100
|
|
|
return $this->token; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* getHeaders. Return defaults header. |
|
105
|
|
|
* |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
private function getDefaultHeaders() |
|
109
|
|
|
{ |
|
110
|
|
|
return [ |
|
111
|
|
|
\GuzzleHttp\RequestOptions::HEADERS => [ |
|
112
|
|
|
'timeout' => $this->options['timeout'], |
|
113
|
|
|
], |
|
114
|
|
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|