phpmyadmin/libraries/classes/Plugins/TwoFactorPlugin.php
Kamil Tekiela 0bdcaba2d2
Add native property types (#18143)
* Add native property types

Includes TypeHints.UnionTypeHintFormat

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Set some default values for properties

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Format and promote properties

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Remove redundant asserts

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Redundant cast

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* $tmanager->theme is never null

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Redundant variable

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Fix empty on $statementInfo bool

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Redundant casts

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Redundant issets

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* getPacked() returns nullable string

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Redundant if

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* $this->content can be null

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Simplify ThemeManager::getInstance()

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Use isset for checking if property is initialized

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Use nullable instead of uninitialized property

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* password is no longer nullable

I can't verify that none of the globals ever tried to set it to null, but the variable should never be nullable.

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Update baselines

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* $same_wide_width param can be float or int

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Update Message.php

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Cast Sub_part to int

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

* Fix one line doc comments

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>

---------

Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
2023-02-26 23:30:41 -03:00

161 lines
3.4 KiB
PHP

<?php
/**
* Two authentication factor handling
*/
declare(strict_types=1);
namespace PhpMyAdmin\Plugins;
use PhpMyAdmin\Core;
use PhpMyAdmin\Message;
use PhpMyAdmin\Template;
use PhpMyAdmin\TwoFactor;
use function __;
use function is_array;
use function parse_url;
use function sprintf;
use function strlen;
/**
* Two factor authentication plugin class
*
* This is basic implementation which does no
* additional authentication, subclasses are expected
* to implement this.
*/
class TwoFactorPlugin
{
public static string $id = '';
/**
* Whether to show submit button in form
*/
public static bool $showSubmit = true;
protected bool $provided = false;
protected string $message = '';
public Template $template;
public function __construct(protected TwoFactor $twofactor)
{
$this->template = new Template();
}
/**
* Returns authentication error message
*/
public function getError(): string
{
if ($this->provided) {
if (! empty($this->message)) {
return Message::rawError(
sprintf(__('Two-factor authentication failed: %s'), $this->message),
)->getDisplay();
}
return Message::rawError(
__('Two-factor authentication failed.'),
)->getDisplay();
}
return '';
}
/**
* Checks authentication, returns true on success
*/
public function check(): bool
{
return true;
}
/**
* Renders user interface to enter two-factor authentication
*
* @return string HTML code
*/
public function render(): string
{
return '';
}
/**
* Renders user interface to configure two-factor authentication
*
* @return string HTML code
*/
public function setup(): string
{
return '';
}
/**
* Performs backend configuration
*/
public function configure(): bool
{
return true;
}
/**
* Get user visible name
*/
public static function getName(): string
{
return __('No Two-Factor Authentication');
}
/**
* Get user visible description
*/
public static function getDescription(): string
{
return __('Login using password only.');
}
/**
* Return an application ID
*
* Either hostname or hostname with scheme.
*
* @param bool $return_url Whether to generate URL
*/
public function getAppId($return_url): string
{
$GLOBALS['config'] ??= null;
$url = $GLOBALS['config']->get('PmaAbsoluteUri');
$parsed = [];
if (! empty($url)) {
$parsedUrl = parse_url($url);
if (is_array($parsedUrl)) {
$parsed = $parsedUrl;
}
}
if (! isset($parsed['scheme']) || strlen($parsed['scheme']) === 0) {
$parsed['scheme'] = $GLOBALS['config']->isHttps() ? 'https' : 'http';
}
if (! isset($parsed['host']) || strlen($parsed['host']) === 0) {
$parsed['host'] = Core::getenv('HTTP_HOST');
}
if ($return_url) {
$port = '';
if (isset($parsed['port'])) {
$port = ':' . $parsed['port'];
}
return sprintf('%s://%s%s', $parsed['scheme'], $parsed['host'], $port);
}
return $parsed['host'];
}
}