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.
14 declare(strict_types
=1);
16 if (false === @include_once
'OpenID/RelyingParty.php') {
20 /* Change this to true if using phpMyAdmin over https */
21 $secure_cookie = false;
24 * Map of authenticated users to MySQL user/password pairs.
27 'https://launchpad.net/~username' => [
33 // phpcs:disable PSR1.Files.SideEffects
36 * Simple function to show HTML page with given content.
38 * @param string $contents Content to include in page
42 function Show_page($contents)
44 header('Content-Type: text/html; charset=utf-8');
45 echo '<?xml version="1.0" encoding="utf-8"?>' , "\n";
48 <html lang
="en" dir
="ltr">
50 <link rel
="icon" href
="../favicon.ico" type
="image/x-icon">
51 <link rel
="shortcut icon" href
="../favicon.ico" type
="image/x-icon">
52 <meta charset
="utf-8">
53 <title
>phpMyAdmin OpenID signon example
</title
>
57 if (isset($_SESSION['PMA_single_signon_error_message'])) {
58 echo '<p class="error">' , $_SESSION['PMA_single_signon_message'] , '</p>';
59 unset($_SESSION['PMA_single_signon_message']);
69 * Display error and exit
71 * @param Exception $e Exception object
75 function Die_error($e)
77 $contents = "<div class='relyingparty_results'>\n";
78 $contents .= '<pre>' . htmlspecialchars($e->getMessage()) . "</pre>\n";
79 $contents .= "</div class='relyingparty_results'>";
86 /* Need to have cookie visible from parent directory */
87 session_set_cookie_params(0, '/', '', $secure_cookie, true);
88 /* Create signon session */
89 $session_name = 'SignonSession';
90 session_name($session_name);
93 // Determine realm and return_to
95 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
98 $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
100 $realm = $base . '/';
101 $returnTo = $base . dirname($_SERVER['PHP_SELF']);
102 if ($returnTo[strlen($returnTo) - 1] != '/') {
105 $returnTo .= 'openid.php';
108 if (! count($_GET) && ! count($_POST) ||
isset($_GET['phpMyAdmin'])) {
109 /* Show simple form */
110 $content = '<form action="openid.php" method="post">
111 OpenID: <input type="text" name="identifier"><br>
112 <input type="submit" name="start">
120 /* Grab identifier */
121 if (isset($_POST['identifier']) && is_string($_POST['identifier'])) {
122 $identifier = $_POST['identifier'];
123 } elseif (isset($_SESSION['identifier']) && is_string($_SESSION['identifier'])) {
124 $identifier = $_SESSION['identifier'];
129 /* Create OpenID object */
131 $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
132 } catch (Throwable
$e) {
136 /* Redirect to OpenID provider */
137 if (isset($_POST['start'])) {
139 $authRequest = $o->prepare();
140 } catch (Throwable
$e) {
144 $url = $authRequest->getAuthorizeURL();
146 header('Location: ' . $url);
149 /* Grab query string */
150 if (! count($_POST)) {
151 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
153 // I hate php sometimes
154 $queryString = file_get_contents('php://input');
159 $message = new OpenID_Message($queryString, OpenID_Message
::FORMAT_HTTP
);
160 } catch (Throwable
$e) {
164 $id = $message->get('openid.claimed_id');
166 if (! empty($id) && isset($AUTH_MAP[$id])) {
167 $_SESSION['PMA_single_signon_user'] = $AUTH_MAP[$id]['user'];
168 $_SESSION['PMA_single_signon_password'] = $AUTH_MAP[$id]['password'];
169 session_write_close();
170 /* Redirect to phpMyAdmin (should use absolute URL here!) */
171 header('Location: ../index.php');
173 Show_page('<p>User not allowed!</p>');