1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\OAuth2\Service; |
4
|
|
|
|
5
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
6
|
|
|
use OAuth\Common\Http\Uri\Uri; |
7
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* MYOB Service |
11
|
|
|
* @link http://developer.myob.com/api/accountright/api-overview/authentication/ |
12
|
|
|
*/ |
13
|
|
|
class Myob extends AbstractService |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string The CompanyFile scope |
17
|
|
|
*/ |
18
|
|
|
const SCOPE_COMPANYFILE = "CompanyFile"; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @inheritdoc |
22
|
|
|
*/ |
23
|
|
|
public function getAuthorizationEndpoint() |
24
|
|
|
{ |
25
|
|
|
return new Uri("https://secure.myob.com/oauth2/account/authorize"); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public function getAccessTokenEndpoint() |
32
|
|
|
{ |
33
|
|
|
return new Uri("https://secure.myob.com/oauth2/v1/authorize"); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
|
|
protected function parseAccessTokenResponse($responseBody) |
40
|
|
|
{ |
41
|
|
|
$data = json_decode($responseBody, true); |
42
|
|
|
|
43
|
|
|
if (null === $data || !is_array($data)) { |
44
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
45
|
|
|
} elseif (isset($data['error_description'])) { |
46
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error_description'] . '"'); |
47
|
|
|
} elseif (isset($data['error'])) { |
48
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$token = new StdOAuth2Token(); |
52
|
|
|
$token->setAccessToken($data['access_token']); |
53
|
|
|
$token->setLifeTime($data['expires_in']); |
54
|
|
|
|
55
|
|
|
if (isset($data['refresh_token'])) { |
56
|
|
|
$token->setRefreshToken($data['refresh_token']); |
57
|
|
|
unset($data['refresh_token']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
unset($data['access_token']); |
61
|
|
|
unset($data['expires_in']); |
62
|
|
|
|
63
|
|
|
$token->setExtraParams($data); |
64
|
|
|
|
65
|
|
|
return $token; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritdoc |
70
|
|
|
*/ |
71
|
|
|
public function requestAccessToken($code, $state = null) |
72
|
|
|
{ |
73
|
|
|
if (null !== $state) { |
74
|
|
|
$this->validateAuthorizationState($state); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$bodyParams = array( |
78
|
|
|
'code' => $code, |
79
|
|
|
'client_id' => $this->credentials->getConsumerId(), |
80
|
|
|
'client_secret' => $this->credentials->getConsumerSecret(), |
81
|
|
|
'redirect_uri' => $this->credentials->getCallbackUrl(), |
82
|
|
|
'grant_type' => 'authorization_code', |
83
|
|
|
'scope' => implode(' ', $this->scopes), |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$responseBody = $this->httpClient->retrieveResponse( |
87
|
|
|
$this->getAccessTokenEndpoint(), |
88
|
|
|
$bodyParams, |
89
|
|
|
$this->getExtraOAuthHeaders() |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$token = $this->parseAccessTokenResponse($responseBody); |
93
|
|
|
$this->storage->storeAccessToken($this->service(), $token); |
94
|
|
|
|
95
|
|
|
return $token; |
96
|
|
|
} |
97
|
|
|
} |