Zend_OpenId-Consumer.xml 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.openid.consumer">
  4. <title>Zend_OpenId_Consumer Basics</title>
  5. <para>
  6. <classname>Zend_OpenId_Consumer</classname> can be used to implement OpenID
  7. authentication for web sites.
  8. </para>
  9. <sect2 id="zend.openid.consumer.authentication">
  10. <title>OpenID Authentication</title>
  11. <para>
  12. From a web site developer's point of view, the OpenID authentication
  13. process consists of three steps:
  14. </para>
  15. <orderedlist>
  16. <listitem>
  17. <para>
  18. Show OpenID authentication form
  19. </para>
  20. </listitem>
  21. <listitem>
  22. <para>
  23. Accept OpenID identity and pass it to the OpenID provider
  24. </para>
  25. </listitem>
  26. <listitem>
  27. <para>
  28. Verify response from the OpenID provider
  29. </para>
  30. </listitem>
  31. </orderedlist>
  32. <para>
  33. The OpenID authentication protocol actually requires more
  34. steps, but many of them are encapsulated inside
  35. <classname>Zend_OpenId_Consumer</classname> and are therefore transparent to the
  36. developer.
  37. </para>
  38. <para>
  39. The end user initiates the OpenID authentication process by
  40. submitting his or her identification credentials with the appropriate form.
  41. The following example shows a simple form that accepts an OpenID
  42. identifier. Note that the example only demonstrates a login.
  43. </para>
  44. <example id="zend.openid.consumer.example-1">
  45. <title>The Simple OpenID Login form</title>
  46. <programlisting language="php"><![CDATA[
  47. <html><body>
  48. <form method="post" action="example-1_2.php"><fieldset>
  49. <legend>OpenID Login</legend>
  50. <input type="text" name="openid_identifier">
  51. <input type="submit" name="openid_action" value="login">
  52. </fieldset></form></body></html>
  53. ]]></programlisting>
  54. </example>
  55. <para>
  56. This form passes the OpenID identity on submission to the following PHP
  57. script that performs the second step of authentication. The
  58. PHP script need only call the <classname>Zend_OpenId_Consumer::login()</classname> method in this step.
  59. The first argument of this
  60. method is an accepted OpenID identity, and the second is the URL of a script
  61. that handles the third and last step of authentication.
  62. </para>
  63. <example id="zend.openid.consumer.example-1_2">
  64. <title>The Authentication Request Handler</title>
  65. <programlisting language="php"><![CDATA[
  66. $consumer = new Zend_OpenId_Consumer();
  67. if (!$consumer->login($_POST['openid_identifier'], 'example-1_3.php')) {
  68. die("OpenID login failed.");
  69. }
  70. ]]></programlisting>
  71. </example>
  72. <para>
  73. The <classname>Zend_OpenId_Consumer::login()</classname> method performs discovery on
  74. a given identifier, and, if successful, obtains the address of the identity
  75. provider and its local identifier. It then creates an association to the
  76. given provider so that both the site and provider share a secret
  77. that is used to sign the subsequent messages. Finally, it passes an
  78. authentication request to the provider. This request redirects the
  79. end user's web browser to an OpenID server site, where the user can
  80. continue the authentication process.
  81. </para>
  82. <para>
  83. An OpenID provider usually asks users for their password (if they
  84. weren't previously logged-in), whether the user trusts this site and what
  85. information may be returned to the site. These interactions are not
  86. visible to the OpenID consumer, so it can not obtain the
  87. user's password or other information that the user did not has not directed the
  88. OpenID provider to share with it.
  89. </para>
  90. <para>
  91. On success, <classname>Zend_OpenId_Consumer::login()</classname> does not
  92. return, instead performing an HTTP redirection. However, if there is an error
  93. it may return false. Errors may occur due to an invalid identity, unresponsive
  94. provider, communication error, etc.
  95. </para>
  96. <para>
  97. The third step of authentication is initiated by the response from the
  98. OpenID provider, after it has authenticated the user's password.
  99. This response is passed indirectly, as an HTTP redirection using the
  100. end user's web browser. The consumer must now simply check
  101. that this response is valid.
  102. </para>
  103. <example id="zend.openid.consumer.example-1_3">
  104. <title>The Authentication Response Verifier</title>
  105. <programlisting language="php"><![CDATA[
  106. $consumer = new Zend_OpenId_Consumer();
  107. if ($consumer->verify($_GET, $id)) {
  108. echo "VALID " . htmlspecialchars($id);
  109. } else {
  110. echo "INVALID " . htmlspecialchars($id);
  111. }
  112. ]]></programlisting>
  113. </example>
  114. <para>
  115. This check is performed using the <classname>Zend_OpenId_Consumer::verify</classname>
  116. method, which takes an array of
  117. the HTTP request's arguments and checks that this response is properly
  118. signed by the OpenID provider. It may assign
  119. the claimed OpenID identity that was entered by end user in the
  120. first step using a second, optional argument.
  121. </para>
  122. </sect2>
  123. <sect2 id="zend.openid.consumer.combine">
  124. <title>Combining all Steps in One Page</title>
  125. <para>
  126. The following example combines all three steps in one script. It doesn't
  127. provide any new functionality. The advantage of using just one script is that
  128. the developer need not specify URL's for a script to handle the next
  129. step. By default, all steps use the same URL. However, the script now
  130. includes some dispatch code to execute the appropriate code for each step of
  131. authentication.
  132. </para>
  133. <example id="zend.openid.consumer.example-2">
  134. <title>The Complete OpenID Login Script</title>
  135. <programlisting language="php"><![CDATA[
  136. <?php
  137. $status = "";
  138. if (isset($_POST['openid_action']) &&
  139. $_POST['openid_action'] == "login" &&
  140. !empty($_POST['openid_identifier'])) {
  141. $consumer = new Zend_OpenId_Consumer();
  142. if (!$consumer->login($_POST['openid_identifier'])) {
  143. $status = "OpenID login failed.";
  144. }
  145. } else if (isset($_GET['openid_mode'])) {
  146. if ($_GET['openid_mode'] == "id_res") {
  147. $consumer = new Zend_OpenId_Consumer();
  148. if ($consumer->verify($_GET, $id)) {
  149. $status = "VALID " . htmlspecialchars($id);
  150. } else {
  151. $status = "INVALID " . htmlspecialchars($id);
  152. }
  153. } else if ($_GET['openid_mode'] == "cancel") {
  154. $status = "CANCELLED";
  155. }
  156. }
  157. ?>
  158. <html><body>
  159. <?php echo "$status<br>" ?>
  160. <form method="post">
  161. <fieldset>
  162. <legend>OpenID Login</legend>
  163. <input type="text" name="openid_identifier" value=""/>
  164. <input type="submit" name="openid_action" value="login"/>
  165. </fieldset>
  166. </form>
  167. </body></html>
  168. ]]></programlisting>
  169. </example>
  170. <para>
  171. In addition, this code differentiates between cancelled and invalid
  172. authentication responses. The provider returns a cancelled response
  173. if the identity provider is not aware of the supplied identity, the user
  174. is not logged in, or the user doesn't trust the site. An invalid response indicates
  175. that the response is not conformant to the OpenID protocol or is incorrectly signed.
  176. </para>
  177. </sect2>
  178. <sect2 id="zend.openid.consumer.realm">
  179. <title>Consumer Realm</title>
  180. <para>
  181. When an OpenID-enabled site passes authentication requests to a
  182. provider, it identifies itself with a realm URL. This URL may be
  183. considered a root of a trusted site. If the user trusts the realm URL, he or she
  184. should also trust matched and subsequent URLs.
  185. </para>
  186. <para>
  187. By default, the realm URL is automatically set to the URL of the
  188. directory in which the login script resides. This default value is useful for most, but
  189. not all, cases. Sometimes an entire domain, and not a directory should be trusted. Or even a
  190. combination of several servers in one domain.
  191. </para>
  192. <para>
  193. To override the default value, developers may pass the realm URL as a
  194. third argument to the <classname>Zend_OpenId_Consumer::login</classname> method. In
  195. the following example, a single interaction asks for trusted access to
  196. all php.net sites.
  197. </para>
  198. <example id="zend.openid.consumer.example-3_2">
  199. <title>Authentication Request for Specified Realm</title>
  200. <programlisting language="php"><![CDATA[
  201. $consumer = new Zend_OpenId_Consumer();
  202. if (!$consumer->login($_POST['openid_identifier'],
  203. 'example-3_3.php',
  204. 'http://*.php.net/')) {
  205. die("OpenID login failed.");
  206. }
  207. ]]></programlisting>
  208. </example>
  209. <para>
  210. This example implements only the second step of authentication;
  211. the first and third steps are similar to the examples above.
  212. </para>
  213. </sect2>
  214. <sect2 id="zend.openid.consumer.check">
  215. <title>Immediate Check</title>
  216. <para>
  217. In some cases, an application need only check if a user is already
  218. logged in to a trusted OpenID server without any interaction with the
  219. user. The <classname>Zend_OpenId_Consumer::check</classname> method does precisely
  220. that. It is executed with the same arguments as
  221. <classname>Zend_OpenId_Consumer::login</classname>, but it doesn't display any
  222. OpenID server pages to the user. From the users point of view this process is
  223. transparent, and it appears as though they never left the site. The third step
  224. succeeds if the user is already logged in and trusted by the site, otherwise
  225. it will fail.
  226. </para>
  227. <example id="zend.openid.consumer.example-4">
  228. <title>Immediate Check without Interaction</title>
  229. <programlisting language="php"><![CDATA[
  230. $consumer = new Zend_OpenId_Consumer();
  231. if (!$consumer->check($_POST['openid_identifier'], 'example-4_3.php')) {
  232. die("OpenID login failed.");
  233. }
  234. ]]></programlisting>
  235. </example>
  236. <para>
  237. This example implements only the second step of authentication;
  238. the first and third steps are similar to the examples above.
  239. </para>
  240. </sect2>
  241. <sect2 id="zend.openid.consumer.storage">
  242. <title>Zend_OpenId_Consumer_Storage</title>
  243. <para>
  244. There are three steps in the OpenID authentication procedure, and each
  245. step is performed by a separate HTTP request. To store information between
  246. requests, <classname>Zend_OpenId_Consumer</classname> uses internal storage.
  247. </para>
  248. <para>
  249. Developers do not necessarily have to be aware of this storage because by default
  250. <classname>Zend_OpenId_Consumer</classname> uses file-based storage under the temporary directory-
  251. similar to PHP sessions. However, this storage may be not suitable in all
  252. cases. Some developers may want to store information in a database, while others may
  253. need to use common storage suitable for server farms. Fortunately,
  254. developers may easily replace the default storage with their own. To specify a custom storage mechanism,
  255. one need only extend the <classname>Zend_OpenId_Consumer_Storage</classname> class and pass this subclass
  256. to the <classname>Zend_OpenId_Consumer</classname> constructor in the first argument.
  257. </para>
  258. <para>
  259. The following example demonstrates a simple storage mechanism that uses
  260. <classname>Zend_Db</classname> as its backend and exposes three groups of functions.
  261. The first group contains functions for working with associations, while the second group caches
  262. discovery information, and the third group can be used to check whether a response is unique. This
  263. class can easily be used with existing or new databases; if the required tables don't exist, it will create them.
  264. </para>
  265. <example id="zend.openid.consumer.example-5">
  266. <title>Database Storage</title>
  267. <programlisting language="php"><![CDATA[
  268. class DbStorage extends Zend_OpenId_Consumer_Storage
  269. {
  270. private $_db;
  271. private $_association_table;
  272. private $_discovery_table;
  273. private $_nonce_table;
  274. // Pass in the Zend_Db_Adapter object and the names of the
  275. // required tables
  276. public function __construct($db,
  277. $association_table = "association",
  278. $discovery_table = "discovery",
  279. $nonce_table = "nonce")
  280. {
  281. $this->_db = $db;
  282. $this->_association_table = $association_table;
  283. $this->_discovery_table = $discovery_table;
  284. $this->_nonce_table = $nonce_table;
  285. $tables = $this->_db->listTables();
  286. // If the associations table doesn't exist, create it
  287. if (!in_array($association_table, $tables)) {
  288. $this->_db->getConnection()->exec(
  289. "create table $association_table (" .
  290. " url varchar(256) not null primary key," .
  291. " handle varchar(256) not null," .
  292. " macFunc char(16) not null," .
  293. " secret varchar(256) not null," .
  294. " expires timestamp" .
  295. ")");
  296. }
  297. // If the discovery table doesn't exist, create it
  298. if (!in_array($discovery_table, $tables)) {
  299. $this->_db->getConnection()->exec(
  300. "create table $discovery_table (" .
  301. " id varchar(256) not null primary key," .
  302. " realId varchar(256) not null," .
  303. " server varchar(256) not null," .
  304. " version float," .
  305. " expires timestamp" .
  306. ")");
  307. }
  308. // If the nonce table doesn't exist, create it
  309. if (!in_array($nonce_table, $tables)) {
  310. $this->_db->getConnection()->exec(
  311. "create table $nonce_table (" .
  312. " nonce varchar(256) not null primary key," .
  313. " created timestamp default current_timestamp" .
  314. ")");
  315. }
  316. }
  317. public function addAssociation($url,
  318. $handle,
  319. $macFunc,
  320. $secret,
  321. $expires)
  322. {
  323. $table = $this->_association_table;
  324. $secret = base64_encode($secret);
  325. $this->_db
  326. ->query('insert into ' .
  327. $table (url, handle, macFunc, secret, expires) " .
  328. "values ('$url', '$handle', '$macFunc', " .
  329. "'$secret', $expires)");
  330. return true;
  331. }
  332. public function getAssociation($url,
  333. &$handle,
  334. &$macFunc,
  335. &$secret,
  336. &$expires)
  337. {
  338. $table = $this->_association_table;
  339. $this->_db->query("delete from $table where expires < " . time());
  340. $res = $this->_db->fetchRow('select handle, macFunc, secret, expires ' .
  341. "from $table where url = '$url'");
  342. if (is_array($res)) {
  343. $handle = $res['handle'];
  344. $macFunc = $res['macFunc'];
  345. $secret = base64_decode($res['secret']);
  346. $expires = $res['expires'];
  347. return true;
  348. }
  349. return false;
  350. }
  351. public function getAssociationByHandle($handle,
  352. &$url,
  353. &$macFunc,
  354. &$secret,
  355. &$expires)
  356. {
  357. $table = $this->_association_table;
  358. $this->_db->query("delete from $table where expires < " . time());
  359. $res = $this->_db
  360. ->fetchRow('select url, macFunc, secret, expires ' .
  361. "from $table where handle = '$handle'");
  362. if (is_array($res)) {
  363. $url = $res['url'];
  364. $macFunc = $res['macFunc'];
  365. $secret = base64_decode($res['secret']);
  366. $expires = $res['expires'];
  367. return true;
  368. }
  369. return false;
  370. }
  371. public function delAssociation($url)
  372. {
  373. $table = $this->_association_table;
  374. $this->_db->query("delete from $table where url = '$url'");
  375. return true;
  376. }
  377. public function addDiscoveryInfo($id,
  378. $realId,
  379. $server,
  380. $version,
  381. $expires)
  382. {
  383. $table = $this->_discovery_table;
  384. $this->_db
  385. ->query("insert into $table " .
  386. "(id, realId, server, version, expires) " .
  387. "values " .
  388. "('$id', '$realId', '$server', $version, $expires)");
  389. return true;
  390. }
  391. public function getDiscoveryInfo($id,
  392. &$realId,
  393. &$server,
  394. &$version,
  395. &$expires)
  396. {
  397. $table = $this->_discovery_table;
  398. $this->_db->query("delete from $table where expires < " . time());
  399. $res = $this->_db
  400. ->fetchRow('select realId, server, version, expires ' .
  401. "from $table where id = '$id'");
  402. if (is_array($res)) {
  403. $realId = $res['realId'];
  404. $server = $res['server'];
  405. $version = $res['version'];
  406. $expires = $res['expires'];
  407. return true;
  408. }
  409. return false;
  410. }
  411. public function delDiscoveryInfo($id)
  412. {
  413. $table = $this->_discovery_table;
  414. $this->_db->query("delete from $table where id = '$id'");
  415. return true;
  416. }
  417. public function isUniqueNonce($nonce)
  418. {
  419. $table = $this->_nonce_table;
  420. try {
  421. $ret = $this->_db
  422. ->query("insert into $table (nonce) values ('$nonce')");
  423. } catch (Zend_Db_Statement_Exception $e) {
  424. return false;
  425. }
  426. return true;
  427. }
  428. public function purgeNonces($date=null)
  429. {
  430. }
  431. }
  432. $db = Zend_Db::factory('Pdo_Sqlite',
  433. array('dbname'=>'/tmp/openid_consumer.db'));
  434. $storage = new DbStorage($db);
  435. $consumer = new Zend_OpenId_Consumer($storage);
  436. ]]></programlisting>
  437. </example>
  438. <para>
  439. This example doesn't list the OpenID authentication code itself, but this
  440. code would be the same as that for other examples in this chapter.
  441. examples.
  442. </para>
  443. </sect2>
  444. <sect2 id="zend.openid.consumer.sreg">
  445. <title>Simple Registration Extension</title>
  446. <para>
  447. In addition to authentication, the OpenID standard can be used for
  448. lightweight profile exchange to make information about a user portable across multiple sites. This feature is not covered by the OpenID
  449. authentication specification, but by the OpenID Simple Registration
  450. Extension protocol. This protocol allows OpenID-enabled sites to ask for
  451. information about end users from OpenID providers. Such information may
  452. include:
  453. </para>
  454. <itemizedlist>
  455. <listitem>
  456. <para>
  457. <emphasis>nickname</emphasis>
  458. - any UTF-8 string that the end user uses as a nickname
  459. </para>
  460. </listitem>
  461. <listitem>
  462. <para>
  463. <emphasis>email</emphasis>
  464. - the email address of the user as specified in section 3.4.1
  465. of RFC2822
  466. </para>
  467. </listitem>
  468. <listitem>
  469. <para>
  470. <emphasis>fullname</emphasis>
  471. - a UTF-8 string representation of the user's full name
  472. </para>
  473. </listitem>
  474. <listitem>
  475. <para>
  476. <emphasis>dob</emphasis>
  477. - the user's date of birth in the format 'YYYY-MM-DD'. Any values whose
  478. representation uses fewer than the specified number of digits in this format
  479. should be zero-padded. In other words, the length of this value must always be
  480. 10. If the end user does not want to reveal any particular
  481. part of this value (i.e., year, month or day), it must be set to zero. For example,
  482. if the user wants to specify that his date of birth falls in 1980,
  483. but not specify the month or day, the value returned should be '1980-00-00'.
  484. </para>
  485. </listitem>
  486. <listitem>
  487. <para>
  488. <emphasis>gender</emphasis>
  489. - the user's gender: "M" for male, "F" for female
  490. </para>
  491. </listitem>
  492. <listitem>
  493. <para>
  494. <emphasis>postcode</emphasis>
  495. - a UTF-8 string that conforms to the postal system of the user's country
  496. </para>
  497. </listitem>
  498. <listitem>
  499. <para>
  500. <emphasis>country</emphasis>
  501. - the user's country of residence as specified by ISO3166
  502. </para>
  503. </listitem>
  504. <listitem>
  505. <para>
  506. <emphasis>language</emphasis>
  507. - the user's preferred language as specified by ISO639
  508. </para>
  509. </listitem>
  510. <listitem>
  511. <para>
  512. <emphasis>timezone</emphasis>
  513. - an ASCII string from a TimeZone database. For example,
  514. "Europe/Paris" or "America/Los_Angeles".
  515. </para>
  516. </listitem>
  517. </itemizedlist>
  518. <para>
  519. An OpenID-enabled web site may ask for any combination of these
  520. fields. It may also strictly require some information and allow users
  521. to provide or hide additional information. The following example instantiates
  522. the <classname>Zend_OpenId_Extension_Sreg</classname> class, requiring
  523. a <emphasis>nickname</emphasis> and optionally requests
  524. an <emphasis>email</emphasis> and a <emphasis>fullname</emphasis>.
  525. </para>
  526. <example id="zend.openid.consumer.example-6_2">
  527. <title>Sending Requests with a Simple Registration Extension</title>
  528. <programlisting language="php"><![CDATA[
  529. $sreg = new Zend_OpenId_Extension_Sreg(array(
  530. 'nickname'=>true,
  531. 'email'=>false,
  532. 'fullname'=>false), null, 1.1);
  533. $consumer = new Zend_OpenId_Consumer();
  534. if (!$consumer->login($_POST['openid_identifier'],
  535. 'example-6_3.php',
  536. null,
  537. $sreg)) {
  538. die("OpenID login failed.");
  539. }
  540. ]]></programlisting>
  541. </example>
  542. <para>
  543. As you can see, the <classname>Zend_OpenId_Extension_Sreg</classname>
  544. constructor accepts an array of OpenID fields. This array has the names of
  545. fields as indexes to a flag indicating whether the field is required;
  546. <emphasis>true</emphasis> means the field is required and
  547. <emphasis>false</emphasis> means the field is optional. The
  548. <classname>Zend_OpenId_Consumer::login</classname> method accepts an extension or an array of
  549. extensions as its fourth argument.
  550. </para>
  551. <para>
  552. On the third step of authentication, the
  553. <classname>Zend_OpenId_Extension_Sreg</classname> object should be passed to
  554. <classname>Zend_OpenId_Consumer::verify</classname>. Then on successful authentication
  555. the <classname>Zend_OpenId_Extension_Sreg::getProperties</classname> method will return an
  556. associative array of requested fields.
  557. </para>
  558. <example id="zend.openid.consumer.example-6_3">
  559. <title>Verifying Responses with a Simple Registration Extension</title>
  560. <programlisting language="php"><![CDATA[
  561. $sreg = new Zend_OpenId_Extension_Sreg(array(
  562. 'nickname'=>true,
  563. 'email'=>false,
  564. 'fullname'=>false), null, 1.1);
  565. $consumer = new Zend_OpenId_Consumer();
  566. if ($consumer->verify($_GET, $id, $sreg)) {
  567. echo "VALID " . htmlspecialchars($id) ."<br>\n";
  568. $data = $sreg->getProperties();
  569. if (isset($data['nickname'])) {
  570. echo "nickname: " . htmlspecialchars($data['nickname']) . "<br>\n";
  571. }
  572. if (isset($data['email'])) {
  573. echo "email: " . htmlspecialchars($data['email']) . "<br>\n";
  574. }
  575. if (isset($data['fullname'])) {
  576. echo "fullname: " . htmlspecialchars($data['fullname']) . "<br>\n";
  577. }
  578. } else {
  579. echo "INVALID " . htmlspecialchars($id);
  580. }
  581. ]]></programlisting>
  582. </example>
  583. <para>
  584. If the <classname>Zend_OpenId_Extension_Sreg</classname> object was created without any
  585. arguments, the user code should check for the existence of the required
  586. data itself. However, if the object is created with the same list of
  587. required fields as on the second step, it will automatically check for the
  588. existence of required data. In this case, <classname>Zend_OpenId_Consumer::verify</classname>
  589. will return <emphasis>false</emphasis> if any of the required fields are
  590. missing.
  591. </para>
  592. <para>
  593. <classname>Zend_OpenId_Extension_Sreg</classname> uses version
  594. 1.0 by default, because the specification for version 1.1 is not yet finalized.
  595. However, some libraries don't fully support version 1.0. For example,
  596. www.myopenid.com requires an SREG namespace in requests which is only
  597. available in 1.1. To work with such a server, you must explicitly set the version to
  598. 1.1 in the <classname>Zend_OpenId_Extension_Sreg</classname> constructor.
  599. </para>
  600. <para>
  601. The second argument of the <classname>Zend_OpenId_Extension_Sreg</classname>
  602. constructor is a policy URL, that should be provided to the user by
  603. the identity provider.
  604. </para>
  605. </sect2>
  606. <sect2 id="zend.openid.consumer.auth">
  607. <title>Integration with Zend_Auth</title>
  608. <para>
  609. Zend Framework provides a special class to support user
  610. authentication: <classname>Zend_Auth</classname>. This class can be used together
  611. with <classname>Zend_OpenId_Consumer</classname>. The following example shows how
  612. <code>OpenIdAdapter</code> implements
  613. the <classname>Zend_Auth_Adapter_Interface</classname> with the
  614. <code>authenticate</code> method. This performs an authentication query and
  615. verification.
  616. </para>
  617. <para>
  618. The big difference between this adapter and existing ones, is that
  619. it works on two HTTP requests and includes a dispatch code to perform the
  620. second or third step of OpenID authentication.
  621. </para>
  622. <example id="zend.openid.consumer.example-7">
  623. <title>Zend_Auth Adapter for OpenID</title>
  624. <programlisting language="php"><![CDATA[
  625. <?php
  626. class OpenIdAdapter implements Zend_Auth_Adapter_Interface {
  627. private $_id = null;
  628. public function __construct($id = null) {
  629. $this->_id = $id;
  630. }
  631. public function authenticate() {
  632. $id = $this->_id;
  633. if (!empty($id)) {
  634. $consumer = new Zend_OpenId_Consumer();
  635. if (!$consumer->login($id)) {
  636. $ret = false;
  637. $msg = "Authentication failed.";
  638. }
  639. } else {
  640. $consumer = new Zend_OpenId_Consumer();
  641. if ($consumer->verify($_GET, $id)) {
  642. $ret = true;
  643. $msg = "Authentication successful";
  644. } else {
  645. $ret = false;
  646. $msg = "Authentication failed";
  647. }
  648. }
  649. return new Zend_Auth_Result($ret, $id, array($msg));
  650. }
  651. }
  652. $status = "";
  653. $auth = Zend_Auth::getInstance();
  654. if ((isset($_POST['openid_action']) &&
  655. $_POST['openid_action'] == "login" &&
  656. !empty($_POST['openid_identifier'])) ||
  657. isset($_GET['openid_mode'])) {
  658. $adapter = new OpenIdAdapter(@$_POST['openid_identifier']);
  659. $result = $auth->authenticate($adapter);
  660. if ($result->isValid()) {
  661. Zend_OpenId::redirect(Zend_OpenId::selfURL());
  662. } else {
  663. $auth->clearIdentity();
  664. foreach ($result->getMessages() as $message) {
  665. $status .= "$message<br>\n";
  666. }
  667. }
  668. } else if ($auth->hasIdentity()) {
  669. if (isset($_POST['openid_action']) &&
  670. $_POST['openid_action'] == "logout") {
  671. $auth->clearIdentity();
  672. } else {
  673. $status = "You are logged in as " . $auth->getIdentity() . "<br>\n";
  674. }
  675. }
  676. ?>
  677. <html><body>
  678. <?php echo htmlspecialchars($status);?>
  679. <form method="post"><fieldset>
  680. <legend>OpenID Login</legend>
  681. <input type="text" name="openid_identifier" value="">
  682. <input type="submit" name="openid_action" value="login">
  683. <input type="submit" name="openid_action" value="logout">
  684. </fieldset></form></body></html>
  685. ]]></programlisting>
  686. </example>
  687. <para>
  688. With <classname>Zend_Auth</classname> the end-user's identity is saved in the
  689. session's data. It may be checked with <classname>Zend_Auth::hasIdentity</classname>
  690. and <classname>Zend_Auth::getIdentity</classname>.
  691. </para>
  692. </sect2>
  693. <sect2 id="zend.openid.consumer.mvc">
  694. <title>Integration with Zend_Controller</title>
  695. <para>
  696. Finally a couple of words about integration into
  697. Model-View-Controller applications: such Zend Framework applications are
  698. implemented using the <classname>Zend_Controller</classname> class and they use
  699. objects of the <classname>Zend_Controller_Response_Http</classname> class to prepare
  700. HTTP responses and send them back to the user's web browser.
  701. </para>
  702. <para>
  703. <classname>Zend_OpenId_Consumer</classname> doesn't provide any GUI
  704. capabilities but it performs HTTP redirections on success of
  705. <classname>Zend_OpenId_Consumer::login</classname> and
  706. <classname>Zend_OpenId_Consumer::check</classname>. These redirections may work
  707. incorrectly or not at all if some data was already sent to the
  708. web browser. To properly perform HTTP redirection in MVC code the real
  709. <classname>Zend_Controller_Response_Http</classname> should be sent to
  710. <classname>Zend_OpenId_Consumer::login</classname> or
  711. <classname>Zend_OpenId_Consumer::check</classname> as the last argument.
  712. </para>
  713. </sect2>
  714. </sect1>
  715. <!--
  716. vim:se ts=4 sw=4 et:
  717. -->