Zend_Gdata_AuthSub.xml 6.3 KB

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