AuthenticationSignon.php 8.6 KB

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