diff options
author | tailor <cygnus@janrain.com> | 2007-03-10 00:34:22 +0000 |
---|---|---|
committer | tailor <cygnus@janrain.com> | 2007-03-10 00:34:22 +0000 |
commit | c3fc5ffa72ceee8aedd043e718297f50eecc4426 (patch) | |
tree | 6358757436a8616127809fae07f6394e96132f4f /examples/consumer | |
parent | 34ab4583f9f1b7d6488024d36a4958f7d3e17fe5 (diff) | |
download | php-openid-c3fc5ffa72ceee8aedd043e718297f50eecc4426.zip php-openid-c3fc5ffa72ceee8aedd043e718297f50eecc4426.tar.gz php-openid-c3fc5ffa72ceee8aedd043e718297f50eecc4426.tar.bz2 |
[project @ Refactored example consumer]
Diffstat (limited to 'examples/consumer')
-rw-r--r-- | examples/consumer/common.php | 72 | ||||
-rw-r--r-- | examples/consumer/finish_auth.php | 62 | ||||
-rw-r--r-- | examples/consumer/try_auth.php | 114 |
3 files changed, 149 insertions, 99 deletions
diff --git a/examples/consumer/common.php b/examples/consumer/common.php index cecf8d7..c4eed70 100644 --- a/examples/consumer/common.php +++ b/examples/consumer/common.php @@ -5,37 +5,53 @@ $path = ini_get('include_path'); $path = $path_extra . PATH_SEPARATOR . $path; ini_set('include_path', $path); -/** - * Require the OpenID consumer code. - */ -require_once "Auth/OpenID/Consumer.php"; - -/** - * Require the "file store" module, which we'll need to store OpenID - * information. - */ -require_once "Auth/OpenID/FileStore.php"; - -/** - * This is where the example will store its OpenID information. You - * should change this path if you want the example store to be created - * elsewhere. After you're done playing with the example script, - * you'll have to remove this directory manually. - */ -$store_path = "/tmp/_php_consumer_test"; - -if (!file_exists($store_path) && - !mkdir($store_path)) { - print "Could not create the FileStore directory '$store_path'. ". - " Please check the effective permissions."; +function displayError($message) { + $error = $message; + include 'index.php'; exit(0); } -$store = new Auth_OpenID_FileStore($store_path); +function doIncludes() { + /** + * Require the OpenID consumer code. + */ + require_once "Auth/OpenID/Consumer.php"; + + /** + * Require the "file store" module, which we'll need to store + * OpenID information. + */ + require_once "Auth/OpenID/FileStore.php"; +} + +doIncludes(); + +function &getStore() { + /** + * This is where the example will store its OpenID information. + * You should change this path if you want the example store to be + * created elsewhere. After you're done playing with the example + * script, you'll have to remove this directory manually. + */ + $store_path = "/tmp/_php_consumer_test"; + + if (!file_exists($store_path) && + !mkdir($store_path)) { + print "Could not create the FileStore directory '$store_path'. ". + " Please check the effective permissions."; + exit(0); + } + + return new Auth_OpenID_FileStore($store_path); +} -/** - * Create a consumer object using the store object created earlier. - */ -$consumer = new Auth_OpenID_Consumer($store); +function &getConsumer() { + /** + * Create a consumer object using the store object created + * earlier. + */ + $store = getStore(); + return new Auth_OpenID_Consumer($store); +} ?>
\ No newline at end of file diff --git a/examples/consumer/finish_auth.php b/examples/consumer/finish_auth.php index a75811c..b674155 100644 --- a/examples/consumer/finish_auth.php +++ b/examples/consumer/finish_auth.php @@ -3,36 +3,44 @@ require_once "common.php"; session_start(); -// Complete the authentication process using the server's response. -$response = $consumer->complete($_GET); - -if ($response->status == Auth_OpenID_CANCEL) { - // This means the authentication was cancelled. - $msg = 'Verification cancelled.'; -} else if ($response->status == Auth_OpenID_FAILURE) { - $msg = "OpenID authentication failed: " . $response->message; -} else if ($response->status == Auth_OpenID_SUCCESS) { - // This means the authentication succeeded. - $openid = $response->identity_url; - $esc_identity = htmlspecialchars($openid, ENT_QUOTES); - $success = sprintf('You have successfully verified ' . - '<a href="%s">%s</a> as your identity.', - $esc_identity, $esc_identity); - - if ($response->endpoint->canonicalID) { - $success .= ' (XRI CanonicalID: '.$response->endpoint->canonicalID.') '; +function run() { + $consumer = getConsumer(); + + // Complete the authentication process using the server's response. + $response = $consumer->complete($_GET); + + if ($response->status == Auth_OpenID_CANCEL) { + // This means the authentication was cancelled. + $msg = 'Verification cancelled.'; + } else if ($response->status == Auth_OpenID_FAILURE) { + $msg = "OpenID authentication failed: " . $response->message; + } else if ($response->status == Auth_OpenID_SUCCESS) { + // This means the authentication succeeded. + $openid = $response->identity_url; + $esc_identity = htmlspecialchars($openid, ENT_QUOTES); + + $success = sprintf('You have successfully verified ' . + '<a href="%s">%s</a> as your identity.', + $esc_identity, $esc_identity); + + if ($response->endpoint->canonicalID) { + $success .= ' (XRI CanonicalID: '.$response->endpoint->canonicalID.') '; + } + + $sreg = $response->extensionResponse('sreg', true); + + if (@$sreg['email']) { + $success .= " You also returned '".$sreg['email']."' as your email."; + } + + if (@$sreg['postcode']) { + $success .= " Your postal code is '".$sreg['postcode']."'"; + } } - $sreg = $response->extensionResponse('sreg', true); - - if (@$sreg['email']) { - $success .= " You also returned '".$sreg['email']."' as your email."; - } - if (@$sreg['postcode']) { - $success .= " Your postal code is '".$sreg['postcode']."'"; - } + include 'index.php'; } -include 'index.php'; +run(); ?>
\ No newline at end of file diff --git a/examples/consumer/try_auth.php b/examples/consumer/try_auth.php index b53f3cb..97a463a 100644 --- a/examples/consumer/try_auth.php +++ b/examples/consumer/try_auth.php @@ -3,62 +3,88 @@ require_once "common.php"; session_start(); -// Render a default page if we got a submission without an openid -// value. -if (empty($_GET['openid_url'])) { - $error = "Expected an OpenID URL."; - include 'index.php'; - exit(0); +function getOpenIDURL() { + // Render a default page if we got a submission without an openid + // value. + if (empty($_GET['openid_url'])) { + $error = "Expected an OpenID URL."; + include 'index.php'; + exit(0); + } + + return $_GET['openid_url']; } -$scheme = 'http'; -if (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 'on') { - $scheme .= 's'; +function getScheme() { + $scheme = 'http'; + if (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] == 'on') { + $scheme .= 's'; + } + return $scheme; } -$openid = $_GET['openid_url']; -$process_url = sprintf("$scheme://%s:%s%s/finish_auth.php", - $_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'], - dirname($_SERVER['PHP_SELF'])); +function getReturnTo() { + return sprintf("%s://%s:%s%s/finish_auth.php", + getScheme(), $_SERVER['SERVER_NAME'], + $_SERVER['SERVER_PORT'], + dirname($_SERVER['PHP_SELF'])); +} -$trust_root = sprintf("$scheme://%s:%s%s", - $_SERVER['SERVER_NAME'], $_SERVER['SERVER_PORT'], - dirname($_SERVER['PHP_SELF'])); +function getTrustRoot() { + return sprintf("%s://%s:%s%s/", + getScheme(), $_SERVER['SERVER_NAME'], + $_SERVER['SERVER_PORT'], + dirname($_SERVER['PHP_SELF'])); +} -// Begin the OpenID authentication process. -$auth_request = $consumer->begin($openid); +function run() { + $openid = getOpenIDURL(); + $consumer = getConsumer(); -// Handle failure status return values. -if (!$auth_request) { - $error = "Authentication error."; - include 'index.php'; - exit(0); -} + // Begin the OpenID authentication process. + $auth_request = $consumer->begin($openid); -$auth_request->addExtensionArg('sreg', 'optional', 'email'); + // Handle failure status return values. + if (!$auth_request) { + displayError("Authentication error; not a valid OpenID."); + } -// Redirect the user to the OpenID server for authentication. Store -// the token for this authentication so we can verify the response. + $auth_request->addExtensionArg('sreg', 'optional', 'email'); -if ($auth_request->shouldSendRedirect()) { - $redirect_url = $auth_request->redirectURL($trust_root, - $process_url); - header("Location: ".$redirect_url); -} else { - $form_id = 'openid_message'; - $form_html = $auth_request->formMarkup($trust_root, $process_url, false, - $form_tag_attrs=array('id' => $form_id)); + // Redirect the user to the OpenID server for authentication. + // Store the token for this authentication so we can verify the + // response. - if (is_a($form_html, 'Auth_OpenID_FailureResponse')) { - print "Error: " . $form_html->message; - } + if ($auth_request->shouldSendRedirect()) { + $redirect_url = $auth_request->redirectURL(getTrustRoot(), + getReturnTo()); + + if (Auth_OpenID::isFailure($redirect_url)) { + displayError("Could not redirect to server: " . $redirect_url->message); + } else { + header("Location: ".$redirect_url); + } + } else { + $form_id = 'openid_message'; + $form_html = $auth_request->formMarkup(getTrustRoot(), getReturnTo(), + false, array('id' => $form_id)); -?> -<html><head><title>OpenID transaction in progress</title></head> -<body onload='document.getElementById("<?=$form_id?>").submit()'> -<?=$form_html?> -</body></html> -<? + if (Auth_OpenID::isFailure($form_html)) { + displayError("Could not redirect to server: " . $form_html->message); + } else { + $page_contents = array( + "<html><head><title>", + "OpenID transaction in progress", + "</title></head>", + "<body onload='document.getElementById(\"<?=$form_id?>\").submit()'>", + $form_html, + "</body></html>"); + + print implode("\n", $page_contents); + } + } } +run(); + ?>
\ No newline at end of file |