TwoFactorPlugin.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Two authentication factor handling
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins;
  7. use PhpMyAdmin\Core;
  8. use PhpMyAdmin\Message;
  9. use PhpMyAdmin\Template;
  10. use PhpMyAdmin\TwoFactor;
  11. use function is_array;
  12. use function parse_url;
  13. use function sprintf;
  14. use function strlen;
  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. class TwoFactorPlugin
  23. {
  24. /** @var string */
  25. public static $id = '';
  26. /**
  27. * Whether to show submit button in form
  28. *
  29. * @var bool
  30. */
  31. public static $showSubmit = true;
  32. /** @var TwoFactor */
  33. protected $twofactor;
  34. /** @var bool */
  35. protected $provided;
  36. /** @var string */
  37. protected $message;
  38. /** @var Template */
  39. public $template;
  40. /**
  41. * Creates object
  42. *
  43. * @param TwoFactor $twofactor TwoFactor instance
  44. */
  45. public function __construct(TwoFactor $twofactor)
  46. {
  47. $this->twofactor = $twofactor;
  48. $this->provided = false;
  49. $this->message = '';
  50. $this->template = new Template();
  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 bool
  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 bool
  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 Authentication');
  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 bool $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. $parsedUrl = parse_url($url);
  141. if (is_array($parsedUrl)) {
  142. $parsed = $parsedUrl;
  143. }
  144. }
  145. if (! isset($parsed['scheme']) || strlen($parsed['scheme']) === 0) {
  146. $parsed['scheme'] = $PMA_Config->isHttps() ? 'https' : 'http';
  147. }
  148. if (! isset($parsed['host']) || strlen($parsed['host']) === 0) {
  149. $parsed['host'] = Core::getenv('HTTP_HOST');
  150. }
  151. if ($return_url) {
  152. $port = '';
  153. if (isset($parsed['port'])) {
  154. $port = ':' . $parsed['port'];
  155. }
  156. return sprintf('%s://%s%s', $parsed['scheme'], $parsed['host'], $port);
  157. }
  158. return $parsed['host'];
  159. }
  160. }