Gdata_OAuth_Helper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. require_once 'Zend/Oauth/Consumer.php';
  3. require_once 'Zend/Gdata/Query.php';
  4. /**
  5. * Wrapper class for Google's OAuth implementation. In particular, this helper
  6. * bundles the token endpoints and manages the Google-specific parameters such
  7. * as the hd and scope parameter.
  8. *
  9. * @category Zend
  10. * @package Zend_Gdata
  11. * @subpackage Demos
  12. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  13. * @license http://framework.zend.com/license/new-bsd New BSD License
  14. */
  15. class Gdata_OAuth_Helper extends Zend_Oauth_Consumer {
  16. // Google's default oauth parameters/constants.
  17. private $_defaultOptions = array(
  18. 'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER,
  19. 'version' => '1.0',
  20. 'requestTokenUrl' => 'https://www.google.com/accounts/OAuthGetRequestToken',
  21. 'userAuthorizationUrl' => 'https://www.google.com/accounts/OAuthAuthorizeToken',
  22. 'accessTokenUrl' => 'https://www.google.com/accounts/OAuthGetAccessToken'
  23. );
  24. /**
  25. * Create Gdata_OAuth_Helper object
  26. *
  27. * @param string $consumerKey OAuth consumer key (domain).
  28. * @param string $consumerSecret (optional) OAuth consumer secret. Required if
  29. * using HMAC-SHA1 for a signature method.
  30. * @param string $sigMethod (optional) The oauth_signature method to use.
  31. * Defaults to HMAC-SHA1. RSA-SHA1 is also supported.
  32. */
  33. public function __construct($consumerKey, $consumerSecret=null,
  34. $sigMethod='HMAC-SHA1') {
  35. $this->_defaultOptions['consumerKey'] = $consumerKey;
  36. $this->_defaultOptions['consumerSecret'] = $consumerSecret;
  37. $this->_defaultOptions['signatureMethod'] = $sigMethod;
  38. parent::__construct($this->_defaultOptions);
  39. }
  40. /**
  41. * Getter for the oauth options array.
  42. *
  43. * @return array
  44. */
  45. public function getOauthOptions() {
  46. return $this->_defaultOptions;
  47. }
  48. /**
  49. * Fetches a request token.
  50. *
  51. * @param string $scope The API scope or scopes separated by spaces to
  52. * restrict data access to.
  53. * @param mixed $callback The URL to redirect the user to after they have
  54. * granted access on the approval page. Either a string or
  55. * Zend_Gdata_Query object.
  56. * @return Zend_OAuth_Token_Request|null
  57. */
  58. public function fetchRequestToken($scope, $callback) {
  59. if ($callback instanceof Zend_Gdata_Query) {
  60. $uri = $callback->getQueryUrl();
  61. } else {
  62. $uri = $callback;
  63. }
  64. $this->_defaultOptions['callbackUrl'] = $uri;
  65. $this->_config->setCallbackUrl($uri);
  66. if (!isset($_SESSION['ACCESS_TOKEN'])) {
  67. return parent::getRequestToken(array('scope' => $scope));
  68. }
  69. return null;
  70. }
  71. /**
  72. * Redirects the user to the approval page
  73. *
  74. * @param string $domain (optional) The Google Apps domain to logged users in
  75. * under or 'default' for Google Accounts. Leaving this parameter off
  76. * will give users the universal login to choose an account to login
  77. * under.
  78. * @return void
  79. */
  80. public function authorizeRequestToken($domain=null) {
  81. $params = array();
  82. if ($domain != null) {
  83. $params = array('hd' => $domain);
  84. }
  85. $this->redirect($params);
  86. }
  87. /**
  88. * Upgrades an authorized request token to an access token.
  89. *
  90. * @return Zend_OAuth_Token_Access||null
  91. */
  92. public function fetchAccessToken() {
  93. if (!isset($_SESSION['ACCESS_TOKEN'])) {
  94. if (!empty($_GET) && isset($_SESSION['REQUEST_TOKEN'])) {
  95. return parent::getAccessToken(
  96. $_GET, unserialize($_SESSION['REQUEST_TOKEN']));
  97. }
  98. }
  99. return null;
  100. }
  101. }