TwoFactor.php 7.1 KB

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