| 1 |
<?php |
| 2 |
/** |
| 3 |
* Abstract class for defining Two-Factor Authentication providers. |
| 4 |
* |
| 5 |
* @package WP_Defender\Component\Two_Factor |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component\Two_Factor; |
| 9 |
|
| 10 |
use WP_User; |
| 11 |
use Calotes\Base\Component; |
| 12 |
use WP_Defender\Controller\Two_Factor; |
| 13 |
use WP_Defender\Component\Two_Fa as Two_Fa_Component; |
| 14 |
use WP_Defender\Model\Setting\Two_Fa as Two_Fa_Model; |
| 15 |
|
| 16 |
/** |
| 17 |
* Abstract class for defining two-factor authentication. |
| 18 |
*/ |
| 19 |
abstract class Two_Factor_Provider extends Component { |
| 20 |
|
| 21 |
/** |
| 22 |
* Unique identifier for the two-factor provider. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
protected static $slug; |
| 27 |
|
| 28 |
/** |
| 29 |
* Use for cache. |
| 30 |
* |
| 31 |
* @var Two_Fa_Model |
| 32 |
*/ |
| 33 |
public $model; |
| 34 |
|
| 35 |
/** |
| 36 |
* Retrieves the label of the two-factor provider. |
| 37 |
* |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
abstract public function get_label(); |
| 41 |
|
| 42 |
/** |
| 43 |
* Retrieves the label used during the login process. |
| 44 |
* |
| 45 |
* @return string |
| 46 |
*/ |
| 47 |
abstract public function get_login_label(); |
| 48 |
|
| 49 |
/** |
| 50 |
* Retrieves the label for the user interface. |
| 51 |
* |
| 52 |
* @return string |
| 53 |
* @since 2.8.1 |
| 54 |
*/ |
| 55 |
abstract public function get_user_label(); |
| 56 |
|
| 57 |
/** |
| 58 |
* Retrieves the description of the two-factor provider. |
| 59 |
* |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
abstract public function get_description(); |
| 63 |
|
| 64 |
/** |
| 65 |
* Checks if the provider is available for a specific user. |
| 66 |
* |
| 67 |
* @param WP_User $user The user object to check availability against. |
| 68 |
* |
| 69 |
* @return boolean True if available, false otherwise. |
| 70 |
*/ |
| 71 |
abstract public function is_available_for_user( WP_User $user ); |
| 72 |
|
| 73 |
/** |
| 74 |
* Outputs the form needed for authentication with this provider. |
| 75 |
*/ |
| 76 |
abstract public function authentication_form(); |
| 77 |
|
| 78 |
/** |
| 79 |
* Validates the authentication input from the user. |
| 80 |
* |
| 81 |
* @param WP_User $user The user object that is being authenticated. |
| 82 |
*/ |
| 83 |
abstract public function validate_authentication( WP_User $user ); |
| 84 |
|
| 85 |
/** |
| 86 |
* Safe way to get cached model. |
| 87 |
* |
| 88 |
* @return Two_Fa_Model |
| 89 |
*/ |
| 90 |
protected function get_model(): Two_Fa_Model { |
| 91 |
if ( is_object( $this->model ) ) { |
| 92 |
return $this->model; |
| 93 |
} |
| 94 |
$this->model = wd_di()->get( Two_Fa_Model::class ); |
| 95 |
|
| 96 |
return $this->model; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Retrieves the controller instance for two-factor authentication. |
| 101 |
* |
| 102 |
* @return Two_Factor The controller instance. |
| 103 |
*/ |
| 104 |
protected function get_controller(): Two_Factor { |
| 105 |
return wd_di()->get( Two_Factor::class ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Retrieves the component instance for two-factor authentication. |
| 110 |
* |
| 111 |
* @return Two_Fa_Component The component instance. |
| 112 |
*/ |
| 113 |
protected function get_component(): Two_Fa_Component { |
| 114 |
return wd_di()->get( Two_Fa_Component::class ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Constructs a URL for a specific action related to two-factor authentication. |
| 119 |
* |
| 120 |
* @param string $action The action to construct the URL for. |
| 121 |
* |
| 122 |
* @return string The constructed URL. |
| 123 |
*/ |
| 124 |
protected function get_url( $action ): string { |
| 125 |
$controller = $this->get_controller(); |
| 126 |
$routes = $controller->dump_routes_and_nonces(); |
| 127 |
|
| 128 |
return admin_url( 'admin-ajax.php' ) . sprintf( |
| 129 |
'?action=%s&route=%s&_def_nonce=%s', |
| 130 |
defender_base_action(), |
| 131 |
$controller->check_route( $routes['routes'][ $action ] ), |
| 132 |
$routes['nonces'][ $action ] |
| 133 |
); |
| 134 |
} |
| 135 |
} |
| 136 |
|