Application.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Second authentication factor handling
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin\Plugins\TwoFactor;
  9. use PhpMyAdmin\TwoFactor;
  10. use PhpMyAdmin\Template;
  11. use PhpMyAdmin\Plugins\TwoFactorPlugin;
  12. use PragmaRX\Google2FA\Google2FA;
  13. /**
  14. * HOTP and TOTP based two-factor authentication
  15. *
  16. * Also known as Google, Authy, or OTP
  17. */
  18. class Application extends TwoFactorPlugin
  19. {
  20. /**
  21. * @var string
  22. */
  23. public static $id = 'application';
  24. protected $_google2fa;
  25. /**
  26. * Creates object
  27. *
  28. * @param TwoFactor $twofactor TwoFactor instance
  29. */
  30. public function __construct(TwoFactor $twofactor)
  31. {
  32. parent::__construct($twofactor);
  33. $this->_google2fa = new Google2FA();
  34. $this->_google2fa->setWindow(8);
  35. if (!isset($this->_twofactor->config['settings']['secret'])) {
  36. $this->_twofactor->config['settings']['secret'] = '';
  37. }
  38. }
  39. /**
  40. * Get any property of this class
  41. *
  42. * @param string $property name of the property
  43. *
  44. * @return mixed|void if property exist, value of the relevant property
  45. */
  46. public function __get($property)
  47. {
  48. switch ($property) {
  49. case 'google2fa':
  50. return $this->_google2fa;
  51. }
  52. }
  53. /**
  54. * Checks authentication, returns true on success
  55. *
  56. * @return boolean
  57. */
  58. public function check()
  59. {
  60. $this->_provided = false;
  61. if (!isset($_POST['2fa_code'])) {
  62. return false;
  63. }
  64. $this->_provided = true;
  65. return $this->_google2fa->verifyKey(
  66. $this->_twofactor->config['settings']['secret'], $_POST['2fa_code']
  67. );
  68. }
  69. /**
  70. * Renders user interface to enter two-factor authentication
  71. *
  72. * @return string HTML code
  73. */
  74. public function render()
  75. {
  76. return Template::get('login/twofactor/application')->render();
  77. }
  78. /**
  79. * Renders user interface to configure two-factor authentication
  80. *
  81. * @return string HTML code
  82. */
  83. public function setup()
  84. {
  85. $secret = $this->_twofactor->config['settings']['secret'];
  86. $renderArray = ['secret' => $secret];
  87. if (extension_loaded('gd')) {
  88. $inlineUrl = $this->_google2fa->getQRCodeInline(
  89. 'phpMyAdmin (' . $this->getAppId(false) . ')',
  90. $this->_twofactor->user,
  91. $secret
  92. );
  93. $renderArray['image'] = $inlineUrl;
  94. } else {
  95. $inlineUrl = $this->_google2fa->getQRCodeUrl(
  96. 'phpMyAdmin (' . $this->getAppId(false) . ')',
  97. $this->_twofactor->user,
  98. $secret
  99. );
  100. trigger_error(
  101. __(
  102. 'The gd PHP extension was not found.'
  103. . ' The QRcode can not be displayed without the gd PHP extension.'
  104. ),
  105. E_USER_WARNING
  106. );
  107. $renderArray['url'] = $inlineUrl;
  108. }
  109. return Template::get('login/twofactor/application_configure')->render($renderArray);
  110. }
  111. /**
  112. * Performs backend configuration
  113. *
  114. * @return boolean
  115. */
  116. public function configure()
  117. {
  118. if (! isset($_SESSION['2fa_application_key'])) {
  119. $_SESSION['2fa_application_key'] = $this->_google2fa->generateSecretKey();
  120. }
  121. $this->_twofactor->config['settings']['secret'] = $_SESSION['2fa_application_key'];
  122. $result = $this->check();
  123. if ($result) {
  124. unset($_SESSION['2fa_application_key']);
  125. }
  126. return $result;
  127. }
  128. /**
  129. * Get user visible name
  130. *
  131. * @return string
  132. */
  133. public static function getName()
  134. {
  135. return __('Authentication Application (2FA)');
  136. }
  137. /**
  138. * Get user visible description
  139. *
  140. * @return string
  141. */
  142. public static function getDescription()
  143. {
  144. return __('Provides authentication using HOTP and TOTP applications such as FreeOTP, Google Authenticator or Authy.');
  145. }
  146. }