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 http://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 'http://launchpad.net/~username' => array(
34 * Simple function to show HTML page with given content.
38 function show_page($contents)
40 header('Content-Type: text/html; charset=utf-8');
41 echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
44 <html lang
="en" dir
="ltr">
46 <link rel
="icon" href
="../favicon.ico" type
="image/x-icon" />
47 <link rel
="shortcut icon" href
="../favicon.ico" type
="image/x-icon" />
48 <meta charset
="utf-8" />
49 <title
>phpMyAdmin OpenID signon example
</title
>
53 if (isset($_SESSION) && isset($_SESSION['PMA_single_signon_error_message'])) {
54 echo '<p class="error">' . $_SESSION['PMA_single_signon_message'] . '</p>';
55 unset($_SESSION['PMA_single_signon_message']);
64 /* Need to have cookie visible from parent directory */
65 session_set_cookie_params(0, '/', '', 0);
66 /* Create signon session */
67 $session_name = 'SignonSession';
68 session_name($session_name);
71 // Determine realm and return_to
73 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
76 $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
79 $returnTo = $base . dirname($_SERVER['PHP_SELF']);
80 if ($returnTo[strlen($returnTo) - 1] != '/') {
83 $returnTo .= 'openid.php';
86 if (!count($_GET) && !count($_POST) ||
isset($_GET['phpMyAdmin'])) {
87 /* Show simple form */
88 $content = '<form action="openid.php" method="post">
89 OpenID: <input type="text" name="identifier" /><br />
90 <input type="submit" name="start" />
99 if (isset($_POST['identifier'])) {
100 $identifier = $_POST['identifier'];
101 } else if (isset($_SESSION['identifier'])) {
102 $identifier = $_SESSION['identifier'];
107 /* Create OpenID object */
109 $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
110 } catch (OpenID_Exception
$e) {
111 $contents = "<div class='relyingparty_results'>\n";
112 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
113 $contents .= "</div class='relyingparty_results'>";
114 show_page($contents);
118 /* Redirect to OpenID provider */
119 if (isset($_POST['start'])) {
121 $authRequest = $o->prepare();
122 } catch (OpenID_Exception
$e) {
123 $contents = "<div class='relyingparty_results'>\n";
124 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
125 $contents .= "</div class='relyingparty_results'>";
126 show_page($contents);
130 $url = $authRequest->getAuthorizeURL();
132 header("Location: $url");
135 /* Grab query string */
136 if (!count($_POST)) {
137 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
139 // I hate php sometimes
140 $queryString = file_get_contents('php://input');
144 $message = new OpenID_Message($queryString, OpenID_Message
::FORMAT_HTTP
);
146 $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');
155 show_page('<p>User not allowed!</p>');