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

Remove class WP-Auth0 and move methods to functions #756

Merged
merged 17 commits into from
Dec 30, 2019
Merged
Show file tree
Hide file tree
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
588 changes: 198 additions & 390 deletions WP_Auth0.php

Large diffs are not rendered by default.

45 changes: 44 additions & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function wp_auth0_login_override_url( $login_url = null ) {
* @return bool
*/
function wp_auth0_can_show_wp_login_form() {
if ( ! WP_Auth0::ready() ) {
if ( ! wp_auth0_is_ready() ) {
return true;
}

Expand Down Expand Up @@ -171,6 +171,49 @@ function wp_auth0_is_admin_page( $page ) {
return $page === $_REQUEST['page'];
}

/**
* Is the Auth0 plugin ready to process logins?
*
* @return bool
*/
function wp_auth0_is_ready() {
$options = WP_Auth0_Options::Instance();
if ( ! $options->get( 'domain' ) || ! $options->get( 'client_id' ) || ! $options->get( 'client_secret' ) ) {
return false;
}
return true;
}

/**
* Get the tenant region based on a domain.
*
* @param string $domain Tenant domain.
*
* @return string
*/
function wp_auth0_get_tenant_region( $domain ) {
preg_match( '/^[\w\d\-_0-9]+\.([\w\d\-_0-9]*)[\.]*auth0\.com$/', $domain, $matches );
return ! empty( $matches[1] ) ? $matches[1] : 'us';
}

/**
* Get the full tenant name with region.
*
* @param null|string $domain Tenant domain.
*
* @return string
*/
function wp_auth0_get_tenant( $domain = null ) {

if ( empty( $domain ) ) {
$options = WP_Auth0_Options::Instance();
$domain = $options->get( 'domain' );
}

$parts = explode( '.', $domain );
return $parts[0] . '@' . wp_auth0_get_tenant_region( $domain );
}

if ( ! function_exists( 'get_auth0userinfo' ) ) {
/**
* Get the Auth0 profile from the database, if one exists.
Expand Down
2 changes: 1 addition & 1 deletion lib/WP_Auth0_DBManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function install_db( $version_to_install = null ) {
$options->set( 'cache_expiration', 1440, false );

// Update Client
if ( WP_Auth0::ready() ) {
if ( wp_auth0_is_ready() ) {
$options->set( 'client_signing_algorithm', 'HS256', false );
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/WP_Auth0_Embed_Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function form( $instance ) {

public function widget( $args, $instance ) {

if ( WP_Auth0::ready() ) {
if ( wp_auth0_is_ready() ) {

$instance['show_as_modal'] = $this->showAsModal();
$instance['modal_trigger_name'] = isset( $instance['modal_trigger_name'] )
Expand Down
2 changes: 1 addition & 1 deletion lib/WP_Auth0_Ip_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function get_ips_by_domain( $domain = null, $glue = self::IP_STRING_GLUE
if ( empty( $domain ) ) {
$domain = $this->a0_options->get( 'domain' );
}
$region = WP_Auth0::get_tenant_region( $domain );
$region = wp_auth0_get_tenant_region( $domain );
return $this->get_ip_by_region( $region, $glue );
}

Expand Down
24 changes: 20 additions & 4 deletions lib/WP_Auth0_Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function get_lock_options() {
$extraOptions['auth']['redirectUrl'] = $this->wp_options->get_wp_auth0_url( $this->get_callback_protocol() );

if ( $this->wp_options->get( 'custom_domain' ) ) {
$tenant_region = WP_Auth0::get_tenant_region( $this->wp_options->get( 'domain' ) );
$tenant_region = wp_auth0_get_tenant_region( $this->wp_options->get( 'domain' ) );
$extraOptions['configurationBaseUrl'] = sprintf(
'https://cdn%s.auth0.com',
( 'us' === $tenant_region ? '' : '.' . $tenant_region )
Expand Down Expand Up @@ -159,6 +159,22 @@ private function get_callback_protocol() {
return $this->wp_options->get( 'force_https_callback' ) ? 'https' : null;
}

/**
* Render a link at the bottom of a WordPress core login form back to Lock.
*/
public static function render_back_to_lock() {
$title = wp_auth0_get_option( 'form_title' );
if ( empty( $title ) ) {
$title = 'Auth0';
}

printf(
'<div id="extra-options"><a href="?">%s</a></div>',
// translators: The $title variable is the admin-controlled form title.
printf( __( '← Back to %s login', 'wp-auth0' ), $title )
);
}

/**
* Render the Lock form with saved and passed options.
*
Expand All @@ -171,8 +187,8 @@ public static function render( $canShowLegacyLogin = true, $specialSettings = []
}

if ( $canShowLegacyLogin && wp_auth0_can_show_wp_login_form() ) {
add_action( 'login_footer', [ 'WP_Auth0', 'render_back_to_auth0' ] );
add_action( 'woocommerce_after_customer_login_form', [ 'WP_Auth0', 'render_back_to_auth0' ] );
add_action( 'login_footer', [ 'WP_Auth0_Lock', 'render_back_to_lock' ] );
add_action( 'woocommerce_after_customer_login_form', [ 'WP_Auth0_Lock', 'render_back_to_lock' ] );
return;
}

Expand All @@ -188,7 +204,7 @@ public static function render( $canShowLegacyLogin = true, $specialSettings = []
WP_Auth0_Lock::LOCK_GLOBAL_JS_VAR_NAME,
[
'settings' => $lock_options->get_lock_options(),
'ready' => WP_Auth0::ready(),
'ready' => wp_auth0_is_ready(),
'domain' => $options->get_auth_domain(),
'clientId' => $options->get( 'client_id' ),
'stateCookieName' => WP_Auth0_State_Handler::get_storage_cookie_name(),
Expand Down
4 changes: 2 additions & 2 deletions lib/WP_Auth0_LoginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function init_auth0() {

// Not an Auth0 login process or settings are not configured to allow logins.
$cb_type = $this->query_vars( 'auth0' );
if ( ! $cb_type || ! WP_Auth0::ready() ) {
if ( ! $cb_type || ! wp_auth0_is_ready() ) {
return false;
}

Expand Down Expand Up @@ -364,7 +364,7 @@ private function do_login( $user, $userinfo, $is_new, $id_token, $access_token,
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/wp_logout
*/
public function logout() {
if ( ! WP_Auth0::ready() ) {
if ( ! wp_auth0_is_ready() ) {
return;
}

Expand Down
12 changes: 5 additions & 7 deletions lib/WP_Auth0_WooCommerceOverrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ class WP_Auth0_WooCommerceOverrides {
/**
* WP_Auth0_WooCommerceOverrides constructor.
*
* @param WP_Auth0 $plugin - WP_Auth0 instance.
* @param WP_Auth0_Options $options - WP_Auth0_Options instance.
*/
public function __construct( WP_Auth0 $plugin, WP_Auth0_Options $options ) {
$this->plugin = $plugin;
public function __construct( WP_Auth0_Options $options ) {
$this->options = $options;
}

Expand All @@ -43,15 +41,15 @@ public function __construct( WP_Auth0 $plugin, WP_Auth0_Options $options ) {
* @param string $redirect_page - Page slug to redirect to after logging in.
*/
private function render_login_form( $redirect_page ) {
$this->plugin->render_auth0_login_css();
wp_auth0_login_enqueue_scripts();
if ( $this->options->get( 'auto_login', false ) ) {
// Redirecting to WordPress login page.
$redirect_url = get_permalink( wc_get_page_id( $redirect_page ) );
$login_url = wp_login_url( $redirect_url );

printf( "<a class='button' href='%s'>%s</a>", $login_url, __( 'Login', 'wp-auth0' ) );
} else {
echo $this->plugin->render_form( '' );
echo wp_auth0_render_lock_form( '' );
}
}

Expand All @@ -64,7 +62,7 @@ private function render_login_form( $redirect_page ) {
*/
public function override_woocommerce_checkout_login_form( $html ) {

if ( ! WP_Auth0::ready() ) {
if ( ! wp_auth0_is_ready() ) {
return $html;
}

Expand All @@ -84,7 +82,7 @@ public function override_woocommerce_checkout_login_form( $html ) {
*/
public function override_woocommerce_login_form( $html ) {

if ( ! WP_Auth0::ready() ) {
if ( ! wp_auth0_is_ready() ) {
return $html;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/initial-setup/WP_Auth0_InitialSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function rejected_message() {
<div class="notice notice-error">
<p>
<strong>
<?php _e( 'The required scopes were rejected.', 'wp-auth0' ); ?>
<?php _e( 'The required scopes were rejected.', 'wp-auth0' ); ?>
</strong>
</p>
</div>
Expand Down
15 changes: 0 additions & 15 deletions templates/back-to-auth0.php

This file was deleted.

2 changes: 1 addition & 1 deletion templates/initial-setup/connection_profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
.</p>
</div>
</div>
<?php if ( WP_Auth0::ready() ) : ?>
<?php if ( wp_auth0_is_ready() ) : ?>
<div class="row">
<div class="a0-step-text a0-message a0-warning">
<p>
Expand Down
126 changes: 126 additions & 0 deletions tests/testFilterGetAvatar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/**
* Contains Class TestFilterGetAvatar.
*
* @package WP-Auth0
*
* @since 4.0.0
*/

/**
* Class TestFilterGetAvatar
*/
class TestFilterGetAvatar extends WP_Auth0_Test_Case {

use HookHelpers;

use UsersHelper;

public static function setUpBeforeClass() {
parent::setUpBeforeClass();
self::$users_repo = new WP_Auth0_UsersRepo( self::$opts );
}

public function testThatFilterAvatarHookIsAdded() {
$expect_hooked = [
'wp_auth0_filter_get_avatar' => [
'priority' => 1,
'accepted_args' => 5,
],
];
$this->assertHookedFunction( 'get_avatar', $expect_hooked );
}

public function testThatAvatarIsNotFilteredIfSettingIsOff() {
self::$opts->set( 'override_wp_avatars', false );
$this->assertEquals(
'__test_avatar__',
wp_auth0_filter_get_avatar( '__test_avatar__', uniqid(), uniqid(), uniqid(), uniqid() )
);
}

public function testThatAvatarIsNotFilteredIfUserNotFound() {
self::$opts->set( 'override_wp_avatars', true );
$this->assertEquals(
'__test_avatar__',
wp_auth0_filter_get_avatar( '__test_avatar__', '__invalid_user__', uniqid(), uniqid(), uniqid() )
);
}

public function testThatAvatarIsNotFilteredIfAuth0ProfileNotFound() {
self::$opts->set( 'override_wp_avatars', true );
$this->assertEquals(
'__test_avatar__',
wp_auth0_filter_get_avatar( '__test_avatar__', 1, uniqid(), uniqid(), uniqid() )
);
}

public function testThatAvatarIsNotFilteredIfAuth0ProfileImageNotAvailable() {
self::$opts->set( 'override_wp_avatars', true );
$this->storeAuth0Data( 1 );
$this->assertEquals(
'__test_avatar__',
wp_auth0_filter_get_avatar( '__test_avatar__', 1, uniqid(), uniqid(), uniqid() )
);
}

public function testThatAvatarIsFilteredIfUserId() {
self::$opts->set( 'override_wp_avatars', true );
$userinfo = (object) [
'picture' => '__picture__',
'sub' => uniqid(),
];
self::$users_repo->update_auth0_object( 1, $userinfo );
$this->assertEquals(
'<img alt="alt" src="https://__picture__" class="avatar avatar-5 photo avatar-auth0" width="5" height="5"/>',
wp_auth0_filter_get_avatar( '__test_avatar__', 1, 5, uniqid(), 'alt' )
);
}

public function testThatAvatarIsFilteredIfWpUser() {
self::$opts->set( 'override_wp_avatars', true );
$userinfo = (object) [
'picture' => '__picture__',
'sub' => uniqid(),
];
self::$users_repo->update_auth0_object( 1, $userinfo );
$user = new WP_User( 1 );
$this->assertEquals(
'<img alt="alt" src="https://__picture__" class="avatar avatar-6 photo avatar-auth0" width="6" height="6"/>',
wp_auth0_filter_get_avatar( '__test_avatar__', $user, 6, uniqid(), 'alt' )
);
}

public function testThatAvatarIsFilteredIfEmail() {
self::$opts->set( 'override_wp_avatars', true );
$userinfo = (object) [
'picture' => '__picture__',
'sub' => uniqid(),
];
self::$users_repo->update_auth0_object( 1, $userinfo );
$this->assertEquals(
'<img alt="alt" src="https://__picture__" class="avatar avatar-7 photo avatar-auth0" width="7" height="7"/>',
wp_auth0_filter_get_avatar( '__test_avatar__', '[email protected]', 7, uniqid(), 'alt' )
);
}

public function testThatAvatarIsFilteredIfWpPost() {
self::$opts->set( 'override_wp_avatars', true );
$userinfo = (object) [
'picture' => '__picture__',
'sub' => uniqid(),
];
self::$users_repo->update_auth0_object( 1, $userinfo );
$post_id = wp_insert_post(
[
'post_author' => 1,
'post_title' => uniqid(),
]
);
$post = WP_Post::get_instance( $post_id );
$this->assertEquals(
'<img alt="alt" src="https://__picture__" class="avatar avatar-8 photo avatar-auth0" width="8" height="8"/>',
wp_auth0_filter_get_avatar( '__test_avatar__', $post, 8, uniqid(), 'alt' )
);
}
}
4 changes: 2 additions & 2 deletions tests/testLoginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function testThatLoggedInUserIsRedirectedFromWpLogin() {

// Configure Auth0.
self::auth0Ready();
$this->assertTrue( WP_Auth0::ready() );
$this->assertTrue( wp_auth0_is_ready() );

// Set the current user to admin.
$this->setGlobalUser();
Expand Down Expand Up @@ -159,7 +159,7 @@ public function testUlpRedirect() {
self::$opts->set( 'auto_login', 1 );
self::auth0Ready( true );
self::$opts->set( 'domain', 'test-wp.auth0.com' );
$this->assertTrue( WP_Auth0::ready() );
$this->assertTrue( wp_auth0_is_ready() );

$caught_redirect = [
'location' => null,
Expand Down
Loading