TwoFactorPlugin.php 3.4 KB

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