Translated using Weblate (Ukrainian)
[phpmyadmin.git] / examples / openid.php
blobbfe30da397c5e4bb7e6cd627e1a0a4cb575824f7
1 <?php
2 /**
3 * Single signon for phpMyAdmin using OpenID
5 * This is just example how to use single signon with phpMyAdmin, it is
6 * not intended to be perfect code and look, only shows how you can
7 * integrate this functionality in your application.
9 * It uses OpenID pear package, see https://pear.php.net/package/OpenID
11 * User first authenticates using OpenID and based on content of $AUTH_MAP
12 * the login information is passed to phpMyAdmin in session data.
15 declare(strict_types=1);
17 if (false === @include_once 'OpenID/RelyingParty.php') {
18 exit;
21 /* Change this to true if using phpMyAdmin over https */
22 $secureCookie = false;
24 /**
25 * Map of authenticated users to MySQL user/password pairs.
27 $authMap = ['https://launchpad.net/~username' => ['user' => 'root', 'password' => '']];
29 // phpcs:disable PSR1.Files.SideEffects,Squiz.Functions.GlobalFunction
31 /**
32 * Simple function to show HTML page with given content.
34 * @param string $contents Content to include in page
36 function Show_page(string $contents): void
38 header('Content-Type: text/html; charset=utf-8');
40 echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
41 echo '<!DOCTYPE HTML>
42 <html lang="en" dir="ltr">
43 <head>
44 <link rel="icon" href="../favicon.ico" type="image/x-icon">
45 <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
46 <meta charset="utf-8">
47 <title>phpMyAdmin OpenID signon example</title>
48 </head>
49 <body>';
51 if (isset($_SESSION['PMA_single_signon_error_message'])) {
52 echo '<p class="error">' . $_SESSION['PMA_single_signon_message'] . '</p>';
53 unset($_SESSION['PMA_single_signon_message']);
56 echo $contents;
57 echo '</body></html>';
60 /**
61 * Display error and exit
63 * @param Exception $e Exception object
65 function Die_error(Throwable $e): void
67 $contents = "<div class='relyingparty_results'>\n";
68 $contents .= '<pre>' . htmlspecialchars($e->getMessage()) . "</pre>\n";
69 $contents .= "</div class='relyingparty_results'>";
70 Show_page($contents);
71 exit;
74 // phpcs:enable
76 /* Need to have cookie visible from parent directory */
77 session_set_cookie_params(0, '/', '', $secureCookie, true);
78 /* Create signon session */
79 $sessionName = 'SignonSession';
80 session_name($sessionName);
81 @session_start();
83 // Determine realm and return_to
84 $base = 'http';
85 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
86 $base .= 's';
89 $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
91 $realm = $base . '/';
92 $returnTo = $base . dirname($_SERVER['PHP_SELF']);
93 if ($returnTo[strlen($returnTo) - 1] !== '/') {
94 $returnTo .= '/';
97 $returnTo .= 'openid.php';
99 /* Display form */
100 if ((! count($_GET) && ! count($_POST)) || isset($_GET['phpMyAdmin'])) {
101 /* Show simple form */
102 $content = '<form action="openid.php" method="post">
103 OpenID: <input type="text" name="identifier"><br>
104 <input type="submit" name="start">
105 </form>';
106 Show_page($content);
107 exit;
110 /* Grab identifier */
111 $identifier = null;
112 if (isset($_POST['identifier']) && is_string($_POST['identifier'])) {
113 $identifier = $_POST['identifier'];
114 } elseif (isset($_SESSION['identifier']) && is_string($_SESSION['identifier'])) {
115 $identifier = $_SESSION['identifier'];
118 /* Create OpenID object */
119 try {
120 $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
121 } catch (Throwable $e) {
122 Die_error($e);
125 /* Redirect to OpenID provider */
126 if (isset($_POST['start'])) {
127 try {
128 $authRequest = $o->prepare();
129 } catch (Throwable $e) {
130 Die_error($e);
133 $url = $authRequest->getAuthorizeURL();
135 header('Location: ' . $url);
136 exit;
139 /* Grab query string */
140 if (! count($_POST)) {
141 [, $queryString] = explode('?', $_SERVER['REQUEST_URI']);
142 } else {
143 // Fetch the raw query body
144 $queryString = file_get_contents('php://input');
147 /* Check reply */
148 try {
149 $message = new OpenID_Message($queryString, OpenID_Message::FORMAT_HTTP);
150 } catch (Throwable $e) {
151 Die_error($e);
154 $id = $message->get('openid.claimed_id');
156 if (empty($id) || ! isset($authMap[$id])) {
157 Show_page('<p>User not allowed!</p>');
158 exit;
161 $_SESSION['PMA_single_signon_user'] = $authMap[$id]['user'];
162 $_SESSION['PMA_single_signon_password'] = $authMap[$id]['password'];
163 $_SESSION['PMA_single_signon_HMAC_secret'] = hash('sha1', uniqid(strval(random_int(0, mt_getrandmax())), true));
164 session_write_close();
165 /* Redirect to phpMyAdmin (should use absolute URL here!) */
166 header('Location: ../index.php');