| @@ -14,31 +14,34 @@ | ||
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | 16 | * Contains modifications to the WordPress login form. |
| 17 | 17 | */ |
| 18 | -class Login_Form extends Static_Instance { | |
| 18 | +class Login_Form extends Singleton { | |
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | - * Load external resources for the public-facing site. | |
| 21 | + * Load script to display message to anonymous users browing a site (only | |
| 22 | + * enqueue if configured to only allow logged in users to view the site and | |
| 23 | + * show a warning to anonymous users). | |
| 22 | 24 | * |
| 23 | 25 | * Action: wp_enqueue_scripts |
| 24 | 26 | */ |
| 25 | 27 | public function auth_public_scripts() { |
| 26 | 28 | // Load (and localize) public scripts. |
| 27 | - $options = Options::get_instance(); | |
| 28 | - $current_path = ! empty( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : home_url(); | |
| 29 | - wp_enqueue_script( 'auth_public_scripts', plugins_url( '/js/authorizer-public.js', plugin_root() ), array( 'jquery' ), '2.8.0', false ); | |
| 30 | - $auth_localized = array( | |
| 31 | - 'wpLoginUrl' => wp_login_url( $current_path ), | |
| 32 | - 'publicWarning' => get_option( 'auth_settings_advanced_public_notice' ), | |
| 33 | - 'anonymousNotice' => $options->get( 'access_redirect_to_message' ), | |
| 34 | - 'logIn' => esc_html__( 'Log In', 'authorizer' ), | |
| 35 | - ); | |
| 36 | - wp_localize_script( 'auth_public_scripts', 'auth', $auth_localized ); | |
| 37 | - | |
| 38 | - // Load public css. | |
| 39 | - wp_register_style( 'authorizer-public-css', plugins_url( 'css/authorizer-public.css', plugin_root() ), array(), '2.8.0' ); | |
| 40 | - wp_enqueue_style( 'authorizer-public-css' ); | |
| 29 | + $options = Options::get_instance(); | |
| 30 | + if ( | |
| 31 | + 'logged_in_users' === $options->get( 'access_who_can_view' ) && | |
| 32 | + 'warning' === $options->get( 'access_public_warning' ) && | |
| 33 | + get_option( 'auth_settings_advanced_public_notice' ) | |
| 34 | + ) { | |
| 35 | + $current_path = ! empty( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : home_url(); | |
| 36 | + wp_enqueue_script( 'auth_public_scripts', plugins_url( '/js/authorizer-public.js', plugin_root() ), array( 'jquery' ), '3.2.2', false ); | |
| 37 | + $auth_localized = array( | |
| 38 | + 'wpLoginUrl' => wp_login_url( $current_path ), | |
| 39 | + 'anonymousNotice' => $options->get( 'access_redirect_to_message' ), | |
| 40 | + 'logIn' => esc_html__( 'Log In', 'authorizer' ), | |
| 41 | + ); | |
| 42 | + wp_localize_script( 'auth_public_scripts', 'auth', $auth_localized ); | |
| 43 | + } | |
| 41 | 44 | } |
| 42 | 45 | |
| 43 | 46 | |
| 44 | 47 | /** |
| @@ -56,9 +59,9 @@ | ||
| 56 | 59 | // Enqueue scripts appearing on wp-login.php. |
| 57 | 60 | wp_enqueue_script( 'auth_login_scripts', plugins_url( '/js/authorizer-login.js', plugin_root() ), array( 'jquery' ), '2.8.0', false ); |
| 58 | 61 | |
| 59 | 62 | // Enqueue styles appearing on wp-login.php. |
| 60 | - wp_register_style( 'authorizer-login-css', plugins_url( '/css/authorizer-login.css', plugin_root() ), array(), '2.9.8' ); | |
| 63 | + wp_register_style( 'authorizer-login-css', plugins_url( '/css/authorizer-login.css', plugin_root() ), array(), '3.2.0' ); | |
| 61 | 64 | wp_enqueue_style( 'authorizer-login-css' ); |
| 62 | 65 | |
| 63 | 66 | /** |
| 64 | 67 | * Developers can use the `authorizer_add_branding_option` filter |
| @@ -112,9 +115,9 @@ | ||
| 112 | 115 | $auth_settings = $options->get_all( Helper::SINGLE_CONTEXT, 'allow override' ); |
| 113 | 116 | $ajaxurl = admin_url( 'admin-ajax.php' ); |
| 114 | 117 | if ( '1' === $auth_settings['google'] ) : |
| 115 | 118 | ?> |
| 116 | -<script type="text/javascript"> | |
| 119 | +<script> | |
| 117 | 120 | /* global location, window */ |
| 118 | 121 | // Reload login page if reauth querystring param exists, |
| 119 | 122 | // since reauth interrupts external logins (e.g., google). |
| 120 | 123 | if ( location.search.indexOf( 'reauth=1' ) >= 0 ) { |
| @@ -150,8 +153,15 @@ | ||
| 150 | 153 | // console.log( response ); |
| 151 | 154 | |
| 152 | 155 | // Reload wp-login.php to continue the authentication process. |
| 153 | 156 | var newHref = authUpdateQuerystringParam( location.href, 'external', 'google' ); |
| 157 | + | |
| 158 | + // If we have a login form embedded via [authorizer_login_form], we are | |
| 159 | + // not on wp-login.php, so change the location to wp-login.php. | |
| 160 | + if ( 'undefined' !== typeof auth && auth.hasOwnProperty( 'wpLoginUrl' ) ) { | |
| 161 | + newHref = authUpdateQuerystringParam( auth.wpLoginUrl, 'external', 'google' ); | |
| 162 | + } | |
| 163 | + | |
| 154 | 164 | if ( location.href === newHref ) { |
| 155 | 165 | location.reload(); |
| 156 | 166 | } else { |
| 157 | 167 | location.href = newHref; |
| @@ -192,10 +202,27 @@ | ||
| 192 | 202 | <p><a id="googleplus_button" class="button button-primary button-external button-google"><span class="dashicons dashicons-googleplus"></span><span class="label"><?php esc_html_e( 'Sign in with Google', 'authorizer' ); ?></span></a></p> |
| 193 | 203 | <?php wp_nonce_field( 'google_csrf_nonce', 'nonce_google_auth-' . Helper::get_cookie_value() ); ?> |
| 194 | 204 | <?php endif; ?> |
| 195 | 205 | |
| 206 | + <?php if ( '1' === $auth_settings['oauth2'] ) : ?> | |
| 207 | + <p><a class="button button-primary button-external button-<?php echo esc_attr( $auth_settings['oauth2_provider'] ); ?>" href="<?php echo esc_attr( Helper::modify_current_url_for_external_login( 'oauth2' ) ); ?>"> | |
| 208 | + <span class="dashicons dashicons-lock"></span> | |
| 209 | + <span class="label"> | |
| 210 | + <?php | |
| 211 | + echo esc_html( | |
| 212 | + sprintf( | |
| 213 | + /* TRANSLATORS: %s: Custom OAuth2 label from authorizer options */ | |
| 214 | + __( 'Sign in with %s', 'authorizer' ), | |
| 215 | + $auth_settings['oauth2_custom_label'] | |
| 216 | + ) | |
| 217 | + ); | |
| 218 | + ?> | |
| 219 | + </span> | |
| 220 | + </a></p> | |
| 221 | + <?php endif; ?> | |
| 222 | + | |
| 196 | 223 | <?php if ( '1' === $auth_settings['cas'] ) : ?> |
| 197 | - <p><a class="button button-primary button-external button-cas" href="<?php echo esc_attr( Helper::modify_current_url_for_cas_login() ); ?>"> | |
| 224 | + <p><a class="button button-primary button-external button-cas" href="<?php echo esc_attr( Helper::modify_current_url_for_external_login( 'cas' ) ); ?>"> | |
| 198 | 225 | <span class="dashicons dashicons-lock"></span> |
| 199 | 226 | <span class="label"> |
| 200 | 227 | <?php |
| 201 | 228 | echo esc_html( |
| @@ -209,9 +236,9 @@ | ||
| 209 | 236 | </span> |
| 210 | 237 | </a></p> |
| 211 | 238 | <?php endif; ?> |
| 212 | 239 | |
| 213 | - <?php if ( isset( $auth_settings['advanced_hide_wp_login'] ) && '1' === $auth_settings['advanced_hide_wp_login'] && isset( $_SERVER['QUERY_STRING'] ) && false === strpos( $_SERVER['QUERY_STRING'], 'external=wordpress' ) ) : // phpcs:ignore WordPress.Security.ValidatedSanitizedInput ?> | |
| 240 | + <?php if ( ( isset( $auth_settings['advanced_hide_wp_login'] ) && '1' === $auth_settings['advanced_hide_wp_login'] && isset( $_SERVER['QUERY_STRING'] ) && false === strpos( $_SERVER['QUERY_STRING'], 'external=wordpress' ) ) || ( isset( $auth_settings['advanced_disable_wp_login'] ) && '1' === $auth_settings['advanced_disable_wp_login'] && '1' !== $auth_settings['ldap'] && ( '1' === $auth_settings['cas'] || '1' === $auth_settings['google'] ) ) ) : // phpcs:ignore WordPress.Security.ValidatedSanitizedInput ?> | |
| 214 | 241 | <style type="text/css"> |
| 215 | 242 | body.login-action-login form { |
| 216 | 243 | padding-bottom: 8px; |
| 217 | 244 | } |
| @@ -223,9 +250,9 @@ | ||
| 223 | 250 | body.login-action-login #nav { /* csslint allow: ids */ |
| 224 | 251 | display: none; |
| 225 | 252 | } |
| 226 | 253 | </style> |
| 227 | - <?php elseif ( '1' === $auth_settings['cas'] || '1' === $auth_settings['google'] ) : ?> | |
| 254 | + <?php elseif ( '1' === $auth_settings['cas'] || '1' === $auth_settings['google'] || '1' === $auth_settings['oauth2'] ) : ?> | |
| 228 | 255 | <h3> — <?php esc_html_e( 'or', 'authorizer' ); ?> — </h3> |
| 229 | 256 | <?php endif; ?> |
| 230 | 257 | </div> |
| 231 | 258 | <?php |
| @@ -258,11 +285,12 @@ | ||
| 258 | 285 | array_key_exists( 'cas_auto_login', $auth_settings ) && '1' === $auth_settings['cas_auto_login'] && |
| 259 | 286 | array_key_exists( 'cas', $auth_settings ) && '1' === $auth_settings['cas'] && |
| 260 | 287 | ( ! array_key_exists( 'ldap', $auth_settings ) || '1' !== $auth_settings['ldap'] ) && |
| 261 | 288 | ( ! array_key_exists( 'google', $auth_settings ) || '1' !== $auth_settings['google'] ) && |
| 289 | + ( ! array_key_exists( 'oauth2', $auth_settings ) || '1' !== $auth_settings['oauth2'] ) && | |
| 262 | 290 | array_key_exists( 'advanced_hide_wp_login', $auth_settings ) && '1' === $auth_settings['advanced_hide_wp_login'] |
| 263 | 291 | ) { |
| 264 | - wp_redirect( Helper::modify_current_url_for_cas_login() ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect | |
| 292 | + wp_redirect( Helper::modify_current_url_for_external_login( 'cas' ) ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect | |
| 265 | 293 | exit; |
| 266 | 294 | } |
| 267 | 295 | |
| 268 | 296 | return $errors; |
| @@ -345,8 +373,31 @@ | ||
| 345 | 373 | } else { |
| 346 | 374 | update_option( 'auth_settings_advanced_lockouts_time_last_failed', time() ); |
| 347 | 375 | update_option( 'auth_settings_advanced_lockouts_failed_attempts', $num_attempts + 1 ); |
| 348 | 376 | } |
| 377 | + | |
| 378 | + // Log a lockout if we hit the configured limit (via Simple History plugin). | |
| 379 | + $lockouts = $auth_settings['advanced_lockouts']; | |
| 380 | + $num_attempts_short_lockout = $lockouts['attempts_1']; | |
| 381 | + $num_attempts_long_lockout = $lockouts['attempts_1'] + $lockouts['attempts_2']; | |
| 382 | + if ( $num_attempts >= $num_attempts_short_lockout ) { | |
| 383 | + $lockout_length_in_seconds = $num_attempts >= $num_attempts_long_lockout ? $lockouts['duration_2'] * 60 : $lockouts['duration_1'] * 60; | |
| 384 | + apply_filters( | |
| 385 | + 'simple_history_log_warning', | |
| 386 | + sprintf( | |
| 387 | + /* TRANSLATORS: 1: duration of lockout 2: username 3: ordinal number of invalid attempts */ | |
| 388 | + __( 'Authorizer lockout triggered for %1$s on user %2$s after the %3$s invalid attempt.', 'authorizer' ), | |
| 389 | + Helper::seconds_as_sentence( $lockout_length_in_seconds ), | |
| 390 | + $username, | |
| 391 | + Helper::ordinal( $num_attempts ) | |
| 392 | + ), | |
| 393 | + array( | |
| 394 | + 'seconds' => $lockout_length_in_seconds, | |
| 395 | + 'username' => $username, | |
| 396 | + 'attempts' => $num_attempts, | |
| 397 | + ) | |
| 398 | + ); | |
| 399 | + } | |
| 349 | 400 | } |
| 350 | 401 | |
| 351 | 402 | |
| 352 | 403 | /** |
| @@ -386,7 +437,25 @@ | ||
| 386 | 437 | $error = get_option( 'auth_settings_advanced_login_error' ); |
| 387 | 438 | delete_option( 'auth_settings_advanced_login_error' ); |
| 388 | 439 | $errors = ' ' . $error . "<br />\n"; |
| 389 | 440 | return $errors; |
| 441 | + } | |
| 442 | + | |
| 443 | + | |
| 444 | + /** | |
| 445 | + * Render the [authorizer_login_form] shortcode. | |
| 446 | + * | |
| 447 | + * Shortcode: authorizer_login_form | |
| 448 | + */ | |
| 449 | + public function shortcode_authorizer_login_form() { | |
| 450 | + ob_start(); | |
| 451 | + | |
| 452 | + $this->login_enqueue_scripts_and_styles(); | |
| 453 | + $this->login_form_add_external_service_links(); | |
| 454 | + $this->load_login_footer_js(); | |
| 455 | + | |
| 456 | + wp_login_form(); | |
| 457 | + | |
| 458 | + return ob_get_clean(); | |
| 390 | 459 | } |
| 391 | 460 | |
| 392 | 461 | } |