-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add a Two_Factor_TOTP class variant that uses encryption. * Upgrade unencrypted keys to encrypted upon retrieval. * Switch to using authenticated encryption. The encrypted keys are only valid per user ID. * Tests: Test that the class is expected, but also that it returns the expected key. * Tests: Validate that the TOTP key is stored in an encrypted format in the user_meta. * Set the users TOTP key via the provider, to allow it to be encrypted. * Tests: Add a test that verifies that Secret keys are upgraded from stored non-encrypted to encrypted. --------- Co-authored-by: Paul Kevan <[email protected]>
- Loading branch information
Showing
3 changed files
with
121 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
namespace WordPressdotorg\Two_Factor; | ||
use Two_Factor_Totp; | ||
|
||
/** | ||
* Extends the default Two_Factor_Totp class to encrypt the TOTP key. | ||
*/ | ||
class Encrypted_Totp_Provider extends Two_Factor_Totp { | ||
/** | ||
* Use the parent class as the "key" in the Two Factor UI. | ||
*/ | ||
public function get_key() { | ||
return parent::class; | ||
} | ||
|
||
/** | ||
* When saving the key, encrypt it first. | ||
* | ||
* @param int $user_id User ID. | ||
* @param string $key TOTP key. | ||
* @return bool True if the key was saved, false otherwise. | ||
*/ | ||
public function set_user_totp_key( $user_id, $key ) { | ||
if ( function_exists( 'wporg_encrypt' ) ) { | ||
$key = wporg_encrypt( $key, (string) $user_id, 'two-factor' ); | ||
} | ||
|
||
return parent::set_user_totp_key( $user_id, (string) $key ); | ||
} | ||
|
||
/** | ||
* When retrieving the key, decrypt it first. | ||
* | ||
* If the key isn't currently stored encrypted, it's upgraded to encrypted status. | ||
* | ||
* @param int $user_id User ID. | ||
* @return string|false TOTP key, or false if not set. | ||
*/ | ||
public function get_user_totp_key( $user_id ) { | ||
$key = parent::get_user_totp_key( $user_id ); | ||
|
||
if ( $key && function_exists( 'wporg_is_encrypted' ) ) { | ||
if ( wporg_is_encrypted( $key ) ) { | ||
$key = (string) wporg_decrypt( $key, (string) $user_id, 'two-factor' ); | ||
} else { | ||
// Upgrade the key to be encrypted. | ||
$this->set_user_totp_key( $user_id, $key ); | ||
} | ||
} | ||
|
||
return $key; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters