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