try_auth.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. require_once "common.php";
  3. session_start();
  4. function getOpenIDURL() {
  5. // Render a default page if we got a submission without an openid
  6. // value.
  7. if (empty($_GET['openid_identifier'])) {
  8. $error = "Expected an OpenID URL.";
  9. include 'index.php';
  10. exit(0);
  11. }
  12. return $_GET['openid_identifier'];
  13. }
  14. function run() {
  15. $openid = getOpenIDURL();
  16. $consumer = getConsumer();
  17. // Begin the OpenID authentication process.
  18. $auth_request = $consumer->begin($openid);
  19. // No auth request means we can't begin OpenID.
  20. if (!$auth_request) {
  21. displayError("Authentication error; not a valid OpenID.");
  22. }
  23. $sreg_request = Auth_OpenID_SRegRequest::build(
  24. // Required
  25. array('nickname'),
  26. // Optional
  27. array('fullname', 'email'));
  28. if ($sreg_request) {
  29. $auth_request->addExtension($sreg_request);
  30. }
  31. $policy_uris = $_GET['policies'];
  32. $pape_request = new Auth_OpenID_PAPE_Request($policy_uris);
  33. if ($pape_request) {
  34. $auth_request->addExtension($pape_request);
  35. }
  36. // Redirect the user to the OpenID server for authentication.
  37. // Store the token for this authentication so we can verify the
  38. // response.
  39. // For OpenID 1, send a redirect. For OpenID 2, use a Javascript
  40. // form to send a POST request to the server.
  41. if ($auth_request->shouldSendRedirect()) {
  42. $redirect_url = $auth_request->redirectURL(getTrustRoot(),
  43. getReturnTo());
  44. // If the redirect URL can't be built, display an error
  45. // message.
  46. if (Auth_OpenID::isFailure($redirect_url)) {
  47. displayError("Could not redirect to server: " . $redirect_url->message);
  48. } else {
  49. // Send redirect.
  50. header("Location: ".$redirect_url);
  51. }
  52. } else {
  53. // Generate form markup and render it.
  54. $form_id = 'openid_message';
  55. $form_html = $auth_request->htmlMarkup(getTrustRoot(), getReturnTo(),
  56. false, array('id' => $form_id));
  57. // Display an error if the form markup couldn't be generated;
  58. // otherwise, render the HTML.
  59. if (Auth_OpenID::isFailure($form_html)) {
  60. displayError("Could not redirect to server: " . $form_html->message);
  61. } else {
  62. print $form_html;
  63. }
  64. }
  65. }
  66. run();
  67. ?>