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: Update password detection flow #41365

Merged
Show file tree
Hide file tree
Changes from 104 commits
Commits
Show all changes
106 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
9838e09
Fix phan errors
dkmyta Jan 29, 2025
7d4b46f
Revert prior change
dkmyta Jan 29, 2025
730407a
Send auth code via wpcom only
dkmyta Jan 30, 2025
39cd995
Update method name
dkmyta Jan 30, 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 @@ -43,6 +43,8 @@ public function __construct( ?Modules $modules = null, ?Password_Detection $pass

/**
* Initializes the configurations needed for the account protection module.
*
* @return void
*/
public function init(): void {
$this->register_hooks();
Expand All @@ -54,6 +56,8 @@ public function init(): void {

/**
* Register hooks for module activation and environment validation.
*
* @return void
*/
private function register_hooks(): void {
// Account protection activation/deactivation hooks
Expand All @@ -67,44 +71,44 @@ private function register_hooks(): void {

/**
* Register hooks for runtime operations.
*
* @return void
*/
private function register_runtime_hooks(): void {
// Validate password after successful login
add_action( 'wp_authenticate_user', array( $this->password_detection, 'login_form_password_detection' ), 10, 2 );

// Handle password detection login failure
add_action( 'wp_login_failed', array( $this->password_detection, 'handle_password_detection_validation_error' ), 10, 2 );

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

// Remove password detection usermeta after password reset and on profile password update
add_action( 'after_password_reset', array( $this->password_detection, 'delete_usermeta_after_password_reset' ), 10, 2 );
add_action( 'profile_update', array( $this->password_detection, 'delete_usermeta_on_profile_update' ), 10, 2 );

// Register AJAX resend password reset email action
add_action( 'wp_ajax_resend_password_reset', array( $this->password_detection, 'ajax_resend_password_reset_email' ) );
}

/**
* Activate the account protection on module activation.
*
* @return void
*/
public function on_account_protection_activation(): void {
// Activation logic can be added here
}

/**
* Deactivate the account protection on module deactivation.
*
* @return void
*/
public function on_account_protection_deactivation(): void {
// Remove password detection user meta on deactivation
// TODO: Run on Jetpack and Protect deactivation
$this->password_detection->delete_all_usermeta();
// Deactivation logic can be added here
}

/**
* Determines if the account protection module is enabled on the site.
*
* @return bool
*/
public function is_enabled() {
public function is_enabled(): bool {
return $this->modules->is_active( self::ACCOUNT_PROTECTION_MODULE_NAME );
}

Expand All @@ -113,7 +117,7 @@ public function is_enabled() {
*
* @return bool
*/
public function enable() {
public function enable(): bool {
// Return true if already enabled.
if ( $this->is_enabled() ) {
return true;
Expand Down
19 changes: 19 additions & 0 deletions projects/packages/account-protection/src/class-config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Class used to define Config.
*
* @package automattic/jetpack-account-protection
*/

namespace Automattic\Jetpack\Account_Protection;

/**
* 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;
}
168 changes: 168 additions & 0 deletions projects/packages/account-protection/src/class-email-service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php
/**
* Class used to define Email Service.
*
* @package automattic/jetpack-account-protection
*/

namespace Automattic\Jetpack\Account_Protection;

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Jetpack_Options;

/**
* Class Email_Service
*/
class Email_Service {
/**
* Send auth email.
*
* @param \WP_User $user The user.
* @param string $auth_code The authentication code.
*
* @return bool True if the email was sent successfully, false otherwise.
*/
public function send_auth_email( \WP_User $user, string $auth_code ): bool {
$wp_send = $this->wp_send_auth_email( $user, $auth_code );

if ( ! $wp_send ) {
return $this->api_send_auth_email( $user, $auth_code );
}

return true;
}

/**
* Send the email using wp_mail().
*
* @param \WP_User $user The user.
* @param string $auth_code The authentication code.
*
* @return bool True if the email was sent successfully, false otherwise.
*/
private function wp_send_auth_email( \WP_User $user, string $auth_code ): bool {
$blog_name = esc_html( get_bloginfo( 'name' ) );
$blog_url = esc_url( get_bloginfo( 'url' ) );

$subject = esc_html__( 'Verify your identity at Jetpack', 'jetpack-account-protection' );
$message = sprintf(
/* translators: 1: User login, 2: User URL, 3: Blog name, 4: Authentication code, 5: User URL, 6: Blog name */
__(
'<p>Hi %1$s,</p>
<p>Your current password for <a href="%2$s">%3$s</a> was found in a public leak, which means your account might be at risk.</p>
<p>To help protect your account, please enter this code at the login prompt:</p>
<p><strong>%4$s</strong></p>
<p>If you didn\'t just log into %5$s, please do so now and change your password.</p>
<p>Stay secure,<br>Jetpack</p>',
'jetpack-account-protection'
),
esc_html( $user->user_login ),
$blog_url,
$blog_name,
esc_html( $auth_code ),
$blog_url,
$blog_name
);

$headers = array( 'Content-Type: text/html; charset=UTF-8' );

return wp_mail( $user->user_email, $subject, $message, $headers );
}

/**
* Send the email using the API.
*
* @param \WP_User $user The user.
* @param string $auth_code The authentication code.
*
* @return bool True if the email was sent successfully, false otherwise.
*/
private function api_send_auth_email( \WP_User $user, string $auth_code ): bool {
$blog_id = Jetpack_Options::get_option( 'id' );
$is_connected = ( new Connection_Manager() )->is_connected();

if ( ! $blog_id || ! $is_connected ) {
return false;
}

$body = array(
'user_login' => $user->user_login,
'user_email' => $user->user_email,
'code' => $auth_code,
);

$response = Client::wpcom_json_api_request_as_blog(
sprintf( '/sites/%d/jetpack-protect-send-verification-code', $blog_id ),
'2',
array(
'method' => 'POST',
),
$body,
'wpcom'
);

$response_code = wp_remote_retrieve_response_code( $response );
if ( is_wp_error( $response ) || 200 !== $response_code || empty( $response['body'] ) ) {
return false;
}

$body = json_decode( wp_remote_retrieve_body( $response ), true );
return $body['success'] ?? false;
}

/**
* Resend email attempts.
*
* @param \WP_User $user The user.
* @param array $transient_data The transient data.
* @param string $token The token.
*
* @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 ) {
return false;
}

$auth_code = $this->generate_auth_code();
$transient_data['auth_code'] = $auth_code;

if ( ! $this->send_auth_email( $user, $auth_code ) ) {
return false;
}

++$transient_data['resend_attempts'];

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

return true;
}

/**
* Generate an auth code.
*
* @return string The generated auth code.
*/
public function generate_auth_code(): string {
return (string) wp_rand( 100000, 999999 );
}

/**
* Mask an email address like d*****@g*****.com.
*
* @param string $email The email address to mask.
*
* @return string The masked email address.
*/
public function mask_email_address( string $email ): string {
$parts = explode( '@', $email );
$name = substr( $parts[0], 0, 1 ) . str_repeat( '*', strlen( $parts[0] ) - 1 );
$domain_parts = explode( '.', $parts[1] );
$domain = substr( $domain_parts[0], 0, 1 ) . str_repeat( '*', strlen( $domain_parts[0] ) - 1 );

return "{$name}@{$domain}.{$domain_parts[1]}";
}
}
Loading
Loading