Application.php 4.2 KB

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