1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Hubic service. |
4
|
|
|
* |
5
|
|
|
* @author Pedro Amorim <[email protected]> |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
7
|
|
|
* @link https://api.hubic.com/docs/ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace OAuth\OAuth2\Service; |
11
|
|
|
|
12
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
13
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
14
|
|
|
use OAuth\Common\Http\Uri\Uri; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Hubic service. |
18
|
|
|
* |
19
|
|
|
* @author Pedro Amorim <[email protected]> |
20
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
21
|
|
|
* @link https://api.hubic.com/docs/ |
22
|
|
|
*/ |
23
|
|
|
class Hubic extends AbstractService |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
// Scopes |
27
|
|
|
const SCOPE_USAGE_GET = 'usage.r'; |
28
|
|
|
const SCOPE_ACCOUNT_GET = 'account.r'; |
29
|
|
|
const SCOPE_GETALLLINKS_GET = 'getAllLinks.r'; |
30
|
|
|
const SCOPE_CREDENTIALS_GET = 'credentials.r'; |
31
|
|
|
const SCOPE_SPONSORCODE_GET = 'sponsorCode.r'; |
32
|
|
|
const SCOPE_ACTIVATE_POST = 'activate.w'; |
33
|
|
|
const SCOPE_SPONSORED_GET = 'sponsored.r'; |
34
|
|
|
const SCOPE_LINKS_GET = 'links.r'; |
35
|
|
|
const SCOPE_LINKS_POST = 'links.rw'; |
36
|
|
|
const SCOPE_LINKS_ALL = 'links.drw'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
protected function init() |
42
|
|
|
{ |
43
|
|
|
$this->stateParameterInAuthUrl = true; |
44
|
|
|
|
45
|
|
|
if( $this->baseApiUri === null ) { |
|
|
|
|
46
|
|
|
$this->baseApiUri = new Uri('https://api.hubic.com/'); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function getAuthorizationEndpoint() |
54
|
|
|
{ |
55
|
|
|
return new Uri('https://api.hubic.com/oauth/auth'); |
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function getAccessTokenEndpoint() |
63
|
|
|
{ |
64
|
|
|
return new Uri('https://api.hubic.com/oauth/token'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
protected function getAuthorizationMethod() |
71
|
|
|
{ |
72
|
|
|
return static::AUTHORIZATION_METHOD_HEADER_BEARER; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
protected function parseAccessTokenResponse($responseBody) |
79
|
|
|
{ |
80
|
|
|
$data = json_decode($responseBody, true); |
81
|
|
|
|
82
|
|
|
if (null === $data || !is_array($data)) { |
83
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
84
|
|
|
} elseif (isset($data['error'])) { |
85
|
|
|
throw new TokenResponseException( |
86
|
|
|
'Error in retrieving token: "' . $data['error'] . '"' |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$token = new StdOAuth2Token(); |
91
|
|
|
$token->setAccessToken($data['access_token']); |
92
|
|
|
$token->setLifetime($data['expires_in']); |
93
|
|
|
|
94
|
|
|
if (isset($data['refresh_token'])) { |
95
|
|
|
$token->setRefreshToken($data['refresh_token']); |
96
|
|
|
unset($data['refresh_token']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
unset($data['access_token']); |
100
|
|
|
unset($data['expires_in']); |
101
|
|
|
|
102
|
|
|
$token->setExtraParams($data); |
103
|
|
|
|
104
|
|
|
return $token; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function getAuthorizationUri(array $additionalParameters = array()) |
112
|
|
|
{ |
113
|
|
|
$parameters = array_merge( |
114
|
|
|
$additionalParameters, |
115
|
|
|
array( |
116
|
|
|
'type' => 'web_server', |
117
|
|
|
'client_id' => $this->credentials->getConsumerId(), |
118
|
|
|
'redirect_uri' => $this->credentials->getCallbackUrl(), |
119
|
|
|
'response_type' => 'code', |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
// special, hubic use a param scope with commas |
124
|
|
|
// between scopes instead of spaces |
125
|
|
|
$parameters['scope'] = implode(',', $this->scopes); |
126
|
|
|
|
127
|
|
|
if ($this->needsStateParameterInAuthUrl()) { |
128
|
|
|
if (!isset($parameters['state'])) { |
129
|
|
|
$parameters['state'] = $this->generateAuthorizationState(); |
130
|
|
|
} |
131
|
|
|
$this->storeAuthorizationState($parameters['state']); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Build the url |
135
|
|
|
$url = clone $this->getAuthorizationEndpoint(); |
136
|
|
|
foreach ($parameters as $key => $val) { |
137
|
|
|
$url->addToQuery($key, $val); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $url; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|