Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account Protection: Add password validation #41401

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
146 commits
Select commit Hold shift + click to select a range
3f56ee7
Add Account Protection toggle to Jetpack security settings
dkmyta Jan 9, 2025
719f950
Import package and run activation/deactivation on module toggle
dkmyta Jan 9, 2025
c83c604
changelog
dkmyta Jan 9, 2025
ab4f99a
Add Protect Settings page and hook up Account Protection toggle
dkmyta Jan 9, 2025
c2e8f1e
changelog
dkmyta Jan 9, 2025
b64fdaf
Update changelog
dkmyta Jan 9, 2025
13949a6
Merge branch 'add/jetpack/account-protection-security-settings' into …
dkmyta Jan 9, 2025
3f68536
Register modules on plugin activation
dkmyta Jan 9, 2025
3628b02
Ensure package is initialized on plugin activation
dkmyta Jan 9, 2025
3f90fe4
Make account protection class init static
dkmyta Jan 9, 2025
911e1db
Merge branch 'add/jetpack/account-protection-security-settings' into …
dkmyta Jan 9, 2025
3bfbcb3
Add auth hooks, redirect and a custom login action template
dkmyta Jan 13, 2025
289dbdb
Reorg, add Password_Detection class
dkmyta Jan 14, 2025
7eabdd3
Remove user cxn req and banner
dkmyta Jan 14, 2025
5a1af0b
Do not enabled module by default
dkmyta Jan 14, 2025
3b35efe
Add strict mode option and settings toggle
dkmyta Jan 15, 2025
4ddb644
changelog
dkmyta Jan 15, 2025
2a0e811
Merge branch 'add/jetpack/account-protection-security-settings' into …
dkmyta Jan 15, 2025
b62811b
Add strict mode toggle
dkmyta Jan 15, 2025
28f5820
Add strict mode toggle and endpoints
dkmyta Jan 15, 2025
da260ea
Rebase
dkmyta Jan 15, 2025
b72e93a
Reorg and add kill switch and is supported check
dkmyta Jan 15, 2025
7fad7f9
Add testing infrastructure
dkmyta Jan 15, 2025
39a28d5
Add email handlings, resend AJAX action, and attempt limitations
dkmyta Jan 16, 2025
da85a18
Add nonces, checks and template error handling
dkmyta Jan 17, 2025
6a43ec0
Use method over template to avoid lint errors
dkmyta Jan 17, 2025
7557056
Improve render_password_detection_template, update SVG file ext
dkmyta Jan 17, 2025
16845a7
Remove template file and include
dkmyta Jan 17, 2025
5f959f1
Prep for validation endpoints
dkmyta Jan 17, 2025
a4ba959
Update classes to be dynamic
dkmyta Jan 17, 2025
992f288
Add constructors
dkmyta Jan 17, 2025
43d4cd0
Reorg user meta methods
dkmyta Jan 17, 2025
3cec891
Add type declarations and hinting
dkmyta Jan 17, 2025
c61877b
Simplify method naming
dkmyta Jan 17, 2025
92d0ea6
Rebase, fix conflicts
dkmyta Jan 20, 2025
7634ed2
Use dynamic classes
dkmyta Jan 20, 2025
692db33
Update class dependencies
dkmyta Jan 20, 2025
22d2678
Fix copy
dkmyta Jan 20, 2025
0fd3e41
Revert unrelated changes
dkmyta Jan 20, 2025
805b367
Rebase, fix conflicts
dkmyta Jan 20, 2025
4383b5e
Revert unrelated changes
dkmyta Jan 20, 2025
9a70647
Fix method calls
dkmyta Jan 20, 2025
969102f
Do not activate by default
dkmyta Jan 20, 2025
8356bd4
Fix phan errors
dkmyta Jan 20, 2025
dad19c7
Merge branch 'add/jetpack/account-protection-security-settings' into …
dkmyta Jan 20, 2025
a5f1467
Rebase, fix conflicts
dkmyta Jan 20, 2025
32f3ef6
Changelog
dkmyta Jan 20, 2025
b02d511
Update composer deps
dkmyta Jan 20, 2025
7c255ac
Update lock files, add constructor method
dkmyta Jan 20, 2025
cdb0ac8
Fix php warning
dkmyta Jan 20, 2025
19efaea
Merge branch 'add/jetpack/account-protection-security-settings' into …
dkmyta Jan 20, 2025
1ce68b9
Update lock file
dkmyta Jan 20, 2025
7a06508
Changelog
dkmyta Jan 20, 2025
ddfa535
Rebase
dkmyta Jan 20, 2025
c128cf5
Fix Password_Detection constructor
dkmyta Jan 20, 2025
7a56b48
Changelog
dkmyta Jan 20, 2025
bc7aa77
More changelogs
dkmyta Jan 20, 2025
b28c8cf
Remove comments
dkmyta Jan 20, 2025
4bb5401
Fix static analysis errors
dkmyta Jan 20, 2025
20dec01
Remove top level phpunit.xml.dist
dkmyta Jan 20, 2025
2bdbf8e
Remove never return type
dkmyta Jan 20, 2025
a80c024
Revert tests dir changes in favour of a dedicated task
dkmyta Jan 20, 2025
f07e52e
Add tests dir
dkmyta Jan 20, 2025
80d0e92
Reapply default test infrastructure
dkmyta Jan 20, 2025
c03d626
Reorg and rename
dkmyta Jan 20, 2025
ae3b6b6
Update @package
dkmyta Jan 20, 2025
30f2329
Use never phpdoc return type as per static analysis error
dkmyta Jan 21, 2025
55ccadb
Merge branch 'add/account-protection' into add/jetpack/account-protec…
dkmyta Jan 21, 2025
3fed240
Merge branch 'add/jetpack/account-protection-security-settings' into …
dkmyta Jan 21, 2025
2beaca5
Merge branch 'add/protect/account-protection-settings' into add/packa…
dkmyta Jan 21, 2025
aad7ff6
Enable module by default
dkmyta Jan 21, 2025
de4fc75
Merge branch 'add/jetpack/account-protection-security-settings' into …
dkmyta Jan 21, 2025
448079b
Enable module by default
dkmyta Jan 21, 2025
bc263e0
Merge branch 'add/protect/account-protection-settings' into add/packa…
dkmyta Jan 21, 2025
4b18375
Remove all reference to and functionality of strict mode
dkmyta Jan 24, 2025
bbec51a
Rebase, fix conflicts
dkmyta Jan 24, 2025
7d72fd9
Remove unneeded strict mode code, update Protect settings UI
dkmyta Jan 24, 2025
36f0945
Updates/fixes
dkmyta Jan 24, 2025
37e0aa2
Fix import
dkmyta Jan 24, 2025
d51016d
Update placeholder content
dkmyta Jan 24, 2025
30b86d5
Revert unrelated changes
dkmyta Jan 24, 2025
6b34d25
Remove missed code
dkmyta Jan 24, 2025
40a6edf
Update reset email to two factor auth email
dkmyta Jan 27, 2025
528ee1d
Updates and improvements
dkmyta Jan 28, 2025
612f655
Reorg
dkmyta Jan 28, 2025
9b2bb3e
Optimizations and reorganizations
dkmyta Jan 28, 2025
664558b
Hook up email service
dkmyta Jan 28, 2025
915504d
Update error handling todos, fix weak password check
dkmyta Jan 29, 2025
87445c2
Test
dkmyta Jan 29, 2025
9ef7e9d
Localize text content
dkmyta Jan 29, 2025
4c794c5
Fix lint warnings/errors
dkmyta Jan 29, 2025
0b493b8
Update todos
dkmyta Jan 29, 2025
82d9ff2
Add error handling, enforce input restrictions
dkmyta Jan 29, 2025
b466475
Move main constants back entry file
dkmyta Jan 29, 2025
7f7b57d
Fix package version check
dkmyta Jan 29, 2025
fe79de3
Optimize setting error transient
dkmyta Jan 29, 2025
6743841
Add nonce check for resend email action
dkmyta Jan 29, 2025
88eed6e
Fix spacing
dkmyta Jan 29, 2025
4e0be98
Fix resend nonce handling
dkmyta Jan 29, 2025
8f79bab
Merge branch 'add/account-protection' into update/packages/account-pr…
dkmyta Jan 29, 2025
490e50b
Email service fixes
dkmyta Jan 29, 2025
d47a220
Fixes, improvements to doc consistency
dkmyta Jan 29, 2025
7e87875
Add remaining password validation
dkmyta Jan 29, 2025
38a2d15
Update weak password check returns
dkmyta Jan 29, 2025
9838e09
Fix phan errors
dkmyta Jan 29, 2025
7d4b46f
Revert prior change
dkmyta Jan 29, 2025
6c52261
Fix meta key
dkmyta Jan 29, 2025
c9acdcb
Rebase
dkmyta Jan 29, 2025
06f6008
Add process for add/updating recent pass list
dkmyta Jan 30, 2025
730407a
Send auth code via wpcom only
dkmyta Jan 30, 2025
39cd995
Update method name
dkmyta Jan 30, 2025
022f9ab
Rebase, fix weak password method returns
dkmyta Jan 30, 2025
abfe635
Rebase, fix conflicts
dkmyta Jan 30, 2025
56ee7aa
Optimize validation
dkmyta Jan 31, 2025
c5e658e
Fix key, remove testing code
dkmyta Jan 31, 2025
9637bd3
Fix docs
dkmyta Jan 31, 2025
fd002db
Rebase, fix conflicts
dkmyta Jan 31, 2025
81a2325
Fix tests
dkmyta Jan 31, 2025
b173e09
Merge branch 'add/account-protection' into add/packages/account-prote…
dkmyta Feb 2, 2025
61ca3ed
Merge branch 'add/account-protection' into add/packages/account-prote…
dkmyta Feb 2, 2025
8159db7
Merge branch 'add/account-protection' into add/packages/account-prote…
dkmyta Feb 2, 2025
5e96389
Improve matches user data logic
dkmyta Feb 3, 2025
c5c8acd
Remove password reset nonce verification code
dkmyta Feb 3, 2025
8f8f934
Updates and fixes
dkmyta Feb 3, 2025
8388caa
Include tests for new validation methods
dkmyta Feb 3, 2025
2c59a84
Include tests for new validation methods
dkmyta Feb 3, 2025
7fb0b5a
Add password manager class tests
dkmyta Feb 4, 2025
b651b2b
Remove custom nonce, add core create-user nonce check
dkmyta Feb 5, 2025
16d54a6
Remove todos - always run server side validation
dkmyta Feb 5, 2025
c9f5d32
Update constant naming
dkmyta Feb 5, 2025
5559bce
Translate error message
dkmyta Feb 5, 2025
b716b6f
Ensure styles are enqueued when viewing the password detection page
dkmyta Feb 5, 2025
d296b26
Use global page now and action check to enqueue styles
dkmyta Feb 5, 2025
5515183
Skip recent password checks during create user action
dkmyta Feb 5, 2025
a8e6b8d
Additional skips, and comment clarification
dkmyta Feb 5, 2025
b5699a6
Revert skips of user specific reset form validation, hook provides ac…
dkmyta Feb 5, 2025
556fc84
Revert unintended additions
dkmyta Feb 5, 2025
6e725cb
Return early if update is irrelevant
dkmyta Feb 6, 2025
afb4621
Only verify nonce if pass is set
dkmyta Feb 6, 2025
62d30c2
Skip validation if bypass enabled
dkmyta Feb 6, 2025
6c6eea4
Merge branch 'add/account-protection' into add/packages/account-prote…
dkmyta Feb 11, 2025
55f5617
Merge branch 'add/account-protection' into add/packages/account-prote…
dkmyta Feb 12, 2025
3a2993c
Fix test
dkmyta Feb 12, 2025
7c5733c
Update methods, removes nonce checks, fix tests
dkmyta Feb 12, 2025
bd3e654
Fix test
dkmyta Feb 12, 2025
647be75
Remove comment
dkmyta Feb 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class Account_Protection {
const PACKAGE_VERSION = '0.1.0-alpha';
const ACCOUNT_PROTECTION_MODULE_NAME = 'account-protection';

/**
* Flag to track if hooks have been registered.
*
* @var bool
*/
private static $hooks_registered = false;

/**
* Modules instance.
*
Expand All @@ -30,15 +37,24 @@ class Account_Protection {
*/
private $password_detection;

/**
* Password manager instance
*
* @var Password_Manager
*/
private $password_manager;

/**
* Account_Protection constructor.
*
* @param ?Modules $modules Modules instance.
* @param ?Password_Detection $password_detection Password detection instance.
* @param ?Password_Manager $password_manager Validation service instance.
*/
public function __construct( ?Modules $modules = null, ?Password_Detection $password_detection = null ) {
public function __construct( ?Modules $modules = null, ?Password_Detection $password_detection = null, ?Password_Manager $password_manager = null ) {
$this->modules = $modules ?? new Modules();
$this->password_detection = $password_detection ?? new Password_Detection();
$this->password_manager = $password_manager ?? new Password_Manager();
}

/**
Expand All @@ -47,11 +63,17 @@ public function __construct( ?Modules $modules = null, ?Password_Detection $pass
* @return void
*/
public function init(): void {
if ( self::$hooks_registered ) {
return;
}

$this->register_hooks();

if ( $this->is_enabled() ) {
$this->register_runtime_hooks();
}

self::$hooks_registered = true;
}

/**
Expand Down Expand Up @@ -83,6 +105,16 @@ protected function register_runtime_hooks(): void {

// Add password detection flow
add_action( 'login_form_password-detection', array( $this->password_detection, 'render_page' ), 10, 2 );
add_action( 'wp_enqueue_scripts', array( $this->password_detection, 'enqueue_styles' ) );
Copy link
Contributor

@nateweller nateweller Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the flow correctly, will using the "wp_enqueue_scripts" action here load these styles on every request?

If so, could we use login_enqueue_scripts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your understanding is correct - this does fire on every request but for some odd reason login_enqueue_scripts was not working to limit it to login requests. It seems to enqueue the styles on the base wp-login.php page but the minute we are redirected to wp-login.php?action=account-protection... (after failed password validation) the styles are missing. I suspect it might have to do with the order in which these actions are fired - at the time the custom password detection wp-login.php page is generated this action isn't able to attach the styles?

As an alternative, we can continue to use wp_enqueue_scripts but add some logic to the callback so that we are only enqueuing the styles if $_GET[ 'action' ] is set and equals 'account-protection' - we could take it one step further and also check ! wp_style_is( $handle, 'enqueued' ). Thoughts/opinions?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conditionally enqueuing works for me 👍

We could use $_GLOBALS['pagenow'] === 'wp-login.php' to narrow it down to the specific page, plus the action check.

IIRC we shouldn't need to worry about checking if the style was enqueued or not already.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d296b26 and b716b6f


// Add password validation

add_action( 'user_profile_update_errors', array( $this->password_manager, 'validate_profile_update' ), 10, 3 );
add_action( 'validate_password_reset', array( $this->password_manager, 'validate_password_reset' ), 10, 2 );

// Update recent passwords list
add_action( 'profile_update', array( $this->password_manager, 'on_profile_update' ), 10, 2 );
add_action( 'after_password_reset', array( $this->password_manager, 'on_password_reset' ), 10, 1 );
}

/**
Expand Down
11 changes: 6 additions & 5 deletions projects/packages/account-protection/src/class-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
* Class Config
*/
class Config {
public const TRANSIENT_PREFIX = 'password_detection';
public const ERROR_CODE = 'password_detection_validation_error';
public const ERROR_MESSAGE = 'Password validation failed.';
public const EMAIL_SENT_EXPIRATION = 600; // 10 minutes
public const MAX_RESEND_ATTEMPTS = 3;
public const PASSWORD_DETECTION_TRANSIENT_PREFIX = 'password_detection';
public const PASSWORD_DETECTION_ERROR_CODE = 'password_detection_validation_error';
public const PASSWORD_DETECTION_EMAIL_SENT_EXPIRATION = 600; // 10 minutes
public const PASSWORD_DETECTION_MAX_RESEND_ATTEMPTS = 3;

public const VALIDATION_SERVICE_RECENT_PASSWORD_HASHES_USER_META_KEY = 'jetpack_account_protection_recent_password_hashes';
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected function send_email_request( int $blog_id, array $body ) {
* @return bool True if the email was resent successfully, false otherwise.
*/
public function resend_auth_email( \WP_User $user, array $transient_data, string $token ): bool {
if ( $transient_data['resend_attempts'] >= Config::MAX_RESEND_ATTEMPTS ) {
if ( $transient_data['resend_attempts'] >= Config::PASSWORD_DETECTION_MAX_RESEND_ATTEMPTS ) {
return false;
}

Expand All @@ -108,7 +108,7 @@ public function resend_auth_email( \WP_User $user, array $transient_data, string

++$transient_data['resend_attempts'];

if ( ! set_transient( Config::TRANSIENT_PREFIX . "_{$token}", $transient_data, Config::EMAIL_SENT_EXPIRATION ) ) {
if ( ! set_transient( Config::PASSWORD_DETECTION_TRANSIENT_PREFIX . "_{$token}", $transient_data, Config::PASSWORD_DETECTION_EMAIL_SENT_EXPIRATION ) ) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function login_form_password_detection( $user, string $password ) {
}

if ( $this->validation_service->is_weak_password( $password ) ) {
// TODO: Every time the user logs in we generate a new token based transient. This might not be ideal.
$transient = $this->generate_and_store_transient_data( $user->ID );

$email_sent = $this->email_service->api_send_auth_email( $user, $transient['auth_code'] );
Expand All @@ -59,8 +58,8 @@ public function login_form_password_detection( $user, string $password ) {
}

return new \WP_Error(
Config::ERROR_CODE,
Config::ERROR_MESSAGE,
Config::PASSWORD_DETECTION_ERROR_CODE,
__( 'Password validation failed.', 'jetpack-account-protection' ),
array( 'token' => $transient['token'] )
);
}
Expand Down Expand Up @@ -126,7 +125,7 @@ public function render_page() {
}

$token = isset( $_GET['token'] ) ? sanitize_text_field( wp_unslash( $_GET['token'] ) ) : null;
$transient_data = get_transient( Config::TRANSIENT_PREFIX . "_{$token}" );
$transient_data = get_transient( Config::PASSWORD_DETECTION_TRANSIENT_PREFIX . "_{$token}" );
if ( ! $transient_data ) {
$this->redirect_to_login();
// @phan-suppress-next-line PhanPluginUnreachableCode This would fall through in unit tests otherwise.
Expand All @@ -141,8 +140,6 @@ public function render_page() {
return;
}

add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );

// Handle resend email request
if ( isset( $_GET['resend_email'] ) && $_GET['resend_email'] === '1' ) {
if ( isset( $_GET['_wpnonce'] )
Expand All @@ -152,7 +149,7 @@ public function render_page() {
if ( ! $email_resent ) {
$message = __( 'Failed to resend authentication email. Please try again.', 'jetpack-account-protection' );

if ( $transient_data['resend_attempts'] >= Config::MAX_RESEND_ATTEMPTS ) {
if ( $transient_data['resend_attempts'] >= Config::PASSWORD_DETECTION_MAX_RESEND_ATTEMPTS ) {
$message = __( 'Resend limit exceeded. Please try again later.', 'jetpack-account-protection' );
}

Expand Down Expand Up @@ -190,7 +187,7 @@ public function render_page() {
* @return void
*/
public function render_content( \WP_User $user, string $token ): void {
$transient_key = Config::TRANSIENT_PREFIX . "_error_{$user->ID}";
$transient_key = Config::PASSWORD_DETECTION_TRANSIENT_PREFIX . "_error_{$user->ID}";
$error_message = get_transient( $transient_key );
delete_transient( $transient_key );

Expand Down Expand Up @@ -286,7 +283,7 @@ private function generate_and_store_transient_data( int $user_id ): array {
'resend_attempts' => 0,
);

$transient_set = set_transient( Config::TRANSIENT_PREFIX . "_{$token}", $data, Config::EMAIL_SENT_EXPIRATION );
$transient_set = set_transient( Config::PASSWORD_DETECTION_TRANSIENT_PREFIX . "_{$token}", $data, Config::PASSWORD_DETECTION_EMAIL_SENT_EXPIRATION );
if ( ! $transient_set ) {
$this->set_transient_error( $user_id, __( 'Failed to set transient data. Please try again.', 'jetpack-account-protection' ) );
}
Expand Down Expand Up @@ -330,7 +327,7 @@ private function get_redirect_url( string $token ): string {
private function handle_auth_form_submission( \WP_User $user, string $token, string $auth_code, string $user_input ): void {
if ( $auth_code && $auth_code === $user_input ) {
// TODO: Ensure all transient are also removed on module and/or plugin deactivation
delete_transient( Config::TRANSIENT_PREFIX . "_{$token}" );
delete_transient( Config::PASSWORD_DETECTION_TRANSIENT_PREFIX . "_{$token}" );
wp_set_auth_cookie( $user->ID, true );
// TODO: Notify user to update their password/redirect to password update page
$this->redirect_and_exit( admin_url() );
Expand All @@ -349,7 +346,7 @@ private function handle_auth_form_submission( \WP_User $user, string $token, str
* @return void
*/
private function set_transient_error( int $user_id, string $message, int $expiration = 60 ): void {
set_transient( Config::TRANSIENT_PREFIX . "_error_{$user_id}", $message, $expiration );
set_transient( Config::PASSWORD_DETECTION_TRANSIENT_PREFIX . "_error_{$user_id}", $message, $expiration );
}

/**
Expand All @@ -358,11 +355,15 @@ private function set_transient_error( int $user_id, string $message, int $expira
* @return void
*/
public function enqueue_styles(): void {
wp_enqueue_style(
'password-detection-styles',
plugin_dir_url( __FILE__ ) . 'css/password-detection.css',
array(),
Account_Protection::PACKAGE_VERSION
);
// No nonce verification necessary - reading only
// phpcs:disable WordPress.Security.NonceVerification
if ( ( isset( $GLOBALS['pagenow'] ) && $GLOBALS['pagenow'] === 'wp-login.php' ) && ( isset( $_GET['action'] ) && $_GET['action'] === 'password-detection' ) ) {
wp_enqueue_style(
'password-detection-styles',
plugin_dir_url( __FILE__ ) . 'css/password-detection.css',
array(),
Account_Protection::PACKAGE_VERSION
);
}
}
}
155 changes: 155 additions & 0 deletions projects/packages/account-protection/src/class-password-manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
/**
* Class used to define Password Manager.
*
* @package automattic/jetpack-account-protection
*/

namespace Automattic\Jetpack\Account_Protection;

/**
* Class Password_Manager
*/
class Password_Manager {
/**
* Validaton service instance
*
* @var Validation_Service
*/
private $validation_service;

/**
* Validation_Service constructor.
*
* @param ?Validation_Service $validation_service Password manager instance.
*/
public function __construct( ?Validation_Service $validation_service = null ) {
$this->validation_service = $validation_service ?? new Validation_Service();
}

/**
* Validate the profile update.
*
* @param \WP_Error $errors The error object.
* @param bool $update Whether the user is being updated.
* @param \stdClass $user A copy of the new user object.
*
* @return void
*/
public function validate_profile_update( \WP_Error $errors, bool $update, \stdClass $user ): void {
if ( empty( $user->user_pass ) ) {
return;
}

// If bypass is enabled, do not validate the password
// phpcs:ignore WordPress.Security.NonceVerification
if ( isset( $_POST['pw_weak'] ) && 'on' === $_POST['pw_weak'] ) {
return;
}

if ( $update ) {
if ( $this->validation_service->is_current_password( $user->ID, $user->user_pass ) ) {
$errors->add( 'password_error', __( '<strong>Error:</strong> The password was used recently.', 'jetpack-account-protection' ) );
return;
}
}

$context = $update ? 'update' : 'create-user';
$error = $this->validation_service->return_first_validation_error( $user, $user->user_pass, $context );

if ( ! empty( $error ) ) {
$errors->add( 'password_error', $error );
return;
}
}

/**
* Validate the password reset.
*
* @param \WP_Error $errors The error object.
* @param \WP_User|\WP_Error $user The user object.
*
* @return void
*/
public function validate_password_reset( \WP_Error $errors, $user ): void {
if ( is_wp_error( $user ) ) {
return;
}

// phpcs:ignore WordPress.Security.NonceVerification
if ( empty( $_POST['pass1'] ) ) {
return;
}

// If bypass is enabled, do not validate the password
// phpcs:ignore WordPress.Security.NonceVerification
if ( isset( $_POST['pw_weak'] ) && 'on' === $_POST['pw_weak'] ) {
return;
}

// phpcs:ignore WordPress.Security.NonceVerification
$password = sanitize_text_field( wp_unslash( $_POST['pass1'] ) );
if ( $this->validation_service->is_current_password( $user->ID, $password ) ) {
$errors->add( 'password_error', __( '<strong>Error:</strong> The password was used recently.', 'jetpack-account-protection' ) );
return;
}

$error = $this->validation_service->return_first_validation_error( $user, $password, 'reset' );
if ( ! empty( $error ) ) {
$errors->add( 'password_error', $error );
return;
}
}

/**
* Handle the profile update.
*
* @param int $user_id The user ID.
* @param \WP_User $old_user_data Object containing user data prior to update.
*
* @return void
*/
public function on_profile_update( int $user_id, \WP_User $old_user_data ): void {
// phpcs:ignore WordPress.Security.NonceVerification
if ( isset( $_POST['action'] ) && $_POST['action'] === 'update' ) {
$this->save_recent_password( $user_id, $old_user_data->user_pass );
}
}

/**
* Handle the password reset.
*
* @param \WP_User $user The user.
*
* @return void
*/
public function on_password_reset( $user ): void {
$this->save_recent_password( $user->ID, $user->user_pass );
}

/**
* Save the new password hash to the user's recent passwords list.
*
* @param int $user_id The user ID.
* @param string $password_hash The password hash to store.
*
* @return void
*/
public function save_recent_password( int $user_id, string $password_hash ): void {
$recent_passwords = get_user_meta( $user_id, Config::VALIDATION_SERVICE_RECENT_PASSWORD_HASHES_USER_META_KEY, true );

if ( ! is_array( $recent_passwords ) ) {
$recent_passwords = array();
}

if ( in_array( $password_hash, $recent_passwords, true ) ) {
return;
}

// Add the new hashed password and keep only the last 10
array_unshift( $recent_passwords, $password_hash );
$recent_passwords = array_slice( $recent_passwords, 0, 10 );

update_user_meta( $user_id, Config::VALIDATION_SERVICE_RECENT_PASSWORD_HASHES_USER_META_KEY, $recent_passwords );
}
}
Loading
Loading