2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Single signon for phpMyAdmin using OpenID
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.
10 * It uses OpenID pear package, see https://pear.php.net/package/OpenID
12 * User first authenticates using OpenID and based on content of $AUTH_MAP
13 * the login information is passed to phpMyAdmin in session data.
19 if (false === @include_once
'OpenID/RelyingParty.php') {
23 /* Change this to true if using phpMyAdmin over https */
24 $secure_cookie = false;
27 * Map of authenticated users to MySQL user/password pairs.
30 'https://launchpad.net/~username' => array(
37 * Simple function to show HTML page with given content.
39 * @param string $contents Content to include in page
43 function Show_page($contents)
45 header('Content-Type: text/html; charset=utf-8');
46 echo '<?xml version="1.0" encoding="utf-8"?>' , "\n";
49 <html lang
="en" dir
="ltr">
51 <link rel
="icon" href
="../favicon.ico" type
="image/x-icon" />
52 <link rel
="shortcut icon" href
="../favicon.ico" type
="image/x-icon" />
53 <meta charset
="utf-8" />
54 <title
>phpMyAdmin OpenID signon example
</title
>
58 if (isset($_SESSION) && isset($_SESSION['PMA_single_signon_error_message'])) {
59 echo '<p class="error">' , $_SESSION['PMA_single_signon_message'] , '</p>';
60 unset($_SESSION['PMA_single_signon_message']);
70 * Display error and exit
72 * @param Exception $e Exception object
76 function Die_error($e)
78 $contents = "<div class='relyingparty_results'>\n";
79 $contents .= "<pre>" . htmlspecialchars($e->getMessage()) . "</pre>\n";
80 $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 (Exception
$e) {
136 /* Redirect to OpenID provider */
137 if (isset($_POST['start'])) {
139 $authRequest = $o->prepare();
140 } catch (Exception
$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 (Exception
$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>');