2
0

Zend_OpenId-Provider.xml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.openid.provider">
  4. <title>Zend_OpenId_Provider</title>
  5. <para>
  6. <classname>Zend_OpenId_Provider</classname> can be used to implement OpenID
  7. servers. This chapter provides examples that demonstrate how to
  8. build a very basic server. However, for implementation of a production OpenID
  9. server (such as <ulink url="http://www.myopenid.com">www.myopenid.com</ulink>) you
  10. may have to deal with more complex issues.
  11. </para>
  12. <sect2 id="zend.openid.provider.start">
  13. <title>Quick Start</title>
  14. <para>
  15. The following example includes code for creating a user account
  16. using <classname>Zend_OpenId_Provider::register</classname>. The link element with
  17. <code>rel="openid.server"</code> points to our own server script. If you
  18. submit this identity to an OpenID-enabled site, it will perform
  19. authentication on this server.
  20. </para>
  21. <para>
  22. The code before the &lt;html&gt; tag is just a trick that automatically
  23. creates a user account. You won't need such code when using real
  24. identities.
  25. </para>
  26. <example id="zend.openid.provider.example-1">
  27. <title>The Identity</title>
  28. <programlisting language="php"><![CDATA[
  29. <?php
  30. // Set up test identity
  31. define("TEST_SERVER", Zend_OpenId::absoluteURL("example-8.php"));
  32. define("TEST_ID", Zend_OpenId::selfURL());
  33. define("TEST_PASSWORD", "123");
  34. $server = new Zend_OpenId_Provider();
  35. if (!$server->hasUser(TEST_ID)) {
  36. $server->register(TEST_ID, TEST_PASSWORD);
  37. }
  38. ?>
  39. <html><head>
  40. <link rel="openid.server" href="<?php echo TEST_SERVER;?>" />
  41. </head><body>
  42. <?php echo TEST_ID;?>
  43. </body></html>
  44. ]]></programlisting>
  45. </example>
  46. <para>
  47. The following identity server script handles two kinds of requests
  48. from OpenID-enabled sites (for association and authentication). Both of
  49. them are handled by the same method:
  50. <classname>Zend_OpenId_Provider::handle</classname>. The two arguments to the
  51. <classname>Zend_OpenId_Provider</classname> constructor are URLs of login and trust pages, which
  52. ask for input from the end user.
  53. </para>
  54. <para>
  55. On success, the method <classname>Zend_OpenId_Provider::handle</classname>
  56. returns a string that should be passed back to the OpenID-enabled site. On
  57. failure, it returns <constant>FALSE</constant>. This example will return an
  58. HTTP 403 response if <classname>Zend_OpenId_Provider::handle</classname> fails. You will get this response if you open this script with a
  59. web browser, because it sends a non-OpenID conforming request.
  60. </para>
  61. <example id="zend.openid.provider.example-2">
  62. <title>Simple Identity Provider</title>
  63. <programlisting language="php"><![CDATA[
  64. $server = new Zend_OpenId_Provider("example-8-login.php",
  65. "example-8-trust.php");
  66. $ret = $server->handle();
  67. if (is_string($ret)) {
  68. echo $ret;
  69. } else if ($ret !== true) {
  70. header('HTTP/1.0 403 Forbidden');
  71. echo 'Forbidden';
  72. }
  73. ]]></programlisting>
  74. </example>
  75. <note>
  76. <para>
  77. It is a good idea to use a secure connection (HTTPS) for these scripts-
  78. especially for the following interactive scripts- to prevent password
  79. disclosure.
  80. </para>
  81. </note>
  82. <para>
  83. The following script implements a login screen for an identity
  84. server using <classname>Zend_OpenId_Provider</classname> and redirects to this page when a
  85. required user has not yet logged in. On this page, a user will enter his password
  86. to login.
  87. </para>
  88. <para>
  89. You should use the password "123" that was used in the identity script above.
  90. </para>
  91. <para>
  92. On submit, the script calls <classname>Zend_OpenId_Provider::login</classname>
  93. with the accepted user's identity and password, then redirects back
  94. to the main identity provider's script. On success, the
  95. <classname>Zend_OpenId_Provider::login</classname> establishes a session between the
  96. user and the identity provider and stores the information about
  97. the user, who is now logged in. All following requests from the same user won't
  98. require a login procedure- even if they come from another OpenID enabled
  99. web site.
  100. </para>
  101. <note>
  102. <para>
  103. Note that this session is between end-user and identity provider
  104. only. OpenID enabled sites know nothing about it.
  105. </para>
  106. </note>
  107. <example id="zend.openid.provider.example-3">
  108. <title>Simple Login Screen</title>
  109. <programlisting language="php"><![CDATA[
  110. <?php
  111. $server = new Zend_OpenId_Provider();
  112. if ($_SERVER['REQUEST_METHOD'] == 'POST' &&
  113. isset($_POST['openid_action']) &&
  114. $_POST['openid_action'] === 'login' &&
  115. isset($_POST['openid_identifier']) &&
  116. isset($_POST['openid_password'])) {
  117. $server->login($_POST['openid_identifier'],
  118. $_POST['openid_password']);
  119. Zend_OpenId::redirect("example-8.php", $_GET);
  120. }
  121. ?>
  122. <html>
  123. <body>
  124. <form method="post">
  125. <fieldset>
  126. <legend>OpenID Login</legend>
  127. <table border=0>
  128. <tr>
  129. <td>Name:</td>
  130. <td>
  131. <input type="text"
  132. name="openid_identifier"
  133. value="<?php echo htmlspecialchars($_GET['openid_identity']);?>">
  134. </td>
  135. </tr>
  136. <tr>
  137. <td>Password:</td>
  138. <td>
  139. <input type="text"
  140. name="openid_password"
  141. value="">
  142. </td>
  143. </tr>
  144. <tr>
  145. <td>&nbsp;</td>
  146. <td>
  147. <input type="submit"
  148. name="openid_action"
  149. value="login">
  150. </td>
  151. </tr>
  152. </table>
  153. </fieldset>
  154. </form>
  155. </body>
  156. </html>
  157. ]]></programlisting>
  158. </example>
  159. <para>
  160. The fact that the user is now logged in doesn't mean that the
  161. authentication must necessarily succeed. The user may decide not to trust
  162. particular OpenID enabled sites. The following trust screen allows the
  163. end user to make that choice. This choice may either be made only for current
  164. requests or forever. In the second case, information about
  165. trusted/untrusted sites is stored in an internal database, and all
  166. following authentication requests from this site will be handled
  167. automatically without user interaction.
  168. </para>
  169. <example id="zend.openid.provider.example-4">
  170. <title>Simple Trust Screen</title>
  171. <programlisting language="php"><![CDATA[
  172. <?php
  173. $server = new Zend_OpenId_Provider();
  174. if ($_SERVER['REQUEST_METHOD'] == 'POST' &&
  175. isset($_POST['openid_action']) &&
  176. $_POST['openid_action'] === 'trust') {
  177. if (isset($_POST['allow'])) {
  178. if (isset($_POST['forever'])) {
  179. $server->allowSite($server->getSiteRoot($_GET));
  180. }
  181. $server->respondToConsumer($_GET);
  182. } else if (isset($_POST['deny'])) {
  183. if (isset($_POST['forever'])) {
  184. $server->denySite($server->getSiteRoot($_GET));
  185. }
  186. Zend_OpenId::redirect($_GET['openid_return_to'],
  187. array('openid.mode'=>'cancel'));
  188. }
  189. }
  190. ?>
  191. <html>
  192. <body>
  193. <p>A site identifying as
  194. <a href="<?php echo htmlspecialchars($server->getSiteRoot($_GET));?>">
  195. <?php echo htmlspecialchars($server->getSiteRoot($_GET));?>
  196. </a>
  197. has asked us for confirmation that
  198. <a href="<?php echo htmlspecialchars($server->getLoggedInUser());?>">
  199. <?php echo htmlspecialchars($server->getLoggedInUser());?>
  200. </a>
  201. is your identity URL.
  202. </p>
  203. <form method="post">
  204. <input type="checkbox" name="forever">
  205. <label for="forever">forever</label><br>
  206. <input type="hidden" name="openid_action" value="trust">
  207. <input type="submit" name="allow" value="Allow">
  208. <input type="submit" name="deny" value="Deny">
  209. </form>
  210. </body>
  211. </html>
  212. ]]></programlisting>
  213. </example>
  214. <para>
  215. Production OpenID servers usually support the Simple Registration
  216. Extension that allows consumers to request some information about the user from
  217. the provider. In this case, the trust page can be extended to allow
  218. entering requested fields or selecting a specific user profile.
  219. </para>
  220. </sect2>
  221. <sect2 id="zend.openid.provider.all">
  222. <title>Combined Provide Scripts</title>
  223. <para>
  224. It is possible to combine all provider functionality in one script. In
  225. this case login and trust URLs are omitted, and
  226. <classname>Zend_OpenId_Provider</classname> assumes that they point to the same page
  227. with the additional "openid.action" GET argument.
  228. </para>
  229. <note>
  230. <para>
  231. The following example is not complete. It doesn't provide GUI code for
  232. the user, instead performing an automatic login and trust relationship instead.
  233. This is done just to simplify the example; a production server should include some code
  234. from previous examples.
  235. </para>
  236. </note>
  237. <example id="zend.openid.provider.example-5">
  238. <title>Everything Together</title>
  239. <programlisting language="php"><![CDATA[
  240. $server = new Zend_OpenId_Provider();
  241. define("TEST_ID", Zend_OpenId::absoluteURL("example-9-id.php"));
  242. define("TEST_PASSWORD", "123");
  243. if ($_SERVER['REQUEST_METHOD'] == 'GET' &&
  244. isset($_GET['openid_action']) &&
  245. $_GET['openid_action'] === 'login') {
  246. $server->login(TEST_ID, TEST_PASSWORD);
  247. unset($_GET['openid_action']);
  248. Zend_OpenId::redirect(Zend_OpenId::selfUrl(), $_GET);
  249. } else if ($_SERVER['REQUEST_METHOD'] == 'GET' &&
  250. isset($_GET['openid_action']) &&
  251. $_GET['openid_action'] === 'trust') {
  252. unset($_GET['openid_action']);
  253. $server->respondToConsumer($_GET);
  254. } else {
  255. $ret = $server->handle();
  256. if (is_string($ret)) {
  257. echo $ret;
  258. } else if ($ret !== true) {
  259. header('HTTP/1.0 403 Forbidden');
  260. echo 'Forbidden';
  261. }
  262. }
  263. ]]></programlisting>
  264. </example>
  265. <para>
  266. If you compare this example with previous examples split in to
  267. separate pages, you will see only the one
  268. difference besides the dispatch code: <code>unset($_GET['openid_action'])</code>. This
  269. call to <code>unset</code> is necessary to route the next request to main
  270. handler.
  271. </para>
  272. </sect2>
  273. <sect2 id="zend.openid.provider.sreg">
  274. <title>Simple Registration Extension</title>
  275. <para>
  276. Again, the code before the &lt;html&gt; tag is just a trick to demonstrate functionality. It creates a new user
  277. account and associates it with a profile (nickname and password). Such
  278. tricks aren't needed in deployed providers where end users register on OpenID
  279. servers and fill in their profiles. Implementing this GUI is out of scope for
  280. this manual.
  281. </para>
  282. <example id="zend.openid.provider.example-6">
  283. <title>Identity with Profile</title>
  284. <programlisting language="php"><![CDATA[
  285. <?php
  286. define("TEST_SERVER", Zend_OpenId::absoluteURL("example-10.php"));
  287. define("TEST_ID", Zend_OpenId::selfURL());
  288. define("TEST_PASSWORD", "123");
  289. $server = new Zend_OpenId_Provider();
  290. if (!$server->hasUser(TEST_ID)) {
  291. $server->register(TEST_ID, TEST_PASSWORD);
  292. $server->login(TEST_ID, TEST_PASSWORD);
  293. $sreg = new Zend_OpenId_Extension_Sreg(array(
  294. 'nickname' =>'test',
  295. 'email' => 'test@test.com'
  296. ));
  297. $root = Zend_OpenId::absoluteURL(".");
  298. Zend_OpenId::normalizeUrl($root);
  299. $server->allowSite($root, $sreg);
  300. $server->logout();
  301. }
  302. ?>
  303. <html>
  304. <head>
  305. <link rel="openid.server" href="<?php echo TEST_SERVER;?>" />
  306. </head>
  307. <body>
  308. <?php echo TEST_ID;?>
  309. </body>
  310. </html>
  311. ]]></programlisting>
  312. </example>
  313. <para>
  314. You should now pass this identity to the OpenID-enabled web site (use the Simple
  315. Registration Extension example from the previous section), and it should use the
  316. following OpenID server script.
  317. </para>
  318. <para>
  319. This script is a variation of the script in the "Everything Together" example. It uses the
  320. same automatic login mechanism, but doesn't contain any code for a trust
  321. page. The user already trusts the example scripts forever. This trust was
  322. established by calling the <classname>Zend_OpenId_Provider::allowSite()</classname> method in the identity
  323. script. The same method associates the profile with the trusted URL. This
  324. profile will be returned automatically for a request from the trusted
  325. URL.
  326. </para>
  327. <para>
  328. To make Simple Registration Extension work, you must simply
  329. pass an instance of <classname>Zend_OpenId_Extension_Sreg</classname> as the second
  330. argument to the <classname>Zend_OpenId_Provider::handle()</classname> method.
  331. </para>
  332. <example id="zend.openid.provider.example-7">
  333. <title>Provider with SREG</title>
  334. <programlisting language="php"><![CDATA[
  335. $server = new Zend_OpenId_Provider();
  336. $sreg = new Zend_OpenId_Extension_Sreg();
  337. define("TEST_ID", Zend_OpenId::absoluteURL("example-10-id.php"));
  338. define("TEST_PASSWORD", "123");
  339. if ($_SERVER['REQUEST_METHOD'] == 'GET' &&
  340. isset($_GET['openid_action']) &&
  341. $_GET['openid_action'] === 'login') {
  342. $server->login(TEST_ID, TEST_PASSWORD);
  343. unset($_GET['openid_action']);
  344. Zend_OpenId::redirect(Zend_OpenId::selfUrl(), $_GET);
  345. } else if ($_SERVER['REQUEST_METHOD'] == 'GET' &&
  346. isset($_GET['openid_action']) &&
  347. $_GET['openid_action'] === 'trust') {
  348. echo "UNTRUSTED DATA" ;
  349. } else {
  350. $ret = $server->handle(null, $sreg);
  351. if (is_string($ret)) {
  352. echo $ret;
  353. } else if ($ret !== true) {
  354. header('HTTP/1.0 403 Forbidden');
  355. echo 'Forbidden';
  356. }
  357. }
  358. ]]></programlisting>
  359. </example>
  360. </sect2>
  361. <sect2 id="zend.openid.provider.else">
  362. <title>Anything Else?</title>
  363. <para>
  364. Building OpenID providers is much less common than building
  365. OpenID-enabled sites, so this manual doesn't cover all
  366. <classname>Zend_OpenId_Provider</classname> features exhaustively, as was done for
  367. <classname>Zend_OpenId_Consumer</classname>.
  368. </para>
  369. <para>
  370. To summamize, <classname>Zend_OpenId_Provider</classname> contains:
  371. </para>
  372. <itemizedlist>
  373. <listitem>
  374. <para>
  375. A set of methods to build an end-user GUI that allows
  376. users to register and manage their trusted sites and profiles
  377. </para>
  378. </listitem>
  379. <listitem>
  380. <para>
  381. An abstract storage layer to store information about users,
  382. their sites and their profiles. It also stores associations between
  383. the provider and OpenID-enabled sites. This layer is very similar
  384. to that of the <classname>Zend_OpenId_Consumer</classname> class. It also uses
  385. file storage by default, but may used with another backend.
  386. </para>
  387. </listitem>
  388. <listitem>
  389. <para>
  390. An abstract user-association layer that may associate
  391. a user's web browser with a logged-in identity
  392. </para>
  393. </listitem>
  394. </itemizedlist>
  395. <para>
  396. The <classname>Zend_OpenId_Provider</classname> class doesn't attempt to cover all possible
  397. features that can be implemented by OpenID servers, e.g. digital
  398. certificates, but it can be extended easily using
  399. <classname>Zend_OpenId_Extension</classname>s or by standard object-oriented extension.
  400. </para>
  401. </sect2>
  402. </sect1>
  403. <!--
  404. vim:se ts=4 sw=4 et:
  405. -->