|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Surge\LaravelSalesforce; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
|
6
|
|
|
|
|
7
|
|
|
class SalesforceAuth |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var string |
|
11
|
|
|
*/ |
|
12
|
|
|
public $id; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
public $accessToken; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
public $instanceUrl; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
public $url; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var bool |
|
31
|
|
|
*/ |
|
32
|
|
|
private $authenticated = false; |
|
33
|
|
|
|
|
34
|
|
|
private $client; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
private $version = [ |
|
40
|
|
|
'label' => 'Summer 17', |
|
41
|
|
|
'url' => '/services/data/v39.0', |
|
42
|
|
|
'version' => '39.0', |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* SalesforceAuth constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param $client |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(ClientInterface $client) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->client = $client; |
|
53
|
|
|
|
|
54
|
|
|
if (config('laravel-salesforce.disable_on_local') === false) { |
|
55
|
|
|
$this->login(); |
|
56
|
|
|
} else { |
|
57
|
|
|
$this->id = '##########'; |
|
58
|
|
|
$this->accessToken = '@@@@@@'; |
|
59
|
|
|
$this->instanceUrl = 'http://localhost'; |
|
60
|
|
|
$this->url = 'http://localhost'; |
|
61
|
|
|
$this->authenticated = true; |
|
62
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Check if authenticated |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
|
|
public function isAuthenticated(): bool |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->authenticated; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Login. |
|
78
|
|
|
*/ |
|
79
|
|
|
private function login() |
|
80
|
|
|
{ |
|
81
|
|
|
$authEndpoint = config('laravel-salesforce.auth_endpoint'); |
|
82
|
|
|
$body = [ |
|
83
|
|
|
'grant_type' => 'password', |
|
84
|
|
|
'client_id' => config('laravel-salesforce.client_id'), |
|
85
|
|
|
'client_secret' => config('laravel-salesforce.client_secret'), |
|
86
|
|
|
'username' => config('laravel-salesforce.username'), |
|
87
|
|
|
'password' => config('laravel-salesforce.password'), |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
|
|
$response = $this->client->post($authEndpoint, [ |
|
91
|
|
|
'form_params' => $body, |
|
92
|
|
|
])->getBody()->getContents(); |
|
93
|
|
|
|
|
94
|
|
|
$responseObject = \GuzzleHttp\json_decode($response); |
|
95
|
|
|
|
|
96
|
|
|
$this->id = $responseObject->id; |
|
97
|
|
|
$this->accessToken = $responseObject->access_token; |
|
98
|
|
|
$this->instanceUrl = $responseObject->instance_url; |
|
99
|
|
|
$this->url = $responseObject->instance_url . $this->version['url']; |
|
100
|
|
|
|
|
101
|
|
|
if ($this->accessToken !== null) { |
|
102
|
|
|
$this->authenticated = true; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|