AuthenticationSignon.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /**
  3. * SignOn Authentication plugin for phpMyAdmin
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Auth;
  7. use PhpMyAdmin\Core;
  8. use PhpMyAdmin\Plugins\AuthenticationPlugin;
  9. use PhpMyAdmin\Util;
  10. use const PHP_VERSION;
  11. use function array_merge;
  12. use function defined;
  13. use function file_exists;
  14. use function in_array;
  15. use function session_get_cookie_params;
  16. use function session_id;
  17. use function session_name;
  18. use function session_set_cookie_params;
  19. use function session_start;
  20. use function session_write_close;
  21. use function version_compare;
  22. /**
  23. * Handles the SignOn authentication method
  24. */
  25. class AuthenticationSignon extends AuthenticationPlugin
  26. {
  27. /**
  28. * Displays authentication form
  29. *
  30. * @return bool always true (no return indeed)
  31. */
  32. public function showLoginForm()
  33. {
  34. unset($_SESSION['LAST_SIGNON_URL']);
  35. if (empty($GLOBALS['cfg']['Server']['SignonURL'])) {
  36. Core::fatalError('You must set SignonURL!');
  37. } else {
  38. Core::sendHeaderLocation($GLOBALS['cfg']['Server']['SignonURL']);
  39. }
  40. if (! defined('TESTSUITE')) {
  41. exit;
  42. }
  43. return false;
  44. }
  45. /**
  46. * Set cookie params
  47. *
  48. * @param array $sessionCookieParams The cookie params
  49. */
  50. public function setCookieParams(?array $sessionCookieParams = null): void
  51. {
  52. /* Session cookie params from config */
  53. if ($sessionCookieParams === null) {
  54. $sessionCookieParams = (array) $GLOBALS['cfg']['Server']['SignonCookieParams'];
  55. }
  56. /* Sanitize cookie params */
  57. $defaultCookieParams = static function (string $key) {
  58. switch ($key) {
  59. case 'lifetime':
  60. return 0;
  61. case 'path':
  62. return '/';
  63. case 'domain':
  64. return '';
  65. case 'secure':
  66. return false;
  67. case 'httponly':
  68. return false;
  69. }
  70. return null;
  71. };
  72. foreach (['lifetime', 'path', 'domain', 'secure', 'httponly'] as $key) {
  73. if (isset($sessionCookieParams[$key])) {
  74. continue;
  75. }
  76. $sessionCookieParams[$key] = $defaultCookieParams($key);
  77. }
  78. if (isset($sessionCookieParams['samesite'])
  79. && ! in_array($sessionCookieParams['samesite'], ['Lax', 'Strict'])
  80. ) {
  81. // Not a valid value for samesite
  82. unset($sessionCookieParams['samesite']);
  83. }
  84. if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
  85. session_set_cookie_params($sessionCookieParams);
  86. } else {
  87. session_set_cookie_params(
  88. $sessionCookieParams['lifetime'],
  89. $sessionCookieParams['path'],
  90. $sessionCookieParams['domain'],
  91. $sessionCookieParams['secure'],
  92. $sessionCookieParams['httponly']
  93. );
  94. }
  95. }
  96. /**
  97. * Gets authentication credentials
  98. *
  99. * @return bool whether we get authentication settings or not
  100. */
  101. public function readCredentials()
  102. {
  103. /* Check if we're using same signon server */
  104. $signon_url = $GLOBALS['cfg']['Server']['SignonURL'];
  105. if (isset($_SESSION['LAST_SIGNON_URL'])
  106. && $_SESSION['LAST_SIGNON_URL'] != $signon_url
  107. ) {
  108. return false;
  109. }
  110. /* Script name */
  111. $script_name = $GLOBALS['cfg']['Server']['SignonScript'];
  112. /* Session name */
  113. $session_name = $GLOBALS['cfg']['Server']['SignonSession'];
  114. /* Login URL */
  115. $signon_url = $GLOBALS['cfg']['Server']['SignonURL'];
  116. /* Current host */
  117. $single_signon_host = $GLOBALS['cfg']['Server']['host'];
  118. /* Current port */
  119. $single_signon_port = $GLOBALS['cfg']['Server']['port'];
  120. /* No configuration updates */
  121. $single_signon_cfgupdate = [];
  122. /* Handle script based auth */
  123. if (! empty($script_name)) {
  124. if (! @file_exists($script_name)) {
  125. Core::fatalError(
  126. __('Can not find signon authentication script:')
  127. . ' ' . $script_name
  128. );
  129. }
  130. include $script_name;
  131. [$this->user, $this->password]
  132. = get_login_credentials($GLOBALS['cfg']['Server']['user']);
  133. } elseif (isset($_COOKIE[$session_name])) { /* Does session exist? */
  134. /* End current session */
  135. $old_session = session_name();
  136. $old_id = session_id();
  137. $oldCookieParams = session_get_cookie_params();
  138. if (! defined('TESTSUITE')) {
  139. session_write_close();
  140. }
  141. /* Load single signon session */
  142. if (! defined('TESTSUITE')) {
  143. $this->setCookieParams();
  144. session_name($session_name);
  145. session_id($_COOKIE[$session_name]);
  146. session_start();
  147. }
  148. /* Clear error message */
  149. unset($_SESSION['PMA_single_signon_error_message']);
  150. /* Grab credentials if they exist */
  151. if (isset($_SESSION['PMA_single_signon_user'])) {
  152. $this->user = $_SESSION['PMA_single_signon_user'];
  153. }
  154. if (isset($_SESSION['PMA_single_signon_password'])) {
  155. $this->password = $_SESSION['PMA_single_signon_password'];
  156. }
  157. if (isset($_SESSION['PMA_single_signon_host'])) {
  158. $single_signon_host = $_SESSION['PMA_single_signon_host'];
  159. }
  160. if (isset($_SESSION['PMA_single_signon_port'])) {
  161. $single_signon_port = $_SESSION['PMA_single_signon_port'];
  162. }
  163. if (isset($_SESSION['PMA_single_signon_cfgupdate'])) {
  164. $single_signon_cfgupdate = $_SESSION['PMA_single_signon_cfgupdate'];
  165. }
  166. /* Also get token as it is needed to access subpages */
  167. if (isset($_SESSION['PMA_single_signon_token'])) {
  168. /* No need to care about token on logout */
  169. $pma_token = $_SESSION['PMA_single_signon_token'];
  170. }
  171. $HMACSecret = Util::generateRandom(16);
  172. if (isset($_SESSION['PMA_single_signon_HMAC_secret'])) {
  173. $HMACSecret = $_SESSION['PMA_single_signon_HMAC_secret'];
  174. }
  175. /* End single signon session */
  176. if (! defined('TESTSUITE')) {
  177. session_write_close();
  178. }
  179. /* Restart phpMyAdmin session */
  180. if (! defined('TESTSUITE')) {
  181. $this->setCookieParams($oldCookieParams);
  182. if ($old_session !== null) {
  183. session_name($old_session);
  184. }
  185. if (! empty($old_id)) {
  186. session_id($old_id);
  187. }
  188. session_start();
  189. }
  190. /* Set the single signon host */
  191. $GLOBALS['cfg']['Server']['host'] = $single_signon_host;
  192. /* Set the single signon port */
  193. $GLOBALS['cfg']['Server']['port'] = $single_signon_port;
  194. /* Configuration update */
  195. $GLOBALS['cfg']['Server'] = array_merge(
  196. $GLOBALS['cfg']['Server'],
  197. $single_signon_cfgupdate
  198. );
  199. /* Restore our token */
  200. if (! empty($pma_token)) {
  201. $_SESSION[' PMA_token '] = $pma_token;
  202. $_SESSION[' HMAC_secret '] = $HMACSecret;
  203. }
  204. /**
  205. * Clear user cache.
  206. */
  207. Util::clearUserCache();
  208. }
  209. // Returns whether we get authentication settings or not
  210. if (empty($this->user)) {
  211. unset($_SESSION['LAST_SIGNON_URL']);
  212. return false;
  213. }
  214. $_SESSION['LAST_SIGNON_URL'] = $GLOBALS['cfg']['Server']['SignonURL'];
  215. return true;
  216. }
  217. /**
  218. * User is not allowed to login to MySQL -> authentication failed
  219. *
  220. * @param string $failure String describing why authentication has failed
  221. *
  222. * @return void
  223. */
  224. public function showFailure($failure)
  225. {
  226. parent::showFailure($failure);
  227. /* Session name */
  228. $session_name = $GLOBALS['cfg']['Server']['SignonSession'];
  229. /* Does session exist? */
  230. if (isset($_COOKIE[$session_name])) {
  231. if (! defined('TESTSUITE')) {
  232. /* End current session */
  233. session_write_close();
  234. /* Load single signon session */
  235. $this->setCookieParams();
  236. session_name($session_name);
  237. session_id($_COOKIE[$session_name]);
  238. session_start();
  239. }
  240. /* Set error message */
  241. $_SESSION['PMA_single_signon_error_message'] = $this->getErrorMessage($failure);
  242. }
  243. $this->showLoginForm();
  244. }
  245. /**
  246. * Returns URL for login form.
  247. *
  248. * @return string
  249. */
  250. public function getLoginFormURL()
  251. {
  252. return $GLOBALS['cfg']['Server']['SignonURL'];
  253. }
  254. }