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 Bitrix24 extends AbstractService |
10
|
|
|
{ |
11
|
|
|
const SCOPE_DEPARTMENT = 'department'; |
12
|
|
|
const SCOPE_CRM = 'crm'; |
13
|
|
|
const SCOPE_CALENDAR = 'calendar'; |
14
|
|
|
const SCOPE_USER = 'user'; |
15
|
|
|
const SCOPE_ENTITY = 'entity'; |
16
|
|
|
const SCOPE_TASK = 'task'; |
17
|
|
|
const SCOPE_TASKS_EXTENDED = 'tasks_extended'; |
18
|
|
|
const SCOPE_IM = 'im'; |
19
|
|
|
const SCOPE_LOG = 'log'; |
20
|
|
|
const SCOPE_SONET_GROUP = 'sonet_group'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function getAuthorizationEndpoint() |
26
|
|
|
{ |
27
|
|
|
return new Uri(sprintf('%s/oauth/authorize/', $this->baseApiUri)); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function getAccessTokenEndpoint() |
34
|
|
|
{ |
35
|
|
|
return new Uri(sprintf('%s/oauth/token/', $this->baseApiUri)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
protected function getAuthorizationMethod() |
42
|
|
|
{ |
43
|
|
|
return static::AUTHORIZATION_METHOD_QUERY_STRING_V4; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function requestAccessToken($code, $state = null) |
50
|
|
|
{ |
51
|
|
|
if (null !== $state) { |
52
|
|
|
$this->validateAuthorizationState($state); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$responseBody = $this->httpClient->retrieveResponse( |
56
|
|
|
$this->getAccessTokenUri($code), |
57
|
|
|
[], |
58
|
|
|
$this->getExtraOAuthHeaders(), |
59
|
|
|
'GET' |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$token = $this->parseAccessTokenResponse($responseBody); |
63
|
|
|
$this->storage->storeAccessToken($this->service(), $token); |
64
|
|
|
|
65
|
|
|
return $token; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function getAccessTokenUri($code) |
72
|
|
|
{ |
73
|
|
|
$parameters = [ |
74
|
|
|
'code' => $code, |
75
|
|
|
'client_id' => $this->credentials->getConsumerId(), |
76
|
|
|
'client_secret' => $this->credentials->getConsumerSecret(), |
77
|
|
|
'redirect_uri' => $this->credentials->getCallbackUrl(), |
78
|
|
|
'grant_type' => 'authorization_code', |
79
|
|
|
'scope' => $this->scopes, |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
$parameters['scope'] = implode(' ', $this->scopes); |
83
|
|
|
|
84
|
|
|
// Build the url |
85
|
|
|
$url = $this->getAccessTokenEndpoint(); |
86
|
|
|
foreach ($parameters as $key => $val) { |
87
|
|
|
$url->addToQuery($key, $val); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $url; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
protected function parseAccessTokenResponse($responseBody) |
97
|
|
|
{ |
98
|
|
|
$data = json_decode($responseBody, true); |
99
|
|
|
|
100
|
|
|
if (null === $data || !is_array($data)) { |
101
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
102
|
|
|
} elseif (isset($data['error'])) { |
103
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$token = new StdOAuth2Token(); |
107
|
|
|
$token->setAccessToken($data['access_token']); |
108
|
|
|
$token->setLifetime($data['expires_in']); |
109
|
|
|
|
110
|
|
|
if (isset($data['refresh_token'])) { |
111
|
|
|
$token->setRefreshToken($data['refresh_token']); |
112
|
|
|
unset($data['refresh_token']); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
unset($data['access_token'], $data['expires_in']); |
116
|
|
|
|
117
|
|
|
$token->setExtraParams($data); |
118
|
|
|
|
119
|
|
|
return $token; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|