Zend_OpenId-Consumer.xml 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 <acronym>PHP</acronym>
  57. script that performs the second step of authentication. The
  58. <acronym>PHP</acronym> script need only call the <methodname>Zend_OpenId_Consumer::login()</methodname> method in this step.
  59. The first argument of this
  60. method is an accepted OpenID identity, and the second is the <acronym>URL</acronym> 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 <methodname>Zend_OpenId_Consumer::login()</methodname> 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, <methodname>Zend_OpenId_Consumer::login()</methodname> does not
  92. return, instead performing an <acronym>HTTP</acronym> redirection. However, if there is
  93. an error it may return <constant>FALSE</constant>. Errors may occur due to an invalid
  94. identity, unresponsive 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 <acronym>HTTP</acronym> 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 <acronym>HTTP</acronym> 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 <acronym>URL</acronym>'s for a script to handle the next
  129. step. By default, all steps use the same <acronym>URL</acronym>. 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 <acronym>URL</acronym>. This <acronym>URL</acronym> may be
  183. considered a root of a trusted site. If the user trusts the realm <acronym>URL</acronym>, he or she
  184. should also trust matched and subsequent <acronym>URL</acronym>s.
  185. </para>
  186. <para>
  187. By default, the realm <acronym>URL</acronym> is automatically set to the <acronym>URL</acronym> 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 <acronym>URL</acronym> 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 <acronym>HTTP</acronym> 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 <acronym>PHP</acronym> 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->insert($table, array(
  326. 'url' => $url,
  327. 'handle' => $handle,
  328. 'macFunc' => $macFunc,
  329. 'secret' => $secret,
  330. 'expires' => $expires,
  331. ));
  332. return true;
  333. }
  334. public function getAssociation($url,
  335. &$handle,
  336. &$macFunc,
  337. &$secret,
  338. &$expires)
  339. {
  340. $table = $this->_association_table;
  341. $this->_db->delete(
  342. $table, $this->_db->quoteInto('expires < ?', time())
  343. );
  344. $select = $this-_db->select()
  345. ->from($table, array('handle', 'macFunc', 'secret', 'expires'))
  346. ->where('url = ?', $url);
  347. $res = $this->_db->fetchRow($select);
  348. if (is_array($res)) {
  349. $handle = $res['handle'];
  350. $macFunc = $res['macFunc'];
  351. $secret = base64_decode($res['secret']);
  352. $expires = $res['expires'];
  353. return true;
  354. }
  355. return false;
  356. }
  357. public function getAssociationByHandle($handle,
  358. &$url,
  359. &$macFunc,
  360. &$secret,
  361. &$expires)
  362. {
  363. $table = $this->_association_table;
  364. $this->_db->delete(
  365. $table, $this->_db->quoteInto('expires < ', time())
  366. );
  367. $select = $this->_db->select()
  368. ->from($table, array('url', 'macFunc', 'secret', 'expires')
  369. ->where('handle = ?', $handle);
  370. $res = $select->fetchRow($select);
  371. if (is_array($res)) {
  372. $url = $res['url'];
  373. $macFunc = $res['macFunc'];
  374. $secret = base64_decode($res['secret']);
  375. $expires = $res['expires'];
  376. return true;
  377. }
  378. return false;
  379. }
  380. public function delAssociation($url)
  381. {
  382. $table = $this->_association_table;
  383. $this->_db->query("delete from $table where url = '$url'");
  384. return true;
  385. }
  386. public function addDiscoveryInfo($id,
  387. $realId,
  388. $server,
  389. $version,
  390. $expires)
  391. {
  392. $table = $this->_discovery_table;
  393. $this->_db->insert($table, array(
  394. 'id' => $id,
  395. 'realId' => $realId,
  396. 'server' => $server,
  397. 'version' => $version,
  398. 'expires' => $expires,
  399. ));
  400. return true;
  401. }
  402. public function getDiscoveryInfo($id,
  403. &$realId,
  404. &$server,
  405. &$version,
  406. &$expires)
  407. {
  408. $table = $this->_discovery_table;
  409. $this->_db->delete($table, $this->quoteInto('expires < ?', time()));
  410. $select = $this->_db->select()
  411. ->from($table, array('realId', 'server', 'version', 'expires'))
  412. ->where('id = ?', $id);
  413. $res = $this->_db->fetchRow($select);
  414. if (is_array($res)) {
  415. $realId = $res['realId'];
  416. $server = $res['server'];
  417. $version = $res['version'];
  418. $expires = $res['expires'];
  419. return true;
  420. }
  421. return false;
  422. }
  423. public function delDiscoveryInfo($id)
  424. {
  425. $table = $this->_discovery_table;
  426. $this->_db->delete($table, $this->_db->quoteInto('id = ?', $id));
  427. return true;
  428. }
  429. public function isUniqueNonce($nonce)
  430. {
  431. $table = $this->_nonce_table;
  432. try {
  433. $ret = $this->_db->insert($table, array(
  434. 'nonce' => $nonce,
  435. ));
  436. } catch (Zend_Db_Statement_Exception $e) {
  437. return false;
  438. }
  439. return true;
  440. }
  441. public function purgeNonces($date=null)
  442. {
  443. }
  444. }
  445. $db = Zend_Db::factory('Pdo_Sqlite',
  446. array('dbname'=>'/tmp/openid_consumer.db'));
  447. $storage = new DbStorage($db);
  448. $consumer = new Zend_OpenId_Consumer($storage);
  449. ]]></programlisting>
  450. </example>
  451. <para>
  452. This example doesn't list the OpenID authentication code itself, but this
  453. code would be the same as that for other examples in this chapter.
  454. examples.
  455. </para>
  456. </sect2>
  457. <sect2 id="zend.openid.consumer.sreg">
  458. <title>Simple Registration Extension</title>
  459. <para>
  460. In addition to authentication, the OpenID standard can be used for
  461. lightweight profile exchange to make information about a user portable across multiple sites. This feature is not covered by the OpenID
  462. authentication specification, but by the OpenID Simple Registration
  463. Extension protocol. This protocol allows OpenID-enabled sites to ask for
  464. information about end users from OpenID providers. Such information may
  465. include:
  466. </para>
  467. <itemizedlist>
  468. <listitem>
  469. <para>
  470. <emphasis>nickname</emphasis>
  471. - any UTF-8 string that the end user uses as a nickname
  472. </para>
  473. </listitem>
  474. <listitem>
  475. <para>
  476. <emphasis>email</emphasis>
  477. - the email address of the user as specified in section 3.4.1
  478. of RFC2822
  479. </para>
  480. </listitem>
  481. <listitem>
  482. <para>
  483. <emphasis>fullname</emphasis>
  484. - a UTF-8 string representation of the user's full name
  485. </para>
  486. </listitem>
  487. <listitem>
  488. <para>
  489. <emphasis>dob</emphasis>
  490. - the user's date of birth in the format 'YYYY-MM-DD'. Any values whose
  491. representation uses fewer than the specified number of digits in this format
  492. should be zero-padded. In other words, the length of this value must always be
  493. 10. If the end user does not want to reveal any particular
  494. part of this value (i.e., year, month or day), it must be set to zero. For example,
  495. if the user wants to specify that his date of birth falls in 1980,
  496. but not specify the month or day, the value returned should be '1980-00-00'.
  497. </para>
  498. </listitem>
  499. <listitem>
  500. <para>
  501. <emphasis>gender</emphasis>
  502. - the user's gender: "M" for male, "F" for female
  503. </para>
  504. </listitem>
  505. <listitem>
  506. <para>
  507. <emphasis>postcode</emphasis>
  508. - a UTF-8 string that conforms to the postal system of the user's country
  509. </para>
  510. </listitem>
  511. <listitem>
  512. <para>
  513. <emphasis>country</emphasis>
  514. - the user's country of residence as specified by ISO3166
  515. </para>
  516. </listitem>
  517. <listitem>
  518. <para>
  519. <emphasis>language</emphasis>
  520. - the user's preferred language as specified by ISO639
  521. </para>
  522. </listitem>
  523. <listitem>
  524. <para>
  525. <emphasis>timezone</emphasis>
  526. - an <acronym>ASCII</acronym> string from a TimeZone database. For example,
  527. "Europe/Paris" or "America/Los_Angeles".
  528. </para>
  529. </listitem>
  530. </itemizedlist>
  531. <para>
  532. An OpenID-enabled web site may ask for any combination of these
  533. fields. It may also strictly require some information and allow users
  534. to provide or hide additional information. The following example instantiates
  535. the <classname>Zend_OpenId_Extension_Sreg</classname> class, requiring
  536. a <emphasis>nickname</emphasis> and optionally requests
  537. an <emphasis>email</emphasis> and a <emphasis>fullname</emphasis>.
  538. </para>
  539. <example id="zend.openid.consumer.example-6_2">
  540. <title>Sending Requests with a Simple Registration Extension</title>
  541. <programlisting language="php"><![CDATA[
  542. $sreg = new Zend_OpenId_Extension_Sreg(array(
  543. 'nickname'=>true,
  544. 'email'=>false,
  545. 'fullname'=>false), null, 1.1);
  546. $consumer = new Zend_OpenId_Consumer();
  547. if (!$consumer->login($_POST['openid_identifier'],
  548. 'example-6_3.php',
  549. null,
  550. $sreg)) {
  551. die("OpenID login failed.");
  552. }
  553. ]]></programlisting>
  554. </example>
  555. <para>
  556. As you can see, the <classname>Zend_OpenId_Extension_Sreg</classname>
  557. constructor accepts an array of OpenID fields. This array has the names of
  558. fields as indexes to a flag indicating whether the field is required;
  559. <constant>TRUE</constant> means the field is required and
  560. <constant>FALSE</constant> means the field is optional. The
  561. <classname>Zend_OpenId_Consumer::login</classname> method accepts an extension or an
  562. array of extensions as its fourth argument.
  563. </para>
  564. <para>
  565. On the third step of authentication, the
  566. <classname>Zend_OpenId_Extension_Sreg</classname> object should be passed to
  567. <classname>Zend_OpenId_Consumer::verify</classname>. Then on successful authentication
  568. the <classname>Zend_OpenId_Extension_Sreg::getProperties</classname> method will return an
  569. associative array of requested fields.
  570. </para>
  571. <example id="zend.openid.consumer.example-6_3">
  572. <title>Verifying Responses with a Simple Registration Extension</title>
  573. <programlisting language="php"><![CDATA[
  574. $sreg = new Zend_OpenId_Extension_Sreg(array(
  575. 'nickname'=>true,
  576. 'email'=>false,
  577. 'fullname'=>false), null, 1.1);
  578. $consumer = new Zend_OpenId_Consumer();
  579. if ($consumer->verify($_GET, $id, $sreg)) {
  580. echo "VALID " . htmlspecialchars($id) ."<br>\n";
  581. $data = $sreg->getProperties();
  582. if (isset($data['nickname'])) {
  583. echo "nickname: " . htmlspecialchars($data['nickname']) . "<br>\n";
  584. }
  585. if (isset($data['email'])) {
  586. echo "email: " . htmlspecialchars($data['email']) . "<br>\n";
  587. }
  588. if (isset($data['fullname'])) {
  589. echo "fullname: " . htmlspecialchars($data['fullname']) . "<br>\n";
  590. }
  591. } else {
  592. echo "INVALID " . htmlspecialchars($id);
  593. }
  594. ]]></programlisting>
  595. </example>
  596. <para>
  597. If the <classname>Zend_OpenId_Extension_Sreg</classname> object was created without any
  598. arguments, the user code should check for the existence of the required
  599. data itself. However, if the object is created with the same list of
  600. required fields as on the second step, it will automatically check for the
  601. existence of required data. In this case, <classname>Zend_OpenId_Consumer::verify</classname>
  602. will return <constant>FALSE</constant> if any of the required fields are
  603. missing.
  604. </para>
  605. <para>
  606. <classname>Zend_OpenId_Extension_Sreg</classname> uses version
  607. 1.0 by default, because the specification for version 1.1 is not yet finalized.
  608. However, some libraries don't fully support version 1.0. For example,
  609. www.myopenid.com requires an SREG namespace in requests which is only
  610. available in 1.1. To work with such a server, you must explicitly set the version to
  611. 1.1 in the <classname>Zend_OpenId_Extension_Sreg</classname> constructor.
  612. </para>
  613. <para>
  614. The second argument of the <classname>Zend_OpenId_Extension_Sreg</classname>
  615. constructor is a policy <acronym>URL</acronym>, that should be provided to the user by
  616. the identity provider.
  617. </para>
  618. </sect2>
  619. <sect2 id="zend.openid.consumer.auth">
  620. <title>Integration with Zend_Auth</title>
  621. <para>
  622. Zend Framework provides a special class to support user
  623. authentication: <classname>Zend_Auth</classname>. This class can be used together
  624. with <classname>Zend_OpenId_Consumer</classname>. The following example shows how
  625. <code>OpenIdAdapter</code> implements
  626. the <classname>Zend_Auth_Adapter_Interface</classname> with the
  627. <code>authenticate</code> method. This performs an authentication query and
  628. verification.
  629. </para>
  630. <para>
  631. The big difference between this adapter and existing ones, is that
  632. it works on two <acronym>HTTP</acronym> requests and includes a dispatch code to perform the
  633. second or third step of OpenID authentication.
  634. </para>
  635. <example id="zend.openid.consumer.example-7">
  636. <title>Zend_Auth Adapter for OpenID</title>
  637. <programlisting language="php"><![CDATA[
  638. <?php
  639. class OpenIdAdapter implements Zend_Auth_Adapter_Interface {
  640. private $_id = null;
  641. public function __construct($id = null) {
  642. $this->_id = $id;
  643. }
  644. public function authenticate() {
  645. $id = $this->_id;
  646. if (!empty($id)) {
  647. $consumer = new Zend_OpenId_Consumer();
  648. if (!$consumer->login($id)) {
  649. $ret = false;
  650. $msg = "Authentication failed.";
  651. }
  652. } else {
  653. $consumer = new Zend_OpenId_Consumer();
  654. if ($consumer->verify($_GET, $id)) {
  655. $ret = true;
  656. $msg = "Authentication successful";
  657. } else {
  658. $ret = false;
  659. $msg = "Authentication failed";
  660. }
  661. }
  662. return new Zend_Auth_Result($ret, $id, array($msg));
  663. }
  664. }
  665. $status = "";
  666. $auth = Zend_Auth::getInstance();
  667. if ((isset($_POST['openid_action']) &&
  668. $_POST['openid_action'] == "login" &&
  669. !empty($_POST['openid_identifier'])) ||
  670. isset($_GET['openid_mode'])) {
  671. $adapter = new OpenIdAdapter(@$_POST['openid_identifier']);
  672. $result = $auth->authenticate($adapter);
  673. if ($result->isValid()) {
  674. Zend_OpenId::redirect(Zend_OpenId::selfURL());
  675. } else {
  676. $auth->clearIdentity();
  677. foreach ($result->getMessages() as $message) {
  678. $status .= "$message<br>\n";
  679. }
  680. }
  681. } else if ($auth->hasIdentity()) {
  682. if (isset($_POST['openid_action']) &&
  683. $_POST['openid_action'] == "logout") {
  684. $auth->clearIdentity();
  685. } else {
  686. $status = "You are logged in as " . $auth->getIdentity() . "<br>\n";
  687. }
  688. }
  689. ?>
  690. <html><body>
  691. <?php echo htmlspecialchars($status);?>
  692. <form method="post"><fieldset>
  693. <legend>OpenID Login</legend>
  694. <input type="text" name="openid_identifier" value="">
  695. <input type="submit" name="openid_action" value="login">
  696. <input type="submit" name="openid_action" value="logout">
  697. </fieldset></form></body></html>
  698. ]]></programlisting>
  699. </example>
  700. <para>
  701. With <classname>Zend_Auth</classname> the end-user's identity is saved in the
  702. session's data. It may be checked with <classname>Zend_Auth::hasIdentity</classname>
  703. and <classname>Zend_Auth::getIdentity</classname>.
  704. </para>
  705. </sect2>
  706. <sect2 id="zend.openid.consumer.mvc">
  707. <title>Integration with Zend_Controller</title>
  708. <para>
  709. Finally a couple of words about integration into
  710. Model-View-Controller applications: such Zend Framework applications are
  711. implemented using the <classname>Zend_Controller</classname> class and they use
  712. objects of the <classname>Zend_Controller_Response_Http</classname> class to prepare
  713. <acronym>HTTP</acronym> responses and send them back to the user's web browser.
  714. </para>
  715. <para>
  716. <classname>Zend_OpenId_Consumer</classname> doesn't provide any GUI
  717. capabilities but it performs <acronym>HTTP</acronym> redirections on success of
  718. <classname>Zend_OpenId_Consumer::login</classname> and
  719. <classname>Zend_OpenId_Consumer::check</classname>. These redirections may work
  720. incorrectly or not at all if some data was already sent to the
  721. web browser. To properly perform <acronym>HTTP</acronym> redirection in <acronym>MVC</acronym> code the real
  722. <classname>Zend_Controller_Response_Http</classname> should be sent to
  723. <classname>Zend_OpenId_Consumer::login</classname> or
  724. <classname>Zend_OpenId_Consumer::check</classname> as the last argument.
  725. </para>
  726. </sect2>
  727. </sect1>
  728. <!--
  729. vim:se ts=4 sw=4 et:
  730. -->