Zend_OpenId-Consumer.xml 32 KB

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