1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Akeneo\SalesForce\Authentification; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
|
7
|
|
|
class AccessTokenGenerator |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Create an access token from stored json data. |
11
|
|
|
* |
12
|
|
|
* @param $text |
13
|
|
|
* |
14
|
|
|
* @return AccessToken |
15
|
|
|
*/ |
16
|
|
|
public function createFromJson($text) |
17
|
|
|
{ |
18
|
|
|
$savedToken = json_decode($text, true); |
19
|
|
|
|
20
|
|
|
$id = $savedToken[TokenFields::ID]; |
21
|
|
|
|
22
|
|
|
$dateIssued = Carbon::parse($savedToken[TokenFields::DATE_ISSUED]); |
23
|
|
|
|
24
|
|
|
$dateExpires = Carbon::parse($savedToken[TokenFields::DATE_EXPIRES]); |
25
|
|
|
|
26
|
|
|
$scope = $savedToken[TokenFields::SCOPE]; |
27
|
|
|
|
28
|
|
|
$tokenType = $savedToken[TokenFields::TOKEN_TYPE]; |
29
|
|
|
|
30
|
|
|
$refreshToken = $savedToken[TokenFields::REFRESH_TOKEN]; |
31
|
|
|
|
32
|
|
|
$signature = $savedToken[TokenFields::SIGNATURE]; |
33
|
|
|
|
34
|
|
|
$accessToken = $savedToken[TokenFields::ACCESS_TOKEN]; |
35
|
|
|
|
36
|
|
|
$apiUrl = $savedToken[TokenFields::API_URL]; |
37
|
|
|
|
38
|
|
|
$token = new AccessToken( |
39
|
|
|
$id, |
40
|
|
|
$dateIssued, |
41
|
|
|
$dateExpires, |
42
|
|
|
$scope, |
43
|
|
|
$tokenType, |
44
|
|
|
$refreshToken, |
45
|
|
|
$signature, |
46
|
|
|
$accessToken, |
47
|
|
|
$apiUrl |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
return $token; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create an access token object from the salesforce response data. |
55
|
|
|
* |
56
|
|
|
* @param array $salesforceToken |
57
|
|
|
* |
58
|
|
|
* @return AccessToken |
59
|
|
|
*/ |
60
|
|
|
public function createFromSalesforceResponse(array $salesforceToken) |
61
|
|
|
{ |
62
|
|
|
$dateIssued = Carbon::createFromTimestamp((int) ($salesforceToken[TokenFields::ISSUED_AT] / 1000)); |
63
|
|
|
|
64
|
|
|
$dateExpires = $dateIssued->copy()->addHour()->subMinutes(5); |
65
|
|
|
|
66
|
|
|
$id = $this->getKeyIfSet($salesforceToken, TokenFields::ID); |
67
|
|
|
|
68
|
|
|
$scope = explode(' ', $this->getKeyIfSet($salesforceToken, TokenFields::SCOPE)); |
69
|
|
|
|
70
|
|
|
$refreshToken = $this->getKeyIfSet($salesforceToken, TokenFields::SF_REFRESH_TOKEN); |
71
|
|
|
|
72
|
|
|
$signature = $this->getKeyIfSet($salesforceToken, TokenFields::SIGNATURE); |
73
|
|
|
|
74
|
|
|
$tokenType = $this->getKeyIfSet($salesforceToken, TokenFields::SF_TOKEN_TYPE); |
75
|
|
|
|
76
|
|
|
$accessToken = $salesforceToken[TokenFields::SF_ACCESS_TOKEN]; |
77
|
|
|
|
78
|
|
|
$apiUrl = $salesforceToken[TokenFields::SF_INSTANCE_URL]; |
79
|
|
|
|
80
|
|
|
$token = new AccessToken( |
81
|
|
|
$id, |
82
|
|
|
$dateIssued, |
83
|
|
|
$dateExpires, |
84
|
|
|
$scope, |
85
|
|
|
$tokenType, |
86
|
|
|
$refreshToken, |
87
|
|
|
$signature, |
88
|
|
|
$accessToken, |
89
|
|
|
$apiUrl |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
return $token; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param array $array |
97
|
|
|
* @param mixed $key |
98
|
|
|
*/ |
99
|
|
|
private function getKeyIfSet($array, $key) |
100
|
|
|
{ |
101
|
|
|
if (isset($array[$key])) { |
102
|
|
|
return $array[$key]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|