TwoFactor.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Two authentication factor handling
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin;
  10. use PhpMyAdmin\Message;
  11. use PhpMyAdmin\Plugins\TwoFactor\Application;
  12. use PhpMyAdmin\Plugins\TwoFactor\Invalid;
  13. use PhpMyAdmin\Plugins\TwoFactor\Key;
  14. use PhpMyAdmin\Plugins\TwoFactorPlugin;
  15. use PhpMyAdmin\UserPreferences;
  16. use PragmaRX\Google2FAQRCode\Google2FA;
  17. use Samyoul\U2F\U2FServer\U2FServer;
  18. /**
  19. * Two factor authentication wrapper class
  20. *
  21. * @package PhpMyAdmin
  22. */
  23. class TwoFactor
  24. {
  25. /**
  26. * @var string
  27. */
  28. public $user;
  29. /**
  30. * @var array
  31. */
  32. public $config;
  33. /**
  34. * @var boolean
  35. */
  36. protected $_writable;
  37. /**
  38. * @var TwoFactorPlugin
  39. */
  40. protected $_backend;
  41. /**
  42. * @var array
  43. */
  44. protected $_available;
  45. /**
  46. * @var UserPreferences
  47. */
  48. private $userPreferences;
  49. /**
  50. * Creates new TwoFactor object
  51. *
  52. * @param string $user User name
  53. */
  54. public function __construct($user)
  55. {
  56. /** @var DatabaseInterface $dbi */
  57. global $dbi;
  58. $dbi->initRelationParamsCache();
  59. $this->userPreferences = new UserPreferences();
  60. $this->user = $user;
  61. $this->_available = $this->getAvailable();
  62. $this->config = $this->readConfig();
  63. $this->_writable = ($this->config['type'] == 'db');
  64. $this->_backend = $this->getBackend();
  65. }
  66. /**
  67. * Reads the configuration
  68. *
  69. * @return array
  70. */
  71. public function readConfig()
  72. {
  73. $result = [];
  74. $config = $this->userPreferences->load();
  75. if (isset($config['config_data']['2fa'])) {
  76. $result = $config['config_data']['2fa'];
  77. }
  78. $result['type'] = $config['type'];
  79. if (! isset($result['backend'])) {
  80. $result['backend'] = '';
  81. }
  82. if (! isset($result['settings'])) {
  83. $result['settings'] = [];
  84. }
  85. return $result;
  86. }
  87. /**
  88. * Get any property of this class
  89. *
  90. * @param string $property name of the property
  91. *
  92. * @return mixed|void if property exist, value of the relevant property
  93. */
  94. public function __get($property)
  95. {
  96. switch ($property) {
  97. case 'backend':
  98. return $this->_backend;
  99. case 'available':
  100. return $this->_available;
  101. case 'writable':
  102. return $this->_writable;
  103. case 'showSubmit':
  104. $backend = $this->_backend;
  105. return $backend::$showSubmit;
  106. }
  107. }
  108. /**
  109. * Returns list of available backends
  110. *
  111. * @return array
  112. */
  113. public function getAvailable()
  114. {
  115. $result = [];
  116. if ($GLOBALS['cfg']['DBG']['simple2fa']) {
  117. $result[] = 'simple';
  118. }
  119. if (class_exists(Google2FA::class)) {
  120. $result[] = 'application';
  121. }
  122. if (class_exists(U2FServer::class)) {
  123. $result[] = 'key';
  124. }
  125. return $result;
  126. }
  127. /**
  128. * Returns list of missing dependencies
  129. *
  130. * @return array
  131. */
  132. public function getMissingDeps()
  133. {
  134. $result = [];
  135. if (! class_exists(Google2FA::class)) {
  136. $result[] = [
  137. 'class' => Application::getName(),
  138. 'dep' => 'pragmarx/google2fa-qrcode',
  139. ];
  140. }
  141. if (! class_exists('BaconQrCode\Renderer\Image\Png')) {
  142. $result[] = [
  143. 'class' => Application::getName(),
  144. 'dep' => 'bacon/bacon-qr-code',
  145. ];
  146. }
  147. if (! class_exists(U2FServer::class)) {
  148. $result[] = [
  149. 'class' => Key::getName(),
  150. 'dep' => 'samyoul/u2f-php-server',
  151. ];
  152. }
  153. return $result;
  154. }
  155. /**
  156. * Returns class name for given name
  157. *
  158. * @param string $name Backend name
  159. *
  160. * @return string
  161. */
  162. public function getBackendClass($name)
  163. {
  164. $result = TwoFactorPlugin::class;
  165. if (in_array($name, $this->_available)) {
  166. $result = 'PhpMyAdmin\\Plugins\\TwoFactor\\' . ucfirst($name);
  167. } elseif (! empty($name)) {
  168. $result = Invalid::class;
  169. }
  170. return $result;
  171. }
  172. /**
  173. * Returns backend for current user
  174. *
  175. * @return TwoFactorPlugin
  176. */
  177. public function getBackend()
  178. {
  179. $name = $this->getBackendClass($this->config['backend']);
  180. return new $name($this);
  181. }
  182. /**
  183. * Checks authentication, returns true on success
  184. *
  185. * @param boolean $skip_session Skip session cache
  186. *
  187. * @return boolean
  188. */
  189. public function check($skip_session = false)
  190. {
  191. if ($skip_session) {
  192. return $this->_backend->check();
  193. }
  194. if (empty($_SESSION['two_factor_check'])) {
  195. $_SESSION['two_factor_check'] = $this->_backend->check();
  196. }
  197. return $_SESSION['two_factor_check'];
  198. }
  199. /**
  200. * Renders user interface to enter two-factor authentication
  201. *
  202. * @return string HTML code
  203. */
  204. public function render()
  205. {
  206. return $this->_backend->getError() . $this->_backend->render();
  207. }
  208. /**
  209. * Renders user interface to configure two-factor authentication
  210. *
  211. * @return string HTML code
  212. */
  213. public function setup()
  214. {
  215. return $this->_backend->getError() . $this->_backend->setup();
  216. }
  217. /**
  218. * Saves current configuration.
  219. *
  220. * @return true|Message
  221. */
  222. public function save()
  223. {
  224. return $this->userPreferences->persistOption('2fa', $this->config, null);
  225. }
  226. /**
  227. * Changes two-factor authentication settings
  228. *
  229. * The object might stay in partialy changed setup
  230. * if configuration fails.
  231. *
  232. * @param string $name Backend name
  233. *
  234. * @return boolean
  235. */
  236. public function configure($name)
  237. {
  238. $this->config = [
  239. 'backend' => $name,
  240. ];
  241. if ($name === '') {
  242. $cls = $this->getBackendClass($name);
  243. $this->config['settings'] = [];
  244. $this->_backend = new $cls($this);
  245. } else {
  246. if (! in_array($name, $this->_available)) {
  247. return false;
  248. }
  249. $cls = $this->getBackendClass($name);
  250. $this->config['settings'] = [];
  251. $this->_backend = new $cls($this);
  252. if (! $this->_backend->configure()) {
  253. return false;
  254. }
  255. }
  256. $result = $this->save();
  257. if ($result !== true) {
  258. $result->display();
  259. }
  260. return true;
  261. }
  262. /**
  263. * Returns array with all available backends
  264. *
  265. * @return array
  266. */
  267. public function getAllBackends()
  268. {
  269. $all = array_merge([''], $this->available);
  270. $backends = [];
  271. foreach ($all as $name) {
  272. $cls = $this->getBackendClass($name);
  273. $backends[] = [
  274. 'id' => $cls::$id,
  275. 'name' => $cls::getName(),
  276. 'description' => $cls::getDescription(),
  277. ];
  278. }
  279. return $backends;
  280. }
  281. }