| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Component\Two_Factor\Providers; |
| 4 |
|
| 5 |
use Calotes\Helper\HTTP; |
| 6 |
use WP_Defender\Component\Error_Code; |
| 7 |
use WP_Defender\Component\Two_Factor\Two_Factor_Provider; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Fallback_Email |
| 11 |
* @since 2.8.0 |
| 12 |
* @package WP_Defender\Component\Two_Factor\Providers |
| 13 |
*/ |
| 14 |
class Fallback_Email extends Two_Factor_Provider { |
| 15 |
static $slug = 'fallback-email'; |
| 16 |
|
| 17 |
/** |
| 18 |
* @type string |
| 19 |
*/ |
| 20 |
public const FALLBACK_EMAIL_KEY = 'defenderAuthEmail'; |
| 21 |
|
| 22 |
public function __construct() { |
| 23 |
add_action( 'wd_2fa_user_options_' . self::$slug, array( $this, 'user_options' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Get the name of the provider. |
| 28 |
* |
| 29 |
* @return string |
| 30 |
*/ |
| 31 |
public function get_label() { |
| 32 |
return __( 'Fallback Email', 'wpdef' ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function get_login_label() { |
| 39 |
return $this->get_label(); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @return string |
| 44 |
*/ |
| 45 |
public function get_user_label() { |
| 46 |
return __( 'Fallback email', 'wpdef' ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get the desc of the provider. |
| 51 |
* |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
public function get_description() { |
| 55 |
return __( 'If you ever lose your device, you can send a fallback passcode to this email address.', 'wpdef' ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Display auth method. |
| 60 |
* |
| 61 |
* @param WP_User $user |
| 62 |
*/ |
| 63 |
public function user_options( $user ) { |
| 64 |
$service = $this->get_component(); |
| 65 |
$class = $service->is_checked_enabled_provider_by_slug( $user, self::$slug ) ? '' : 'hidden'; |
| 66 |
$this->get_controller()->render_partial( |
| 67 |
'two-fa/providers/fallback-email', |
| 68 |
array( |
| 69 |
'class' => $class, |
| 70 |
'backup_email' => self::get_backup_email( $user->ID ), |
| 71 |
) |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Whether this 2FA provider is configured and available for the user specified. |
| 77 |
* |
| 78 |
* @param WP_User $user WP_User object of the logged-in user. |
| 79 |
* |
| 80 |
* @return boolean |
| 81 |
*/ |
| 82 |
public function is_available_for_user( $user ) { |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
public function authentication_form() { |
| 87 |
?> |
| 88 |
<p class="wpdef-2fa-label"><?php echo $this->get_label(); ?></p> |
| 89 |
<p class="wpdef-2fa-text"> |
| 90 |
<?php echo __( "Can't access your device? We've sent a passcode to your fallback email address. Enter it below to log in to your account.", 'wpdef' ); ?> |
| 91 |
</p> |
| 92 |
<input type="text" autofocus value="" autocomplete="off" name="otp" /> |
| 93 |
<button class="button button-primary float-r" type="submit"><?php _e( 'Authenticate', 'wpdef' ) ?></button> |
| 94 |
<p class="wpdef-2fa-email-resend"> |
| 95 |
<input type="submit" class="button" name="button_resend_code" value="<?php esc_attr_e( 'Resend Code', 'wpdef' ); ?>" /> |
| 96 |
</p> |
| 97 |
<?php |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @param $user_id |
| 102 |
* |
| 103 |
* @return bool|string |
| 104 |
*/ |
| 105 |
public static function get_backup_email( $user_id = null ) { |
| 106 |
if ( is_null( $user_id ) ) { |
| 107 |
$user_id = get_current_user_id(); |
| 108 |
} |
| 109 |
$email = get_user_meta( $user_id, self::FALLBACK_EMAIL_KEY, true ); |
| 110 |
if ( empty( $email ) ) { |
| 111 |
$user = get_user_by( 'id', $user_id ); |
| 112 |
if ( ! is_object( $user ) ) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
$email = $user->user_email; |
| 116 |
} |
| 117 |
|
| 118 |
return $email; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Whether this 2FA provider is configured and available for the user specified. |
| 123 |
* |
| 124 |
* @param WP_User $user |
| 125 |
* |
| 126 |
* @return bool|\WP_Error |
| 127 |
*/ |
| 128 |
public function validate_authentication( $user ) { |
| 129 |
$otp = HTTP::post( 'otp' ); |
| 130 |
$backup_code = get_user_meta( $user->ID, 'defenderBackupCode', true ); |
| 131 |
if ( ! empty( $backup_code ) && $backup_code['code'] === $otp && strtotime( |
| 132 |
'+3 minutes', |
| 133 |
$backup_code['time'] |
| 134 |
) > time() ) { |
| 135 |
delete_user_meta( $user->ID, 'defenderBackupCode' ); |
| 136 |
|
| 137 |
return true; |
| 138 |
} else { |
| 139 |
return new \WP_Error( 'opt_fail', __( 'ERROR: Invalid passcode.', 'wpdef' ) ); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|