Skip to content
Merged
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
52 changes: 36 additions & 16 deletions providers/class-two-factor-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,22 @@ public function delete_token( $user_id ) {
delete_user_meta( $user_id, self::TOKEN_META_KEY );
}

/**
* Get the client IP address for the current request.
*
* Note that the IP address is used only for information purposes

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain the expected setup for this and known limitations.

* and is expected to be configured correctly, if behind proxy.
*
* @return string|null
*/
private function get_client_ip() {
if ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) { // phpcs:ignore WordPressVIPMinimum.Variables.ServerVariables.UserControlledHeaders -- don't have more reliable option for now.
return preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPressVIPMinimum.Variables.ServerVariables.UserControlledHeaders, WordPressVIPMinimum.Variables.RestrictedVariables.cache_constraints___SERVER__REMOTE_ADDR__ -- we're limit the allowed characters.
}

return null;
}

/**
* Generate and email the user token.
*
Expand All @@ -244,28 +260,32 @@ public function delete_token( $user_id ) {
*/
public function generate_and_email_token( $user ) {
$token = $this->generate_token( $user->ID );
$remote_ip = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] );
$remote_ip = $this->get_client_ip();

/* translators: %s: site name */
$subject = wp_strip_all_tags( sprintf( __( 'Your login confirmation code for %s', 'two-factor' ), wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) ) );
$subject = wp_strip_all_tags(
sprintf(
/* translators: %s: site name */
__( 'Your login confirmation code for %s', 'two-factor' ),
wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES )
)
);

$message = wp_strip_all_tags(
$message_parts = array(
sprintf(
/* translators: %1$s: token, $2$s: IP address of user, %3$s: `user_login` of authenticated user */
__(
'Enter %1$s to log in.

Didn\'t expect this?
A user from %2$s has successfully authenticated as %3$s.
If this wasn\'t you, please change your password.',
'two-factor'
),
$token,
/* translators: %s: token */
__( 'Enter %s to log in.', 'two-factor' ),
$token
),
sprintf(
/* translators: $1$s: IP address of user, %2$s: `user_login` of authenticated user */
__( 'Didn\'t expect this? A user from %1$s has successfully authenticated as %2$s. If this wasn\'t you, please change your password.', 'two-factor' ),
$remote_ip,
$user->user_login
)
),
);

$message = wp_strip_all_tags( implode( "\n\n", $message_parts ) );

/**
* Filter the token email subject.
*
Expand Down Expand Up @@ -337,7 +357,7 @@ public function authentication_page( $user ) {
* @return boolean
*/
public function pre_process_authentication( $user ) {
if ( isset( $user->ID ) && isset( $_REQUEST[ self::INPUT_NAME_RESEND_CODE ] ) ) {
if ( isset( $user->ID ) && isset( $_REQUEST[ self::INPUT_NAME_RESEND_CODE ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- non-distructive option that relies on user state.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed the rest of PHPCS warnings for the file.

$this->generate_and_email_token( $user );
return true;
}
Expand Down