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