openid.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 https://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. /* Change this to true if using phpMyAdmin over https */
  22. $secure_cookie = false;
  23. /**
  24. * Map of authenticated users to MySQL user/password pairs.
  25. */
  26. $AUTH_MAP = array(
  27. 'https://launchpad.net/~username' => array(
  28. 'user' => 'root',
  29. 'password' => '',
  30. ),
  31. );
  32. /**
  33. * Simple function to show HTML page with given content.
  34. *
  35. * @param string $contents Content to include in page
  36. *
  37. * @return void
  38. */
  39. function Show_page($contents)
  40. {
  41. header('Content-Type: text/html; charset=utf-8');
  42. echo '<?xml version="1.0" encoding="utf-8"?>' , "\n";
  43. ?>
  44. <!DOCTYPE HTML>
  45. <html lang="en" dir="ltr">
  46. <head>
  47. <link rel="icon" href="../favicon.ico" type="image/x-icon" />
  48. <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
  49. <meta charset="utf-8" />
  50. <title>phpMyAdmin OpenID signon example</title>
  51. </head>
  52. <body>
  53. <?php
  54. if (isset($_SESSION) && isset($_SESSION['PMA_single_signon_error_message'])) {
  55. echo '<p class="error">' , $_SESSION['PMA_single_signon_message'] , '</p>';
  56. unset($_SESSION['PMA_single_signon_message']);
  57. }
  58. echo $contents;
  59. ?>
  60. </body>
  61. </html>
  62. <?php
  63. }
  64. /**
  65. * Display error and exit
  66. *
  67. * @param Exception $e Exception object
  68. *
  69. * @return void
  70. */
  71. function Die_error($e)
  72. {
  73. $contents = "<div class='relyingparty_results'>\n";
  74. $contents .= "<pre>" . htmlspecialchars($e->getMessage()) . "</pre>\n";
  75. $contents .= "</div class='relyingparty_results'>";
  76. Show_page($contents);
  77. exit;
  78. }
  79. /* Need to have cookie visible from parent directory */
  80. session_set_cookie_params(0, '/', '', $secure_cookie, true);
  81. /* Create signon session */
  82. $session_name = 'SignonSession';
  83. session_name($session_name);
  84. @session_start();
  85. // Determine realm and return_to
  86. $base = 'http';
  87. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
  88. $base .= 's';
  89. }
  90. $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
  91. $realm = $base . '/';
  92. $returnTo = $base . dirname($_SERVER['PHP_SELF']);
  93. if ($returnTo[strlen($returnTo) - 1] != '/') {
  94. $returnTo .= '/';
  95. }
  96. $returnTo .= 'openid.php';
  97. /* Display form */
  98. if (!count($_GET) && !count($_POST) || isset($_GET['phpMyAdmin'])) {
  99. /* Show simple form */
  100. $content = '<form action="openid.php" method="post">
  101. OpenID: <input type="text" name="identifier" /><br />
  102. <input type="submit" name="start" />
  103. </form>
  104. </body>
  105. </html>';
  106. Show_page($content);
  107. exit;
  108. }
  109. /* Grab identifier */
  110. if (isset($_POST['identifier']) && is_string($_POST['identifier'])) {
  111. $identifier = $_POST['identifier'];
  112. } elseif (isset($_SESSION['identifier']) && is_string($_SESSION['identifier'])) {
  113. $identifier = $_SESSION['identifier'];
  114. } else {
  115. $identifier = null;
  116. }
  117. /* Create OpenID object */
  118. try {
  119. $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
  120. } catch (Exception $e) {
  121. Die_error($e);
  122. }
  123. /* Redirect to OpenID provider */
  124. if (isset($_POST['start'])) {
  125. try {
  126. $authRequest = $o->prepare();
  127. } catch (Exception $e) {
  128. Die_error($e);
  129. }
  130. $url = $authRequest->getAuthorizeURL();
  131. header("Location: $url");
  132. exit;
  133. } else {
  134. /* Grab query string */
  135. if (!count($_POST)) {
  136. list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
  137. } else {
  138. // I hate php sometimes
  139. $queryString = file_get_contents('php://input');
  140. }
  141. /* Check reply */
  142. try {
  143. $message = new OpenID_Message($queryString, OpenID_Message::FORMAT_HTTP);
  144. } catch (Exception $e) {
  145. Die_error($e);
  146. }
  147. $id = $message->get('openid.claimed_id');
  148. if (!empty($id) && isset($AUTH_MAP[$id])) {
  149. $_SESSION['PMA_single_signon_user'] = $AUTH_MAP[$id]['user'];
  150. $_SESSION['PMA_single_signon_password'] = $AUTH_MAP[$id]['password'];
  151. session_write_close();
  152. /* Redirect to phpMyAdmin (should use absolute URL here!) */
  153. header('Location: ../index.php');
  154. } else {
  155. Show_page('<p>User not allowed!</p>');
  156. exit;
  157. }
  158. }