PageRenderTime 733ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/GData/ClientLogin.php

https://github.com/Exercise/zf2
PHP | 175 lines | 88 code | 13 blank | 74 comment | 17 complexity | c35b148520b5b33fc2a925f884b6d7ab MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage Gdata
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @namespace
  24. */
  25. namespace Zend\GData;
  26. /**
  27. * Class to facilitate Google's "Account Authentication
  28. * for Installed Applications" also known as "ClientLogin".
  29. * @see http://code.google.com/apis/accounts/AuthForInstalledApps.html
  30. *
  31. * @uses \Zend\GData\App\AuthException
  32. * @uses \Zend\GData\App\CaptchaRequiredException
  33. * @uses \Zend\GData\App\HttpException
  34. * @uses \Zend\GData\HttpClient
  35. * @uses \Zend\Version
  36. * @category Zend
  37. * @package Zend_Gdata
  38. * @subpackage Gdata
  39. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class ClientLogin
  43. {
  44. /**
  45. * The Google client login URI
  46. *
  47. */
  48. const CLIENTLOGIN_URI = 'https://www.google.com/accounts/ClientLogin';
  49. /**
  50. * The default 'source' parameter to send to Google
  51. *
  52. */
  53. const DEFAULT_SOURCE = 'Zend-ZendFramework';
  54. /**
  55. * Set Google authentication credentials.
  56. * Must be done before trying to do any Google Data operations that
  57. * require authentication.
  58. * For example, viewing private data, or posting or deleting entries.
  59. *
  60. * @param string $email
  61. * @param string $password
  62. * @param string $service
  63. * @param \Zend\GData\HttpClient $client
  64. * @param string $source
  65. * @param string $loginToken The token identifier as provided by the server.
  66. * @param string $loginCaptcha The user's response to the CAPTCHA challenge.
  67. * @param string $accountType An optional string to identify whether the
  68. * account to be authenticated is a google or a hosted account. Defaults to
  69. * 'HOSTED_OR_GOOGLE'. See: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#Request
  70. * @throws \Zend\GData\App\AuthException
  71. * @throws \Zend\GData\App\HttpException
  72. * @throws \Zend\GData\App\CaptchaRequiredException
  73. * @return \Zend\GData\HttpClient
  74. */
  75. public static function getHttpClient($email, $password, $service = 'xapi',
  76. $client = null,
  77. $source = self::DEFAULT_SOURCE,
  78. $loginToken = null,
  79. $loginCaptcha = null,
  80. $loginUri = self::CLIENTLOGIN_URI,
  81. $accountType = 'HOSTED_OR_GOOGLE')
  82. {
  83. if (! ($email && $password)) {
  84. throw new App\AuthException(
  85. 'Please set your Google credentials before trying to ' .
  86. 'authenticate');
  87. }
  88. if ($client == null) {
  89. $client = new HttpClient();
  90. }
  91. if (!$client instanceof \Zend\Http\Client) {
  92. throw new App\HttpException(
  93. 'Client is not an instance of Zend\Http\Client.');
  94. }
  95. // Build the HTTP client for authentication
  96. $client->setUri($loginUri);
  97. $useragent = $source . ' Zend_Framework_Gdata/' . \Zend\Version::VERSION;
  98. $client->setConfig(array(
  99. 'maxredirects' => 0,
  100. 'strictredirects' => true,
  101. 'useragent' => $useragent
  102. )
  103. );
  104. $client->setParameterPost('accountType', $accountType);
  105. $client->setParameterPost('Email', (string) $email);
  106. $client->setParameterPost('Passwd', (string) $password);
  107. $client->setParameterPost('service', (string) $service);
  108. $client->setParameterPost('source', (string) $source);
  109. if ($loginToken || $loginCaptcha) {
  110. if($loginToken && $loginCaptcha) {
  111. $client->setParameterPost('logintoken', (string) $loginToken);
  112. $client->setParameterPost('logincaptcha',
  113. (string) $loginCaptcha);
  114. }
  115. else {
  116. throw new App\AuthException(
  117. 'Please provide both a token ID and a user\'s response ' .
  118. 'to the CAPTCHA challenge.');
  119. }
  120. }
  121. // Send the authentication request
  122. // For some reason Google's server causes an SSL error. We use the
  123. // output buffer to supress an error from being shown. Ugly - but works!
  124. ob_start();
  125. try {
  126. $response = $client->request('POST');
  127. } catch (\Zend\Http\Client\Exception $e) {
  128. throw new App\HttpException($e->getMessage(), $e);
  129. }
  130. ob_end_clean();
  131. // Parse Google's response
  132. $goog_resp = array();
  133. foreach (explode("\n", $response->getBody()) as $l) {
  134. $l = chop($l);
  135. if ($l) {
  136. list($key, $val) = explode('=', chop($l), 2);
  137. $goog_resp[$key] = $val;
  138. }
  139. }
  140. if ($response->getStatus() == 200) {
  141. $client->setClientLoginToken($goog_resp['Auth']);
  142. $useragent = $source . ' Zend_Framework_Gdata/' . \Zend\Version::VERSION;
  143. $client->setConfig(array(
  144. 'strictredirects' => true,
  145. 'useragent' => $useragent
  146. )
  147. );
  148. return $client;
  149. } elseif ($response->getStatus() == 403) {
  150. // Check if the server asked for a CAPTCHA
  151. if (array_key_exists('Error', $goog_resp) &&
  152. $goog_resp['Error'] == 'CaptchaRequired') {
  153. throw new App\CaptchaRequiredException(
  154. $goog_resp['CaptchaToken'], $goog_resp['CaptchaUrl']);
  155. }
  156. else {
  157. throw new App\AuthException('Authentication with Google failed. Reason: ' .
  158. (isset($goog_resp['Error']) ? $goog_resp['Error'] : 'Unspecified.'));
  159. }
  160. }
  161. }
  162. }