Главная / Zend Manual Core


The Zend_InfoCard component implements relying-party support for Information Cards. Information Cards are used for identity management on the internet and authentication of users to web sites. The web sites that the user ultimately authenticates to are called relying-parties.

Detailed information about information cards and their importance to the internet identity metasystem can be found on the » IdentityBlog.

Basic Theory of Usage

Usage of Zend_InfoCard can be done one of two ways: either as part of the larger Zend_Auth component via the Zend_InfoCard authentication adapter or as a stand-alone component. In both cases an information card can be requested from a user by using the following HTML block in your HTML login form:

  1. <form action="http://example.com/server" method="POST">
  2.   <input type='image' src='/images/ic.png' align='center'
  3.         width='120px' style='cursor:pointer' />
  4.   <object type="application/x-informationCard"
  5.           name="xmlToken">
  6.    <param name="tokenType"
  7.          value="urn:oasis:names:tc:SAML:1.0:assertion" />
  8.    <param name="requiredClaims"
  9.          value="http://.../claims/privatepersonalidentifier
  10.          http://.../claims/givenname
  11.          http://.../claims/surname" />
  12.  </object>
  13. </form>

In the example above, the requiredClaims <param> tag is used to identify pieces of information known as claims (i.e. person's first name, last name) which the web site (a.k.a "relying party") needs in order a user to authenticate using an information card. For your reference, the full URI (for instance the givenname claim) is as follows: http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname

When the above HTML is activated by a user (clicks on it), the browser will bring up a card selection program which not only shows them which information cards meet the requirements of the site, but also allows them to select which information card to use if multiple meet the criteria. This information card is transmitted as an XML document to the specified POST URL and is ready to be processed by the Zend_InfoCard component.

Note, Information cards can only be HTTP POSTed to SSL-encrypted URLs. Please consult your web server's documentation on how to set up SSL encryption.

Using as part of Zend_Auth

In order to use the component as part of the Zend_Auth authentication system, you must use the provided Zend_Auth_Adapter_InfoCard to do so (not available in the standalone Zend_InfoCard distribution). An example of its usage is shown below:

  1. <?php
  2. if (isset($_POST['xmlToken'])) {
  4.     $adapter = new Zend_Auth_Adapter_InfoCard($_POST['xmlToken']);
  6.     $adapter->addCertificatePair('/usr/local/Zend/apache2/conf/server.key',
  7.                                  '/usr/local/Zend/apache2/conf/server.crt');
  9.     $auth = Zend_Auth::getInstance();
  11.     $result = $auth->authenticate($adapter);
  13.     switch ($result->getCode()) {
  14.         case Zend_Auth_Result::SUCCESS:
  15.             $claims = $result->getIdentity();
  16.             print "Given Name: {$claims->givenname}<br />";
  17.             print "Surname: {$claims->surname}<br />";
  18.             print "Email Address: {$claims->emailaddress}<br />";
  19.             print "PPI: {$claims->getCardID()}<br />";
  20.             break;
  21.         case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
  22.             print "The Credential you provided did not pass validation";
  23.             break;
  24.         default:
  25.         case Zend_Auth_Result::FAILURE:
  26.             print "There was an error processing your credentials.";
  27.             break;
  28.     }
  30.     if (count($result->getMessages()) > 0) {
  31.         print "<pre>";
  32.         var_dump($result->getMessages());
  33.         print "</pre>";
  34.     }
  36. }
  37. ?>
  38. <hr />
  39. <div id="login" style="font-family: arial; font-size: 2em;">
  40. <p>Simple Login Demo</p>
  41.  <form method="post">
  42.   <input type="submit" value="Login" />
  43.    <object type="application/x-informationCard" name="xmlToken">
  44.     <param name="tokenType"
  45.           value="urn:oasis:names:tc:SAML:1.0:assertion" />
  46.     <param name="requiredClaims"
  47.           value="http://.../claims/givenname
  48.                  http://.../claims/surname
  49.                  http://.../claims/emailaddress
  50.                  http://.../claims/privatepersonalidentifier" />
  51.   </object>
  52.  </form>
  53. </div>

In the example above, we first create an instance of the Zend_Auth_Adapter_InfoCard and pass the XML data posted by the card selector into it. Once an instance has been created you must then provide at least one SSL certificate public/private key pair used by the web server that received the HTTP POST. These files are used to validate the destination of the information posted to the server and are a requirement when using Information Cards.

Once the adapter has been configured, you can then use the standard Zend_Auth facilities to validate the provided information card token and authenticate the user by examining the identity provided by the getIdentity() method.

Using the Zend_InfoCard component standalone

It is also possible to use the Zend_InfoCard component as a standalone component by interacting with the Zend_InfoCard class directly. Using the Zend_InfoCard class is very similar to its use with the Zend_Auth component. An example of its use is shown below:

  1. <?php
  2. if (isset($_POST['xmlToken'])) {
  3.     $infocard = new Zend_InfoCard();
  4.     $infocard->addCertificatePair('/usr/local/Zend/apache2/conf/server.key',
  5.                                   '/usr/local/Zend/apache2/conf/server.crt');
  7.     $claims = $infocard->process($_POST['xmlToken']);
  9.     if($claims->isValid()) {
  10.         print "Given Name: {$claims->givenname}<br />";
  11.         print "Surname: {$claims->surname}<br />";
  12.         print "Email Address: {$claims->emailaddress}<br />";
  13.         print "PPI: {$claims->getCardID()}<br />";
  14.     } else {
  15.         print "Error Validating identity: {$claims->getErrorMsg()}";
  16.     }
  17. }
  18. ?>
  19. <hr />
  20. <div id="login" style="font-family: arial; font-size: 2em;">
  21.  <p>Simple Login Demo</p>
  22.  <form method="post">
  23.   <input type="submit" value="Login" />
  24.    <object type="application/x-informationCard" name="xmlToken">
  25.     <param name="tokenType"
  26.           value="urn:oasis:names:tc:SAML:1.0:assertion" />
  27.     <param name="requiredClaims"
  28.           value="http://.../claims/givenname
  29.                  http://.../claims/surname
  30.                  http://.../claims/emailaddress
  31.                  http://.../claims/privatepersonalidentifier" />
  32.    </object>
  33.  </form>
  34. </div>

In the example above, we use the Zend_InfoCard component independently to validate the token provided by the user. As was the case with the Zend_Auth_Adapter_InfoCard, we create an instance of Zend_InfoCard and then set one or more SSL certificate public/private key pairs used by the web server. Once configured, we can use the process() method to process the information card and return the results.

Working with a Claims object

Regardless of whether the Zend_InfoCard component is used as a standalone component or as part of Zend_Auth via Zend_Auth_Adapter_InfoCard, the ultimate result of the processing of an information card is a Zend_InfoCard_Claims object. This object contains the assertions (a.k.a. claims) made by the submitting user based on the data requested by your web site when the user authenticated. As shown in the examples above, the validity of the information card can be ascertained by calling the Zend_InfoCard_Claims::isValid() method. Claims themselves can either be retrieved by simply accessing the identifier desired (i.e. givenname) as a property of the object or through the getClaim() method.

In most cases you will never need to use the getClaim() method. However, if your requiredClaims mandate that you request claims from multiple different sources/namespaces then you will need to extract them explicitly using this method (simply pass it the full URI of the claim to retrieve its value from within the information card). Generally speaking however, the Zend_InfoCard component will set the default URI for claims to be the one used the most frequently within the information card itself and the simplified property-access method can be used.

As part of the validation process, it is the developer's responsibility to examine the issuing source of the claims contained within the information card and to decide if that source is a trusted source of information. To do so, the getIssuer() method is provided within the Zend_InfoCard_Claims object which returns the URI of the issuer of the information card claims.

Attaching Information Cards to existing accounts

It is possible to add support for information cards to an existing authentication system by storing the private personal identifier (PPI) to a previously traditionally-authenticated account and including at least the http://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier claim as part of the requiredClaims of the request. If this claim is requested then the Zend_InfoCard_Claims object will provide a unique identifier for the specific card that was submitted by calling the getCardID() method.

An example of how to attach an information card to an existing traditional-authentication account is shown below:

  1. // ...
  2. public function submitinfocardAction()
  3. {
  4.     if (!isset($_REQUEST['xmlToken'])) {
  5.         throw new ZBlog_Exception('Expected an encrypted token ' .
  6.                                   'but was not provided');
  7.     }
  9.     $infoCard = new Zend_InfoCard();
  10.     $infoCard->addCertificatePair(SSL_CERTIFICATE_PRIVATE,
  11.                                   SSL_CERTIFICATE_PUB);
  13.     try {
  14.         $claims = $infoCard->process($request['xmlToken']);
  15.     } catch(Zend_InfoCard_Exception $e) {
  16.         // TODO Error processing your request
  17.         throw $e;
  18.     }
  20.     if ($claims->isValid()) {
  21.         $db = ZBlog_Data::getAdapter();
  23.         $ppi = $db->quote($claims->getCardID());
  24.         $fullname = $db->quote("{$claims->givenname} {$claims->surname}");
  26.         $query = "UPDATE blogusers
  27.                      SET ppi = $ppi,
  28.                          real_name = $fullname
  29.                    WHERE username='administrator'";
  31.         try {
  32.             $db->query($query);
  33.         } catch(Exception $e) {
  34.             // TODO Failed to store in DB
  35.         }
  37.         $this->view->render();
  38.         return;
  39.     } else {
  40.         throw new
  41.             ZBlog_Exception("Infomation card failed security checks");
  42.     }
  43. }

Creating Zend_InfoCard Adapters

The Zend_InfoCard component was designed to allow for growth in the information card standard through the use of a modular architecture. At this time, many of these hooks are unused and can be ignored, but there is one class that should be written for any serious information card implementation: the Zend_InfoCard adapter.

The Zend_InfoCard adapter is used as a callback mechanism within the component to perform various tasks, such as storing and retrieving Assertion IDs for information cards when they are processed by the component. While storing the assertion IDs of submitted information cards is not necessary, failing to do so opens up the possibility of the authentication scheme being compromised through a replay attack.

To prevent this, one must implement the Zend_InfoCard_Adapter_Interface and set an instance of this interface prior to calling either the process() (standalone) or authenticate() method as a Zend_Auth adapter. To set this interface, the setAdapter() method should be used. In the example below, we set a Zend_InfoCard adapter and use it in our application:

  1. class myAdapter implements Zend_InfoCard_Adapter_Interface
  2. {
  3.     public function storeAssertion($assertionURI,
  4.                                    $assertionID,
  5.                                    $conditions)
  6.     {
  7.         /* Store the assertion and its conditions by ID and URI */
  8.     }
  10.     public function retrieveAssertion($assertionURI, $assertionID)
  11.     {
  12.         /* Retrieve the assertion by URI and ID */
  13.     }
  15.     public function removeAssertion($assertionURI, $assertionID)
  16.     {
  17.         /* Delete a given assertion by URI/ID */
  18.     }
  19. }
  21. $adapter  = new myAdapter();
  23. $infoCard = new Zend_InfoCard();
  24. $infoCard->addCertificatePair(SSL_PRIVATE, SSL_PUB);
  25. $infoCard->setAdapter($adapter);
  27. $claims = $infoCard->process($_POST['xmlToken']);