<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[6748] sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-two-factor/wporg-two-factor.php: 2FA: `WPORG_Two_Factor` now extends `Two_Factor_Core` so that we can override some of the larger surface methods.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/6748">6748</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/6748","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-02-26 01:22:21 +0000 (Mon, 26 Feb 2018)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>2FA: `WPORG_Two_Factor` now extends `Two_Factor_Core` so that we can override some of the larger surface methods.

This is being done to allow us to customise how some functionality of the plugin works.

See <a href="http://meta.trac.wordpress.org/ticket/77">#77</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginswporgtwofactorwporgtwofactorphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-two-factor/wporg-two-factor.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginswporgtwofactorwporgtwofactorphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-two-factor/wporg-two-factor.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-two-factor/wporg-two-factor.php    2018-02-26 01:08:02 UTC (rev 6747)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-two-factor/wporg-two-factor.php      2018-02-26 01:22:21 UTC (rev 6748)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10,7 +10,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @package WordPressdotorg\TwoFactor
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-class WPORG_Two_Factor {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+class WPORG_Two_Factor extends Two_Factor_Core {
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function __construct() {
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'two_factor_providers', [ $this, 'two_factor_providers' ] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -22,16 +23,168 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        add_action( 'show_user_profile', [ $this, 'user_two_factor_options' ] );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_action( 'wp_ajax_two-factor-totp-verify-code',[ $this, 'ajax_verify_code' ] );
-               add_action( 'wp_ajax_two-factor-disable',[ $this, 'ajax_disable' ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_action( 'wp_ajax_two-factor-totp-verify-code', [ $this, 'ajax_verify_code' ] );
+               add_action( 'wp_ajax_two-factor-disable',          [ $this, 'ajax_disable' ] );
+
+               // Auth cookie unsetting.
+               remove_action( 'wp_login',                [ 'Two_Factor_Core', 'wp_login' ], 10, 2 );
+               remove_action( 'login_form_validate_2fa', [ 'Two_Factor_Core', 'login_form_validate_2fa' ] );
+               remove_action( 'login_form_backup_2fa',   [ 'Two_Factor_Core', 'backup_2fa' ] );
+
+               add_action( 'wp_login',                [ $this, 'wp_login' ], 10, 2 );
+               add_action( 'login_form_validate_2fa', [ $this, 'login_form_validate_2fa' ] );
+               add_action( 'login_form_backup_2fa',   [ $this, 'backup_2fa' ] );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function two_factor_providers( $providers ) {
-               // Limit 2FA to certain users during Development.
-               if ( ! is_user_logged_in() || ! in_array( get_current_user_id(), [ 148148, 196012, 8772187 ] ) ) {
-                       return array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Handle the browser-based login.
+        *
+        * @since 0.1-dev
+        *
+        * @param string  $user_login Username.
+        * @param WP_User $user WP_User object of the logged-in user.
+        */
+       public static function wp_login( $user_login, $user ) {
+               if ( ! self::is_user_using_two_factor( $user->ID ) ) {
+                       return;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                wp_clear_auth_cookie();
+
+               self::show_two_factor_login( $user );
+               exit;
+       }
+
+       /**
+        * Login form validation.
+        *
+        * @since 0.1-dev
+        */
+       public static function login_form_validate_2fa() {
+               if ( ! isset( $_POST['wp-auth-id'], $_POST['wp-auth-nonce'] ) ) {
+                       return;
+               }
+
+               $user = get_userdata( $_POST['wp-auth-id'] );
+               if ( ! $user ) {
+                       return;
+               }
+
+               $nonce = $_POST['wp-auth-nonce'];
+               if ( true !== self::verify_login_nonce( $user->ID, $nonce ) ) {
+                       wp_safe_redirect( get_bloginfo( 'url' ) );
+                       exit;
+               }
+
+               if ( isset( $_POST['provider'] ) ) {
+                       $providers = self::get_available_providers_for_user( $user );
+                       if ( isset( $providers[ $_POST['provider'] ] ) ) {
+                               $provider = $providers[ $_POST['provider'] ];
+                       } else {
+                               wp_die( esc_html__( 'Cheatin&#8217; uh?' ), 403 );
+                       }
+               } else {
+                       $provider = self::get_primary_provider_for_user( $user->ID );
+               }
+
+               // Allow the provider to re-send codes, etc.
+               if ( true === $provider->pre_process_authentication( $user ) ) {
+                       $login_nonce = self::create_login_nonce( $user->ID );
+                       if ( ! $login_nonce ) {
+                               wp_die( esc_html__( 'Failed to create a login nonce.', 'two-factor' ) );
+                       }
+
+                       self::login_html( $user, $login_nonce['key'], $_REQUEST['redirect_to'], '', $provider );
+                       exit;
+               }
+
+               // Ask the provider to verify the second factor.
+               if ( true !== $provider->validate_authentication( $user ) ) {
+                       do_action( 'wp_login_failed', $user->user_login );
+
+                       $login_nonce = self::create_login_nonce( $user->ID );
+                       if ( ! $login_nonce ) {
+                               wp_die( esc_html__( 'Failed to create a login nonce.', 'two-factor' ) );
+                       }
+
+                       self::login_html( $user, $login_nonce['key'], $_REQUEST['redirect_to'], esc_html__( 'ERROR: Invalid verification code.', 'two-factor' ), $provider );
+                       exit;
+               }
+
+               self::delete_login_nonce( $user->ID );
+
+               $rememberme = false;
+               if ( isset( $_REQUEST['rememberme'] ) && $_REQUEST['rememberme'] ) {
+                       $rememberme = true;
+               }
+
+               wp_set_auth_cookie( $user->ID, $rememberme );
+
+               // Must be global because that's how login_header() uses it.
+               global $interim_login;
+               $interim_login = isset( $_REQUEST['interim-login'] ); // WPCS: override ok.
+
+               if ( $interim_login ) {
+                       $customize_login = isset( $_REQUEST['customize-login'] );
+                       if ( $customize_login ) {
+                               wp_enqueue_script( 'customize-base' );
+                       }
+                       $message = '<p class="message">' . __( 'You have logged in successfully.' ) . '</p>';
+                       $interim_login = 'success'; // WPCS: override ok.
+                       login_header( '', $message ); ?>
+                       </div>
+                       <?php
+                       /** This action is documented in wp-login.php */
+                       do_action( 'login_footer' ); ?>
+                       <?php if ( $customize_login ) : ?>
+                               <script type="text/javascript">setTimeout( function(){ new wp.customize.Messenger({ url: '<?php echo wp_customize_url(); /* WPCS: XSS OK. */ ?>', channel: 'login' }).send('login') }, 1000 );</script>
+                       <?php endif; ?>
+                       </body></html>
+                       <?php
+                       exit;
+               }
+               $redirect_to = apply_filters( 'login_redirect', $_REQUEST['redirect_to'], $_REQUEST['redirect_to'], $user );
+               wp_safe_redirect( $redirect_to );
+
+               exit;
+       }
+
+
+       /**
+        * Add short description. @todo
+        *
+        * @since 0.1-dev
+        */
+       public static function backup_2fa() {
+               if ( ! isset( $_GET['wp-auth-id'], $_GET['wp-auth-nonce'], $_GET['provider'] ) ) {
+                       return;
+               }
+
+               $user = get_userdata( $_GET['wp-auth-id'] );
+               if ( ! $user ) {
+                       return;
+               }
+
+               $nonce = $_GET['wp-auth-nonce'];
+               if ( true !== self::verify_login_nonce( $user->ID, $nonce ) ) {
+                       wp_safe_redirect( get_bloginfo( 'url' ) );
+                       exit;
+               }
+
+               $providers = self::get_available_providers_for_user( $user );
+               if ( isset( $providers[ $_GET['provider'] ] ) ) {
+                       $provider = $providers[ $_GET['provider'] ];
+               } else {
+                       wp_die( esc_html__( 'Cheatin&#8217; uh?' ), 403 );
+               }
+
+               self::login_html( $user, $_GET['wp-auth-nonce'], $_GET['redirect_to'], '', $provider );
+
+               exit;
+       }
+
+       public function two_factor_providers( $providers) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $wporg_providers = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'WPORG_Two_Factor_Email'        => __DIR__ . '/providers/class-wporg-two-factor-email.php',
</span><span class="cx" style="display: block; padding: 0 10px">                        'WPORG_Two_Factor_Totp'         => __DIR__ . '/providers/class-wporg-two-factor-totp.php',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -47,7 +200,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param \WP_User $user User object.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function user_two_factor_options( $user ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static function user_two_factor_options( $user ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 wp_enqueue_script( 'two-factor-edit', plugins_url( 'js/profile-edit.js' , __FILE__ ), [ 'jquery' ], 1, true );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $key       = get_user_meta( $user->ID, Two_Factor_Totp::SECRET_META_KEY, true );
</span></span></pre>
</div>
</div>

</body>
</html>