|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OAuth\OAuth2\Service; |
|
4
|
|
|
|
|
5
|
|
|
use OAuth\OAuth2\Service\AbstractService; |
|
6
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
|
7
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
|
8
|
|
|
use OAuth\Common\Http\Uri\Uri; |
|
9
|
|
|
use OAuth\Common\Consumer\CredentialsInterface; |
|
10
|
|
|
use OAuth\Common\Http\Client\ClientInterface; |
|
11
|
|
|
use OAuth\Common\Storage\TokenStorageInterface; |
|
12
|
|
|
use OAuth\Common\Http\Uri\UriInterface; |
|
13
|
|
|
|
|
14
|
|
|
class Salesforce extends AbstractService |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Scopes |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
const SCOPE_API = 'api', |
|
22
|
|
|
SCOPE_REFRESH_TOKEN = 'refresh_token'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getAuthorizationEndpoint() |
|
28
|
|
|
{ |
|
29
|
|
|
return new Uri('https://login.salesforce.com/services/oauth2/authorize'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getAccessTokenEndpoint() |
|
36
|
|
|
{ |
|
37
|
|
|
return new Uri('https://na1.salesforce.com/services/oauth2/token'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function parseRequestTokenResponse($responseBody) |
|
44
|
|
|
{ |
|
45
|
|
|
parse_str($responseBody, $data); |
|
46
|
|
|
|
|
47
|
|
|
if (null === $data || !is_array($data)) { |
|
48
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
|
49
|
|
|
} elseif (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true') { |
|
50
|
|
|
throw new TokenResponseException('Error in retrieving token.'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this->parseAccessTokenResponse($responseBody); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function parseAccessTokenResponse($responseBody) |
|
60
|
|
|
{ |
|
61
|
|
|
$data = json_decode($responseBody, true); |
|
62
|
|
|
|
|
63
|
|
|
if (null === $data || !is_array($data)) { |
|
64
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
|
65
|
|
|
} elseif (isset($data['error'])) { |
|
66
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$token = new StdOAuth2Token(); |
|
70
|
|
|
$token->setAccessToken($data['access_token']); |
|
71
|
|
|
// Salesforce tokens evidently never expire... |
|
72
|
|
|
$token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES); |
|
73
|
|
|
unset($data['access_token']); |
|
74
|
|
|
|
|
75
|
|
|
if (isset($data['refresh_token'])) { |
|
76
|
|
|
$token->setRefreshToken($data['refresh_token']); |
|
77
|
|
|
unset($data['refresh_token']); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$token->setExtraParams($data); |
|
81
|
|
|
|
|
82
|
|
|
return $token; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getExtraOAuthHeaders() |
|
89
|
|
|
{ |
|
90
|
|
|
return array('Accept' => 'application/json'); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|