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\Common\Token\TokenInterface; |
8
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
9
|
|
|
|
10
|
|
|
class Harvest extends AbstractService |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
|
|
protected function init() |
16
|
|
|
{ |
17
|
|
|
if( $this->baseApiUri === null ) { |
|
|
|
|
18
|
|
|
$this->baseApiUri = new Uri('https://api.harvestapp.com/'); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function getAuthorizationUri(array $additionalParameters = array()) |
26
|
|
|
{ |
27
|
|
|
$parameters = array_merge( |
28
|
|
|
$additionalParameters, |
29
|
|
|
array( |
30
|
|
|
'client_id' => $this->credentials->getConsumerId(), |
31
|
|
|
'redirect_uri' => $this->credentials->getCallbackUrl(), |
32
|
|
|
'state' => 'optional-csrf-token', |
33
|
|
|
'response_type' => 'code', |
34
|
|
|
) |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
// Build the url |
38
|
|
|
$url = clone $this->getAuthorizationEndpoint(); |
39
|
|
|
foreach ($parameters as $key => $val) { |
40
|
|
|
$url->addToQuery($key, $val); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $url; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function getAuthorizationEndpoint() |
50
|
|
|
{ |
51
|
|
|
return new Uri('https://api.harvestapp.com/oauth2/authorize'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function getAccessTokenEndpoint() |
58
|
|
|
{ |
59
|
|
|
return new Uri('https://api.harvestapp.com/oauth2/token'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
protected function getAuthorizationMethod() |
66
|
|
|
{ |
67
|
|
|
return static::AUTHORIZATION_METHOD_QUERY_STRING; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
protected function parseAccessTokenResponse($responseBody) |
74
|
|
|
{ |
75
|
|
|
$data = json_decode($responseBody, true); |
76
|
|
|
|
77
|
|
|
if (null === $data || ! is_array($data)) { |
78
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
79
|
|
|
} elseif (isset($data['error'])) { |
80
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$token = new StdOAuth2Token(); |
84
|
|
|
$token->setAccessToken($data['access_token']); |
85
|
|
|
$token->setLifetime($data['expires_in']); |
86
|
|
|
$token->setRefreshToken($data['refresh_token']); |
87
|
|
|
|
88
|
|
|
unset($data['access_token']); |
89
|
|
|
|
90
|
|
|
$token->setExtraParams($data); |
91
|
|
|
|
92
|
|
|
return $token; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Refreshes an OAuth2 access token. |
97
|
|
|
* |
98
|
|
|
* @param TokenInterface $token |
99
|
|
|
* |
100
|
|
|
* @return TokenInterface $token |
101
|
|
|
* |
102
|
|
|
* @throws MissingRefreshTokenException |
103
|
|
|
*/ |
104
|
|
|
public function refreshAccessToken(TokenInterface $token) |
105
|
|
|
{ |
106
|
|
|
$refreshToken = $token->getRefreshToken(); |
107
|
|
|
|
108
|
|
|
if (empty($refreshToken)) { |
109
|
|
|
throw new MissingRefreshTokenException(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$parameters = array( |
113
|
|
|
'grant_type' => 'refresh_token', |
114
|
|
|
'type' => 'web_server', |
115
|
|
|
'client_id' => $this->credentials->getConsumerId(), |
116
|
|
|
'client_secret' => $this->credentials->getConsumerSecret(), |
117
|
|
|
'refresh_token' => $refreshToken, |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$responseBody = $this->httpClient->retrieveResponse( |
121
|
|
|
$this->getAccessTokenEndpoint(), |
122
|
|
|
$parameters, |
123
|
|
|
$this->getExtraOAuthHeaders() |
124
|
|
|
); |
125
|
|
|
$token = $this->parseAccessTokenResponse($responseBody); |
126
|
|
|
$this->storage->storeAccessToken($this->service(), $token, $this->account()); |
127
|
|
|
|
128
|
|
|
return $token; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
protected function getExtraOAuthHeaders() |
135
|
|
|
{ |
136
|
|
|
return array('Accept' => 'application/json'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Return any additional headers always needed for this service implementation's API calls. |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
protected function getExtraApiHeaders() |
145
|
|
|
{ |
146
|
|
|
return array('Accept' => 'application/json'); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|