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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?PHP
  2. /* Poidsy 0.6 - http://chris.smith.name/projects/poidsy
  3. * Copyright (c) 2008-2010 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. // Poidsy returns its results in a session variable, so we need to start the
  24. // session here in order to get access to the results
  25. session_start();
  26. // Request some information from the identity provider. Note that providers
  27. // don't have to implement the extension that provides this, so you can't
  28. // rely on getting results back. You can use OPENID_SREG_REQUEST to more
  29. // strongly request the information (it implies that the user will have to
  30. // manually enter the data if the provider doesn't supply it).
  31. //
  32. // The fields listed here are all the valid SREG fields. Anything else
  33. // almost certainly won't work (but you can of course omit ones you don't
  34. // need).
  35. define('OPENID_SREG_OPTIONAL',
  36. 'nickname,email,fullname,dob,gender,postcode,country,language,timezone');
  37. if (isset($_POST['openid_url']) || isset($_REQUEST['openid_mode'])) {
  38. // There are two cases when poidsy's processor needs to be invoked - firstly,
  39. // when the user has just submitted an OpenID identifier to be verified, in
  40. // which case $_POST['openid_url'] will be present (poidsy has special
  41. // handling for inputs named openid_url. If you want to use a URL from
  42. // another source, you can define the OPENID_URL constant instead.).
  43. // Secondly, if the user is being redirected back from their provider, the
  44. // openid.mode parameter will be present (which PHP translates to openid_mode)
  45. if (isset($_POST['openid_type']) && $_POST['openid_type'] != 'openid_url') {
  46. // This allows users to select one of the pre-defined identity providers
  47. // using the provided radio buttons. The values of the radio buttons specify
  48. // an URL on which we can perform Yadis discovery to find the OpenID
  49. // endpoint.
  50. define('OPENID_URL', $_POST['openid_type']);
  51. }
  52. // Include the simple registration extension
  53. require('../../sreg.ext.php');
  54. // Include and configure the attribute exchange extension
  55. require('../../ax.ext.php');
  56. AttributeExchange::addRequiredType('email', AttributeExchange::EMAIL);
  57. require('../../processor.php');
  58. } else {
  59. // If we don't have any processing to be doing, show them the form and
  60. // results.
  61. ?>
  62. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  63. "http://www.w3.org/TR/html4/strict.dtd">
  64. <html>
  65. <head>
  66. <title>OpenID consumer demonstration</title>
  67. <style type="text/css">
  68. input#openid_url {
  69. background: url('../../openid.gif') no-repeat; padding-left: 20px;
  70. margin: 5px 0px 0px 40px;
  71. }
  72. p { padding-left: 10px; }
  73. p.error { border-left: 10px solid #f00; }
  74. p.succ { border-left: 10px solid #0f0; }
  75. caption { text-align: left; }
  76. table { margin: 10px; }
  77. ul { list-style-type: none; }
  78. input[type="radio"] { width: 20px; }
  79. </style>
  80. </head>
  81. <body>
  82. <h1>OpenID consumer demo</h1>
  83. <?PHP
  84. if (isset($_SESSION['openid']['error'])) {
  85. // If the error variable is set, it means that poidsy has encountered an
  86. // error while trying to validate the identifier. We just tell the user
  87. // what went wrong, and unset the session vars so the messages don't persist
  88. echo '<p class="error">An error occured: ', htmlentities($_SESSION['openid']['error']), '</p>';
  89. unset($_SESSION['openid']['error']);
  90. } else if (isset($_SESSION['openid']['validated']) && $_SESSION['openid']['validated']) {
  91. // Upon a successful validation, the validated field will have been set to
  92. // true. It's important to check the validated field, as the identity
  93. // will be specified in the array throughout the process, so it would be
  94. // possible for the user to request the page with an identity specified
  95. // but before Poidsy had validated it. As above, we unset the session
  96. // vars so that the details don't persist.
  97. echo '<p class="succ">Success: your OpenID identifier is <em>', htmlentities($_SESSION['openid']['identity']), '</em></p>';
  98. unset($_SESSION['openid']['validated']);
  99. // Show the SREG data returned, if any. SREG data is only present if you
  100. // defined one of the OPENID_SREG constants before the request was sent,
  101. // if the user's identity provider supports SREG, and if (depending on the
  102. // provider) the user gives permission for you to have the data.
  103. if (isset($_SESSION['openid']['sreg'])) {
  104. echo '<table>';
  105. echo '<caption>Simple Registration Extension data</caption>';
  106. foreach ($_SESSION['openid']['sreg'] as $type => $data) {
  107. echo '<tr><th>', htmlentities($type), '</th>';
  108. echo '<td>', htmlentities($data), '</td></tr>';
  109. }
  110. echo '</table>';
  111. unset($_SESSION['openid']['sreg']);
  112. }
  113. // Show the attribute exchange data returned, if any.
  114. if (isset($_SESSION['openid']['ax'])) {
  115. echo '<table>';
  116. echo '<caption>Attribute Exchange Extension data</caption>';
  117. foreach ($_SESSION['openid']['ax']['types'] as $type => $uri) {
  118. echo '<tr><th>', htmlentities($type), '</th>';
  119. echo '<td>', htmlentities($uri), '</td>';
  120. echo '<td>', $count = $_SESSION['openid']['ax']['counts'][$type], '</td>';
  121. echo '<td>';
  122. if ($count == 1) {
  123. echo htmlentities($_SESSION['openid']['ax']['data'][$type]);
  124. } else if ($count > 1) {
  125. echo '<ol>';
  126. foreach ($_SESSION['openid']['ax']['data'][$type] as $value) {
  127. echo '<li>', htmlentities($value), '</li>';
  128. }
  129. echo '</ol>';
  130. }
  131. echo '</td>';
  132. echo '</tr>';
  133. }
  134. echo '</table>';
  135. unset($_SESSION['openid']['sreg']);
  136. }
  137. }
  138. ?>
  139. <form action="<?PHP echo htmlentities($_SERVER['REQUEST_URI']); ?>"
  140. method="post">
  141. <ul>
  142. <li><label><input type="radio" name="openid_type" value="https://www.google.com/accounts/o8/id"> <img src="google.png" alt="Google"> Login with my Google account</label></li>
  143. <li><label><input type="radio" name="openid_type" value="yahoo.com"> <img src="yahoo.png" alt="Yahoo!"> Login with my Yahoo! account</label></li>
  144. <li><label><input type="radio" name="openid_type" value="openid_url" checked="checked"> Login with another OpenID identity:</label> <br>
  145. <input type="text" name="openid_url" id="openid_url"></li>
  146. </ul>
  147. <input type="submit" value="Login">
  148. </form>
  149. </body>
  150. </html>
  151. <?PHP
  152. }
  153. ?>