PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/GData/ClientLogin.php

http://github.com/zendframework/zf2
PHP | 180 lines | 89 code | 18 blank | 73 comment | 17 complexity | 54d5de6e2ae7ead559a6a4c9ac12cb58 MD5 | raw file
Possible License(s): BSD-3-Clause
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\GData;
  25. /**
  26. * Class to facilitate Google's "Account Authentication
  27. * for Installed Applications" also known as "ClientLogin".
  28. * @see http://code.google.com/apis/accounts/AuthForInstalledApps.html
  29. *
  30. * @uses \Zend\GData\App\AuthException
  31. * @uses \Zend\GData\App\CaptchaRequiredException
  32. * @uses \Zend\GData\App\HttpException
  33. * @uses \Zend\GData\HttpClient
  34. * @uses \Zend\Version
  35. * @category Zend
  36. * @package Zend_Gdata
  37. * @subpackage Gdata
  38. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class ClientLogin
  42. {
  43. /**
  44. * The Google client login URI
  45. *
  46. */
  47. const CLIENTLOGIN_URI = 'https://www.google.com/accounts/ClientLogin';
  48. /**
  49. * The default 'source' parameter to send to Google
  50. *
  51. */
  52. const DEFAULT_SOURCE = 'Zend-ZendFramework';
  53. /**
  54. * Set Google authentication credentials.
  55. * Must be done before trying to do any Google Data operations that
  56. * require authentication.
  57. * For example, viewing private data, or posting or deleting entries.
  58. *
  59. * @param string $email
  60. * @param string $password
  61. * @param string $service
  62. * @param \Zend\GData\HttpClient $client
  63. * @param string $source
  64. * @param string $loginToken The token identifier as provided by the server.
  65. * @param string $loginCaptcha The user's response to the CAPTCHA challenge.
  66. * @param string $accountType An optional string to identify whether the
  67. * account to be authenticated is a google or a hosted account. Defaults to
  68. * 'HOSTED_OR_GOOGLE'. See: http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#Request
  69. * @throws \Zend\GData\App\AuthException
  70. * @throws \Zend\GData\App\HttpException
  71. * @throws \Zend\GData\App\CaptchaRequiredException
  72. * @return \Zend\GData\HttpClient
  73. */
  74. public static function getHttpClient($email, $password, $service = 'xapi',
  75. $client = null,
  76. $source = self::DEFAULT_SOURCE,
  77. $loginToken = null,
  78. $loginCaptcha = null,
  79. $loginUri = self::CLIENTLOGIN_URI,
  80. $accountType = 'HOSTED_OR_GOOGLE')
  81. {
  82. if (! ($email && $password)) {
  83. throw new App\AuthException(
  84. 'Please set your Google credentials before trying to ' .
  85. 'authenticate');
  86. }
  87. if ($client == null) {
  88. $client = new HttpClient();
  89. }
  90. if (!$client instanceof \Zend\Http\Client) {
  91. throw new App\HttpException(
  92. 'Client is not an instance of Zend\Http\Client.');
  93. }
  94. // Build the HTTP client for authentication
  95. $client->setUri($loginUri);
  96. $client->setMethod('POST');
  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->setEncType('multipart/form-data');
  105. $postParams = array('accountType' => $accountType,
  106. 'Email' => (string)$email,
  107. 'Passwd' => (string) $password,
  108. 'service' => (string) $service,
  109. 'source' => (string) $source);
  110. if ($loginToken || $loginCaptcha) {
  111. if($loginToken && $loginCaptcha) {
  112. $postParams += array('logintoken' => (string)$loginToken,
  113. 'logincaptcha' => (string)$loginCaptcha);
  114. } else {
  115. throw new App\AuthException(
  116. 'Please provide both a token ID and a user\'s response ' .
  117. 'to the CAPTCHA challenge.');
  118. }
  119. }
  120. $client->setParameterPost($postParams);
  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->send();
  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 = rtrim($l);
  135. if ($l) {
  136. list($key, $val) = explode('=', rtrim($l), 2);
  137. $goog_resp[$key] = $val;
  138. }
  139. }
  140. if ($response->getStatusCode() == 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->getStatusCode() == 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. }