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/try_auth.php | |
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/try_auth.php')
-rw-r--r-- | examples/consumer/try_auth.php | 114 |
1 files changed, 70 insertions, 44 deletions
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 |