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
|
|
|
/** |
15
|
|
|
* CleverReach service. |
16
|
|
|
* |
17
|
|
|
* @author Moritz Beller <[email protected]> |
18
|
|
|
* @link https://rest.cleverreach.com/explorer/ |
19
|
|
|
*/ |
20
|
|
|
class CleverReach extends AbstractService |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* default scope |
24
|
|
|
*/ |
25
|
|
|
const SCOPE_BASIC = 'basic'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* scope for read access |
29
|
|
|
*/ |
30
|
|
|
const SCOPE_READ = 'read'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* scope for write access |
34
|
|
|
*/ |
35
|
|
|
const SCOPE_WRITE = 'write'; |
36
|
|
|
|
37
|
|
|
public function __construct( |
38
|
|
|
CredentialsInterface $credentials, |
39
|
|
|
ClientInterface $httpClient, |
40
|
|
|
TokenStorageInterface $storage, |
41
|
|
|
$scopes = array(), |
42
|
|
|
UriInterface $baseApiUri = null |
43
|
|
|
) |
44
|
|
|
{ |
|
|
|
|
45
|
|
|
parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri); |
46
|
|
|
if (null === $baseApiUri) { |
47
|
|
|
$this->baseApiUri = new Uri('https://rest.cleverreach.com'); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function getAuthorizationEndpoint() |
55
|
|
|
{ |
56
|
|
|
return new Uri('https://rest.cleverreach.com/oauth/authorize.php'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function getAccessTokenEndpoint(){ |
63
|
|
|
return new Uri('https://rest.cleverreach.com/oauth/token.php'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
protected function getAuthorizationMethod() |
70
|
|
|
{ |
71
|
|
|
return static::AUTHORIZATION_METHOD_HEADER_BEARER; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Used to configure response type -- we want JSON, default is query string format |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
protected function getExtraOAuthHeaders() |
80
|
|
|
{ |
81
|
|
|
return array('Accept' => 'application/json'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
protected function parseAccessTokenResponse($responseBody) |
88
|
|
|
{ |
89
|
|
|
$data = json_decode($responseBody, true); |
90
|
|
|
|
91
|
|
|
if (null === $data || !is_array($data)) { |
92
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
93
|
|
|
} elseif (isset($data['error'])) { |
94
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
95
|
|
|
} |
96
|
|
|
$token = new StdOAuth2Token(); |
97
|
|
|
$token->setAccessToken($data['access_token']); |
98
|
|
|
// CleverReach never expire... |
99
|
|
|
$token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES); |
100
|
|
|
unset($data['access_token']); |
101
|
|
|
$token->setExtraParams($data); |
102
|
|
|
return $token; |
103
|
|
|
} |
104
|
|
|
} |