Bitrix24.php 3.4 KB

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