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 require_once 'OpenID/RelyingParty.php';
22 * Map of authenticated users to MySQL user/password pairs.
25 'http://launchpad.net/~username' => array(
32 * Simple function to show HTML page with given content.
34 function show_page($contents) {
35 header('Content-Type: text/html; charset=utf-8');
36 echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
38 <!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
39 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
40 <html xmlns
="http://www.w3.org/1999/xhtml" xml
:lang
="en" lang
="en" dir
="ltr">
42 <link rel
="icon" href
="../favicon.ico" type
="image/x-icon" />
43 <link rel
="shortcut icon" href
="../favicon.ico" type
="image/x-icon" />
44 <title
>phpMyAdmin OpenID signon example
</title
>
48 if (isset($_SESSION) && isset($_SESSION['PMA_single_signon_error_message'])) {
49 echo '<p class="error">' . $_SESSION['PMA_single_signon_message'] . '</p>';
50 unset($_SESSION['PMA_single_signon_message']);
59 /* Need to have cookie visible from parent directory */
60 session_set_cookie_params(0, '/', '', 0);
61 /* Create signon session */
62 $session_name = 'SignonSession';
63 session_name($session_name);
66 // Determine realm and return_to
68 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
71 $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
74 $returnTo = $base . dirname($_SERVER['PHP_SELF']);
75 if ($returnTo[strlen($returnTo) - 1] != '/') {
78 $returnTo .= 'openid.php';
81 if (!count($_GET) && !count($_POST) ||
isset($_GET['phpMyAdmin'])) {
82 /* Show simple form */
83 $content = '<form action="openid.php" method="post">
84 OpenID: <input type="text" name="identifier" /><br />
85 <input type="submit" name="start" />
94 if (isset($_POST['identifier'])) {
95 $identifier = $_POST['identifier'];
96 } else if (isset($_SESSION['identifier'])) {
97 $identifier = $_SESSION['identifier'];
102 /* Create OpenID object */
104 $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
105 } catch (OpenID_Exception
$e) {
106 $contents = "<div class='relyingparty_results'>\n";
107 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
108 $contents .= "</div class='relyingparty_results'>";
109 show_page($contents);
113 /* Redirect to OpenID provider */
114 if (isset($_POST['start'])) {
116 $authRequest = $o->prepare();
117 } catch (OpenID_Exception
$e) {
118 $contents = "<div class='relyingparty_results'>\n";
119 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
120 $contents .= "</div class='relyingparty_results'>";
121 show_page($contents);
125 $url = $authRequest->getAuthorizeURL();
127 header("Location: $url");
130 /* Grab query string */
131 if (!count($_POST)) {
132 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
134 // I hate php sometimes
135 $queryString = file_get_contents('php://input');
139 $message = new OpenID_Message($queryString, OpenID_Message
::FORMAT_HTTP
);
141 $id = $message->get('openid.claimed_id');
143 if (!empty($id) && isset($AUTH_MAP[$id])) {
144 $_SESSION['PMA_single_signon_user'] = $AUTH_MAP[$id]['user'];
145 $_SESSION['PMA_single_signon_password'] = $AUTH_MAP[$id]['password'];
146 session_write_close();
147 /* Redirect to phpMyAdmin (should use absolute URL here!) */
148 header('Location: ../index.php');
150 show_page('<p>User not allowed!</p>');