PHP OpenID consumer
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // 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. require('../../processor.php');
  53. } else {
  54. // If we don't have any processing to be doing, show them the form and
  55. // results.
  56. ?>
  57. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  58. "http://www.w3.org/TR/html4/strict.dtd">
  59. <html>
  60. <head>
  61. <title>OpenID consumer demonstration</title>
  62. <style type="text/css">
  63. input#openid_url {
  64. background: url('../../openid.gif') no-repeat; padding-left: 20px;
  65. margin: 5px 0px 0px 40px;
  66. }
  67. p { padding-left: 10px; }
  68. p.error { border-left: 10px solid #f00; }
  69. p.succ { border-left: 10px solid #0f0; }
  70. caption { text-align: left; }
  71. table { margin: 10px; }
  72. ul { list-style-type: none; }
  73. input[type="radio"] { width: 20px; }
  74. </style>
  75. </head>
  76. <body>
  77. <h1>OpenID consumer demo</h1>
  78. <?PHP
  79. if (isset($_SESSION['openid']['error'])) {
  80. // If the error variable is set, it means that poidsy has encountered an
  81. // error while trying to validate the identifier. We just tell the user
  82. // what went wrong, and unset the session vars so the messages don't persist
  83. echo '<p class="error">An error occured: ', htmlentities($_SESSION['openid']['error']), '</p>';
  84. unset($_SESSION['openid']['error']);
  85. } else if (isset($_SESSION['openid']['validated']) && $_SESSION['openid']['validated']) {
  86. // Upon a successful validation, the validated field will have been set to
  87. // true. It's important to check the validated field, as the identity
  88. // will be specified in the array throughout the process, so it would be
  89. // possible for the user to request the page with an identity specified
  90. // but before Poidsy had validated it. As above, we unset the session
  91. // vars so that the details don't persist.
  92. echo '<p class="succ">Success: your OpenID identifier is <em>', htmlentities($_SESSION['openid']['identity']), '</em></p>';
  93. unset($_SESSION['openid']['validated']);
  94. // Show the SREG data returned, if any. SREG data is only present if you
  95. // defined one of the OPENID_SREG constants before the request was sent,
  96. // if the user's identity provider supports SREG, and if (depending on the
  97. // provider) the user gives permission for you to have the data.
  98. if (isset($_SESSION['openid']['sreg'])) {
  99. echo '<table>';
  100. echo '<caption>Simple Registration Extension data</caption>';
  101. foreach ($_SESSION['openid']['sreg'] as $type => $data) {
  102. echo '<tr><th>', htmlentities($type), '</th>';
  103. echo '<td>', htmlentities($data), '</td></tr>';
  104. }
  105. echo '</table>';
  106. unset($_SESSION['openid']['sreg']);
  107. }
  108. }
  109. ?>
  110. <form action="<?PHP echo htmlentities($_SERVER['REQUEST_URI']); ?>"
  111. method="post">
  112. <ul>
  113. <li><label><input type="radio" name="openid_type" value="gmail.com"> <img src="google.png" alt="Google"> Login with my Google account</label></li>
  114. <li><label><input type="radio" name="openid_type" value="yahoo.com"> <img src="yahoo.png" alt="Yahoo!"> Login with my Yahoo! account</label></li>
  115. <li><label><input type="radio" name="openid_type" value="openid_url" checked="checked"> Login with another OpenID identity:</label> <br>
  116. <input type="text" name="openid_url" id="openid_url"></li>
  117. </ul>
  118. <input type="submit" value="Login">
  119. </form>
  120. </body>
  121. </html>
  122. <?PHP
  123. }
  124. ?>