Zend_Gdata_AuthSub.xml 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.gdata.authsub">
  4. <title>Authenticating with AuthSub</title>
  5. <para>
  6. The AuthSub mechanism enables you to write web applications
  7. that acquire authenticated access Google Data services,
  8. without having to write code that handles user credentials.
  9. </para>
  10. <para>
  11. See <ulink url="http://code.google.com/apis/accounts/AuthForWebApps.html">http://code.google.com/apis/accounts/AuthForWebApps.html</ulink>
  12. for more information about Google Data AuthSub authentication.
  13. </para>
  14. <para>
  15. The Google documentation says the ClientLogin mechanism is appropriate
  16. for "installed applications" whereas the AuthSub mechanism is
  17. for "web applications." The difference is that AuthSub requires
  18. interaction from the user, and a browser interface that can react
  19. to redirection requests. The ClientLogin solution uses PHP code to
  20. supply the account credentials; the user is not required to enter her
  21. credentials interactively.
  22. </para>
  23. <para>
  24. The account credentials supplied via the AuthSub mechanism are
  25. entered by the user of the web application. Therefore they must be
  26. account credentials that are known to that user.
  27. </para>
  28. <note>
  29. <title>Registered applications</title>
  30. <para>
  31. <classname>Zend_Gdata</classname> currently does not support use of secure tokens, because
  32. the AuthSub authentication does not support passing a digital certificate
  33. to acquire a secure token.
  34. </para>
  35. </note>
  36. <sect2 id="zend.gdata.authsub.login">
  37. <title>Creating an AuthSub authenticated Http Client</title>
  38. <para>
  39. Your PHP application should provide a hyperlink to the
  40. Google URL that performs authentication. The static function
  41. <classname>Zend_Gdata_AuthSub::getAuthSubTokenUri()</classname>
  42. provides the correct URL. The arguments to this function include
  43. the URL to your PHP application so that Google can redirect the
  44. user's browser back to your application after the user's
  45. credentials have been verified.
  46. </para>
  47. <para>
  48. After Google's authentication server redirects the user's browser
  49. back to the current application, a GET request parameter is set,
  50. called <code>token</code>.
  51. The value of this parameter is a single-use token that can be
  52. used for authenticated access.
  53. This token can be converted into a multi-use token and stored
  54. in your session.
  55. </para>
  56. <para>
  57. Then use the token value in a call to
  58. <classname>Zend_Gdata_AuthSub::getHttpClient()</classname>.
  59. This function returns an instance of <classname>Zend_Http_Client</classname>,
  60. with appropriate headers set so that subsequent requests your
  61. application submits using that Http Client are also authenticated.
  62. </para>
  63. <para>
  64. Below is an example of PHP code for a web application
  65. to acquire authentication to use the Google Calendar service
  66. and create a <classname>Zend_Gdata</classname> client object using that authenticated
  67. Http Client.
  68. </para>
  69. <programlisting language="php"><![CDATA[
  70. $my_calendar = 'http://www.google.com/calendar/feeds/default/private/full';
  71. if (!isset($_SESSION['cal_token'])) {
  72. if (isset($_GET['token'])) {
  73. // You can convert the single-use token to a session token.
  74. $session_token =
  75. Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
  76. // Store the session token in our session.
  77. $_SESSION['cal_token'] = $session_token;
  78. } else {
  79. // Display link to generate single-use token
  80. $googleUri = Zend_Gdata_AuthSub::getAuthSubTokenUri(
  81. 'http://'. $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'],
  82. $my_calendar, 0, 1);
  83. echo "Click <a href='$googleUri'>here</a> " .
  84. "to authorize this application.";
  85. exit();
  86. }
  87. }
  88. // Create an authenticated HTTP Client to talk to Google.
  89. $client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['cal_token']);
  90. // Create a Gdata object using the authenticated Http Client
  91. $cal = new Zend_Gdata_Calendar($client);
  92. ]]></programlisting>
  93. </sect2>
  94. <sect2 id="zend.gdata.authsub.logout">
  95. <title>Revoking AuthSub authentication</title>
  96. <para>
  97. To terminate the authenticated status of a given token, use the
  98. <classname>Zend_Gdata_AuthSub::AuthSubRevokeToken()</classname>
  99. static function. Otherwise, the token is still valid for
  100. some time.
  101. </para>
  102. <programlisting language="php"><![CDATA[
  103. // Carefully construct this value to avoid application security problems.
  104. $php_self = htmlentities(substr($_SERVER['PHP_SELF'],
  105. 0,
  106. strcspn($_SERVER['PHP_SELF'], "\n\r")),
  107. ENT_QUOTES);
  108. if (isset($_GET['logout'])) {
  109. Zend_Gdata_AuthSub::AuthSubRevokeToken($_SESSION['cal_token']);
  110. unset($_SESSION['cal_token']);
  111. header('Location: ' . $php_self);
  112. exit();
  113. }
  114. ]]></programlisting>
  115. <note>
  116. <title>Security notes</title>
  117. <para>
  118. The treatment of the <code>$php_self</code> variable in the
  119. example above is a general security guideline, it is not
  120. specific to <classname>Zend_Gdata</classname>. You should always filter content you
  121. output to http headers.
  122. </para>
  123. <para>
  124. Regarding revoking authentication tokens, it is recommended to
  125. do this when the user is finished with her Google Data session.
  126. The possibility that someone can intercept the token and use
  127. it for malicious purposes is very small, but nevertheless it is
  128. a good practice to terminate authenticated access to any service.
  129. </para>
  130. </note>
  131. </sect2>
  132. </sect1>