class-login-attempts.php
179 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Responsible for the plugin login attempts |
| 4 | * |
| 5 | * @package wp2fa |
| 6 | * @subpackage admin_controllers |
| 7 | * @copyright 2023 WP White Security |
| 8 | * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0 |
| 9 | * @link https://wordpress.org/plugins/wp-2fa/ |
| 10 | */ |
| 11 | |
| 12 | namespace WP2FA\Admin\Controllers; |
| 13 | |
| 14 | use WP2FA\Admin\Helpers\User_Helper; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. |
| 17 | |
| 18 | /** |
| 19 | * Login Attempts class |
| 20 | */ |
| 21 | if ( ! class_exists( '\WP2FA\Admin\Controllers\Login_Attempts' ) ) { |
| 22 | |
| 23 | /** |
| 24 | * Responsible for the login attempts |
| 25 | * |
| 26 | * @since 2.0.0 |
| 27 | */ |
| 28 | class Login_Attempts { |
| 29 | |
| 30 | /** |
| 31 | * Holds the name of the meta key |
| 32 | * |
| 33 | * @var string |
| 34 | * |
| 35 | * @since 2.0.0 |
| 36 | */ |
| 37 | private $meta_key = WP_2FA_PREFIX . 'login-attempts'; |
| 38 | |
| 39 | /** |
| 40 | * Holds the number of allowed attempts to login |
| 41 | * |
| 42 | * @var integer |
| 43 | * |
| 44 | * @since 2.0.0 |
| 45 | */ |
| 46 | private $number_of_allowed_attempts = 3; |
| 47 | |
| 48 | /** |
| 49 | * Default constructor |
| 50 | * |
| 51 | * @param string $meta_key - The meta key name. |
| 52 | * @param integer $attempts - Number of the allowed login attempts. |
| 53 | * |
| 54 | * @since 2.0.0 |
| 55 | */ |
| 56 | public function __construct( string $meta_key = '', int $attempts = 0 ) { |
| 57 | if ( '' !== trim( $meta_key ) ) { |
| 58 | $this->meta_key = $meta_key; |
| 59 | } |
| 60 | if ( 0 !== $attempts ) { |
| 61 | $this->number_of_allowed_attempts = $attempts; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Increasing login attempts for User |
| 67 | * |
| 68 | * @since 2.0.0 |
| 69 | * |
| 70 | * @param \WP_User $user - the WP User. |
| 71 | * |
| 72 | * @return void |
| 73 | */ |
| 74 | public function increase_login_attempts( \WP_User $user ) { |
| 75 | $attempts = $this->get_login_attempts( $user ); |
| 76 | if ( '' === $attempts ) { |
| 77 | $attempts = 0; |
| 78 | } |
| 79 | User_Helper::set_meta( $this->meta_key, ++$attempts, $user ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the number of unsuccessful attempts for the User |
| 84 | * |
| 85 | * @since 2.0.0 |
| 86 | * |
| 87 | * @param \WP_User $user - the WP User. |
| 88 | * |
| 89 | * @return integer |
| 90 | */ |
| 91 | public function get_login_attempts( \WP_User $user ): int { |
| 92 | return (int) User_Helper::get_meta( $this->meta_key, $user ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Clearing login attempts for User |
| 97 | * |
| 98 | * @since 2.0.0 |
| 99 | * |
| 100 | * @param \WP_User $user - the WP User. |
| 101 | * |
| 102 | * @return void |
| 103 | */ |
| 104 | public function clear_login_attempts( \WP_User $user ) { |
| 105 | User_Helper::remove_meta( $this->meta_key, $user ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns the number of allowed login attempts |
| 110 | * |
| 111 | * @return integer |
| 112 | * |
| 113 | * @since 2.0.0 |
| 114 | */ |
| 115 | public function get_allowed_login_attempts(): int { |
| 116 | return $this->number_of_allowed_attempts; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Sets the number of allowed attempts |
| 121 | * |
| 122 | * @param integer $number - The number of the allowed attempts. |
| 123 | * |
| 124 | * @return integer |
| 125 | * |
| 126 | * @since 2.0.0 |
| 127 | */ |
| 128 | public function set_number_of_login_attempts( int $number ): int { |
| 129 | $this->number_of_allowed_attempts = $number; |
| 130 | |
| 131 | return $this->number_of_allowed_attempts; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns the name of the meta key holding the login attempts for the user |
| 136 | * |
| 137 | * @return string |
| 138 | * |
| 139 | * @since 2.0.0 |
| 140 | */ |
| 141 | public function get_meta_key(): string { |
| 142 | |
| 143 | return $this->meta_key; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Sets the login attempts meta key |
| 148 | * |
| 149 | * @param string $meta_key - The name of the meta. |
| 150 | * |
| 151 | * @return string |
| 152 | * |
| 153 | * @since 2.0.0 |
| 154 | */ |
| 155 | public function set_meta_key( string $meta_key ): string { |
| 156 | $this->meta_key = $meta_key; |
| 157 | |
| 158 | return $this->meta_key; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Checks the number of login attempts |
| 163 | * |
| 164 | * @param \WP_User $user - The user we have to check for. |
| 165 | * |
| 166 | * @return boolean |
| 167 | * |
| 168 | * @since 2.0.0 |
| 169 | */ |
| 170 | public function check_number_of_attempts( \WP_User $user ):bool { |
| 171 | if ( $this->get_allowed_login_attempts() < $this->get_login_attempts( $user ) ) { |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | return true; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 |