diff --git a/README.md b/README.md index 2e99b96e8..31fd765db 100644 --- a/README.md +++ b/README.md @@ -80,13 +80,11 @@ Under some situations, you may end up with a user with two accounts. **WordPres ### Can I customize the Login Widget? -You can style the login form by adding a filter like this +You can style the login form by adding your css on the "Customize the Login Widget CSS" Auth0 setting and the widget settings - add_filter( 'auth0_login_css', function() { - return "form a.a0-btn-small { background-color: red }"; - } ); + form a.a0-btn-small { background-color: red !important; } -The Login Widget is Open Source. For more information about it: https://github.com/auth0/widget +The Login Widget is Open Source. For more information about it: https://github.com/auth0/lock ### Can I access the user profile information? diff --git a/WP_Auth0.php b/WP_Auth0.php index 41490fb20..1514d97c5 100644 --- a/WP_Auth0.php +++ b/WP_Auth0.php @@ -2,7 +2,7 @@ /** * Plugin Name: Wordpress Auth0 Integration * Description: Implements the Auth0 Single Sign On solution into Wordpress - * Version: 1.1.2 + * Version: 1.1.3 * Author: Auth0 * Author URI: https://auth0.com */ @@ -52,16 +52,59 @@ public static function init(){ add_action( 'widgets_init', array(__CLASS__, 'wp_register_widget')); + add_filter('query_vars', array(__CLASS__, 'a0_register_query_vars')); + + $plugin = plugin_basename(__FILE__); add_filter("plugin_action_links_$plugin", array(__CLASS__, 'wp_add_plugin_settings_link')); + if (isset($_GET['message'])) + { + add_action( 'wp_footer', array( __CLASS__, 'a0_render_message' ) ); + } + + WP_Auth0_Settings_Section::init(); WP_Auth0_Admin::init(); + WP_Auth0_ErrorLog::init(); + } + + public static function getPluginDirUrl() + { + return plugin_dir_url( __FILE__ ); + } + + public static function a0_register_query_vars( $qvars ) { + $qvars[] = 'error_description'; + return $qvars; + } + + public static function a0_render_message() + { + $message = null; + + switch (strtolower($_GET['message'])) + { + //case '': $message = ""; break; + } + + if ($message) + { + echo "
$message (Close)
"; + echo ''; + } } // Add settings link on plugin page public static function wp_add_plugin_settings_link($links) { - $settings_link = 'Settings'; + + $settings_link = 'Error Log'; + array_unshift($links, $settings_link); + + $settings_link = 'Settings'; array_unshift($links, $settings_link); + return $links; } @@ -75,6 +118,11 @@ public static function wp_enqueue(){ if (trim($client_id) == "") return; + if (isset($_GET['message'])) + { + wp_enqueue_script('jquery'); + } + wp_enqueue_style( 'auth0-widget', WPA0_PLUGIN_URL . 'assets/css/main.css' ); } @@ -154,6 +202,9 @@ public static function buildSettings($settings) $options_obj['dict'] = $settings['dict']; } } + if (self::IsValid($settings,'custom_css')) { + $options_obj['customCSS'] = $settings['custom_css']; + } if (self::IsValid($settings,'social_big_buttons')) { $options_obj['socialBigButtons'] = self::GetBoolean($settings['social_big_buttons']); } @@ -206,6 +257,23 @@ public static function init_auth0(){ return; } + if (isset($wp_query->query_vars['error_description']) && trim($wp_query->query_vars['error_description']) != '') + { + $msg = __('There was a problem with your log in:', WPA0_LANG); + $msg .= ' '.$wp_query->query_vars['error_description']; + $msg .= '

'; + $msg .= '' . __('← Login', WPA0_LANG) . ''; + wp_die($msg); + } + if (isset($wp_query->query_vars['error']) && trim($wp_query->query_vars['error']) != '') + { + $msg = __('There was a problem with your log in:', WPA0_LANG); + $msg .= ' '.$wp_query->query_vars['error']; + $msg .= '

'; + $msg .= '' . __('← Login', WPA0_LANG) . ''; + wp_die($msg); + } + $code = $wp_query->query_vars['code']; $state = $wp_query->query_vars['state']; $stateFromGet = json_decode(stripcslashes($state)); @@ -222,7 +290,7 @@ public static function init_auth0(){ $body = array( 'client_id' => $client_id, 'redirect_uri' => home_url(), - 'client_secret' => $client_secret, + 'client_secret' =>$client_secret, 'code' => $code, 'grant_type' => 'authorization_code' ); @@ -238,6 +306,9 @@ public static function init_auth0(){ )); if ($response instanceof WP_Error) { + + self::insertAuth0Error('init_auth0_oauth/token',$response); + error_log($response->get_error_message()); $msg = __('Sorry. There was a problem logging you in.', WPA0_LANG); $msg .= '

'; @@ -246,12 +317,16 @@ public static function init_auth0(){ } $data = json_decode( $response['body'] ); + if(isset($data->access_token)){ // Get the user information $response = wp_remote_get( $endpoint . 'userinfo/?access_token=' . $data->access_token ); if ($response instanceof WP_Error) { + + self::insertAuth0Error('init_auth0_userinfo',$response); + error_log($response->get_error_message()); - $msg = __('Sorry, there was a problem logging you in.', WPA0_LANG); + $msg = __('There was a problem with your log in.', WPA0_LANG); $msg .= '

'; $msg .= '' . __('← Login', WPA0_LANG) . ''; wp_die($msg); @@ -267,7 +342,31 @@ public static function init_auth0(){ wp_safe_redirect( home_url() ); } } + }elseif (is_array($response['response']) && $response['response']['code'] == 401) { + + $error = new WP_Error('401', 'auth/token response code: 401 Unauthorized'); + + self::insertAuth0Error('init_auth0_oauth/token',$error); + + $msg = __('Error: the Client Secret configured on the Auth0 plugin is wrong. Make sure to copy the right one from the Auth0 dashboard.', WPA0_LANG); + $msg .= '

'; + $msg .= '' . __('← Login', WPA0_LANG) . ''; + wp_die($msg); + }else{ + + $error = ''; + $description = ''; + + if (isset($data->error)) $error = $data->error; + if (isset($data->error_description)) $description = $data->error_description; + + if (!empty($error) || !empty($description)) + { + $error = new WP_Error($error, $description); + self::insertAuth0Error('init_auth0_oauth/token',$error); + } + // Login failed! wp_redirect( home_url() . '?message=' . $data->error_description ); //echo "Error logging in! Description received was:
" . $data->error_description; @@ -283,7 +382,11 @@ private static function findAuth0User($id) { JOIN ' . $wpdb->users . ' u ON a.wp_id = u.id WHERE a.auth0_id = %s'; $userRow = $wpdb->get_row($wpdb->prepare($sql, $id)); - if (is_null($userRow) || $userRow instanceof WP_Error ) { + + if (is_null($userRow)) { + return null; + }elseif($userRow instanceof WP_Error ) { + self::insertAuth0Error('findAuth0User',$userRow); return null; } $user = new WP_User(); @@ -308,6 +411,25 @@ private static function insertAuth0User($userinfo, $user_id) { ); } + public static function insertAuth0Error($section, WP_Error $wp_error) { + global $wpdb; + $wpdb->insert( + $wpdb->auth0_error_logs, + array( + 'section' => $section, + 'date' => date('c'), + 'code' => $wp_error->get_error_code(), + 'message' => $wp_error->get_error_message() + ), + array( + '%s', + '%s', + '%s', + '%s' + ) + ); + } + private static function updateAuth0Object($userinfo) { global $wpdb; $wpdb->update( @@ -377,10 +499,13 @@ private static function login_user( $userinfo, $data ){ // If the user has a verified email or is a database user try to see if there is // a user to join with. The isDatabase is because we don't want to allow database // user creation if there is an existing one with no verified email - if ($userinfo->email_verified || $isDatabaseUser) { + + if (isset($userinfo->email) && ((isset($userinfo->email_verified) && $userinfo->email_verified) || $isDatabaseUser)) { $joinUser = get_user_by( 'email', $userinfo->email ); } + $allow_signup = WP_Auth0_Options::is_wp_registration_enabled(); + if (!is_null($joinUser) && $joinUser instanceof WP_User) { // If we are here, we have a potential join user // Don't allow creation or assignation of user if the email is not verified, that would @@ -389,13 +514,20 @@ private static function login_user( $userinfo, $data ){ self::dieWithVerifyEmail($userinfo, $data); } $user_id = $joinUser->ID; - } else { + } elseif ($allow_signup) { // If we are here, we need to create the user $user_id = (int)WP_Auth0_Users::create_user($userinfo); // Check if user was created - if($user_id == -2){ + if( is_wp_error($user_id) ) { + $msg = __('Error: Could not create user.', WPA0_LANG); + $msg = ' ' . $user_id->get_error_message(); + $msg .= '

'; + $msg .= '' . __('← Go back', WPA0_LANG) . ''; + wp_die($msg); + + }elseif($user_id == -2){ $msg = __('Error: Could not create user. The registration process were rejected. Please verify that your account is whitelisted for this system.', WPA0_LANG); $msg .= '

'; $msg .= '' . __('← Go back', WPA0_LANG) . ''; @@ -407,6 +539,11 @@ private static function login_user( $userinfo, $data ){ $msg .= '' . __('← Go back', WPA0_LANG) . ''; wp_die($msg); } + } else { + $msg = __('Error: Could not create user. The registration process is not available.', WPA0_LANG); + $msg .= '

'; + $msg .= '' . __('← Go back', WPA0_LANG) . ''; + wp_die($msg); } // If we are here we should have a valid $user_id with a new user or an existing one // log him in, and update the auth0_user table @@ -479,6 +616,15 @@ private static function install_db(){ PRIMARY KEY (auth0_id) );"; + $sql[] = "CREATE TABLE ".$wpdb->auth0_error_logs." ( + id INT(11) AUTO_INCREMENT NOT NULL, + date DATETIME NOT NULL, + section VARCHAR(255), + code VARCHAR(255), + message TEXT, + PRIMARY KEY (id) + );"; + require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); foreach($sql as $s) { @@ -499,6 +645,7 @@ public static function initialize_wpdb_tables(){ $wpdb->auth0_log = $wpdb->prefix."auth0_log"; $wpdb->auth0_user = $wpdb->prefix."auth0_user"; + $wpdb->auth0_error_logs = $wpdb->prefix."auth0_error_logs"; } private static function autoloader($class){ @@ -536,6 +683,9 @@ function get_currentauth0userinfo() { WHERE wp_id = %d'; $result = $wpdb->get_row($wpdb->prepare($sql, $current_user->ID)); if (is_null($result) || $result instanceof WP_Error ) { + + self::insertAuth0Error('get_currentauth0userinfo',$result); + return null; } $currentauth0_user = unserialize($result->auth0_obj); diff --git a/assets/css/main.css b/assets/css/main.css index 0851e9c1c..535097497 100755 --- a/assets/css/main.css +++ b/assets/css/main.css @@ -9,4 +9,20 @@ } .auth0-login .form-signin-heading { margin-bottom: 10px; +} +.a0-message{ + position: fixed; + top: 0; + left: 0; + width: 100%; + background: #FFF; + line-height: 2em; + text-align: center; + font-weight: bold; + z-index: 999; +} + +.a0-message small { + font-weight: normal; + cursor: pointer; } \ No newline at end of file diff --git a/assets/css/settings.css b/assets/css/settings.css index 96091d2c5..b58132803 100644 --- a/assets/css/settings.css +++ b/assets/css/settings.css @@ -5,4 +5,31 @@ input[type=text] { textarea { width: 70%; height: 100px; +} + +.a0-table { + border-spacing: 0; + margin-top: 20px; +} + +.a0-table tr td, +.a0-table tr th{ + padding: 8px 10px; + margin:0; +} + +.a0-table tr th { + border-bottom: 2px solid #999; +} + +.a0-table tr:nth-child(2n+1) { + background: #f9f9f9; +} + +.a0-table tr td.message +{ + padding: 25px; + font-weight: bold; + text-align: center; + font-size: 18px; } \ No newline at end of file diff --git a/assets/img/a0icon.png b/assets/img/a0icon.png new file mode 100644 index 000000000..7748a4042 Binary files /dev/null and b/assets/img/a0icon.png differ diff --git a/lib/WP_Auth0_Admin.php b/lib/WP_Auth0_Admin.php index 07712fea5..751133df0 100755 --- a/lib/WP_Auth0_Admin.php +++ b/lib/WP_Auth0_Admin.php @@ -2,7 +2,6 @@ class WP_Auth0_Admin{ public static function init(){ - add_action( 'admin_menu', array(__CLASS__, 'init_menu') ); add_action( 'admin_init', array(__CLASS__, 'init_admin')); add_action( 'admin_enqueue_scripts', array(__CLASS__, 'admin_enqueue')); } @@ -66,6 +65,7 @@ public static function init_admin(){ array('id' => 'wpa0_social_big_buttons', 'name' => 'Show big social buttons', 'function' => 'render_social_big_buttons'), array('id' => 'wpa0_icon_url', 'name' => 'Icon URL', 'function' => 'render_icon_url'), array('id' => 'wpa0_gravatar', 'name' => 'Enable Gravatar integration', 'function' => 'render_gravatar'), + array('id' => 'wpa0_custom_css', 'name' => 'Customize the Login Widget CSS', 'function' => 'render_custom_css'), )); @@ -132,6 +132,12 @@ public static function render_dict(){ echo '
' . __('This is the widget\'s dict param.', WPA0_LANG) . '' . __('More info', WPA0_LANG) . ''; } + public static function render_custom_css(){ + $v = WP_Auth0_Options::get( 'custom_css' ); + echo ''; + echo '
' . __('This should be a valid CSS to customize the Auth0 login widget. ', WPA0_LANG) . '' . __('More info', WPA0_LANG) . ''; + } + public static function render_username_style(){ $v = WP_Auth0_Options::get( 'username_style' ); echo ''; @@ -195,9 +201,23 @@ public static function render_verified_email () { } public static function render_allow_signup () { - $v = absint(WP_Auth0_Options::get( 'allow_signup' )); - echo ''; - echo '
' . __('If you have database connection you can allow users to signup in the widget', WPA0_LANG) . ''; + $allow_signup = WP_Auth0_Options::is_wp_registration_enabled(); + + echo '' . __('Signup will be ', WPA0_LANG); + + if ($allow_signup){ + echo '' . __('disabled', WPA0_LANG) . ''; + echo __(' because you have turned on the setting " Anyone can register" off WordPress', WPA0_LANG) . '
'; + } + else{ + echo '' . __('enabled', WPA0_LANG) . ''; + echo __(' because you have turned on the setting " Anyone can register" on WordPress', WPA0_LANG) . '
'; + } + + + + echo __('You can manage this setting on Settings > General > Membership, Anyone can register', WPA0_LANG) . '
'; + } public static function render_allow_wordpress_login () { @@ -206,7 +226,6 @@ public static function render_allow_wordpress_login () { echo '
' . __('Mark this if you want to enable the regular WordPress login', WPA0_LANG) . ''; } - public static function render_basic_description(){ } @@ -219,11 +238,6 @@ public static function render_advanced_description(){ } - - public static function init_menu(){ - add_options_page( __('Auth0 Settings', WPA0_LANG), __('Auth0 Settings', WPA0_LANG), 'manage_options', 'wpa0', array(__CLASS__, 'render_settings_page') ); - } - public static function render_settings_page(){ include WPA0_PLUGIN_DIR . 'templates/settings.php'; } diff --git a/lib/WP_Auth0_ErrorLog.php b/lib/WP_Auth0_ErrorLog.php new file mode 100644 index 000000000..61561f6e9 --- /dev/null +++ b/lib/WP_Auth0_ErrorLog.php @@ -0,0 +1,35 @@ +auth0_error_logs .' + WHERE date > %s + ORDER BY date DESC'; + + $data = $wpdb->get_results($wpdb->prepare($sql, date('c', strtotime('1 month ago')))); + + if (is_null($data) || $data instanceof WP_Error ) { + return null; + } + + include WPA0_PLUGIN_DIR . 'templates/a0-error-log.php'; + } + +} \ No newline at end of file diff --git a/lib/WP_Auth0_Options.php b/lib/WP_Auth0_Options.php index a8aa1ae74..e812642a3 100755 --- a/lib/WP_Auth0_Options.php +++ b/lib/WP_Auth0_Options.php @@ -4,6 +4,11 @@ class WP_Auth0_Options { const OPTIONS_NAME = 'wp_auth0_settings'; private static $_opt = null; + public static function is_wp_registration_enabled() + { + return (get_option('users_can_register', 0) == 1); + } + public static function get_options(){ if(empty(self::$_opt)){ $options = get_option( self::OPTIONS_NAME, array()); @@ -47,13 +52,13 @@ private static function defaults(){ 'ip_ranges' => '', 'cdn_url' => '//cdn.auth0.com/js/lock-6.min.js', 'requires_verified_email' => true, - 'allow_signup' => true, 'wordpress_login_enabled' => true, 'dict' => '', 'social_big_buttons' => false, 'username_style' => 'email', 'extra_conf' => '', 'remember_last_login' => true, + 'custom_css' => '', 'gravatar' => true, ); } diff --git a/lib/WP_Auth0_Settings_Section.php b/lib/WP_Auth0_Settings_Section.php new file mode 100644 index 000000000..767ab3996 --- /dev/null +++ b/lib/WP_Auth0_Settings_Section.php @@ -0,0 +1,18 @@ +email; + $email = null; + if (isset($userinfo->email)) + { + $email = $userinfo->email; + } if (empty($email)) { $email = "change_this_email@" . uniqid() .".com"; } @@ -46,7 +50,7 @@ public static function create_user( $userinfo ){ $user_id = wp_insert_user( $user_data ); if(!is_numeric($user_id)) - return -1; + return $user_id; do_action( 'wpa0_user_created', $user_id, $email, $password, $firstname, $lastname ); diff --git a/readme.txt b/readme.txt index 998a88ff4..f4dff7067 100644 --- a/readme.txt +++ b/readme.txt @@ -105,13 +105,11 @@ Under some situations, you may end up with a user with two accounts. Wordpress a = Can I customize the Login Widget? = -You can style the login form by adding a filter like this +You can style the login form by adding your css on the "Customize the Login Widget CSS" Auth0 setting and the widget settings - add_filter( 'auth0_login_css', function() { - return "form a.a0-btn-small { background-color: red }"; - } ); + form a.a0-btn-small { background-color: red !important; } -The Login Widget is Open Source. For more information about it: https://github.com/auth0/widget +The Login Widget is Open Source. For more information about it: https://github.com/auth0/lock = Can I access the user profile information? = diff --git a/templates/a0-error-log.php b/templates/a0-error-log.php new file mode 100644 index 000000000..8e0996b33 --- /dev/null +++ b/templates/a0-error-log.php @@ -0,0 +1,40 @@ +
+ +

+ + + + + + + + + + + + + + + + + + + + + + + + + +
DateSectionError codeMessage
No errors.
date)); ?>section; ?>code; ?>message; ?>
+
\ No newline at end of file diff --git a/templates/a0-widget-setup-form.php b/templates/a0-widget-setup-form.php index 8f5df3c1f..62dc5fc7b 100644 --- a/templates/a0-widget-setup-form.php +++ b/templates/a0-widget-setup-form.php @@ -10,6 +10,7 @@ $dict = isset($instance[ 'dict' ]) ? $instance[ 'dict' ] : ''; $extra_conf = isset($instance[ 'extra_conf' ]) ? $instance[ 'extra_conf' ] : ''; $remember_last_login = isset($instance[ 'remember_last_login' ]) ? $instance[ 'remember_last_login' ] : ''; +$custom_css = isset($instance[ 'custom_css' ]) ? $instance[ 'custom_css' ] : ''; ?> @@ -29,62 +30,65 @@

- - /> - - - /> - - - /> - - +
+

+ /> + +   + /> + +   + /> + +

- - /> - - - /> - - - /> - - +
+

+ /> + +   + /> + +   + /> + +

- - /> - - - /> - - - /> - - +
+

+ /> + +   + /> + +   + /> + +

@@ -130,6 +134,18 @@ class="button-secondary">

+

+ + +
+ + + + +

diff --git a/templates/auth0-login-form.php b/templates/auth0-login-form.php index 4b2f0c95f..876fc093a 100644 --- a/templates/auth0-login-form.php +++ b/templates/auth0-login-form.php @@ -5,7 +5,9 @@ $domain = WP_Auth0_Options::get('domain'); $cdn = WP_Auth0_Options::get('cdn_url'); -$allow_signup = WP_Auth0_Options::get('allow_signup') == 1; + +$allow_signup = WP_Auth0_Options::is_wp_registration_enabled(); + $extra_css = apply_filters( 'auth0_login_css', ''); $showAsModal = (isset($specialSettings['show_as_modal']) && $specialSettings['show_as_modal'] == 1); $modalTriggerName = 'Login'; @@ -32,11 +34,42 @@ $stateObj = array("interim" => $interim_login, "uuid" =>uniqid()); $state = json_encode($stateObj); + +$options_obj = WP_Auth0::buildSettings(WP_Auth0_Options::get_options()); + +$options_obj = array_merge( array( + "callbackURL" => site_url('/index.php?auth0=1'), + "authParams" => array("state" => $state), +), $options_obj ); + +if (isset($specialSettings)){ + $options_obj = array_merge( $options_obj , $specialSettings ); +} + +if (!$showAsModal){ + $options_obj['container'] = 'auth0-login-form'; +} + +if (!$allow_signup) { + $options_obj['disableSignupAction'] = true; +} +$options = json_encode($options_obj); + if(empty($client_id) || empty($domain)){ ?>

+ + + + + + + +
@@ -70,35 +103,10 @@ var lock = new Auth0Lock('', ''); - site_url('/index.php?auth0=1'), - "authParams" => array("state" => $state), - ), $options_obj ); - - if (isset($specialSettings)){ - $options_obj = array_merge( $options_obj , $specialSettings ); - } - - if (!$showAsModal){ - $options_obj['container'] = 'auth0-login-form'; - } - - - $options = json_encode($options_obj); - ?> function a0ShowLoginModal() { var options = ; - lock.show(options, callback); - - lock.showSignin(options, callback); - } diff --git a/templates/settings.php b/templates/settings.php index 42fd11da5..1a8b2fb2e 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -1,6 +1,11 @@

+ +
+

+
+