diff --git a/class-two-factor-core.php b/class-two-factor-core.php index cb09f272..a1425e45 100644 --- a/class-two-factor-core.php +++ b/class-two-factor-core.php @@ -157,6 +157,10 @@ public static function add_hooks( $compat ) { add_action( 'login_enqueue_scripts', array( __CLASS__, 'login_enqueue_scripts' ), 5 ); add_action( 'admin_init', array( __CLASS__, 'trigger_user_settings_action' ) ); add_action( 'admin_init', array( __CLASS__, 'add_privacy_policy_content' ) ); + + // Personal data export tool. + add_filter( 'wp_privacy_personal_data_exporters', array( __CLASS__, 'register_personal_data_exporter' ) ); + add_filter( 'two_factor_providers', array( __CLASS__, 'enable_dummy_method_for_debug' ) ); // Add Settings link to plugin action links. @@ -3218,4 +3222,241 @@ public static function add_privacy_policy_content() { wp_kses_post( wpautop( $content, false ) ) ); } + + /** + * Registers the personal data exporter. + * + * @since 0.18.0 + * + * @param array $exporters List of personal data exporters. + * @return array + */ + public static function register_personal_data_exporter( $exporters ) { + $exporters['two-factor'] = array( + 'exporter_friendly_name' => __( 'Two Factor Authentication Data', 'two-factor' ), + 'callback' => array( __CLASS__, 'personal_data_exporter' ), + ); + + return $exporters; + } + + /** + * Exports the Two Factor data stored for a user. + * + * Credentials are described, never included. The TOTP secret, the backup + * codes and the email token hash stay out of the export file so that it + * remains safe to share. + * + * @since 0.18.0 + * + * @param string $email_address The email address of the user. + * @param int $page The page of data being requested. + * @return array + */ + public static function personal_data_exporter( $email_address, $page = 1 ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- Pagination is not needed, all data fits on one page. + $user = get_user_by( 'email', $email_address ); + + if ( ! $user ) { + return array( + 'data' => array(), + 'done' => true, + ); + } + + $providers = self::get_privacy_providers(); + $data = array(); + + $enabled_providers = get_user_meta( $user->ID, self::ENABLED_PROVIDERS_USER_META_KEY, true ); + if ( $enabled_providers ) { + $enabled_labels = array(); + foreach ( (array) $enabled_providers as $provider_key ) { + $enabled_labels[] = self::get_privacy_provider_label( $provider_key, $providers ); + } + + $data[] = array( + 'name' => __( 'Enabled Two Factor methods', 'two-factor' ), + 'value' => implode( ', ', $enabled_labels ), + ); + } + + $primary_provider = get_user_meta( $user->ID, self::PROVIDER_USER_META_KEY, true ); + if ( $primary_provider ) { + $data[] = array( + 'name' => __( 'Primary Two Factor method', 'two-factor' ), + 'value' => self::get_privacy_provider_label( $primary_provider, $providers ), + ); + } + + $failed_attempts = (int) get_user_meta( $user->ID, self::USER_FAILED_LOGIN_ATTEMPTS_KEY, true ); + if ( $failed_attempts ) { + $data[] = array( + 'name' => __( 'Failed Two Factor login attempts', 'two-factor' ), + 'value' => (string) $failed_attempts, + ); + } + + $last_failure = get_user_meta( $user->ID, self::USER_RATE_LIMIT_KEY, true ); + if ( $last_failure ) { + $data[] = array( + 'name' => __( 'Last failed Two Factor login', 'two-factor' ), + 'value' => self::format_privacy_timestamp( $last_failure ), + ); + } + + if ( get_user_meta( $user->ID, self::USER_PASSWORD_WAS_RESET_KEY, true ) ) { + $data[] = array( + 'name' => __( 'Password reset by Two Factor', 'two-factor' ), + 'value' => __( 'The password was reset after too many failed Two Factor attempts. A new password must be created to log in.', 'two-factor' ), + ); + } + + foreach ( $providers as $provider ) { + $provider_data = $provider->privacy_export_data( $user ); + + // A third-party override can return a non-array, which array_merge() rejects. + if ( ! empty( $provider_data ) && is_array( $provider_data ) ) { + $data = array_merge( $data, $provider_data ); + } + } + + $export_items = array(); + + if ( ! empty( $data ) ) { + $export_items[] = array( + 'group_id' => 'two-factor', + 'group_label' => __( 'Two Factor Authentication', 'two-factor' ), + 'group_description' => __( 'Two Factor authentication data for the user.', 'two-factor' ), + 'item_id' => 'two-factor', + 'data' => $data, + ); + } + + // The core exporter skips the Two Factor session details. Using its + // group and item IDs merges them into the matching session. + $sessions = get_user_meta( $user->ID, 'session_tokens', true ); + if ( ! is_array( $sessions ) ) { + $sessions = array(); + } + + foreach ( $sessions as $token_key => $session ) { + if ( empty( $session['two-factor-login'] ) ) { + continue; + } + + // Without a provider, Two Factor was enabled in this session and no code was entered. + $verified = ! empty( $session['two-factor-provider'] ); + + $session_data = array( + array( + 'name' => $verified ? __( 'Two Factor verified', 'two-factor' ) : __( 'Two Factor enabled', 'two-factor' ), + 'value' => self::format_privacy_timestamp( $session['two-factor-login'] ), + ), + array( + 'name' => __( 'Two Factor method', 'two-factor' ), + 'value' => $verified + ? self::get_privacy_provider_label( $session['two-factor-provider'], $providers ) + : __( 'None, Two Factor was enabled during this session', 'two-factor' ), + ), + ); + + $export_items[] = array( + 'group_id' => 'session-tokens', + 'group_label' => __( 'Session Tokens', 'two-factor' ), + 'item_id' => "session-tokens-{$user->ID}-{$token_key}", + 'data' => $session_data, + ); + } + + return array( + 'data' => $export_items, + 'done' => true, + ); + } + + /** + * Get the label of a provider for the personal data export. + * + * Falls back to the provider key for providers that no longer exist. + * + * @since 0.18.0 + * + * @param mixed $provider_key Provider key, as read from user or session meta. + * @param Two_Factor_Provider[] $providers Provider instances indexed by provider key. + * @return string + */ + private static function get_privacy_provider_label( $provider_key, $providers ) { + // Malformed meta can hold an array, which isset() rejects with a TypeError. + if ( ! is_string( $provider_key ) ) { + return __( 'Unknown', 'two-factor' ); + } + + if ( isset( $providers[ $provider_key ] ) ) { + return $providers[ $provider_key ]->get_label(); + } + + return $provider_key; + } + + /** + * Get all registered providers for the personal data exporter. + * + * Same as get_providers(), but providers that are disabled in the + * site-wide settings, including third-party ones, are not removed from + * the list. Data stored for a user must be exported even while its + * provider is disabled. + * + * @since 0.18.0 + * + * @return Two_Factor_Provider[] List of provider instances indexed by provider key. + */ + private static function get_privacy_providers() { + $providers = self::get_default_providers(); + + // Read the site-wide setting as never saved, so that its filter keeps + // disabled providers, including third-party ones, in the list. + add_filter( 'pre_option_' . self::ENABLED_PROVIDERS_OPTION_KEY, '__return_null' ); + + /** This filter is documented in the get_providers() method */ + $additional_providers = apply_filters( 'two_factor_providers', $providers ); + + remove_filter( 'pre_option_' . self::ENABLED_PROVIDERS_OPTION_KEY, '__return_null' ); + + // Merge them with the default providers so that default providers + // removed by other filters are still included. + if ( ! empty( $additional_providers ) ) { + $providers = array_merge( $providers, $additional_providers ); + } + + // Map provider keys to classes so that we can instantiate them. + $providers = self::get_providers_classes( $providers ); + + foreach ( $providers as $provider_key => $provider_class ) { + try { + $providers[ $provider_key ] = call_user_func( array( $provider_class, 'get_instance' ) ); + } catch ( Exception $e ) { + unset( $providers[ $provider_key ] ); + } + } + + return $providers; + } + + /** + * Formats a timestamp for the export report. + * + * @since 0.18.0 + * + * @param int|string $timestamp Unix timestamp to format. + * @return string Formatted date and time, or an empty string when no timestamp is set. + */ + public static function format_privacy_timestamp( $timestamp ) { + if ( empty( $timestamp ) ) { + return ''; + } + + return wp_date( + get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), + (int) $timestamp + ); + } } diff --git a/providers/class-two-factor-backup-codes.php b/providers/class-two-factor-backup-codes.php index 87d8f7c7..e2289937 100644 --- a/providers/class-two-factor-backup-codes.php +++ b/providers/class-two-factor-backup-codes.php @@ -734,4 +734,34 @@ public static function uninstall_user_meta_keys() { self::BACKUP_CODES_META_KEY, ); } + + /** + * Return the personal data stored for a user for the exporter. + * + * The codes and their hashes are never included, only how many are left. + * + * @since 0.18.0 + * + * @param WP_User $user WP_User object of the user. + * @return array + */ + public function privacy_export_data( $user ) { + // The meta stays after the last code is used, so 0 remaining is still reported. + if ( ! metadata_exists( 'user', $user->ID, self::BACKUP_CODES_META_KEY ) ) { + return array(); + } + + $remaining = self::codes_remaining_for_user( $user ); + + return array( + array( + 'name' => __( 'Recovery codes', 'two-factor' ), + 'value' => sprintf( + /* translators: %d: number of unused codes */ + _n( '%d unused code', '%d unused codes', $remaining, 'two-factor' ), + $remaining + ), + ), + ); + } } diff --git a/providers/class-two-factor-email.php b/providers/class-two-factor-email.php index 6e7534e6..e6046f78 100644 --- a/providers/class-two-factor-email.php +++ b/providers/class-two-factor-email.php @@ -504,4 +504,35 @@ public static function uninstall_user_meta_keys() { self::TOKEN_META_KEY_TIMESTAMP, ); } + + /** + * Return the personal data stored for a user for the exporter. + * + * The hashed token is never included, only when the code was sent. + * The timestamp outlives the token after the code is consumed, so it + * is reported on its own. + * + * @since 0.18.0 + * + * @param WP_User $user WP_User object of the user. + * @return array + */ + public function privacy_export_data( $user ) { + $timestamp = (int) get_user_meta( $user->ID, self::TOKEN_META_KEY_TIMESTAMP, true ); + + if ( ! $timestamp ) { + return array(); + } + + return array( + array( + 'name' => __( 'Email login code', 'two-factor' ), + 'value' => sprintf( + /* translators: %s: date and time */ + __( 'A code was sent on %s.', 'two-factor' ), + Two_Factor_Core::format_privacy_timestamp( $timestamp ) + ), + ), + ); + } } diff --git a/providers/class-two-factor-provider.php b/providers/class-two-factor-provider.php index c3567be1..7a64c2b8 100644 --- a/providers/class-two-factor-provider.php +++ b/providers/class-two-factor-provider.php @@ -234,4 +234,20 @@ public static function uninstall_user_meta_keys() { public static function uninstall_options() { return array(); } + + /** + * Return the personal data that the provider stores for a user. + * + * Returns name-value pairs for the personal data exporter. Secrets and + * hashes must not be included, describe the credential instead so that + * the export file stays safe to share. + * + * @since 0.18.0 + * + * @param WP_User $user WP_User object of the user. + * @return array + */ + public function privacy_export_data( $user ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Base implementation keeps the provider interface signature but does not use the user. + return array(); + } } diff --git a/providers/class-two-factor-totp.php b/providers/class-two-factor-totp.php index 3a1895c9..ad6a98b8 100644 --- a/providers/class-two-factor-totp.php +++ b/providers/class-two-factor-totp.php @@ -967,4 +967,37 @@ public static function uninstall_user_meta_keys() { self::LAST_SUCCESSFUL_LOGIN_META_KEY, ); } + + /** + * Return the personal data stored for a user for the exporter. + * + * The secret key is never included, the credential is only described. + * + * @since 0.18.0 + * + * @param WP_User $user WP_User object of the user. + * @return array + */ + public function privacy_export_data( $user ) { + $data = array(); + + if ( ! $this->get_user_totp_key( $user->ID ) ) { + return $data; + } + + $data[] = array( + 'name' => __( 'Authenticator app (TOTP)', 'two-factor' ), + 'value' => __( 'Configured', 'two-factor' ), + ); + + $last_login = (int) get_user_meta( $user->ID, self::LAST_SUCCESSFUL_LOGIN_META_KEY, true ); + if ( $last_login ) { + $data[] = array( + 'name' => __( 'Last successful TOTP verification', 'two-factor' ), + 'value' => Two_Factor_Core::format_privacy_timestamp( $last_login ), + ); + } + + return $data; + } } diff --git a/tests/class-two-factor-core.php b/tests/class-two-factor-core.php index d9c75522..0a5905ce 100644 --- a/tests/class-two-factor-core.php +++ b/tests/class-two-factor-core.php @@ -4322,4 +4322,337 @@ public function test_add_settings_action_link() { $this->assertStringContainsString( 'Settings', $first ); $this->assertStringContainsString( 'options-general.php', $first ); } + + /** + * Create a user with every Two Factor record in place. + * + * Returns the user along with the sensitive strings that must never + * show up in an export. + * + * @return array + */ + private function get_fully_configured_user() { + $user = self::factory()->user->create_and_get(); + + update_user_meta( $user->ID, Two_Factor_Core::ENABLED_PROVIDERS_USER_META_KEY, array( 'Two_Factor_Totp', 'Two_Factor_Email', 'Two_Factor_Backup_Codes' ) ); + update_user_meta( $user->ID, Two_Factor_Core::PROVIDER_USER_META_KEY, 'Two_Factor_Totp' ); + update_user_meta( $user->ID, Two_Factor_Core::USER_FAILED_LOGIN_ATTEMPTS_KEY, 3 ); + update_user_meta( $user->ID, Two_Factor_Core::USER_RATE_LIMIT_KEY, time() - 100 ); + update_user_meta( $user->ID, Two_Factor_Core::USER_META_NONCE_KEY, 'login-nonce' ); + update_user_meta( $user->ID, Two_Factor_Core::USER_PASSWORD_WAS_RESET_KEY, true ); + + $totp = Two_Factor_Totp::get_instance(); + $totp->set_user_totp_key( $user->ID, Two_Factor_Totp::generate_key() ); + update_user_meta( $user->ID, Two_Factor_Totp::LAST_SUCCESSFUL_LOGIN_META_KEY, time() - 50 ); + + $email = Two_Factor_Email::get_instance(); + $token = $email->generate_token( $user->ID ); + $token_hashed = get_user_meta( $user->ID, Two_Factor_Email::TOKEN_META_KEY, true ); + + $codes = Two_Factor_Backup_Codes::get_instance()->generate_codes( $user ); + $codes_hashed = (array) get_user_meta( $user->ID, Two_Factor_Backup_Codes::BACKUP_CODES_META_KEY, true ); + + $sensitive = array_merge( + array( $totp->get_user_totp_key( $user->ID ), $token, $token_hashed ), + $codes, + $codes_hashed + ); + + return array( + 'user' => $user, + 'sensitive' => array_filter( $sensitive ), + ); + } + + /** + * Verify the personal data exporter is registered. + * + * @covers Two_Factor_Core::add_hooks + */ + public function test_add_hooks_privacy_filters() { + Two_Factor_Core::add_hooks( new Two_Factor_Compat() ); + + $this->assertGreaterThan( + 0, + has_filter( + 'wp_privacy_personal_data_exporters', + array( 'Two_Factor_Core', 'register_personal_data_exporter' ) + ) + ); + } + + /** + * Verify the exporter registration adds the plugin entry. + * + * @covers Two_Factor_Core::register_personal_data_exporter + */ + public function test_register_personal_data_exporter() { + $exporters = Two_Factor_Core::register_personal_data_exporter( array() ); + + $this->assertArrayHasKey( 'two-factor', $exporters ); + $this->assertSame( array( 'Two_Factor_Core', 'personal_data_exporter' ), $exporters['two-factor']['callback'] ); + $this->assertNotEmpty( $exporters['two-factor']['exporter_friendly_name'] ); + } + + /** + * Verify the exporter returns nothing for an unknown email address. + * + * @covers Two_Factor_Core::personal_data_exporter + */ + public function test_personal_data_exporter_unknown_email() { + $response = Two_Factor_Core::personal_data_exporter( 'nobody@example.com' ); + + $this->assertSame( array(), $response['data'] ); + $this->assertTrue( $response['done'] ); + } + + /** + * Verify the exporter returns nothing for a user without Two Factor data. + * + * @covers Two_Factor_Core::personal_data_exporter + */ + public function test_personal_data_exporter_user_without_two_factor() { + $user = self::factory()->user->create_and_get(); + $response = Two_Factor_Core::personal_data_exporter( $user->user_email ); + + $this->assertSame( array(), $response['data'] ); + $this->assertTrue( $response['done'] ); + } + + /** + * Verify the exporter includes the records kept by the core. + * + * @covers Two_Factor_Core::personal_data_exporter + */ + public function test_personal_data_exporter_includes_core_records() { + $setup = $this->get_fully_configured_user(); + $user = $setup['user']; + $response = Two_Factor_Core::personal_data_exporter( $user->user_email ); + + $this->assertTrue( $response['done'] ); + $this->assertSame( 'two-factor', $response['data'][0]['group_id'] ); + + $items = $response['data'][0]['data']; + $names = wp_list_pluck( $items, 'name' ); + + $this->assertContains( 'Enabled Two Factor methods', $names ); + $this->assertContains( 'Primary Two Factor method', $names ); + $this->assertContains( 'Failed Two Factor login attempts', $names ); + $this->assertContains( 'Last failed Two Factor login', $names ); + $this->assertContains( 'Password reset by Two Factor', $names ); + + $values = array_combine( $names, wp_list_pluck( $items, 'value' ) ); + $this->assertSame( 'Authenticator App, Email, Recovery Codes', $values['Enabled Two Factor methods'] ); + $this->assertSame( 'Authenticator App', $values['Primary Two Factor method'] ); + $this->assertSame( '3', $values['Failed Two Factor login attempts'] ); + $this->assertNotEmpty( $values['Last failed Two Factor login'] ); + $this->assertNotEmpty( $values['Password reset by Two Factor'] ); + } + + /** + * Verify the exporter shows the provider key for providers that no + * longer exist. + * + * @covers Two_Factor_Core::personal_data_exporter + */ + public function test_personal_data_exporter_falls_back_to_provider_key() { + $user = self::factory()->user->create_and_get(); + update_user_meta( $user->ID, Two_Factor_Core::ENABLED_PROVIDERS_USER_META_KEY, array( 'Two_Factor_Email', 'Removed_Provider' ) ); + update_user_meta( $user->ID, Two_Factor_Core::PROVIDER_USER_META_KEY, 'Removed_Provider' ); + + $response = Two_Factor_Core::personal_data_exporter( $user->user_email ); + + $items = $response['data'][0]['data']; + $values = array_combine( wp_list_pluck( $items, 'name' ), wp_list_pluck( $items, 'value' ) ); + + $this->assertSame( 'Email, Removed_Provider', $values['Enabled Two Factor methods'] ); + $this->assertSame( 'Removed_Provider', $values['Primary Two Factor method'] ); + } + + /** + * Verify malformed provider meta does not stop the export. + * + * @covers Two_Factor_Core::personal_data_exporter + */ + public function test_personal_data_exporter_handles_malformed_provider_meta() { + $user = self::factory()->user->create_and_get(); + update_user_meta( $user->ID, Two_Factor_Core::ENABLED_PROVIDERS_USER_META_KEY, array( 'Two_Factor_Email', array( 'Two_Factor_Totp' ) ) ); + update_user_meta( $user->ID, Two_Factor_Core::PROVIDER_USER_META_KEY, array( 'Two_Factor_Totp' ) ); + + $response = Two_Factor_Core::personal_data_exporter( $user->user_email ); + + $items = $response['data'][0]['data']; + $values = array_combine( wp_list_pluck( $items, 'name' ), wp_list_pluck( $items, 'value' ) ); + + $this->assertSame( 'Email, Unknown', $values['Enabled Two Factor methods'] ); + $this->assertSame( 'Unknown', $values['Primary Two Factor method'] ); + } + + /** + * Verify the exporter adds the Two Factor details to the sessions + * exported by core. + * + * @covers Two_Factor_Core::personal_data_exporter + */ + public function test_personal_data_exporter_includes_session_data() { + $user = self::factory()->user->create_and_get(); + update_user_meta( + $user->ID, + 'session_tokens', + array( + 'two-factor-session' => array( + 'expiration' => time() + HOUR_IN_SECONDS, + 'login' => time() - 100, + 'two-factor-login' => time() - 90, + 'two-factor-provider' => 'Two_Factor_Totp', + ), + 'enabled-session' => array( + 'expiration' => time() + HOUR_IN_SECONDS, + 'login' => time() - 80, + 'two-factor-login' => time() - 70, + 'two-factor-provider' => '', + ), + 'password-session' => array( + 'expiration' => time() + HOUR_IN_SECONDS, + 'login' => time() - 50, + ), + ) + ); + + $response = Two_Factor_Core::personal_data_exporter( $user->user_email ); + + $this->assertCount( 2, $response['data'] ); + + $item = $response['data'][0]; + $this->assertSame( 'session-tokens', $item['group_id'] ); + $this->assertSame( "session-tokens-{$user->ID}-two-factor-session", $item['item_id'] ); + + $values = array_combine( wp_list_pluck( $item['data'], 'name' ), wp_list_pluck( $item['data'], 'value' ) ); + $this->assertNotEmpty( $values['Two Factor verified'] ); + $this->assertSame( 'Authenticator App', $values['Two Factor method'] ); + + // The item ID must match core so both are merged into one session. + $core_export = wp_user_personal_data_exporter( $user->user_email ); + $this->assertContains( $item['item_id'], wp_list_pluck( $core_export['data'], 'item_id' ) ); + + // A session where Two Factor was enabled has no method to show. + $enabled_item = $response['data'][1]; + $this->assertSame( "session-tokens-{$user->ID}-enabled-session", $enabled_item['item_id'] ); + + $values = array_combine( wp_list_pluck( $enabled_item['data'], 'name' ), wp_list_pluck( $enabled_item['data'], 'value' ) ); + $this->assertArrayNotHasKey( 'Two Factor verified', $values ); + $this->assertNotEmpty( $values['Two Factor enabled'] ); + $this->assertStringContainsString( 'enabled during this session', $values['Two Factor method'] ); + } + + /** + * Verify the exporter includes the records kept by the providers. + * + * @covers Two_Factor_Core::personal_data_exporter + */ + public function test_personal_data_exporter_includes_provider_records() { + $setup = $this->get_fully_configured_user(); + $user = $setup['user']; + $response = Two_Factor_Core::personal_data_exporter( $user->user_email ); + + $items = $response['data'][0]['data']; + $names = wp_list_pluck( $items, 'name' ); + + $this->assertContains( 'Authenticator app (TOTP)', $names ); + $this->assertContains( 'Last successful TOTP verification', $names ); + $this->assertContains( 'Email login code', $names ); + $this->assertContains( 'Recovery codes', $names ); + + $values = array_combine( $names, wp_list_pluck( $items, 'value' ) ); + $this->assertSame( 'Configured', $values['Authenticator app (TOTP)'] ); + $this->assertNotEmpty( $values['Last successful TOTP verification'] ); + $this->assertStringContainsString( 'was sent on', $values['Email login code'] ); + $this->assertStringContainsString( '10 unused codes', $values['Recovery codes'] ); + } + + /** + * Verify the export contains no secret, code or hash. + * + * @covers Two_Factor_Core::personal_data_exporter + */ + public function test_personal_data_exporter_does_not_leak_secrets() { + $setup = $this->get_fully_configured_user(); + $user = $setup['user']; + $response = Two_Factor_Core::personal_data_exporter( $user->user_email ); + + $payload = wp_json_encode( $response ); + + foreach ( $setup['sensitive'] as $secret ) { + $this->assertStringNotContainsString( $secret, $payload ); + } + } + + /** + * Verify the exporter includes providers that are disabled in the + * site-wide settings. + * + * @covers Two_Factor_Core::personal_data_exporter + */ + public function test_personal_data_exporter_includes_site_disabled_providers() { + update_option( Two_Factor_Core::ENABLED_PROVIDERS_OPTION_KEY, array( 'Two_Factor_Email' ) ); + + $setup = $this->get_fully_configured_user(); + $user = $setup['user']; + $response = Two_Factor_Core::personal_data_exporter( $user->user_email ); + + $items = $response['data'][0]['data']; + $names = wp_list_pluck( $items, 'name' ); + + $this->assertContains( 'Authenticator app (TOTP)', $names ); + $this->assertContains( 'Recovery codes', $names ); + } + + /** + * Verify the exporter includes third-party providers that are disabled + * in the site-wide settings, and keeps the site-wide filter in place. + * + * @covers Two_Factor_Core::personal_data_exporter + */ + public function test_personal_data_exporter_includes_site_disabled_third_party_providers() { + update_option( Two_Factor_Core::ENABLED_PROVIDERS_OPTION_KEY, array( 'Two_Factor_Email' ) ); + + // Register a third-party provider before the site-wide filter runs. + $add_provider = static function ( $providers ) { + $providers['Two_Factor_Third_Party'] = ''; + return $providers; + }; + $map_class = static function () { + return 'Two_Factor_Dummy'; + }; + add_filter( 'two_factor_providers', $add_provider, 5 ); + add_filter( 'two_factor_provider_classname_Two_Factor_Third_Party', $map_class ); + + $user = self::factory()->user->create_and_get(); + update_user_meta( $user->ID, Two_Factor_Core::PROVIDER_USER_META_KEY, 'Two_Factor_Third_Party' ); + + $response = Two_Factor_Core::personal_data_exporter( $user->user_email ); + $items = wp_list_pluck( $response['data'][0]['data'], 'value', 'name' ); + + $this->assertSame( 'Dummy Method', $items['Primary Two Factor method'] ); + $this->assertArrayNotHasKey( 'Two_Factor_Third_Party', Two_Factor_Core::get_providers() ); + + remove_filter( 'two_factor_providers', $add_provider, 5 ); + remove_filter( 'two_factor_provider_classname_Two_Factor_Third_Party', $map_class ); + } + + /** + * Verify format_privacy_timestamp() handles empty and set timestamps. + * + * @covers Two_Factor_Core::format_privacy_timestamp + */ + public function test_format_privacy_timestamp() { + $this->assertSame( '', Two_Factor_Core::format_privacy_timestamp( '' ) ); + $this->assertSame( '', Two_Factor_Core::format_privacy_timestamp( 0 ) ); + + $timestamp = time() - 100; + $formatted = Two_Factor_Core::format_privacy_timestamp( $timestamp ); + + $this->assertNotEmpty( $formatted ); + $this->assertStringContainsString( gmdate( 'Y', $timestamp ), $formatted ); + } } diff --git a/tests/providers/class-two-factor-backup-codes.php b/tests/providers/class-two-factor-backup-codes.php index c399eac8..0a487107 100644 --- a/tests/providers/class-two-factor-backup-codes.php +++ b/tests/providers/class-two-factor-backup-codes.php @@ -376,6 +376,60 @@ function () { remove_all_filters( 'two_factor_backup_code_length' ); } + /** + * Verify privacy_export_data reports nothing for a user without codes. + * + * @covers Two_Factor_Backup_Codes::privacy_export_data + */ + public function test_privacy_export_data_without_codes() { + $user = self::factory()->user->create_and_get(); + + $this->assertSame( array(), $this->provider->privacy_export_data( $user ) ); + } + + /** + * Verify privacy_export_data reports the remaining count, not the codes. + * + * @covers Two_Factor_Backup_Codes::privacy_export_data + */ + public function test_privacy_export_data_with_codes() { + $user = self::factory()->user->create_and_get(); + $codes = $this->provider->generate_codes( $user, array( 'number' => 2 ) ); + $codes_hashed = (array) get_user_meta( $user->ID, Two_Factor_Backup_Codes::BACKUP_CODES_META_KEY, true ); + + $data = $this->provider->privacy_export_data( $user ); + + $this->assertCount( 1, $data ); + $this->assertSame( 'Recovery codes', $data[0]['name'] ); + $this->assertStringContainsString( '2 unused codes', $data[0]['value'] ); + + $payload = wp_json_encode( $data ); + foreach ( $codes as $code ) { + $this->assertStringNotContainsString( $code, $payload ); + } + foreach ( $codes_hashed as $hashed_code ) { + $this->assertStringNotContainsString( $hashed_code, $payload ); + } + } + + /** + * Verify privacy_export_data still reports a user who used all codes. + * + * @covers Two_Factor_Backup_Codes::privacy_export_data + */ + public function test_privacy_export_data_with_all_codes_used() { + $user = self::factory()->user->create_and_get(); + $codes = $this->provider->generate_codes( $user, array( 'number' => 1 ) ); + + $this->assertTrue( $this->provider->validate_code( $user, $codes[0] ) ); + + $data = $this->provider->privacy_export_data( $user ); + + $this->assertCount( 1, $data ); + $this->assertSame( 'Recovery codes', $data[0]['name'] ); + $this->assertStringContainsString( '0 unused codes', $data[0]['value'] ); + } + /** * Create a user with recovery codes enabled as a two-factor method. * diff --git a/tests/providers/class-two-factor-email.php b/tests/providers/class-two-factor-email.php index 441b7618..c3b5335d 100644 --- a/tests/providers/class-two-factor-email.php +++ b/tests/providers/class-two-factor-email.php @@ -781,4 +781,52 @@ public function test_uninstall_user_meta_keys() { $this->assertContains( Two_Factor_Email::TOKEN_META_KEY, $keys ); $this->assertContains( Two_Factor_Email::TOKEN_META_KEY_TIMESTAMP, $keys ); } + + /** + * Verify privacy_export_data reports nothing for a user without a token. + * + * @covers Two_Factor_Email::privacy_export_data + */ + public function test_privacy_export_data_without_token() { + $user = self::factory()->user->create_and_get(); + + $this->assertSame( array(), $this->provider->privacy_export_data( $user ) ); + } + + /** + * Verify privacy_export_data reports when the code was sent, not the hash. + * + * @covers Two_Factor_Email::privacy_export_data + */ + public function test_privacy_export_data_with_token() { + $user = self::factory()->user->create_and_get(); + $token = $this->provider->generate_token( $user->ID ); + + $data = $this->provider->privacy_export_data( $user ); + + $this->assertCount( 1, $data ); + $this->assertSame( 'Email login code', $data[0]['name'] ); + $this->assertStringContainsString( 'was sent on', $data[0]['value'] ); + + $payload = wp_json_encode( $data ); + $this->assertStringNotContainsString( $token, $payload ); + $this->assertStringNotContainsString( wp_hash( $token ), $payload ); + } + + /** + * Verify privacy_export_data still reports the send time after the + * token was consumed, because the timestamp meta stays stored. + * + * @covers Two_Factor_Email::privacy_export_data + */ + public function test_privacy_export_data_after_token_deleted() { + $user = self::factory()->user->create_and_get(); + $this->provider->generate_token( $user->ID ); + $this->provider->delete_token( $user->ID ); + + $data = $this->provider->privacy_export_data( $user ); + + $this->assertCount( 1, $data ); + $this->assertStringContainsString( 'was sent on', $data[0]['value'] ); + } } diff --git a/tests/providers/class-two-factor-provider.php b/tests/providers/class-two-factor-provider.php index 09cec1a4..3535e1a0 100644 --- a/tests/providers/class-two-factor-provider.php +++ b/tests/providers/class-two-factor-provider.php @@ -176,6 +176,18 @@ public function test_uninstall_options_base_returns_empty() { $this->assertSame( array(), Two_Factor_Dummy::uninstall_options() ); } + /** + * Verify the base privacy_export_data() returns an empty array. + * + * @covers Two_Factor_Provider::privacy_export_data + */ + public function test_privacy_export_data_base_returns_empty() { + $provider = Two_Factor_Dummy::get_instance(); + $user = self::factory()->user->create_and_get(); + + $this->assertSame( array(), $provider->privacy_export_data( $user ) ); + } + /** * Verify is_authcode_auto_submit_enabled() defaults to true. * diff --git a/tests/providers/class-two-factor-totp.php b/tests/providers/class-two-factor-totp.php index ba2ed709..88c95b4f 100644 --- a/tests/providers/class-two-factor-totp.php +++ b/tests/providers/class-two-factor-totp.php @@ -587,6 +587,44 @@ public function test_pad_secret_zero_length_throws_exception() { $this->call_pad_secret( 'ABC', 0 ); } + /** + * Verify privacy_export_data() reports nothing for a user without a key. + * + * @covers Two_Factor_Totp::privacy_export_data + */ + public function test_privacy_export_data_without_key() { + $user = self::factory()->user->create_and_get(); + $provider = Two_Factor_Totp::get_instance(); + + $this->assertSame( array(), $provider->privacy_export_data( $user ) ); + } + + /** + * Verify privacy_export_data() reports configuration and the last login + * date, but not the secret. + * + * @covers Two_Factor_Totp::privacy_export_data + */ + public function test_privacy_export_data_with_key() { + $user = self::factory()->user->create_and_get(); + $provider = Two_Factor_Totp::get_instance(); + + $key = Two_Factor_Totp::generate_key(); + $provider->set_user_totp_key( $user->ID, $key ); + update_user_meta( $user->ID, Two_Factor_Totp::LAST_SUCCESSFUL_LOGIN_META_KEY, time() - 50 ); + + $data = $provider->privacy_export_data( $user ); + + $this->assertCount( 2, $data ); + $this->assertSame( 'Authenticator app (TOTP)', $data[0]['name'] ); + $this->assertSame( 'Configured', $data[0]['value'] ); + $this->assertSame( 'Last successful TOTP verification', $data[1]['name'] ); + $this->assertNotEmpty( $data[1]['value'] ); + + $payload = wp_json_encode( $data ); + $this->assertStringNotContainsString( $key, $payload ); + } + /** * Verify the TOTP code input enables auto-submit by default. *