Zend_Auth_Adapter_OpenId.xml 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.auth.adapter.openid">
  4. <title>Open ID Authentication</title>
  5. <sect2 id="zend.auth.adapter.openid.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. The <classname>Zend_Auth_Adapter_OpenId</classname> adapter can be used to authenticate
  9. users using remote OpenID servers. This authentication method assumes that the user
  10. submits only their OpenID identity to the web application. They are
  11. then redirected to their OpenID provider to prove identity ownership
  12. using a password or some other method. This password is never provided to
  13. the web application.
  14. </para>
  15. <para>
  16. The OpenID identity is just a <acronym>URI</acronym> that points to a web site
  17. with information about a user, along with special tags that
  18. describes which server to use and which identity to submit there.
  19. You can read more about OpenID at the
  20. <ulink url="http://www.openid.net/">OpenID official site</ulink>.
  21. </para>
  22. <para>
  23. The <classname>Zend_Auth_Adapter_OpenId</classname> class wraps
  24. the <classname>Zend_OpenId_Consumer</classname> component, which implements the
  25. OpenID authentication protocol itself.
  26. </para>
  27. <note>
  28. <para>
  29. <classname>Zend_OpenId</classname> takes advantage of the <ulink
  30. url="http://php.net/gmp">GMP extension</ulink>, where available. Consider
  31. enabling the <acronym>GMP</acronym> extension for better performance when using
  32. <classname>Zend_Auth_Adapter_OpenId</classname>.
  33. </para>
  34. </note>
  35. </sect2>
  36. <sect2 id="zend.auth.adapter.openid.specifics">
  37. <title>Specifics</title>
  38. <para>
  39. As is the case for all <classname>Zend_Auth</classname> adapters, the
  40. <classname>Zend_Auth_Adapter_OpenId</classname> class implements
  41. <classname>Zend_Auth_Adapter_Interface</classname>, which defines one method:
  42. <methodname>authenticate()</methodname>. This method performs the authentication itself,
  43. but the object must be prepared prior to calling it. Such adapter preparation includes
  44. setting up the OpenID identity and some other <classname>Zend_OpenId</classname>
  45. specific options.
  46. </para>
  47. <para>
  48. However, as opposed to other <classname>Zend_Auth</classname> adapters,
  49. <classname>Zend_Auth_Adapter_OpenId</classname> performs authentication on an external
  50. server in two separate <acronym>HTTP</acronym> requests. So the
  51. <methodname>Zend_Auth_Adapter_OpenId::authenticate()</methodname> method must be called
  52. twice. On the first invocation the method won't return, but will redirect the user to
  53. their OpenID server. Then after the user is authenticated on the remote server, they
  54. will be redirected back and the script for this second request must call
  55. <methodname>Zend_Auth_Adapter_OpenId::authenticate()</methodname> again to verify the
  56. signature which comes with the redirected request from the server to complete the
  57. authentication process. On this second invocation, the method will return the
  58. <classname>Zend_Auth_Result</classname> object as expected.
  59. </para>
  60. <para>
  61. The following example shows the usage of
  62. <classname>Zend_Auth_Adapter_OpenId</classname>. As previously mentioned, the
  63. <methodname>Zend_Auth_Adapter_OpenId::authenticate()</methodname> must be called two
  64. times. The first time is after the user submits the <acronym>HTML</acronym> form with
  65. the <varname>$_POST['openid_action']</varname> set to <emphasis>"login"</emphasis>,
  66. and the second time is after the <acronym>HTTP</acronym> redirection from OpenID server
  67. with <varname>$_GET['openid_mode']</varname> or
  68. <varname>$_POST['openid_mode']</varname> set.
  69. </para>
  70. <programlisting language="php"><![CDATA[
  71. <?php
  72. $status = "";
  73. $auth = Zend_Auth::getInstance();
  74. if ((isset($_POST['openid_action']) &&
  75. $_POST['openid_action'] == "login" &&
  76. !empty($_POST['openid_identifier'])) ||
  77. isset($_GET['openid_mode']) ||
  78. isset($_POST['openid_mode'])) {
  79. $result = $auth->authenticate(
  80. new Zend_Auth_Adapter_OpenId(@$_POST['openid_identifier']));
  81. if ($result->isValid()) {
  82. $status = "You are logged in as "
  83. . $auth->getIdentity()
  84. . "<br>\n";
  85. } else {
  86. $auth->clearIdentity();
  87. foreach ($result->getMessages() as $message) {
  88. $status .= "$message<br>\n";
  89. }
  90. }
  91. } else if ($auth->hasIdentity()) {
  92. if (isset($_POST['openid_action']) &&
  93. $_POST['openid_action'] == "logout") {
  94. $auth->clearIdentity();
  95. } else {
  96. $status = "You are logged in as "
  97. . $auth->getIdentity()
  98. . "<br>\n";
  99. }
  100. }
  101. ?>
  102. <html><body>
  103. <?php echo htmlspecialchars($status);?>
  104. <form method="post"><fieldset>
  105. <legend>OpenID Login</legend>
  106. <input type="text" name="openid_identifier" value="">
  107. <input type="submit" name="openid_action" value="login">
  108. <input type="submit" name="openid_action" value="logout">
  109. </fieldset></form></body></html>
  110. */
  111. ]]></programlisting>
  112. <para>
  113. You may customize the OpenID authentication process in several way.
  114. You can, for example, receive the redirect from the OpenID server on a separate page,
  115. specifying the "root" of web site and using a custom
  116. <classname>Zend_OpenId_Consumer_Storage</classname> or a custom
  117. <classname>Zend_Controller_Response</classname>. You may also use
  118. the Simple Registration Extension to retrieve information about
  119. user from the OpenID server. All of these possibilities are described
  120. in more detail in the <classname>Zend_OpenId_Consumer</classname>
  121. chapter.
  122. </para>
  123. </sect2>
  124. </sect1>
  125. <!--
  126. vim:se ts=4 sw=4 et:
  127. -->