Zend_OpenId_Provider - Zend_OpenId


Zend_OpenId_Provider can be used to implement OpenID servers. This chapter provides examples that demonstrate how to build a very basic server. However, for implementation of a production OpenID server (such as » you may have to deal with more complex issues.

Quick start

The following example includes code for creating a user account using Zend_OpenId_Provider::register. The link element with rel="openid.server" points to our own server script. If you submit this identity to an OpenID-enabled site, it will perform authentication on this server.

The code before the <html> tag is just a trick that automatically creates a user account. You won't need such code when using real identities.

Example #1 The Identity

  1. <?php
  2. // Set up test identity
  3. define("TEST_SERVER", Zend_OpenId::absoluteURL("example-8.php"));
  4. define("TEST_ID", Zend_OpenId::selfURL());
  5. define("TEST_PASSWORD", "123");
  6. $server = new Zend_OpenId_Provider();
  7. if (!$server->hasUser(TEST_ID)) {
  8.     $server->register(TEST_ID, TEST_PASSWORD);
  9. }
  10. ?>
  11. <html><head>
  12. <link rel="openid.server" href="<?php echo TEST_SERVER;?>" />
  13. </head><body>
  14. <?php echo TEST_ID;?>
  15. </body></html>

The following identity server script handles two kinds of requests from OpenID-enabled sites (for association and authentication). Both of them are handled by the same method: Zend_OpenId_Provider::handle. The two arguments to the Zend_OpenId_Provider constructor are URLs of login and trust pages, which ask for input from the end user.

On success, the method Zend_OpenId_Provider::handle returns a string that should be passed back to the OpenID-enabled site. On failure, it returns FALSE. This example will return an HTTP 403 response if Zend_OpenId_Provider::handle fails. You will get this response if you open this script with a web browser, because it sends a non-OpenID conforming request.

Example #2 Simple Identity Provider

  1. $server = new Zend_OpenId_Provider("example-8-login.php",
  2.                                    "example-8-trust.php");
  3. $ret = $server->handle();
  4. if (is_string($ret)) {
  5.     echo $ret;
  6. } else if ($ret !== true) {
  7.     header('HTTP/1.0 403 Forbidden');
  8.     echo 'Forbidden';
  9. }

Note: It is a good idea to use a secure connection (HTTPS) for these scripts- especially for the following interactive scripts- to prevent password disclosure.

The following script implements a login screen for an identity server using Zend_OpenId_Provider and redirects to this page when a required user has not yet logged in. On this page, a user will enter his password to login.

You should use the password "123" that was used in the identity script above.

On submit, the script calls Zend_OpenId_Provider::login with the accepted user's identity and password, then redirects back to the main identity provider's script. On success, the Zend_OpenId_Provider::login establishes a session between the user and the identity provider and stores the information about the user, who is now logged in. All following requests from the same user won't require a login procedure- even if they come from another OpenID enabled web site.

Note: Note that this session is between end-user and identity provider only. OpenID enabled sites know nothing about it.

Example #3 Simple Login Screen

  1. <?php
  2. $server = new Zend_OpenId_Provider();
  4. if ($_SERVER['REQUEST_METHOD'] == 'POST' &&
  5.     isset($_POST['openid_action']) &&
  6.     $_POST['openid_action'] === 'login' &&
  7.     isset($_POST['openid_identifier']) &&
  8.     isset($_POST['openid_password'])) {
  9.     $server->login($_POST['openid_identifier'],
  10.                    $_POST['openid_password']);
  11.     Zend_OpenId::redirect("example-8.php", $_GET);
  12. }
  13. ?>
  14. <html>
  15. <body>
  16. <form method="post">
  17. <fieldset>
  18. <legend>OpenID Login</legend>
  19. <table border=0>
  20. <tr>
  21. <td>Name:</td>
  22. <td>
  23. <input type="text"
  24.        name="openid_identifier"
  25.        value="<?php echo htmlspecialchars($_GET['openid_identity']);?>">
  26. </td>
  27. </tr>
  28. <tr>
  29. <td>Password:</td>
  30. <td>
  31. <input type="text"
  32.        name="openid_password"
  33.        value="">
  34. </td>
  35. </tr>
  36. <tr>
  37. <td>&nbsp;</td>
  38. <td>
  39. <input type="submit"
  40.        name="openid_action"
  41.        value="login">
  42. </td>
  43. </tr>
  44. </table>
  45. </fieldset>
  46. </form>
  47. </body>
  48. </html>

The fact that the user is now logged in doesn't mean that the authentication must necessarily succeed. The user may decide not to trust particular OpenID enabled sites. The following trust screen allows the end user to make that choice. This choice may either be made only for current requests or forever. In the second case, information about trusted/untrusted sites is stored in an internal database, and all following authentication requests from this site will be handled automatically without user interaction.

Example #4 Simple Trust Screen

  1. <?php
  2. $server = new Zend_OpenId_Provider();
  4. if ($_SERVER['REQUEST_METHOD'] == 'POST' &&
  5.     isset($_POST['openid_action']) &&
  6.     $_POST['openid_action'] === 'trust') {
  8.     if (isset($_POST['allow'])) {
  9.         if (isset($_POST['forever'])) {
  10.             $server->allowSite($server->getSiteRoot($_GET));
  11.         }
  12.         $server->respondToConsumer($_GET);
  13.     } else if (isset($_POST['deny'])) {
  14.         if (isset($_POST['forever'])) {
  15.             $server->denySite($server->getSiteRoot($_GET));
  16.         }
  17.         Zend_OpenId::redirect($_GET['openid_return_to'],
  18.                               array('openid.mode'=>'cancel'));
  19.     }
  20. }
  21. ?>
  22. <html>
  23. <body>
  24. <p>A site identifying as
  25. <a href="<?php echo htmlspecialchars($server->getSiteRoot($_GET));?>">
  26. <?php echo htmlspecialchars($server->getSiteRoot($_GET));?>
  27. </a>
  28. has asked us for confirmation that
  29. <a href="<?php echo htmlspecialchars($server->getLoggedInUser());?>">
  30. <?php echo htmlspecialchars($server->getLoggedInUser());?>
  31. </a>
  32. is your identity URL.
  33. </p>
  34. <form method="post">
  35. <input type="checkbox" name="forever">
  36. <label for="forever">forever</label><br>
  37. <input type="hidden" name="openid_action" value="trust">
  38. <input type="submit" name="allow" value="Allow">
  39. <input type="submit" name="deny" value="Deny">
  40. </form>
  41. </body>
  42. </html>

Production OpenID servers usually support the Simple Registration Extension that allows consumers to request some information about the user from the provider. In this case, the trust page can be extended to allow entering requested fields or selecting a specific user profile.

Combined Provide Scripts

It is possible to combine all provider functionality in one script. In this case login and trust URLs are omitted, and Zend_OpenId_Provider assumes that they point to the same page with the additional "openid.action" GET argument.

Note: The following example is not complete. It doesn't provide GUI code for the user, instead performing an automatic login and trust relationship instead. This is done just to simplify the example; a production server should include some code from previous examples.

Example #5 Everything Together

  1. $server = new Zend_OpenId_Provider();
  3. define("TEST_ID", Zend_OpenId::absoluteURL("example-9-id.php"));
  4. define("TEST_PASSWORD", "123");
  6. if ($_SERVER['REQUEST_METHOD'] == 'GET' &&
  7.     isset($_GET['openid_action']) &&
  8.     $_GET['openid_action'] === 'login') {
  9.     $server->login(TEST_ID, TEST_PASSWORD);
  10.     unset($_GET['openid_action']);
  11.     Zend_OpenId::redirect(Zend_OpenId::selfUrl(), $_GET);
  12. } else if ($_SERVER['REQUEST_METHOD'] == 'GET' &&
  13.     isset($_GET['openid_action']) &&
  14.     $_GET['openid_action'] === 'trust') {
  15.     unset($_GET['openid_action']);
  16.     $server->respondToConsumer($_GET);
  17. } else {
  18.     $ret = $server->handle();
  19.     if (is_string($ret)) {
  20.         echo $ret;
  21.     } else if ($ret !== true) {
  22.         header('HTTP/1.0 403 Forbidden');
  23.         echo 'Forbidden';
  24.     }
  25. }

If you compare this example with previous examples split in to separate pages, you will see only the one difference besides the dispatch code: unset($_GET['openid_action']). This call to unset() is necessary to route the next request to main handler.

Simple Registration Extension

Again, the code before the <html> tag is just a trick to demonstrate functionality. It creates a new user account and associates it with a profile (nickname and password). Such tricks aren't needed in deployed providers where end users register on OpenID servers and fill in their profiles. Implementing this GUI is out of scope for this manual.

Example #6 Identity with Profile

  1. <?php
  2. define("TEST_SERVER", Zend_OpenId::absoluteURL("example-10.php"));
  3. define("TEST_ID", Zend_OpenId::selfURL());
  4. define("TEST_PASSWORD", "123");
  5. $server = new Zend_OpenId_Provider();
  6. if (!$server->hasUser(TEST_ID)) {
  7.     $server->register(TEST_ID, TEST_PASSWORD);
  8.     $server->login(TEST_ID, TEST_PASSWORD);
  9.     $sreg = new Zend_OpenId_Extension_Sreg(array(
  10.         'nickname' =>'test',
  11.         'email' => ''
  12.     ));
  13.     $root = Zend_OpenId::absoluteURL(".");
  14.     Zend_OpenId::normalizeUrl($root);
  15.     $server->allowSite($root, $sreg);
  16.     $server->logout();
  17. }
  18. ?>
  19. <html>
  20. <head>
  21. <link rel="openid.server" href="<?php echo TEST_SERVER;?>" />
  22. </head>
  23. <body>
  24. <?php echo TEST_ID;?>
  25. </body>
  26. </html>

You should now pass this identity to the OpenID-enabled web site (use the Simple Registration Extension example from the previous section), and it should use the following OpenID server script.

This script is a variation of the script in the "Everything Together" example. It uses the same automatic login mechanism, but doesn't contain any code for a trust page. The user already trusts the example scripts forever. This trust was established by calling the Zend_OpenId_Provider::allowSite() method in the identity script. The same method associates the profile with the trusted URL. This profile will be returned automatically for a request from the trusted URL.

To make Simple Registration Extension work, you must simply pass an instance of Zend_OpenId_Extension_Sreg as the second argument to the Zend_OpenId_Provider::handle() method.

Example #7 Provider with SREG

  1. $server = new Zend_OpenId_Provider();
  2. $sreg = new Zend_OpenId_Extension_Sreg();
  4. define("TEST_ID", Zend_OpenId::absoluteURL("example-10-id.php"));
  5. define("TEST_PASSWORD", "123");
  7. if ($_SERVER['REQUEST_METHOD'] == 'GET' &&
  8.     isset($_GET['openid_action']) &&
  9.     $_GET['openid_action'] === 'login') {
  10.     $server->login(TEST_ID, TEST_PASSWORD);
  11.     unset($_GET['openid_action']);
  12.     Zend_OpenId::redirect(Zend_OpenId::selfUrl(), $_GET);
  13. } else if ($_SERVER['REQUEST_METHOD'] == 'GET' &&
  14.     isset($_GET['openid_action']) &&
  15.     $_GET['openid_action'] === 'trust') {
  16.    echo "UNTRUSTED DATA" ;
  17. } else {
  18.     $ret = $server->handle(null, $sreg);
  19.     if (is_string($ret)) {
  20.         echo $ret;
  21.     } else if ($ret !== true) {
  22.         header('HTTP/1.0 403 Forbidden');
  23.         echo 'Forbidden';
  24.     }
  25. }

Anything Else?

Building OpenID providers is much less common than building OpenID-enabled sites, so this manual doesn't cover all Zend_OpenId_Provider features exhaustively, as was done for Zend_OpenId_Consumer.

To summamize, Zend_OpenId_Provider contains:

  • A set of methods to build an end-user GUI that allows users to register and manage their trusted sites and profiles

  • An abstract storage layer to store information about users, their sites and their profiles. It also stores associations between the provider and OpenID-enabled sites. This layer is very similar to that of the Zend_OpenId_Consumer class. It also uses file storage by default, but may used with another backend.

  • An abstract user-association layer that may associate a user's web browser with a logged-in identity

The Zend_OpenId_Provider class doesn't attempt to cover all possible features that can be implemented by OpenID servers, e.g. digital certificates, but it can be extended easily using Zend_OpenId_Extensions or by standard object-oriented extension.


© 2006-2020 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.