Source for file RelyingParty.php
Documentation is available at RelyingParty.php
* @author Bill Shupp <hostmaster@shupp.org>
* @copyright 2009 Bill Shupp
* @license http://www.opensource.org/licenses/bsd-license.php FreeBSD
* @link http://pearopenid.googlecode.com
require_once 'OpenID.php';
require_once 'OpenID/Store.php';
require_once 'OpenID/Discover.php';
require_once 'OpenID/Association/Request.php';
require_once 'OpenID/Assertion.php';
require_once 'OpenID/Assertion/Result.php';
require_once 'OpenID/Auth/Request.php';
require_once 'Net/URL2.php';
* OpenID_RelyingParty implements all the steps required to verify a claim in two
* step interface: {@link prepare() prepare} and {@link verify() verify}.
* {@link prepare() prepare} sets up the request, which includes performing
* discovery on the identifier, establishing an association with the OpenID Provider
* (optional), and then building an OpenID_Auth_Request object. With this object,
* you can optionally add OpenID_Extension(s), and then perform the request.
* {@link verify() verify} takes a Net_URL2 object as an argument, which represents
* the URL that the end user was redirected to after communicating with the the
* OpenID Provider. It processes the URL, and if it was a positive response from
* the OP, tries to verify that assertion.
* // First set up some things about your relying party:
* $realm = 'http://examplerp.com';
* $returnTo = $realm . '/relyingparty.php';
* // Here is an example user supplied identifier
* $identifier = $_POST['identifier'];
* // You might want to store it in session for use in verify()
* $_SESSION['identifier'] = $identifier;
* // Fire up the OpenID_RelyingParty object
* $rp = new OpenID_RelyingParty($returnTo, $realm, $identifier);
* // Here's an example of prepare() usage ...
* // First, grab your Auth_Request_Object
* $authRequest = $rp->prepare();
* // Then, optionally add an extension
* $sreg = new OpenID_Extension_SREG11(OpenID_Extension::REQUEST);
* $sreg->set('required', 'email');
* $sreg->set('optional', 'nickname,gender,dob');
* // You'll need to add it to OpenID_Auth_Request
* $authRequest->addExtension($sreg);
* // Optionally get association (from cache in this example)
* // Optionally make this a checkid_immediate request
* $auth->setMode(OpenID::MODE_CHECKID_IMMEDIATE);
* header('Location: ' . $auth->getAuthorizeURL());
* // Now, when they come back, you'll want to verify the claim ...
* // Assuming your $realm is the host which they came in to, build a Net_URL2
* // object from this request:
* $request = new Net_URL2($realm . $_SERVER['REQUEST_URI']);
* $result = $rp->verify($request);
* if ($result->success()) {
* @author Bill Shupp <hostmaster@shupp.org>
* @copyright 2009 Bill Shupp
* @license http://www.opensource.org/licenses/bsd-license.php FreeBSD
* @link http://pearopenid.googlecode.com
* The user supplied identifier, normalized
* @see OpenID::normalizeIdentifier()
* The URI used for the openid.return_to parameter
* The URI used for the openid.realm paramater
* Whether or not to use associations
* How far off of the current time to allow for nonce checking
* Sets the identifier, returnTo, and realm to be used for messages. The
* identifier is normalized before being set.
* @param mixed $returnTo The openid.return_to paramater value
* @param mixed $realm The openid.realm paramater value
* @param mixed $identifier The user supplied identifier, defaults to null
* @see OpenID::normalizeIdentifier
public function __construct($returnTo, $realm, $identifier =
null)
if ($identifier !==
null) {
* Enables the use of associations (default)
* Disables the use if associations
* Sets the clock skew for nonce checking
* @param int $skew Skew (or timeout) in seconds
* @throws OpenID_Exception if $skew is not numeric
* Prepares an OpenID_Auth_Request and returns it. This process includes
* performing discovery and optionally creating an association before preparing
* the OpenID_Auth_Request object.
* @return OpenID_Auth_Request
* @throws OpenID_Exception if no identifier was passed to the constructor
$serviceEndpoint =
$discover->services[0];
$opEndpointURL =
array_shift($serviceEndpoint->getURIs());
$serviceEndpoint->getVersion());
$assocHandle =
$assoc->assocHandle;
// Return OpenID_Auth_Request object
* Verifies an assertion response from the OP. If the openid.mode is error, an
* @param Net_URL2 $requestedURL The requested URL (that the user was
* directed to by the OP) as a Net_URL2
* @param OpenID_Message $message The OpenID_Message instance, as extractd
* from the input (GET or POST)
* @throws OpenID_Exception on error or invalid openid.mode
* @return OpenID_Assertion_Response
public function verify(Net_URL2 $requestedURL, OpenID_Message $message)
// Unsolicited assertion?
$unsolicitedID =
$message->get('openid.claimed_id');
$mode =
$message->get('openid.mode');
if ($message->get('openid.ns') ===
null
&&
$message->get('openid.user_setup_url') !==
null) {
// Negative 1.1 checkid_immediate response
$result->setAssertionMethod($mode);
$result->setUserSetupURL($message->get('openid.user_setup_url'));
case OpenID::MODE_SETUP_NEEDED:
$result->setAssertionMethod($mode);
$serviceEndpoint =
$discover->services[0];
$opEndpointURL =
array_shift($serviceEndpoint->getURIs());
// Check via associations
if ($message->get('openid.invalidate_handle') ===
null) {
// Don't fall back to check_authentication
$result->setAssertionMethod(OpenID::MODE_ASSOCIATE);
->getAssociation($opEndpointURL,
$message->get('openid.assoc_handle'));
$assoc->checkMessageSignature($message)) {
$result->setAssertionResult(true);
// If it's not an unsolicited assertion, just return
if (!isset
($unsolicitedID)) {
// Invalidate handle requested. Delete it and fall back to
$this->getStore()->deleteAssociation($opEndpointURL);
// Check via check_authenticate
$result->setAssertionMethod(OpenID::MODE_CHECK_AUTHENTICATION);
$result->setCheckAuthResponse($assertion->checkAuthentication());
if ($result->getCheckAuthResponse()->get('is_valid') ==
'true') {
$result->setAssertionResult(true);
* Gets discovered information from cache if it exists, otherwise performs
* @throws OpenID_Exception if discovery fails
* @see OpenID_Discover::getDiscover()
* @return OpenID_Discover
// @codeCoverageIgnoreStart
// @codeCoverageIgnoreEnd
* Gets an association from cache if it exists, otherwise, creates one.
* @param string $opEndpointURL The OP Endpoint URL to communicate with
* @param string $version The version of OpenID being used
* @return OpenID_Association on success, false on failure
$assocCache =
$this->getStore()->getAssociation($opEndpointURL);
$result =
$assoc->associate();
self::getStore()->setAssociation($result);
* Gets a new OpenID_Association_Request object. Abstracted for testing.
* @param string $opEndpointURL The OP endpoint URL to communicate with
* @param string $version The OpenID version being used
* @return OpenID_Association_Request
* Gets an instance of OpenID_Assertion. Abstracted for testing purposes.
* @param OpenID_Message $message The message passed to verify()
* @param Net_URL2 $requestedURL The URL requested (redirect from OP)
* @return OpenID_Assertion
Documentation generated on Tue, 15 Dec 2009 19:00:57 -0800 by phpDocumentor 1.4.3