1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FAPI\Sylius\Http; |
6
|
|
|
|
7
|
|
|
use FAPI\Sylius\RequestBuilder; |
8
|
|
|
use Http\Client\HttpClient; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class that gets access tokens. |
12
|
|
|
* |
13
|
|
|
* @author Tobias Nyholm <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @internal |
16
|
|
|
*/ |
17
|
|
|
final class Authenticator |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var RequestBuilder |
21
|
|
|
*/ |
22
|
|
|
private $requestBuilder; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var HttpClient |
26
|
|
|
*/ |
27
|
|
|
private $httpClient; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string|null |
31
|
|
|
*/ |
32
|
|
|
private $accessToken; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $clientId; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $clientSecret; |
43
|
|
|
|
44
|
|
|
public function __construct(RequestBuilder $requestBuilder, HttpClient $httpClient, string $clientId, string $clientSecret) |
45
|
|
|
{ |
46
|
|
|
$this->requestBuilder = $requestBuilder; |
47
|
|
|
$this->httpClient = $httpClient; |
48
|
|
|
$this->clientId = $clientId; |
49
|
|
|
$this->clientSecret = $clientSecret; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function createAccessToken(string $username, string $password): ?string |
53
|
|
|
{ |
54
|
|
|
$request = $this->requestBuilder->create('POST', '/api/oauth/v2/token', [ |
55
|
|
|
'Content-type' => 'application/x-www-form-urlencoded', |
56
|
|
|
], \http_build_query([ |
57
|
|
|
'client_id' => $this->clientId, |
58
|
|
|
'client_secret' => $this->clientSecret, |
59
|
|
|
'grant_type' => 'password', |
60
|
|
|
'username' => $username, |
61
|
|
|
'password' => $password, |
62
|
|
|
])); |
63
|
|
|
|
64
|
|
|
$response = $this->httpClient->sendRequest($request); |
65
|
|
|
if (200 !== $response->getStatusCode()) { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->accessToken = $response->getBody()->__toString(); |
70
|
|
|
|
71
|
|
|
return $this->accessToken; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function refreshAccessToken(string $accessToken, string $refreshToken): ?string |
75
|
|
|
{ |
76
|
|
|
$request = $this->requestBuilder->create('POST', '/api/oauth/v2/token', [ |
77
|
|
|
'Authorization' => \sprintf('Bearer %s', $accessToken), |
78
|
|
|
'Content-type' => 'application/x-www-form-urlencoded', |
79
|
|
|
], \http_build_query([ |
80
|
|
|
'refresh_token' => $refreshToken, |
81
|
|
|
'grant_type' => 'refresh_token', |
82
|
|
|
'client_id' => $this->clientId, |
83
|
|
|
'client_secret' => $this->clientSecret, |
84
|
|
|
])); |
85
|
|
|
|
86
|
|
|
$response = $this->httpClient->sendRequest($request); |
87
|
|
|
if (200 !== $response->getStatusCode()) { |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$this->accessToken = $response->getBody()->__toString(); |
92
|
|
|
|
93
|
|
|
return $this->accessToken; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getAccessToken(): ?string |
97
|
|
|
{ |
98
|
|
|
return $this->accessToken; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|