try_auth.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 = null;
  32. if (isset($_GET['policies'])) {
  33. $policy_uris = $_GET['policies'];
  34. }
  35. $pape_request = new Auth_OpenID_PAPE_Request($policy_uris);
  36. if ($pape_request) {
  37. $auth_request->addExtension($pape_request);
  38. }
  39. // Redirect the user to the OpenID server for authentication.
  40. // Store the token for this authentication so we can verify the
  41. // response.
  42. // For OpenID 1, send a redirect. For OpenID 2, use a Javascript
  43. // form to send a POST request to the server.
  44. if ($auth_request->shouldSendRedirect()) {
  45. $redirect_url = $auth_request->redirectURL(getTrustRoot(),
  46. getReturnTo());
  47. // If the redirect URL can't be built, display an error
  48. // message.
  49. if (Auth_OpenID::isFailure($redirect_url)) {
  50. displayError("Could not redirect to server: " . $redirect_url->message);
  51. } else {
  52. // Send redirect.
  53. header("Location: ".$redirect_url);
  54. }
  55. } else {
  56. // Generate form markup and render it.
  57. $form_id = 'openid_message';
  58. $form_html = $auth_request->htmlMarkup(getTrustRoot(), getReturnTo(),
  59. false, array('id' => $form_id));
  60. // Display an error if the form markup couldn't be generated;
  61. // otherwise, render the HTML.
  62. if (Auth_OpenID::isFailure($form_html)) {
  63. displayError("Could not redirect to server: " . $form_html->message);
  64. } else {
  65. print $form_html;
  66. }
  67. }
  68. }
  69. run();
  70. ?>