openid.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Single signon for phpMyAdmin using OpenID
  5. *
  6. * This is just example how to use single signon with phpMyAdmin, it is
  7. * not intended to be perfect code and look, only shows how you can
  8. * integrate this functionality in your application.
  9. *
  10. * It uses OpenID pear package, see http://pear.php.net/package/OpenID
  11. *
  12. * User first authenticates using OpenID and based on content of $AUTH_MAP
  13. * the login information is passed to phpMyAdmin in session data.
  14. *
  15. * @package PhpMyAdmin
  16. * @subpackage Example
  17. */
  18. if (false === @include_once 'OpenID/RelyingParty.php') {
  19. exit;
  20. }
  21. /**
  22. * Map of authenticated users to MySQL user/password pairs.
  23. */
  24. $AUTH_MAP = array(
  25. 'http://launchpad.net/~username' => array(
  26. 'user' => 'root',
  27. 'password' => '',
  28. ),
  29. );
  30. /**
  31. * Simple function to show HTML page with given content.
  32. *
  33. * @param string $contents Content to include in page
  34. *
  35. * @return void
  36. */
  37. function Show_page($contents)
  38. {
  39. header('Content-Type: text/html; charset=utf-8');
  40. echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
  41. ?>
  42. <!DOCTYPE HTML>
  43. <html lang="en" dir="ltr">
  44. <head>
  45. <link rel="icon" href="../favicon.ico" type="image/x-icon" />
  46. <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
  47. <meta charset="utf-8" />
  48. <title>phpMyAdmin OpenID signon example</title>
  49. </head>
  50. <body>
  51. <?php
  52. if (isset($_SESSION) && isset($_SESSION['PMA_single_signon_error_message'])) {
  53. echo '<p class="error">' . $_SESSION['PMA_single_signon_message'] . '</p>';
  54. unset($_SESSION['PMA_single_signon_message']);
  55. }
  56. echo $contents;
  57. ?>
  58. </body>
  59. </html>
  60. <?php
  61. }
  62. /* Need to have cookie visible from parent directory */
  63. session_set_cookie_params(0, '/', '', false);
  64. /* Create signon session */
  65. $session_name = 'SignonSession';
  66. session_name($session_name);
  67. session_start();
  68. // Determine realm and return_to
  69. $base = 'http';
  70. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
  71. $base .= 's';
  72. }
  73. $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
  74. $realm = $base . '/';
  75. $returnTo = $base . dirname($_SERVER['PHP_SELF']);
  76. if ($returnTo[strlen($returnTo) - 1] != '/') {
  77. $returnTo .= '/';
  78. }
  79. $returnTo .= 'openid.php';
  80. /* Display form */
  81. if (!count($_GET) && !count($_POST) || isset($_GET['phpMyAdmin'])) {
  82. /* Show simple form */
  83. $content = '<form action="openid.php" method="post">
  84. OpenID: <input type="text" name="identifier" /><br />
  85. <input type="submit" name="start" />
  86. </form>
  87. </body>
  88. </html>';
  89. Show_page($content);
  90. exit;
  91. }
  92. /* Grab identifier */
  93. if (isset($_POST['identifier'])) {
  94. $identifier = $_POST['identifier'];
  95. } else if (isset($_SESSION['identifier'])) {
  96. $identifier = $_SESSION['identifier'];
  97. } else {
  98. $identifier = null;
  99. }
  100. /* Create OpenID object */
  101. try {
  102. $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
  103. } catch (OpenID_Exception $e) {
  104. $contents = "<div class='relyingparty_results'>\n";
  105. $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
  106. $contents .= "</div class='relyingparty_results'>";
  107. Show_page($contents);
  108. exit;
  109. }
  110. /* Redirect to OpenID provider */
  111. if (isset($_POST['start'])) {
  112. try {
  113. $authRequest = $o->prepare();
  114. } catch (OpenID_Exception $e) {
  115. $contents = "<div class='relyingparty_results'>\n";
  116. $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
  117. $contents .= "</div class='relyingparty_results'>";
  118. Show_page($contents);
  119. exit;
  120. }
  121. $url = $authRequest->getAuthorizeURL();
  122. header("Location: $url");
  123. exit;
  124. } else {
  125. /* Grab query string */
  126. if (!count($_POST)) {
  127. list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
  128. } else {
  129. // I hate php sometimes
  130. $queryString = file_get_contents('php://input');
  131. }
  132. /* Check reply */
  133. $message = new OpenID_Message($queryString, OpenID_Message::FORMAT_HTTP);
  134. $id = $message->get('openid.claimed_id');
  135. if (!empty($id) && isset($AUTH_MAP[$id])) {
  136. $_SESSION['PMA_single_signon_user'] = $AUTH_MAP[$id]['user'];
  137. $_SESSION['PMA_single_signon_password'] = $AUTH_MAP[$id]['password'];
  138. session_write_close();
  139. /* Redirect to phpMyAdmin (should use absolute URL here!) */
  140. header('Location: ../index.php');
  141. } else {
  142. Show_page('<p>User not allowed!</p>');
  143. exit;
  144. }
  145. }