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