1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tbclla\Revolut\Auth\Requests; |
4
|
|
|
|
5
|
|
|
use tbclla\Revolut\Client as RevolutClient; |
6
|
|
|
use tbclla\Revolut\Auth\ClientAssertion; |
7
|
|
|
use tbclla\Revolut\Interfaces\GrantsAccessTokens; |
8
|
|
|
use tbclla\Revolut\Interfaces\MakesHttpRequests; |
9
|
|
|
|
10
|
|
|
class AccessTokenRequest |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The authentication endpoint |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
const ENDPOINT = '/auth/token'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The client assertion |
21
|
|
|
* |
22
|
|
|
* @var \tbclla\Revolut\Auth\ClientAssertion |
23
|
|
|
*/ |
24
|
|
|
private $clientAssertion; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The HTTP client |
28
|
|
|
* |
29
|
|
|
* @var \tbclla\Revolut\Interfaces\MakesHttpRequests |
30
|
|
|
*/ |
31
|
|
|
private $httpClient; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create a new access token request instance |
35
|
|
|
* |
36
|
|
|
* @param \tbclla\Revolut\Auth\ClientAssertion $clientAssertion |
37
|
|
|
* @param \tbclla\Revolut\Interfaces\MakesHttpRequests $httpClient |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
public function __construct(ClientAssertion $clientAssertion, MakesHttpRequests $httpClient) |
41
|
|
|
{ |
42
|
|
|
$this->clientAssertion = $clientAssertion; |
43
|
|
|
$this->httpClient = $httpClient; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Exchange an authorization code or a refresh token for an access token |
48
|
|
|
* |
49
|
|
|
* @param \tbclla\Revolut\Interfaces\GrantsAccessTokens $requestToken |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function exchange(GrantsAccessTokens $requestToken) |
53
|
|
|
{ |
54
|
|
|
return $this->httpClient->post($this->uri(), [ |
55
|
|
|
'form_params' => array_merge( |
56
|
|
|
$this->buildClientParams(), |
57
|
|
|
$this->buildGrantParams($requestToken) |
58
|
|
|
) |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the Uri for the request |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public static function uri() |
68
|
|
|
{ |
69
|
|
|
return RevolutClient::buildUri(self::ENDPOINT); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Build the client parameters |
74
|
|
|
* The request must inlude the client ID, the client assertion (JWT) and client assertion type |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
private function buildClientParams() |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
|
|
'client_assertion_type' => $this->clientAssertion::TYPE, |
82
|
|
|
'client_id' => $this->clientAssertion->clientId, |
83
|
|
|
'client_assertion' => $this->clientAssertion->build(), |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Build the grant parameters |
89
|
|
|
* |
90
|
|
|
* @param \tbclla\Revolut\Interfaces\GrantsAccessTokens $requestToken |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
private function buildGrantParams(GrantsAccessTokens $requestToken) |
94
|
|
|
{ |
95
|
|
|
return [ |
96
|
|
|
'grant_type' => $requestToken->getGrantType(), |
97
|
|
|
$requestToken->getType() => $requestToken->getValue(), |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|