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

Local Env: Only attempt to use encryption when the keys are defined #156

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Changes from all commits
Commits
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
21 changes: 19 additions & 2 deletions class-encrypted-totp-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function get_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' ) ) {
if ( self::encryption_enabled() ) {
$key = wporg_encrypt( $key, (string) $user_id, 'two-factor' );
}

Expand All @@ -39,7 +39,7 @@ public function set_user_totp_key( $user_id, $key ) {
public function get_user_totp_key( $user_id ) {
$key = parent::get_user_totp_key( $user_id );

if ( $key && function_exists( 'wporg_is_encrypted' ) ) {
if ( $key && self::encryption_enabled() ) {
if ( wporg_is_encrypted( $key ) ) {
$key = (string) wporg_decrypt( $key, (string) $user_id, 'two-factor' );
} else {
Expand All @@ -50,4 +50,21 @@ public function get_user_totp_key( $user_id ) {

return $key;
}

/**
* Test whether encryption is available.
*/
private static function encryption_enabled() {
if ( ! function_exists( 'wporg_is_encrypted' ) ) {
return false;
}

// On local systems, encryption is not enabled if the constant is missing.
if ( 'local' == wp_get_environment_type() && ! defined( 'WPORG_TWO_FACTOR_ENCRYPTION_KEY' ) ) {
return false;
}

// Else, encryption functions are available, it's not local, or keys are defined.
return true;
}
}