PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.1.0
Defender Security – Malware Scanner, Login Security & Firewall v3.1.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / two-factor / providers / totp.php

totp.php in Defender Security – Malware Scanner, Login Security & Firewall 3.1.0, at src/component/two-factor/providers/totp.php

174 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WP_Defender\Component\Two_Factor\Providers;
4
5 use Calotes\Helper\HTTP;
6 use WP_Defender\Component\Two_Factor\Two_Factor_Provider;
7
8 /**
9 * Class Totp
10 * Note: key 'defenderAuthOn' only for TOTP method.
11 *
12 * @since 2.8.0
13 * @package WP_Defender\Component\Two_Factor\Providers
14 */
15 class Totp extends Two_Factor_Provider {
16 static $slug = 'totp';
17
18 protected $label;
19
20 protected $description;
21
22 public function __construct() {
23 add_action( 'wd_2fa_init_provider_' . self::$slug, array( &$this, 'init_provider' ) );
24 add_action( 'wd_2fa_user_options_' . self::$slug, array( &$this, 'user_options' ) );
25 }
26
27 /**
28 * Get the name of the provider.
29 *
30 * @return string
31 */
32 public function get_label() {
33 return __( 'TOTP Authenticator App', 'wpdef' ) . $this->label;
34 }
35
36 /**
37 * @return string
38 */
39 public function get_login_label() {
40 return __( 'TOTP Authentication', 'wpdef' );
41 }
42
43 /**
44 * @return string
45 */
46 public function get_user_label() {
47 return __( 'TOTP', 'wpdef' );
48 }
49
50 /**
51 * Get the desc of the provider.
52 *
53 * @return string
54 */
55 public function get_description() {
56 return $this->description;
57 }
58
59 public function authentication_form() {
60 ?>
61 <p class="wpdef-2fa-label"><?php echo $this->get_login_label(); ?></p>
62 <p class="wpdef-2fa-text def-otp-text"><?php esc_html_e( $this->get_model()->app_text ); ?></p>
63 <input type="text" autofocus value="" autocomplete="off" name="otp" />
64 <button class="button button-primary float-r" type="submit"><?php _e( "Authenticate", 'wpdef' ) ?></button>
65 <?php
66 }
67
68 /**
69 * @return array
70 */
71 public function get_auth_apps() {
72 return array(
73 'google-authenticator' => 'Google Authenticator',
74 'microsoft-authenticator' => 'Microsoft Authenticator',
75 'authy' => 'Authy',
76 );
77 }
78
79 /**
80 * @param \WP_User $user
81 */
82 public function init_provider( $user ) {
83 $is_on = $this->is_available_for_user( $user );
84 $this->label = $is_on
85 ? sprintf(
86 /* translators: %s: style class */
87 __( '<button type="button" class="button reset-totp-keys button-secondary hide-if-no-js" %s>Reset Keys</button>', 'wpdef' ),
88 $this->get_component()->is_checked_enabled_provider_by_slug( $user, self::$slug ) ? '' : ' disabled'
89 )
90 : '';
91 $this->description = $is_on
92 ? __( 'TOTP Authentication method is active for this site', 'wpdef' )
93 : __( 'Use an authenticator app to sign in with a separate passcode.', 'wpdef' );
94 }
95
96 /**
97 * Display auth method.
98 *
99 * @param WP_User $user
100 */
101 public function user_options( $user ) {
102 if ( ! wp_script_is( 'clipboard', 'enqueued' ) ) {
103 wp_enqueue_script( 'clipboard' );
104 }
105 $model = $this->get_model();
106 $service = $this->get_component();
107 $default_values = $model->get_default_values();
108 $is_on = $this->is_available_for_user($user);
109 if ( $is_on ) {
110 $this->get_controller()->render_partial(
111 'two-fa/providers/totp-enabled',
112 array(
113 'url' => $this->get_url( 'disable_totp' ),
114 )
115 );
116 } else {
117 $this->get_controller()->render_partial(
118 'two-fa/providers/totp-disabled',
119 array(
120 'url' => $this->get_url( 'verify_otp_for_enabling' ),
121 'default_message' => $default_values['message'],
122 'auth_apps' => $this->get_auth_apps(),
123 'user' => $user,
124 'secret_key' => $service::get_user_secret(),
125 'class' => $service->is_checked_enabled_provider_by_slug( $user, self::$slug ) ? '' : 'hidden',
126 )
127 );
128 }
129 }
130
131 /**
132 * Generate a QR code for apps can use. Apps from get_auth_apps().
133 *
134 * @param string $secret_key
135 */
136 public static function generate_qr_code( $secret_key ) {
137 $settings = new \WP_Defender\Model\Setting\Two_Fa();
138 $issuer = $settings->app_title;
139 $user = wp_get_current_user();
140 $chl = ( 'otpauth://totp/' . rawurlencode( $issuer ) . ':' . rawurlencode( $user->user_email ) . '?secret=' . $secret_key . '&issuer=' . rawurlencode( $issuer ) );
141 require_once defender_path( 'src/extra/phpqrcode/phpqrcode.php' );
142
143 \QRcode::svg( $chl, false, QR_ECLEVEL_L, 4 );
144 }
145
146 /**
147 * Whether this 2FA provider is configured and available for the user specified.
148 *
149 * @param WP_User $user WP_User object of the logged-in user.
150 *
151 * @return boolean
152 */
153 public function is_available_for_user( $user ) {
154 return (bool) get_user_meta( $user->ID, 'defenderAuthOn', true );
155 }
156
157 /**
158 * @param \WP_User $user
159 *
160 * @return bool|\WP_Error
161 */
162 public function validate_authentication( $user ) {
163 $otp = HTTP::post( 'otp' );
164 if ( empty( $otp ) ) {
165 return new \WP_Error(
166 'opt_fail',
167 __( 'Whoops, the passcode you entered was incorrect or expired.', 'wpdef' )
168 );
169 }
170
171 return $this->get_component()->verify_otp( $otp, $user );
172 }
173 }
174