3 * Single signon for phpMyAdmin using OpenID
5 * This is just example how to use single signon with phpMyAdmin, it is
6 * not intended to be perfect code and look, only shows how you can
7 * integrate this functionality in your application.
9 * It uses OpenID pear package, see https://pear.php.net/package/OpenID
11 * User first authenticates using OpenID and based on content of $AUTH_MAP
12 * the login information is passed to phpMyAdmin in session data.
15 declare(strict_types
=1);
17 if (false === @include_once
'OpenID/RelyingParty.php') {
21 /* Change this to true if using phpMyAdmin over https */
22 $secureCookie = false;
25 * Map of authenticated users to MySQL user/password pairs.
27 $authMap = ['https://launchpad.net/~username' => ['user' => 'root', 'password' => '']];
29 // phpcs:disable PSR1.Files.SideEffects,Squiz.Functions.GlobalFunction
32 * Simple function to show HTML page with given content.
34 * @param string $contents Content to include in page
36 function Show_page(string $contents): void
38 header('Content-Type: text/html; charset=utf-8');
40 echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
42 <html lang="en" dir="ltr">
44 <link rel="icon" href="../favicon.ico" type="image/x-icon">
45 <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
46 <meta charset="utf-8">
47 <title>phpMyAdmin OpenID signon example</title>
51 if (isset($_SESSION['PMA_single_signon_error_message'])) {
52 echo '<p class="error">' . $_SESSION['PMA_single_signon_message'] . '</p>';
53 unset($_SESSION['PMA_single_signon_message']);
57 echo '</body></html>';
61 * Display error and exit
63 * @param Exception $e Exception object
65 function Die_error(Throwable
$e): void
67 $contents = "<div class='relyingparty_results'>\n";
68 $contents .= '<pre>' . htmlspecialchars($e->getMessage()) . "</pre>\n";
69 $contents .= "</div class='relyingparty_results'>";
76 /* Need to have cookie visible from parent directory */
77 session_set_cookie_params(0, '/', '', $secureCookie, true);
78 /* Create signon session */
79 $sessionName = 'SignonSession';
80 session_name($sessionName);
83 // Determine realm and return_to
85 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
89 $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
92 $returnTo = $base . dirname($_SERVER['PHP_SELF']);
93 if ($returnTo[strlen($returnTo) - 1] !== '/') {
97 $returnTo .= 'openid.php';
100 if ((! count($_GET) && ! count($_POST)) ||
isset($_GET['phpMyAdmin'])) {
101 /* Show simple form */
102 $content = '<form action="openid.php" method="post">
103 OpenID: <input type="text" name="identifier"><br>
104 <input type="submit" name="start">
110 /* Grab identifier */
112 if (isset($_POST['identifier']) && is_string($_POST['identifier'])) {
113 $identifier = $_POST['identifier'];
114 } elseif (isset($_SESSION['identifier']) && is_string($_SESSION['identifier'])) {
115 $identifier = $_SESSION['identifier'];
118 /* Create OpenID object */
120 $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
121 } catch (Throwable
$e) {
125 /* Redirect to OpenID provider */
126 if (isset($_POST['start'])) {
128 $authRequest = $o->prepare();
129 } catch (Throwable
$e) {
133 $url = $authRequest->getAuthorizeURL();
135 header('Location: ' . $url);
139 /* Grab query string */
140 if (! count($_POST)) {
141 [, $queryString] = explode('?', $_SERVER['REQUEST_URI']);
143 // Fetch the raw query body
144 $queryString = file_get_contents('php://input');
149 $message = new OpenID_Message($queryString, OpenID_Message
::FORMAT_HTTP
);
150 } catch (Throwable
$e) {
154 $id = $message->get('openid.claimed_id');
156 if (empty($id) ||
! isset($authMap[$id])) {
157 Show_page('<p>User not allowed!</p>');
161 $_SESSION['PMA_single_signon_user'] = $authMap[$id]['user'];
162 $_SESSION['PMA_single_signon_password'] = $authMap[$id]['password'];
163 $_SESSION['PMA_single_signon_HMAC_secret'] = hash('sha1', uniqid(strval(random_int(0, mt_getrandmax())), true));
164 session_write_close();
165 /* Redirect to phpMyAdmin (should use absolute URL here!) */
166 header('Location: ../index.php');