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') {
24 * Map of authenticated users to MySQL user/password pairs.
27 'https://launchpad.net/~username' => array(
34 * Simple function to show HTML page with given content.
36 * @param string $contents Content to include in page
40 function Show_page($contents)
42 header('Content-Type: text/html; charset=utf-8');
43 echo '<?xml version="1.0" encoding="utf-8"?>' , "\n";
46 <html lang
="en" dir
="ltr">
48 <link rel
="icon" href
="../favicon.ico" type
="image/x-icon" />
49 <link rel
="shortcut icon" href
="../favicon.ico" type
="image/x-icon" />
50 <meta charset
="utf-8" />
51 <title
>phpMyAdmin OpenID signon example
</title
>
55 if (isset($_SESSION) && isset($_SESSION['PMA_single_signon_error_message'])) {
56 echo '<p class="error">' , $_SESSION['PMA_single_signon_message'] , '</p>';
57 unset($_SESSION['PMA_single_signon_message']);
66 function Die_error($e)
68 $contents = "<div class='relyingparty_results'>\n";
69 $contents .= "<pre>" . htmlspecialchars($e->getMessage()) . "</pre>\n";
70 $contents .= "</div class='relyingparty_results'>";
76 /* Need to have cookie visible from parent directory */
77 session_set_cookie_params(0, '/', '', true, true);
78 /* Create signon session */
79 $session_name = 'SignonSession';
80 session_name($session_name);
83 // Determine realm and return_to
85 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
88 $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
91 $returnTo = $base . dirname($_SERVER['PHP_SELF']);
92 if ($returnTo[mb_strlen($returnTo) - 1] != '/') {
95 $returnTo .= 'openid.php';
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" />
110 /* Grab identifier */
111 if (isset($_POST['identifier']) && is_string($_POST['identifier'])) {
112 $identifier = $_POST['identifier'];
113 } else if (isset($_SESSION['identifier']) && is_string($_SESSION['identifier'])) {
114 $identifier = $_SESSION['identifier'];
119 /* Create OpenID object */
121 $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
122 } catch (Exception
$e) {
126 /* Redirect to OpenID provider */
127 if (isset($_POST['start'])) {
129 $authRequest = $o->prepare();
130 } catch (Exception
$e) {
134 $url = $authRequest->getAuthorizeURL();
136 header("Location: $url");
139 /* Grab query string */
140 if (!count($_POST)) {
141 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
143 // I hate php sometimes
144 $queryString = file_get_contents('php://input');
149 $message = new OpenID_Message($queryString, OpenID_Message
::FORMAT_HTTP
);
150 } catch (Exception
$e) {
154 $id = $message->get('openid.claimed_id');
156 if (!empty($id) && isset($AUTH_MAP[$id])) {
157 $_SESSION['PMA_single_signon_user'] = $AUTH_MAP[$id]['user'];
158 $_SESSION['PMA_single_signon_password'] = $AUTH_MAP[$id]['password'];
159 session_write_close();
160 /* Redirect to phpMyAdmin (should use absolute URL here!) */
161 header('Location: ../index.php');
163 Show_page('<p>User not allowed!</p>');