Bitrix24.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. public function requestAccessToken($code, $state = null)
  40. {
  41. if (null !== $state) {
  42. $this->validateAuthorizationState($state);
  43. }
  44. $responseBody = $this->httpClient->retrieveResponse(
  45. $this->getAccessTokenUri($code),
  46. array(),
  47. $this->getExtraOAuthHeaders(),
  48. 'GET'
  49. );
  50. $token = $this->parseAccessTokenResponse($responseBody);
  51. $this->storage->storeAccessToken($this->service(), $token);
  52. return $token;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getAccessTokenUri($code)
  58. {
  59. $parameters = array(
  60. 'code' => $code,
  61. 'client_id' => $this->credentials->getConsumerId(),
  62. 'client_secret' => $this->credentials->getConsumerSecret(),
  63. 'redirect_uri' => $this->credentials->getCallbackUrl(),
  64. 'grant_type' => 'authorization_code',
  65. 'scope' => $this->scopes
  66. );
  67. $parameters['scope'] = implode(' ', $this->scopes);
  68. // Build the url
  69. $url = $this->getAccessTokenEndpoint();
  70. foreach ($parameters as $key => $val) {
  71. $url->addToQuery($key, $val);
  72. }
  73. return $url;
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. protected function parseAccessTokenResponse($responseBody)
  79. {
  80. $data = json_decode($responseBody, true);
  81. if (null === $data || !is_array($data)) {
  82. throw new TokenResponseException('Unable to parse response.');
  83. } elseif (isset($data['error'])) {
  84. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  85. }
  86. $token = new StdOAuth2Token();
  87. $token->setAccessToken($data['access_token']);
  88. $token->setLifetime($data['expires_in']);
  89. if (isset($data['refresh_token'])) {
  90. $token->setRefreshToken($data['refresh_token']);
  91. unset($data['refresh_token']);
  92. }
  93. unset($data['access_token']);
  94. unset($data['expires_in']);
  95. $token->setExtraParams($data);
  96. return $token;
  97. }
  98. }