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.
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 /* Need to have cookie visible from parent directory */
67 session_set_cookie_params(0, '/', '', false);
68 /* Create signon session */
69 $session_name = 'SignonSession';
70 session_name($session_name);
73 // Determine realm and return_to
75 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
78 $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
81 $returnTo = $base . dirname($_SERVER['PHP_SELF']);
82 if ($returnTo[/*overload*/mb_strlen($returnTo) - 1] != '/') {
85 $returnTo .= 'openid.php';
88 if (!count($_GET) && !count($_POST) ||
isset($_GET['phpMyAdmin'])) {
89 /* Show simple form */
90 $content = '<form action="openid.php" method="post">
91 OpenID: <input type="text" name="identifier" /><br />
92 <input type="submit" name="start" />
100 /* Grab identifier */
101 if (isset($_POST['identifier'])) {
102 $identifier = $_POST['identifier'];
103 } else if (isset($_SESSION['identifier'])) {
104 $identifier = $_SESSION['identifier'];
109 /* Create OpenID object */
111 $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
112 } catch (OpenID_Exception
$e) {
113 $contents = "<div class='relyingparty_results'>\n";
114 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
115 $contents .= "</div class='relyingparty_results'>";
116 Show_page($contents);
120 /* Redirect to OpenID provider */
121 if (isset($_POST['start'])) {
123 $authRequest = $o->prepare();
124 } catch (OpenID_Exception
$e) {
125 $contents = "<div class='relyingparty_results'>\n";
126 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
127 $contents .= "</div class='relyingparty_results'>";
128 Show_page($contents);
132 $url = $authRequest->getAuthorizeURL();
134 header("Location: $url");
137 /* Grab query string */
138 if (!count($_POST)) {
139 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
141 // I hate php sometimes
142 $queryString = file_get_contents('php://input');
146 $message = new OpenID_Message($queryString, OpenID_Message
::FORMAT_HTTP
);
148 $id = $message->get('openid.claimed_id');
150 if (!empty($id) && isset($AUTH_MAP[$id])) {
151 $_SESSION['PMA_single_signon_user'] = $AUTH_MAP[$id]['user'];
152 $_SESSION['PMA_single_signon_password'] = $AUTH_MAP[$id]['password'];
153 session_write_close();
154 /* Redirect to phpMyAdmin (should use absolute URL here!) */
155 header('Location: ../index.php');
157 Show_page('<p>User not allowed!</p>');