JawboneUP.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /**
  11. * Jawbone UP service.
  12. *
  13. * @author Andrii Gakhov <andrii.gakhov@gmail.com>
  14. * @link https://jawbone.com/up/developer/authentication
  15. */
  16. class JawboneUP extends AbstractService
  17. {
  18. /**
  19. * Defined scopes
  20. *
  21. *
  22. * @link https://jawbone.com/up/developer/authentication
  23. */
  24. // general information scopes
  25. const SCOPE_BASIC_READ = 'basic_read';
  26. const SCOPE_EXTENDED_READ = 'extended_read';
  27. const SCOPE_LOCATION_READ = 'location_read';
  28. const SCOPE_FRIENDS_READ = 'friends_read';
  29. // mood scopes
  30. const SCOPE_MOOD_READ = 'mood_read';
  31. const SCOPE_MOOD_WRITE = 'mood_write';
  32. // move scopes
  33. const SCOPE_MOVE_READ = 'move_read';
  34. const SCOPE_MOVE_WRITE = 'move_write';
  35. // sleep scopes
  36. const SCOPE_SLEEP_READ = 'sleep_read';
  37. const SCOPE_SLEEP_WRITE = 'sleep_write';
  38. // meal scopes
  39. const SCOPE_MEAL_READ = 'meal_read';
  40. const SCOPE_MEAL_WRITE = 'meal_write';
  41. // weight scopes
  42. const SCOPE_WEIGHT_READ = 'weight_read';
  43. const SCOPE_WEIGHT_WRITE = 'weight_write';
  44. // generic event scopes
  45. const SCOPE_GENERIC_EVENT_READ = 'generic_event_read';
  46. const SCOPE_GENERIC_EVENT_WRITE = 'generic_event_write';
  47. public function __construct(
  48. CredentialsInterface $credentials,
  49. ClientInterface $httpClient,
  50. TokenStorageInterface $storage,
  51. $scopes = array(),
  52. UriInterface $baseApiUri = null
  53. ) {
  54. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri);
  55. if (null === $baseApiUri) {
  56. $this->baseApiUri = new Uri('https://jawbone.com/nudge/api/v.1.1/');
  57. }
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getAuthorizationUri(array $additionalParameters = array())
  63. {
  64. $parameters = array_merge(
  65. $additionalParameters,
  66. array(
  67. 'client_id' => $this->credentials->getConsumerId(),
  68. 'redirect_uri' => $this->credentials->getCallbackUrl(),
  69. 'response_type' => 'code',
  70. )
  71. );
  72. $parameters['scope'] = implode(' ', $this->scopes);
  73. // Build the url
  74. $url = clone $this->getAuthorizationEndpoint();
  75. foreach ($parameters as $key => $val) {
  76. $url->addToQuery($key, $val);
  77. }
  78. return $url;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getAuthorizationEndpoint()
  84. {
  85. return new Uri('https://jawbone.com/auth/oauth2/auth');
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getAccessTokenEndpoint()
  91. {
  92. return new Uri('https://jawbone.com/auth/oauth2/token');
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. protected function getAuthorizationMethod()
  98. {
  99. return static::AUTHORIZATION_METHOD_HEADER_BEARER;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. protected function parseAccessTokenResponse($responseBody)
  105. {
  106. $data = json_decode($responseBody, true);
  107. if (null === $data || !is_array($data)) {
  108. throw new TokenResponseException('Unable to parse response.');
  109. } elseif (isset($data['error'])) {
  110. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  111. }
  112. $token = new StdOAuth2Token();
  113. $token->setAccessToken($data['access_token']);
  114. $token->setLifeTime($data['expires_in']);
  115. if (isset($data['refresh_token'])) {
  116. $token->setRefreshToken($data['refresh_token']);
  117. unset($data['refresh_token']);
  118. }
  119. unset($data['access_token']);
  120. unset($data['expires_in']);
  121. $token->setExtraParams($data);
  122. return $token;
  123. }
  124. }