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 detection flow #41105

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 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
13cdb4d
Merge branch 'add/account-protection' into add/jetpack/account-protec…
dkmyta Jan 24, 2025
b07ecd1
Update projects/plugins/protect/src/js/data/account-protection/use-ac…
dkmyta Jan 24, 2025
ce17441
Rebase
dkmyta Jan 24, 2025
2f5e6b7
Merge branch 'add/protect/account-protection-settings' into add/packa…
dkmyta Jan 24, 2025
5edfaca
Update lock files
dkmyta Jan 24, 2025
073c105
Merge branch 'add/protect/account-protection-settings' into add/packa…
dkmyta Jan 24, 2025
464c58f
Fix conflicts
dkmyta Jan 24, 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
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Adds the password detection flow
21 changes: 21 additions & 0 deletions projects/packages/account-protection/src/assets/jetpack-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
119 changes: 107 additions & 12 deletions projects/packages/account-protection/src/class-account-protection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* Class Account_Protection
*/
class Account_Protection {

const PACKAGE_VERSION = '0.1.0-alpha';
const ACCOUNT_PROTECTION_MODULE_NAME = 'account-protection';
const STRICT_MODE_OPTION_NAME = 'jetpack_account_protection_strict_mode';
Expand All @@ -25,39 +24,83 @@ class Account_Protection {
*/
private $modules;

/**
* Password detection instance.
*
* @var Password_Detection
*/
private $password_detection;

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

/**
* Initializes the configurations needed for the account protection module.
*/
public function init() {
public function init(): void {
$this->register_hooks();

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

/**
* Register hooks for module activation and environment validation.
*/
private function register_hooks(): void {
// Account protection activation/deactivation hooks
add_action( 'jetpack_activate_module_' . self::ACCOUNT_PROTECTION_MODULE_NAME, array( $this, 'on_account_protection_activation' ) );
add_action( 'jetpack_deactivate_module_' . self::ACCOUNT_PROTECTION_MODULE_NAME, array( $this, 'on_account_protection_deactivation' ) );

// Do not run in unsupported environments
add_action( 'jetpack_get_available_modules', array( $this, 'remove_module_on_unsupported_environments' ) );
add_action( 'jetpack_get_available_standalone_modules', array( $this, 'remove_standalone_module_on_unsupported_environments' ) );

// Register REST routes
add_action( 'rest_api_init', array( new REST_Controller(), 'register_rest_routes' ) );
}

/**
* Register hooks for runtime operations.
*/
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 );

// 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.
*/
public function on_account_protection_activation() {
// Account protection activated
public function on_account_protection_activation(): void {
// Activation logic can be added here
}

/**
* Deactivate the account protection on module activation.
* Deactivate the account protection on module deactivation.
*/
public function on_account_protection_deactivation() {
// Account protection deactivated
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();
}

/**
Expand Down Expand Up @@ -87,20 +130,72 @@ public function enable() {
*
* @return bool
*/
public function disable() {
public function disable(): bool {
// Return true if already disabled.
if ( ! $this->is_enabled() ) {
return true;
}
return $this->modules->deactivate( self::ACCOUNT_PROTECTION_MODULE_NAME );
}

/**
* Determines if Account Protection is supported in the current environment.
*
* @return bool
*/
public function is_supported_environment(): bool {
// Do not run when killswitch is enabled
if ( defined( 'DISABLE_JETPACK_ACCOUNT_PROTECTION' ) && DISABLE_JETPACK_ACCOUNT_PROTECTION ) {
return false;
}

return true;
}

/**
* Disables the Account Protection module when on an unsupported platform in Jetpack.
*
* @param array $modules Filterable value for `jetpack_get_available_modules`.
*
* @return array Array of module slugs.
*/
public function remove_module_on_unsupported_environments( array $modules ): array {
if ( ! $this->is_supported_environment() ) {
// Account protection should never be available on unsupported platforms.
unset( $modules[ self::ACCOUNT_PROTECTION_MODULE_NAME ] );
}

return $modules;
}

/**
* Disables the Account Protection module when on an unsupported platform in a standalone plugin.
*
* @param array $modules Filterable value for `jetpack_get_available_standalone_modules`.
*
* @return array Array of module slugs.
*/
public function remove_standalone_module_on_unsupported_environments( array $modules ): array {
if ( ! $this->is_supported_environment() ) {
// Account Protection should never be available on unsupported platforms.
$modules = array_filter(
$modules,
function ( $module ) {
return $module !== self::ACCOUNT_PROTECTION_MODULE_NAME;
}
);

}

return $modules;
}

/**
* Get the account protection settings.
*
* @return array
*/
public function get_settings() {
public function get_settings(): array {
$settings = array(
self::STRICT_MODE_OPTION_NAME => get_option( self::STRICT_MODE_OPTION_NAME, false ),
);
Expand Down
Loading
Loading