TwoFactorPlugin.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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\Plugins;
  10. use PhpMyAdmin\Config;
  11. use PhpMyAdmin\Core;
  12. use PhpMyAdmin\Message;
  13. use PhpMyAdmin\Template;
  14. use PhpMyAdmin\TwoFactor;
  15. /**
  16. * Two factor authentication plugin class
  17. *
  18. * This is basic implementation which does no
  19. * additional authentication, subclasses are expected
  20. * to implement this.
  21. *
  22. * @package PhpMyAdmin
  23. */
  24. class TwoFactorPlugin
  25. {
  26. /**
  27. * @var string
  28. */
  29. public static $id = '';
  30. /**
  31. * Whether to show submit button in form
  32. */
  33. public static $showSubmit = true;
  34. /**
  35. * @var TwoFactor
  36. */
  37. protected $_twofactor;
  38. /**
  39. * @var boolean
  40. */
  41. protected $_provided;
  42. /**
  43. * @var string
  44. */
  45. protected $_message;
  46. /**
  47. * @var Template
  48. */
  49. public $template;
  50. /**
  51. * Creates object
  52. *
  53. * @param TwoFactor $twofactor TwoFactor instance
  54. */
  55. public function __construct(TwoFactor $twofactor)
  56. {
  57. $this->_twofactor = $twofactor;
  58. $this->_provided = false;
  59. $this->_message = '';
  60. $this->template = new Template();
  61. }
  62. /**
  63. * Returns authentication error message
  64. *
  65. * @return string
  66. */
  67. public function getError()
  68. {
  69. if ($this->_provided) {
  70. if (! empty($this->_message)) {
  71. return Message::rawError(
  72. sprintf(__('Two-factor authentication failed: %s'), $this->_message)
  73. )->getDisplay();
  74. }
  75. return Message::rawError(
  76. __('Two-factor authentication failed.')
  77. )->getDisplay();
  78. }
  79. return '';
  80. }
  81. /**
  82. * Checks authentication, returns true on success
  83. *
  84. * @return boolean
  85. */
  86. public function check()
  87. {
  88. return true;
  89. }
  90. /**
  91. * Renders user interface to enter two-factor authentication
  92. *
  93. * @return string HTML code
  94. */
  95. public function render()
  96. {
  97. return '';
  98. }
  99. /**
  100. * Renders user interface to configure two-factor authentication
  101. *
  102. * @return string HTML code
  103. */
  104. public function setup()
  105. {
  106. return '';
  107. }
  108. /**
  109. * Performs backend configuration
  110. *
  111. * @return boolean
  112. */
  113. public function configure()
  114. {
  115. return true;
  116. }
  117. /**
  118. * Get user visible name
  119. *
  120. * @return string
  121. */
  122. public static function getName()
  123. {
  124. return __('No Two-Factor Authentication');
  125. }
  126. /**
  127. * Get user visible description
  128. *
  129. * @return string
  130. */
  131. public static function getDescription()
  132. {
  133. return __('Login using password only.');
  134. }
  135. /**
  136. * Return an applicaiton ID
  137. *
  138. * Either hostname or hostname with scheme.
  139. *
  140. * @param boolean $return_url Whether to generate URL
  141. *
  142. * @return string
  143. */
  144. public function getAppId($return_url)
  145. {
  146. /** @var Config $PMA_Config */
  147. global $PMA_Config;
  148. $url = $PMA_Config->get('PmaAbsoluteUri');
  149. $parsed = [];
  150. if (! empty($url)) {
  151. $parsed = parse_url($url);
  152. }
  153. if (empty($parsed['scheme'])) {
  154. $parsed['scheme'] = $PMA_Config->isHttps() ? 'https' : 'http';
  155. }
  156. if (empty($parsed['host'])) {
  157. $parsed['host'] = Core::getenv('HTTP_HOST');
  158. }
  159. if ($return_url) {
  160. return $parsed['scheme'] . '://' . $parsed['host'] . (! empty($parsed['port']) ? ':' . $parsed['port'] : '');
  161. } else {
  162. return $parsed['host'];
  163. }
  164. }
  165. }