PHP OpenID consumer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?PHP
  2. /* Poidsy 0.4 - http://chris.smith.name/projects/poidsy
  3. * Copyright (c) 2008 Chris Smith
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. session_start();
  24. require('../../urlbuilder.inc.php');
  25. if (isset($_GET['cs'])) {
  26. unset($_SESSION['openid']);
  27. header('Location: ' . $_SERVER['SCRIPT_NAME']);
  28. exit;
  29. }
  30. if (isset($_POST['openid_url']) || isset($_REQUEST['openid_mode'])) {
  31. // Proxy for non-JS users
  32. require('../../processor.php');
  33. } else {
  34. ?>
  35. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  36. "http://www.w3.org/TR/html4/strict.dtd">
  37. <html>
  38. <head>
  39. <title>OpenID consumer demonstration</title>
  40. <style type="text/css">
  41. input#openid_url {
  42. background: url('../../openid.gif') no-repeat; padding-left: 20px;
  43. }
  44. div { margin: 20px; padding: 5px; }
  45. </style>
  46. <script type="text/javascript">
  47. function tryJsLogin() {
  48. document.getElementById('target').src = 'iframe.php?openid.id=' + document.getElementById('openid_url').value;
  49. }
  50. function doSubmit() {
  51. //alert('Provider is requesting your interaction. Sending you away.');
  52. document.getElementById('form').submit();
  53. }
  54. function doError(msg) {
  55. document.getElementById('status').innerHTML = msg;
  56. document.getElementById('status').style.backgroundColor = "#a00";
  57. }
  58. function doSuccess(msg) {
  59. document.getElementById('status').innerHTML = msg;
  60. document.getElementById('status').style.backgroundColor = "#0a0";
  61. }
  62. </script>
  63. </head>
  64. <body>
  65. <h1>OpenID consumer demo</h1>
  66. <p>
  67. The login form below uses a hidden iframe to process the form
  68. (assuming the user has javascript enabled; if they don't, it falls back
  69. gracefully). If your identity provider implements checkid_immediate
  70. properly (which several don't appear to), and has enough information to
  71. authorise you without requiring your input, the entire login process
  72. should happen without any noticable change except for the status message.
  73. </p><p>
  74. If your identity provider requires interaction with you, the form
  75. will be submitted as usual and you'll leave this page (but, as usual, will
  76. return when your IdP is done with you). If your identity provider is
  77. <em>broken</em>, you won't see anything happening after the initial page
  78. load and redirect. This is because the identity provider is trying to
  79. interact with you (via a hidden iframe) when it has been explicitly told
  80. not to. This is the identity provider's fault (it's violating the OpenID
  81. specifications), not Poidsy's. If you were implementing this on a live site,
  82. you'd probably add a timer to detect if it wasn't working and do a normal
  83. login.
  84. </p>
  85. <p>
  86. Note: if you are using Firefox and have the 'Disallow third party cookies'
  87. preference enabled, Firefox won't send cookies to your provider when it's
  88. loaded in the iframe. This almost certainly will mean that your provider
  89. can't validate your identity immediately, and thus you'll be redirected.
  90. Other browsers (such as IE and Safari) allow these cookies to be sent even
  91. if they disallow setting of third-party cookies.
  92. </p>
  93. <?PHP
  94. echo '<p>Time: ', date('r'), '. <a href="?cs">Clear session info</a></p>';
  95. if (isset($_SESSION['openid']['error'])) {
  96. echo '<div id="status" style="background-color: #a00;">An error occured: ', htmlentities($_SESSION['openid']['error']), '</div>';
  97. unset($_SESSION['openid']['error']);
  98. } else if (isset($_SESSION['openid']['validated']) && $_SESSION['openid']['validated']) {
  99. echo '<div id="status" style="background-color: #0a0;">Logged in as ', htmlentities($_SESSION['openid']['identity']), '</div>';
  100. } else {
  101. echo '<div id="status">Not logged in</div>';
  102. }
  103. ?>
  104. <form action="<?PHP echo htmlentities($_SERVER['REQUEST_URI']); ?>"
  105. method="post" onSubmit="tryJsLogin(); return false;" id="form">
  106. <input type="text" name="openid_url" id="openid_url">
  107. <input type="submit" value="Login">
  108. <iframe id="target" style="display: none;"></iframe>
  109. </form>
  110. </body>
  111. </html>
  112. <?PHP
  113. }
  114. ?>