Google.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace OAuth\OAuth2\Service;
  3. use OAuth\Common\Consumer\CredentialsInterface;
  4. use OAuth\Common\Http\Client\ClientInterface;
  5. use OAuth\Common\Http\Uri\UriInterface;
  6. use OAuth\Common\Storage\TokenStorageInterface;
  7. use OAuth\OAuth2\Token\StdOAuth2Token;
  8. use OAuth\Common\Http\Exception\TokenResponseException;
  9. use OAuth\OAuth2\Service\Exception\InvalidAccessTypeException;
  10. use OAuth\Common\Http\Uri\Uri;
  11. class Google extends AbstractService
  12. {
  13. /**
  14. * Defined scopes - More scopes are listed here:
  15. * https://developers.google.com/oauthplayground/
  16. *
  17. * Make a pull request if you need more scopes.
  18. */
  19. // Basic
  20. const SCOPE_EMAIL = 'email';
  21. const SCOPE_PROFILE = 'profile';
  22. const SCOPE_USERINFO_EMAIL = 'https://www.googleapis.com/auth/userinfo.email';
  23. const SCOPE_USERINFO_PROFILE = 'https://www.googleapis.com/auth/userinfo.profile';
  24. // Google+
  25. const SCOPE_GPLUS_ME = 'https://www.googleapis.com/auth/plus.me';
  26. const SCOPE_GPLUS_LOGIN = 'https://www.googleapis.com/auth/plus.login';
  27. const SCOPE_GPLUS_CIRCLES_READ = 'https://www.googleapis.com/auth/plus.circles.read';
  28. const SCOPE_GPLUS_CIRCLES_WRITE = 'https://www.googleapis.com/auth/plus.circles.write';
  29. const SCOPE_GPLUS_STREAM_READ = 'https://www.googleapis.com/auth/plus.stream.read';
  30. const SCOPE_GPLUS_STREAM_WRITE = 'https://www.googleapis.com/auth/plus.stream.write';
  31. const SCOPE_GPLUS_MEDIA = 'https://www.googleapis.com/auth/plus.media.upload';
  32. // Google Drive
  33. const SCOPE_DOCUMENTSLIST = 'https://docs.google.com/feeds/';
  34. const SCOPE_SPREADSHEETS = 'https://spreadsheets.google.com/feeds/';
  35. const SCOPE_GOOGLEDRIVE = 'https://www.googleapis.com/auth/drive';
  36. const SCOPE_DRIVE_APPS = 'https://www.googleapis.com/auth/drive.appdata';
  37. const SCOPE_DRIVE_APPS_READ_ONLY = 'https://www.googleapis.com/auth/drive.apps.readonly';
  38. const SCOPE_GOOGLEDRIVE_FILES = 'https://www.googleapis.com/auth/drive.file';
  39. const SCOPE_DRIVE_METADATA_READ_ONLY = 'https://www.googleapis.com/auth/drive.metadata.readonly';
  40. const SCOPE_DRIVE_READ_ONLY = 'https://www.googleapis.com/auth/drive.readonly';
  41. const SCOPE_DRIVE_SCRIPTS = 'https://www.googleapis.com/auth/drive.scripts';
  42. // Cloud Print
  43. const SCOPE_CLOUD_PRINT = 'https://www.googleapis.com/auth/cloudprint';
  44. // Adwords
  45. const SCOPE_ADSENSE = 'https://www.googleapis.com/auth/adsense';
  46. const SCOPE_ADWORDS = 'https://www.googleapis.com/auth/adwords/';
  47. const SCOPE_GAN = 'https://www.googleapis.com/auth/gan'; // google affiliate network...?
  48. // Google Analytics
  49. const SCOPE_ANALYTICS = 'https://www.googleapis.com/auth/analytics';
  50. const SCOPE_ANALYTICS_EDIT = 'https://www.googleapis.com/auth/analytics.edit';
  51. const SCOPE_ANALYTICS_MANAGE_USERS = 'https://www.googleapis.com/auth/analytics.manage.users';
  52. const SCOPE_ANALYTICS_READ_ONLY = 'https://www.googleapis.com/auth/analytics.readonly';
  53. //Gmail
  54. const SCOPE_GMAIL_MODIFY = 'https://www.googleapis.com/auth/gmail.modify';
  55. const SCOPE_GMAIL_READONLY = 'https://www.googleapis.com/auth/gmail.readonly';
  56. const SCOPE_GMAIL_COMPOSE = 'https://www.googleapis.com/auth/gmail.compose';
  57. const SCOPE_GMAIL_SEND = 'https://www.googleapis.com/auth/gmail.send';
  58. const SCOPE_GMAIL_INSERT = 'https://www.googleapis.com/auth/gmail.insert';
  59. const SCOPE_GMAIL_LABELS = 'https://www.googleapis.com/auth/gmail.labels';
  60. const SCOPE_GMAIL_FULL = 'https://mail.google.com/';
  61. // Other services
  62. const SCOPE_BOOKS = 'https://www.googleapis.com/auth/books';
  63. const SCOPE_BLOGGER = 'https://www.googleapis.com/auth/blogger';
  64. const SCOPE_CALENDAR = 'https://www.googleapis.com/auth/calendar';
  65. const SCOPE_CALENDAR_READ_ONLY = 'https://www.googleapis.com/auth/calendar.readonly';
  66. const SCOPE_CONTACT = 'https://www.google.com/m8/feeds/';
  67. const SCOPE_CONTACTS_RO = 'https://www.googleapis.com/auth/contacts.readonly';
  68. const SCOPE_CHROMEWEBSTORE = 'https://www.googleapis.com/auth/chromewebstore.readonly';
  69. const SCOPE_GMAIL = 'https://mail.google.com/mail/feed/atom';
  70. const SCOPE_GMAIL_IMAP_SMTP = 'https://mail.google.com';
  71. const SCOPE_PICASAWEB = 'https://picasaweb.google.com/data/';
  72. const SCOPE_SITES = 'https://sites.google.com/feeds/';
  73. const SCOPE_URLSHORTENER = 'https://www.googleapis.com/auth/urlshortener';
  74. const SCOPE_WEBMASTERTOOLS = 'https://www.google.com/webmasters/tools/feeds/';
  75. const SCOPE_TASKS = 'https://www.googleapis.com/auth/tasks';
  76. // Cloud services
  77. const SCOPE_CLOUDSTORAGE = 'https://www.googleapis.com/auth/devstorage.read_write';
  78. const SCOPE_CONTENTFORSHOPPING = 'https://www.googleapis.com/auth/structuredcontent'; // what even is this
  79. const SCOPE_USER_PROVISIONING = 'https://apps-apis.google.com/a/feeds/user/';
  80. const SCOPE_GROUPS_PROVISIONING = 'https://apps-apis.google.com/a/feeds/groups/';
  81. const SCOPE_NICKNAME_PROVISIONING = 'https://apps-apis.google.com/a/feeds/alias/';
  82. // Old
  83. const SCOPE_ORKUT = 'https://www.googleapis.com/auth/orkut';
  84. const SCOPE_GOOGLELATITUDE =
  85. 'https://www.googleapis.com/auth/latitude.all.best https://www.googleapis.com/auth/latitude.all.city';
  86. const SCOPE_OPENID = 'openid';
  87. // YouTube
  88. const SCOPE_YOUTUBE_GDATA = 'https://gdata.youtube.com';
  89. const SCOPE_YOUTUBE_ANALYTICS_MONETARY = 'https://www.googleapis.com/auth/yt-analytics-monetary.readonly';
  90. const SCOPE_YOUTUBE_ANALYTICS = 'https://www.googleapis.com/auth/yt-analytics.readonly';
  91. const SCOPE_YOUTUBE = 'https://www.googleapis.com/auth/youtube';
  92. const SCOPE_YOUTUBE_READ_ONLY = 'https://www.googleapis.com/auth/youtube.readonly';
  93. const SCOPE_YOUTUBE_UPLOAD = 'https://www.googleapis.com/auth/youtube.upload';
  94. const SCOPE_YOUTUBE_PARTNER = 'https://www.googleapis.com/auth/youtubepartner';
  95. const SCOPE_YOUTUBE_PARTNER_AUDIT = 'https://www.googleapis.com/auth/youtubepartner-channel-audit';
  96. // Google Glass
  97. const SCOPE_GLASS_TIMELINE = 'https://www.googleapis.com/auth/glass.timeline';
  98. const SCOPE_GLASS_LOCATION = 'https://www.googleapis.com/auth/glass.location';
  99. // Android Publisher
  100. const SCOPE_ANDROID_PUBLISHER = 'https://www.googleapis.com/auth/androidpublisher';
  101. // Google Gsuite
  102. const SCOPE_ADMIN_DIRECTORY_USER = "https://www.googleapis.com/auth/admin.directory.user";
  103. const SCOPE_ADMIN_DIRECTORY_CUSTOMER = "https://www.googleapis.com/auth/admin.directory.customer";
  104. protected $accessType = 'online';
  105. public function __construct(
  106. CredentialsInterface $credentials,
  107. ClientInterface $httpClient,
  108. TokenStorageInterface $storage,
  109. $scopes = array(),
  110. UriInterface $baseApiUri = null
  111. ) {
  112. parent::__construct($credentials, $httpClient, $storage, $scopes, $baseApiUri, true);
  113. if (null === $baseApiUri) {
  114. $this->baseApiUri = new Uri('https://www.googleapis.com/oauth2/v1/');
  115. }
  116. }
  117. public function setAccessType($accessType)
  118. {
  119. if (!in_array($accessType, array('online', 'offline'), true)) {
  120. throw new InvalidAccessTypeException('Invalid accessType, expected either online or offline');
  121. }
  122. $this->accessType = $accessType;
  123. }
  124. // LDR CHANGE Add approval_prompt to force the prompt if value is set to 'force' so it force return of a "refresh token" in addition to "standard token"
  125. public $approvalPrompt='auto';
  126. public function setApprouvalPrompt($prompt)
  127. {
  128. if (!in_array($prompt, array('auto', 'force'), true)) {
  129. // @todo Maybe could we rename this exception
  130. throw new InvalidAccessTypeException('Invalid approuvalPrompt, expected either auto or force.');
  131. }
  132. $this->approvalPrompt = $prompt;
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function getAuthorizationEndpoint()
  138. {
  139. // LDR CHANGE Add approval_prompt to force the prompt if value is set to 'force' so it force return of a "refresh token" in addition to "standard token"
  140. //return new Uri('https://accounts.google.com/o/oauth2/auth?access_type='.$this->accessType);
  141. $url = 'https://accounts.google.com/o/oauth2/auth?'.($this->approvalPrompt?'approval_prompt='.$this->approvalPrompt.'&':'').'access_type='.$this->accessType;
  142. return new Uri($url);
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function getAccessTokenEndpoint()
  148. {
  149. return new Uri('https://accounts.google.com/o/oauth2/token');
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. protected function parseAccessTokenResponse($responseBody)
  155. {
  156. $data = json_decode($responseBody, true);
  157. if (null === $data || !is_array($data)) {
  158. throw new TokenResponseException('Unable to parse response.');
  159. } elseif (isset($data['error'])) {
  160. throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
  161. }
  162. $token = new StdOAuth2Token();
  163. $token->setAccessToken($data['access_token']);
  164. $token->setLifetime($data['expires_in']);
  165. if (isset($data['refresh_token'])) {
  166. $token->setRefreshToken($data['refresh_token']);
  167. unset($data['refresh_token']);
  168. }
  169. unset($data['access_token']);
  170. unset($data['expires_in']);
  171. $token->setExtraParams($data);
  172. return $token;
  173. }
  174. }