From 3c7ac10ffc4664ee40d179d7919993f59a978f7e Mon Sep 17 00:00:00 2001 From: Hernan Rajchert Date: Mon, 5 May 2014 12:55:22 -0700 Subject: [PATCH 01/10] * Fixed problem with login auto, it wasn't loading the js library from the wp enqueue * Removed necesity of using a redirect, now the callback points directly to /index.php?auth0=1 * Removed unnesesary options in the admin * Added error message if service is unnavailable * Added readme for publishing --- WP_Auth0.php | 468 +++++++++++++++++---------------- lib/WP_Auth0_Admin.php | 539 ++++++++++++++++++++------------------- lib/WP_Auth0_Options.php | 96 +++---- readme.txt | 21 ++ templates/login-auto.php | 32 +-- templates/login-form.php | 68 ++--- 6 files changed, 651 insertions(+), 573 deletions(-) create mode 100644 readme.txt diff --git a/WP_Auth0.php b/WP_Auth0.php index a6e5471c..1f6e4e92 100755 --- a/WP_Auth0.php +++ b/WP_Auth0.php @@ -13,225 +13,253 @@ define('WPA0_LANG', 'wp-auth0'); class WP_Auth0 { - public static function init(){ - spl_autoload_register(array(__CLASS__, 'autoloader')); - register_shutdown_function(array('WP_Auth0_Utils', 'log_crash')); - - WP_Auth0_Referer_Check::init(); - WP_Auth0_Ip_Check::init(); - - add_action( 'init', array(__CLASS__, 'wp_init') ); - - register_activation_hook( WPA0_PLUGIN_FILE, array(__CLASS__, 'install') ); - register_deactivation_hook( WPA0_PLUGIN_FILE, array(__CLASS__, 'uninstall') ); - - add_action( 'plugins_loaded', array(__CLASS__, 'initialize_wpdb_tables')); - add_action( 'template_redirect', array(__CLASS__, 'init_auth0'), 1 ); - - add_filter( 'login_message', array(__CLASS__, 'render_form') ); - add_shortcode( 'auth0', array(__CLASS__, 'shortcode' ) ); - - add_action( 'wp_enqueue_scripts', array(__CLASS__, 'wp_enqueue')); - - WP_Auth0_Admin::init(); - } - - public static function wp_enqueue(){ - $activated = absint(WP_Auth0_Options::get( 'active' )); - if(!$activated) - return; - - $auto_login = absint(WP_Auth0_Options::get( 'auto_login' )); - - if(!$auto_login){ - wp_enqueue_style( 'auth0-widget', WPA0_PLUGIN_URL . 'assets/css/main.css' ); - - if(WP_Auth0_Options::get('wp_login_form')){ - wp_enqueue_script( 'auth0-wp-login-form', WPA0_PLUGIN_URL . 'assets/js/wp-login.js', array('jquery') ); - wp_localize_script( 'auth0-wp-login-form', 'wpa0', array( - 'wp_btn' => WP_Auth0_Options::get('wp_login_btn_text') - )); - } - }else{ - wp_enqueue_script( 'auth0-wp-login-form', WPA0_PLUGIN_URL . 'assets/js/auth0.min.js', array('jquery') ); - } - } - - public static function shortcode( $atts ){ - ob_start(); - include WPA0_PLUGIN_DIR . 'templates/login-form.php'; - $html = ob_get_clean(); - return $html; - } - - public static function render_form( $html ){ - $activated = absint(WP_Auth0_Options::get( 'active' )); - $auto_login = absint(WP_Auth0_Options::get( 'auto_login' )); - - if(!$activated) - return $html; - - ob_start(); - - if(!$auto_login) - include WPA0_PLUGIN_DIR . 'templates/login-form.php'; - else - include WPA0_PLUGIN_DIR . 'templates/login-auto.php'; - - $html = ob_get_clean(); - return $html; - } - - public static function init_auth0(){ - global $wp_query; - - if(!isset($wp_query->query_vars['auth0']) || $wp_query->query_vars['auth0'] != '1') - return; - - $code = $wp_query->query_vars['code']; - $state = $wp_query->query_vars['state']; - $endpoint = WP_Auth0_Options::get( 'endpoint' ); - $client_id = WP_Auth0_Options::get( 'client_id' ); - $client_secret = WP_Auth0_Options::get( 'client_secret' ); - - if(empty($client_id)) wp_die(__('Error: Your Auth0 Client ID has not been entered in the Auth0 SSO plugin settings.', WPA0_LANG)); - if(empty($client_secret)) wp_die(__('Error: Your Auth0 Client Secret has not been entered in the Auth0 SSO plugin settings.', WPA0_LANG)); - if(empty($endpoint)) wp_die(__('Error: No Auth0 Endpoint defined in Wordpress Administration!', WPA0_LANG)); - - $body = array( - 'client_id' => $client_id, - 'redirect_uri' => home_url(), - 'client_secret' => $client_secret, - 'code' => $code, - 'grant_type' => 'authorization_code' - ); - - $headers = array( - 'content-type' => 'application/x-www-form-urlencoded' - ); - - $response = wp_remote_post( $endpoint . 'oauth/token', array( - 'headers' => $headers, - 'body' => $body - )); - - $data = json_decode( $response['body'] ); - - if(isset($data->access_token)){ - $response = wp_remote_get( $endpoint . 'userinfo/?access_token=' . $data->access_token ); - $userinfo = json_decode( $response['body'] ); - - self::login_user($userinfo); - }else{ - // Login failed! - wp_redirect( home_url() . '?message=' . $data->error_description ); - //echo "Error logging in! Description received was:
" . $data->error_description; - } - exit(); - } - - private static function login_user( $userinfo ){ - $user = get_user_by( 'email', $userinfo->email ); - - // Check if we got an instance of a WP_User, which means the user exists - if($user instanceof WP_User){ - // User exists! Log in - wp_set_auth_cookie( $user->ID ); - wp_redirect( home_url() ); - exit(); - }else{ - // User doesn't exist - create it! - $user_id = (int)WP_Auth0_Users::create_user($userinfo); - - // Check if user was created - if($user_id > 0){ - // User created! Login and redirect - wp_set_auth_cookie( $user_id ); - wp_redirect( home_url() ); - exit(); - }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) . ''; - - wp_die($msg); - }else{ - $msg = __('Error: Could not create user.', WPA0_LANG); - $msg .= '

'; - $msg .= '' . __('← Go back', WPA0_LANG) . ''; - wp_die($msg); - } - } - } - - public static function wp_init(){ - self::setup_rewrites(); - } - - private static function setup_rewrites(){ - add_rewrite_tag('%auth0%', '([^&]+)'); - add_rewrite_tag('%code%', '([^&]+)'); - add_rewrite_tag('%state%', '([^&]+)'); - add_rewrite_rule('^auth0', 'index.php?auth0=1', 'top'); - } - - public static function install(){ - self::install_db(); - self::setup_rewrites(); - - flush_rewrite_rules(); - } - - public static function uninstall(){ - flush_rewrite_rules(); - } - - private static function install_db(){ - global $wpdb; - - self::initialize_wpdb_tables(); - - $sql = array(); - - $sql[] = "CREATE TABLE ".$wpdb->auth0_log." ( - id INT(11) AUTO_INCREMENT NOT NULL, - event VARCHAR(100) NOT NULL, - level VARCHAR(100) NOT NULL DEFAULT 'notice', - description TEXT, - details LONGTEXT, - logtime INT(11) NOT NULL, - PRIMARY KEY (id) - );"; - - require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); - - foreach($sql as $s) - dbDelta($s); - } - - public static function initialize_wpdb_tables(){ - global $wpdb; - - $wpdb->auth0_log = $wpdb->prefix."auth0_log"; - } - - private static function autoloader($class){ - $path = WPA0_PLUGIN_DIR; - $paths = array(); - $exts = array('.php', '.class.php'); - - $paths[] = $path; - $paths[] = $path.'lib/'; - - foreach($paths as $p) - foreach($exts as $ext){ - if(file_exists($p.$class.$ext)){ - require_once($p.$class.$ext); - return true; - } - } - - return false; - } + public static function init(){ + spl_autoload_register(array(__CLASS__, 'autoloader')); + register_shutdown_function(array('WP_Auth0_Utils', 'log_crash')); + + // WP_Auth0_Referer_Check::init(); + WP_Auth0_Ip_Check::init(); + + add_action( 'init', array(__CLASS__, 'wp_init') ); + + register_activation_hook( WPA0_PLUGIN_FILE, array(__CLASS__, 'install') ); + register_deactivation_hook( WPA0_PLUGIN_FILE, array(__CLASS__, 'uninstall') ); + + add_action( 'plugins_loaded', array(__CLASS__, 'initialize_wpdb_tables')); + add_action( 'template_redirect', array(__CLASS__, 'init_auth0'), 1 ); + + add_filter( 'login_message', array(__CLASS__, 'render_form') ); + add_shortcode( 'auth0', array(__CLASS__, 'shortcode' ) ); + + add_action( 'wp_enqueue_scripts', array(__CLASS__, 'wp_enqueue')); + + // Filter that handles the showing of an error. + // NOTE: Would love if wordpress just added a simple flash system + add_filter('the_content', array(__CLASS__,'show_error')); + + WP_Auth0_Admin::init(); + } + + public static function wp_enqueue(){ + // die("wp enqueue!"); + $activated = absint(WP_Auth0_Options::get( 'active' )); + if(!$activated) + return; + + $auto_login = absint(WP_Auth0_Options::get( 'auto_login' )); + + if(!$auto_login){ + wp_enqueue_style( 'auth0-widget', WPA0_PLUGIN_URL . 'assets/css/main.css' ); + + if(WP_Auth0_Options::get('wp_login_form')){ + wp_enqueue_script( 'auth0-wp-login-form', WPA0_PLUGIN_URL . 'assets/js/wp-login.js', array('jquery') ); + wp_localize_script( 'auth0-wp-login-form', 'wpa0', array( + 'wp_btn' => WP_Auth0_Options::get('wp_login_btn_text') + )); + } + }else{ + // die("muajaja"); + wp_enqueue_script( 'auth0-wp-login-form', WPA0_PLUGIN_URL . 'assets/js/auth0.min.js', array('jquery') ); + } + } + + public static function shortcode( $atts ){ + ob_start(); + include WPA0_PLUGIN_DIR . 'templates/login-form.php'; + $html = ob_get_clean(); + return $html; + } + + public static function render_form( $html ){ + $activated = absint(WP_Auth0_Options::get( 'active' )); + $auto_login = absint(WP_Auth0_Options::get( 'auto_login' )); + + if(!$activated) + return $html; + + ob_start(); + // die("mudafuca!"); + if(!$auto_login) { + include WPA0_PLUGIN_DIR . 'templates/login-form.php'; + } + else { + // die("EA EA auto login!"); + include WPA0_PLUGIN_DIR . 'templates/login-auto.php'; + } + + $html = ob_get_clean(); + return $html; + } + + public static function show_error($content) { + global $wp_query; + + if(!isset($wp_query->query_vars['auth0_error'])) { + return $content; + } + return "Sorry there was a problem logging you in"; + } + + public static function init_auth0(){ + global $wp_query; + + if(!isset($wp_query->query_vars['auth0']) || $wp_query->query_vars['auth0'] != '1') { + return; + } + + $code = $wp_query->query_vars['code']; + $state = $wp_query->query_vars['state']; + $domain = WP_Auth0_Options::get( 'domain' ); + $endpoint = "https://" . $domain . "/"; + $client_id = WP_Auth0_Options::get( 'client_id' ); + $client_secret = WP_Auth0_Options::get( 'client_secret' ); + + if(empty($client_id)) wp_die(__('Error: Your Auth0 Client ID has not been entered in the Auth0 SSO plugin settings.', WPA0_LANG)); + if(empty($client_secret)) wp_die(__('Error: Your Auth0 Client Secret has not been entered in the Auth0 SSO plugin settings.', WPA0_LANG)); + if(empty($domain)) wp_die(__('Error: No Domain defined in Wordpress Administration!', WPA0_LANG)); + + $body = array( + 'client_id' => $client_id, + 'redirect_uri' => home_url(), + 'client_secret' => $client_secret, + 'code' => $code, + 'grant_type' => 'authorization_code' + ); + + $headers = array( + 'content-type' => 'application/x-www-form-urlencoded' + ); + + + $response = wp_remote_post( $endpoint . 'oauth/token', array( + 'headers' => $headers, + 'body' => $body + )); + // die("-".$endpoint . 'oauth/token-'); + if ($response instanceof WP_Error) { + error_log($response->get_error_message()); + return wp_redirect( home_url() . '?auth0_error=1'); + } + + $data = json_decode( $response['body'] ); + + if(isset($data->access_token)){ + $response = wp_remote_get( $endpoint . 'userinfo/?access_token=' . $data->access_token ); + $userinfo = json_decode( $response['body'] ); + + self::login_user($userinfo); + }else{ + // Login failed! + wp_redirect( home_url() . '?message=' . $data->error_description ); + //echo "Error logging in! Description received was:
" . $data->error_description; + } + + exit(); + } + + private static function login_user( $userinfo ){ + $user = get_user_by( 'email', $userinfo->email ); + + // Check if we got an instance of a WP_User, which means the user exists + if($user instanceof WP_User){ + // User exists! Log in + wp_set_auth_cookie( $user->ID ); + wp_redirect( home_url() ); + exit(); + }else{ + // User doesn't exist - create it! + $user_id = (int)WP_Auth0_Users::create_user($userinfo); + + // Check if user was created + if($user_id > 0){ + // User created! Login and redirect + wp_set_auth_cookie( $user_id ); + wp_redirect( home_url() ); + exit(); + }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) . ''; + + wp_die($msg); + }else{ + $msg = __('Error: Could not create user.', WPA0_LANG); + $msg .= '

'; + $msg .= '' . __('← Go back', WPA0_LANG) . ''; + wp_die($msg); + } + } + } + + public static function wp_init(){ + self::setup_rewrites(); + } + + private static function setup_rewrites(){ + add_rewrite_tag('%auth0%', '([^&]+)'); + add_rewrite_tag('%code%', '([^&]+)'); + add_rewrite_tag('%state%', '([^&]+)'); + add_rewrite_tag('%auth0_error%', '([^&]+)'); + add_rewrite_rule('^auth0', 'index.php?auth0=1', 'top'); + } + + public static function install(){ + self::install_db(); + self::setup_rewrites(); + + flush_rewrite_rules(); + } + + public static function uninstall(){ + flush_rewrite_rules(); + } + + private static function install_db(){ + global $wpdb; + + self::initialize_wpdb_tables(); + + $sql = array(); + + $sql[] = "CREATE TABLE ".$wpdb->auth0_log." ( + id INT(11) AUTO_INCREMENT NOT NULL, + event VARCHAR(100) NOT NULL, + level VARCHAR(100) NOT NULL DEFAULT 'notice', + description TEXT, + details LONGTEXT, + logtime INT(11) NOT NULL, + PRIMARY KEY (id) + );"; + + require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); + + foreach($sql as $s) + dbDelta($s); + } + + public static function initialize_wpdb_tables(){ + global $wpdb; + + $wpdb->auth0_log = $wpdb->prefix."auth0_log"; + } + + private static function autoloader($class){ + $path = WPA0_PLUGIN_DIR; + $paths = array(); + $exts = array('.php', '.class.php'); + + $paths[] = $path; + $paths[] = $path.'lib/'; + + foreach($paths as $p) + foreach($exts as $ext){ + if(file_exists($p.$class.$ext)){ + require_once($p.$class.$ext); + return true; + } + } + + return false; + } } WP_Auth0::init(); \ No newline at end of file diff --git a/lib/WP_Auth0_Admin.php b/lib/WP_Auth0_Admin.php index 52f6298d..61b1bca7 100755 --- a/lib/WP_Auth0_Admin.php +++ b/lib/WP_Auth0_Admin.php @@ -1,267 +1,294 @@ __('Choose your icon', WPA0_LANG), - 'media_button' => __('Choose icon', WPA0_LANG) - )); - } - - public static function init_admin(){ - add_settings_section( - 'wp_auth0_settings_section', - __('Auth0 Settings', WPA0_LANG), - array(__CLASS__, 'render_description'), - WP_Auth0_Options::OPTIONS_NAME - ); - - add_settings_field( - 'wpa0_active', - __('Activate Auth0', WPA0_LANG), - array(__CLASS__, 'render_activate'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_active') - ); + 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')); + } - add_settings_field( - 'wpa0_auto_login', - __('Auto Login (no widget)', WPA0_LANG), - array(__CLASS__, 'render_auto_login'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_auto_login') - ); + public static function admin_enqueue(){ + if(!isset($_REQUEST['page']) || $_REQUEST['page'] != 'wpa0') + return; + + wp_enqueue_media(); + wp_enqueue_script( 'wpa0_admin', WPA0_PLUGIN_URL . 'assets/js/admin.js', array('jquery')); + wp_enqueue_style('media'); + + wp_localize_script( 'wpa0_admin', 'wpa0', array( + 'media_title' => __('Choose your icon', WPA0_LANG), + 'media_button' => __('Choose icon', WPA0_LANG) + )); + } + + public static function init_admin(){ + add_settings_section( + 'wp_auth0_settings_section', + __('Auth0 Settings', WPA0_LANG), + array(__CLASS__, 'render_description'), + WP_Auth0_Options::OPTIONS_NAME + ); + + add_settings_field( + 'wpa0_active', + __('Activate Auth0', WPA0_LANG), + array(__CLASS__, 'render_activate'), + WP_Auth0_Options::OPTIONS_NAME, + 'wp_auth0_settings_section', + array('label_for' => 'wpa0_active') + ); + + add_settings_field( + 'wpa0_domain', + __('Domain', WPA0_LANG), + array(__CLASS__, 'render_domain'), + WP_Auth0_Options::OPTIONS_NAME, + 'wp_auth0_settings_section', + array('label_for' => 'wpa0_domain') + ); + + add_settings_field( + 'wpa0_client_id', + __('Client ID', WPA0_LANG), + array(__CLASS__, 'render_client_id'), + WP_Auth0_Options::OPTIONS_NAME, + 'wp_auth0_settings_section', + array('label_for' => 'wpa0_client_id') + ); - $auto_login = absint(WP_Auth0_Options::get( 'auto_login' )) == 1; - if($auto_login) - add_settings_field( - 'wpa0_auto_login_method', - __('Auto Login Method', WPA0_LANG), - array(__CLASS__, 'render_auto_login_method'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_auto_login_method') - ); - add_settings_field( - 'wpa0_redirect_referer', - __('Enable on /sso/ Redirect', WPA0_LANG), - array(__CLASS__, 'render_redirect_referer'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_redirect_referer') - ); + 'wpa0_client_secret', + __('Client Secret', WPA0_LANG), + array(__CLASS__, 'render_client_secret'), + WP_Auth0_Options::OPTIONS_NAME, + 'wp_auth0_settings_section', + array('label_for' => 'wpa0_client_secret') + ); - add_settings_field( - 'wpa0_ip_range_check', - __('Enable on IP Ranges', WPA0_LANG), - array(__CLASS__, 'render_ip_range_check'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_ip_range_check') - ); + add_settings_field( + 'wpa0_auto_login', + __('Auto Login (no widget)', WPA0_LANG), + array(__CLASS__, 'render_auto_login'), + WP_Auth0_Options::OPTIONS_NAME, + 'wp_auth0_settings_section', + array('label_for' => 'wpa0_auto_login') + ); + + $auto_login = absint(WP_Auth0_Options::get( 'auto_login' )) == 1; + if($auto_login) { + add_settings_field( + 'wpa0_auto_login_method', + __('Auto Login Method', WPA0_LANG), + array(__CLASS__, 'render_auto_login_method'), + WP_Auth0_Options::OPTIONS_NAME, + 'wp_auth0_settings_section', + array('label_for' => 'wpa0_auto_login_method') + ); + } + + // add_settings_field( + // 'wpa0_redirect_referer', + // __('Enable on /sso/ Redirect', WPA0_LANG), + // array(__CLASS__, 'render_redirect_referer'), + // WP_Auth0_Options::OPTIONS_NAME, + // 'wp_auth0_settings_section', + // array('label_for' => 'wpa0_redirect_referer') + // ); + + add_settings_field( + 'wpa0_ip_range_check', + __('Enable on IP Ranges', WPA0_LANG), + array(__CLASS__, 'render_ip_range_check'), + WP_Auth0_Options::OPTIONS_NAME, + 'wp_auth0_settings_section', + array('label_for' => 'wpa0_ip_range_check') + ); + + $use_ip_ranges = absint(WP_Auth0_Options::get( 'ip_range_check' )) == 1; + if($use_ip_ranges) + add_settings_field( + 'wpa0_ip_ranges', + __('IP Ranges', WPA0_LANG), + array(__CLASS__, 'render_ip_ranges'), + WP_Auth0_Options::OPTIONS_NAME, + 'wp_auth0_settings_section', + array('label_for' => 'wpa0_ip_ranges') + ); + add_settings_field( + 'wpa0_wp_login_form', + __('Show WP Login Method', WPA0_LANG), + array(__CLASS__, 'render_wp_login_form'), + WP_Auth0_Options::OPTIONS_NAME, + 'wp_auth0_settings_section', + array('label_for' => 'wpa0_wp_login_form') + ); + add_settings_field( + 'wpa0_wp_login_btn_text', + __('WP Login Button Text', WPA0_LANG), + array(__CLASS__, 'render_wp_login_btn_text'), + WP_Auth0_Options::OPTIONS_NAME, + 'wp_auth0_settings_section', + array('label_for' => 'wpa0_wp_login_btn_text') + ); - $use_ip_ranges = absint(WP_Auth0_Options::get( 'ip_range_check' )) == 1; - if($use_ip_ranges) - add_settings_field( - 'wpa0_ip_ranges', - __('IP Ranges', WPA0_LANG), - array(__CLASS__, 'render_ip_ranges'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_ip_ranges') - ); add_settings_field( - 'wpa0_wp_login_form', - __('Show WP Login Method', WPA0_LANG), - array(__CLASS__, 'render_wp_login_form'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_wp_login_form') - ); + 'wpa0_form_title', + __('Form Title', WPA0_LANG), + array(__CLASS__, 'render_form_title'), + WP_Auth0_Options::OPTIONS_NAME, + 'wp_auth0_settings_section', + array('label_for' => 'wpa0_form_title') + ); add_settings_field( - 'wpa0_wp_login_btn_text', - __('WP Login Button Text', WPA0_LANG), - array(__CLASS__, 'render_wp_login_btn_text'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_wp_login_btn_text') - ); - - add_settings_field( - 'wpa0_client_id', - __('Client ID', WPA0_LANG), - array(__CLASS__, 'render_client_id'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_client_id') - ); - add_settings_field( - 'wpa0_client_secret', - __('Client Secret', WPA0_LANG), - array(__CLASS__, 'render_client_secret'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_client_secret') - ); - add_settings_field( - 'wpa0_endpoint', - __('Auth0 OAuth Endpoint', WPA0_LANG), - array(__CLASS__, 'render_endpoint'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_endpoint') - ); - add_settings_field( - 'wpa0_form_title', - __('Form Title', WPA0_LANG), - array(__CLASS__, 'render_form_title'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_form_title') - ); - add_settings_field( - 'wpa0_form_desc', - __('Form Description', WPA0_LANG), - array(__CLASS__, 'render_form_desc'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_form_desc') - ); - add_settings_field( - 'wpa0_show_icon', - __('Show Icon', WPA0_LANG), - array(__CLASS__, 'render_show_icon'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_show_icon') - ); - add_settings_field( - 'wpa0_icon_url', - __('Icon URL', WPA0_LANG), - array(__CLASS__, 'render_icon_url'), - WP_Auth0_Options::OPTIONS_NAME, - 'wp_auth0_settings_section', - array('label_for' => 'wpa0_icon_url') - ); - - - register_setting(WP_Auth0_Options::OPTIONS_NAME, WP_Auth0_Options::OPTIONS_NAME, array(__CLASS__, 'input_validator')); - } - - public static function render_client_id(){ - $v = WP_Auth0_Options::get( 'client_id' ); - echo ''; - } - public static function render_client_secret(){ - $v = WP_Auth0_Options::get( 'client_secret' ); - echo ''; - } - public static function render_endpoint(){ - $v = WP_Auth0_Options::get( 'endpoint' ); - echo ''; - echo '
' . __('This should only contain the HTTP protocol and domain! Example: https://1337.auth0.com/', WPA0_LANG) . ''; - } - public static function render_form_title(){ - $v = WP_Auth0_Options::get( 'form_title' ); - echo ''; - } - public static function render_form_desc(){ - $v = WP_Auth0_Options::get( 'form_desc' ); - echo ''; - } - public static function render_wp_login_btn_text(){ - $v = WP_Auth0_Options::get( 'wp_login_btn_text' ); - echo ''; - } - - public static function render_activate(){ - $v = absint(WP_Auth0_Options::get( 'active' )); - echo ''; - } - public static function render_auto_login(){ - $v = absint(WP_Auth0_Options::get( 'auto_login' )); - echo ''; - } - public static function render_auto_login_method(){ - $v = WP_Auth0_Options::get( 'auto_login_method' ); - echo ''; - echo '
' . __('To find the method name, log into Auth0 Dashboard, and navigate to: Connection -> [Connection Type] (eg. Social or Enterprise). Click the "down arrow" to expand the wanted method, and use the value in the "Name"-field. Example: google-oauth2', WPA0_LANG) . ''; - } - public static function render_redirect_referer(){ - $v = absint(WP_Auth0_Options::get( 'redirect_referer' )); - echo ''; - } - public static function render_ip_range_check(){ - $v = absint(WP_Auth0_Options::get( 'ip_range_check' )); - echo ''; - } - public static function render_ip_ranges(){ - $v = WP_Auth0_Options::get( 'ip_ranges' ); - echo ''; - echo '
' . __('Only one range per line! Range format should be as: xx.xx.xx.xx - yy.yy.yy.yy (spaces will be trimmed)', WPA0_LANG) . ''; - } - public static function render_wp_login_form(){ - $v = absint(WP_Auth0_Options::get( 'wp_login_form' )); - echo ''; - } - public static function render_show_icon(){ - $v = absint(WP_Auth0_Options::get( 'show_icon' )); - echo ''; - } - - public static function render_icon_url(){ - $v = WP_Auth0_Options::get( 'icon_url' ); - echo ''; - echo ' ' . __( 'Choose Icon', WPA0_LANG ) . ''; - echo '
' . __('The icon should be 32x32 pixels!', WPA0_LANG) . ''; - } - - public static function render_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'; - } - - public static function input_validator( $input ){ - $input['client_id'] = sanitize_text_field( $input['client_id'] ); - $input['form_title'] = sanitize_text_field( $input['form_title'] ); - $input['icon_url'] = esc_url( $input['icon_url'], array( - 'http', - 'https' - )); - if(empty($input['icon_url'])) - $input['show_icon'] = 0; - else - $input['show_icon'] = (isset($input['show_icon']) ? 1 : 0); - $input['active'] = (isset($input['active']) ? 1 : 0); - - - $input['endpoint'] = esc_url( $input['endpoint'], array('https', 'http') ); - if(!empty($input['endpoint'])) - $input['endpoint'] = trailingslashit($input['endpoint']); - - return $input; - } + 'wpa0_form_desc', + __('Form Description', WPA0_LANG), + array(__CLASS__, 'render_form_desc'), + WP_Auth0_Options::OPTIONS_NAME, + 'wp_auth0_settings_section', + array('label_for' => 'wpa0_form_desc') + ); + add_settings_field( + 'wpa0_show_icon', + __('Show Icon', WPA0_LANG), + array(__CLASS__, 'render_show_icon'), + WP_Auth0_Options::OPTIONS_NAME, + 'wp_auth0_settings_section', + array('label_for' => 'wpa0_show_icon') + ); + add_settings_field( + 'wpa0_icon_url', + __('Icon URL', WPA0_LANG), + array(__CLASS__, 'render_icon_url'), + WP_Auth0_Options::OPTIONS_NAME, + 'wp_auth0_settings_section', + array('label_for' => 'wpa0_icon_url') + ); + + + register_setting(WP_Auth0_Options::OPTIONS_NAME, WP_Auth0_Options::OPTIONS_NAME, array(__CLASS__, 'input_validator')); + } + + public static function render_client_id(){ + $v = WP_Auth0_Options::get( 'client_id' ); + echo ''; + echo '
' . __('Application id, copy from the auth0 dashboard', WPA0_LANG) . ''; + } + public static function render_client_secret(){ + $v = WP_Auth0_Options::get( 'client_secret' ); + echo ''; + echo '
' . __('Application secret, copy from the auth0 dashboard', WPA0_LANG) . ''; + } + public static function render_domain(){ + $v = WP_Auth0_Options::get( 'domain' ); + echo ''; + echo '
' . __('Your Auth0 domain, you can see it in the auth0 dashboard', WPA0_LANG) . ''; + } + public static function render_form_title(){ + $v = WP_Auth0_Options::get( 'form_title' ); + echo ''; + } + public static function render_form_desc(){ + $v = WP_Auth0_Options::get( 'form_desc' ); + echo ''; + } + public static function render_wp_login_btn_text(){ + $v = WP_Auth0_Options::get( 'wp_login_btn_text' ); + echo ''; + } + + public static function render_activate(){ + $v = absint(WP_Auth0_Options::get( 'active' )); + echo ''; + } + public static function render_auto_login(){ + $v = absint(WP_Auth0_Options::get( 'auto_login' )); + echo ''; + echo '
' . __('Mark this if you want to avoid using the auth0 widget and use a single login provider', WPA0_LANG) . ''; + } + public static function render_auto_login_method(){ + $v = WP_Auth0_Options::get( 'auto_login_method' ); + echo ''; + echo '
' . __('To find the method name, log into Auth0 Dashboard, and navigate to: Connection -> [Connection Type] (eg. Social or Enterprise). Click the "down arrow" to expand the wanted method, and use the value in the "Name"-field. Example: google-oauth2', WPA0_LANG) . ''; + } + // public static function render_redirect_referer(){ + // $v = absint(WP_Auth0_Options::get( 'redirect_referer' )); + // echo ''; + // } + public static function render_ip_range_check(){ + $v = absint(WP_Auth0_Options::get( 'ip_range_check' )); + echo ''; + } + public static function render_ip_ranges(){ + $v = WP_Auth0_Options::get( 'ip_ranges' ); + echo ''; + echo '
' . __('Only one range per line! Range format should be as: xx.xx.xx.xx - yy.yy.yy.yy (spaces will be trimmed)', WPA0_LANG) . ''; + } + public static function render_wp_login_form(){ + $v = absint(WP_Auth0_Options::get( 'wp_login_form' )); + echo ''; + } + public static function render_show_icon(){ + $v = absint(WP_Auth0_Options::get( 'show_icon' )); + echo ''; + } + + public static function render_icon_url(){ + $v = WP_Auth0_Options::get( 'icon_url' ); + echo ''; + echo ' ' . __( 'Choose Icon', WPA0_LANG ) . ''; + echo '
' . __('The icon should be 32x32 pixels!', WPA0_LANG) . ''; + } + + public static function render_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'; + } + + public static function input_validator( $input ){ + $input['client_id'] = sanitize_text_field( $input['client_id'] ); + $input['form_title'] = sanitize_text_field( $input['form_title'] ); + $input['icon_url'] = esc_url( $input['icon_url'], array( + 'http', + 'https' + )); + if(empty($input['icon_url'])) + $input['show_icon'] = 0; + else + $input['show_icon'] = (isset($input['show_icon']) ? 1 : 0); + $input['active'] = (isset($input['active']) ? 1 : 0); + + $error = ""; + if (empty($input["domain"]) ) { + $error = __("You need to specify domain", WPA0_LANG); + } + if (empty($input["client_id"])) { + $error = __("You need to specify a client id", WPA0_LANG); + } + if (empty($input["client_secret"])) { + $error = __("You need to specify a client secret", WPA0_LANG); + } + + if ($error != "") { + add_settings_error( + WP_Auth0_Options::OPTIONS_NAME, + WP_Auth0_Options::OPTIONS_NAME, + $error, + 'error' + ); + + } + + // $input['endpoint'] = esc_url( $input['endpoint'], array('https', 'http') ); + // if(!empty($input['endpoint'])) + // $input['endpoint'] = trailingslashit($input['endpoint']); + + return $input; + } } \ No newline at end of file diff --git a/lib/WP_Auth0_Options.php b/lib/WP_Auth0_Options.php index c5ac27e1..8dd2f5ee 100755 --- a/lib/WP_Auth0_Options.php +++ b/lib/WP_Auth0_Options.php @@ -1,55 +1,55 @@ 0, - 'auto_login' => 0, - 'auto_login_method' => '', - 'client_id' => '', - 'client_secret' => '', - 'endpoint' => '', - 'form_title' => '', + const OPTIONS_NAME = 'wp_auth0_settings'; + private static $_opt = null; + + private static function get_options(){ + if(empty(self::$_opt)){ + $options = get_option( self::OPTIONS_NAME, array()); + if(!is_array($options)) + $options = self::defaults(); + + $options = array_merge( self::defaults(), $options ); + + self::$_opt = $options; + } + return self::$_opt; + } + + public static function get( $key, $default = null ){ + $options = self::get_options(); + + if(!isset($options[$key])) + return apply_filters( 'wp_auth0_get_option', $default, $key ); + return apply_filters( 'wp_auth0_get_option', $options[$key], $key ); + } + + public static function set( $key, $value ){ + $options = self::get_options(); + + $options[$key] = $value; + + update_option( self::OPTIONS_NAME, $options ); + } + + private static function defaults(){ + return array( + 'active' => 0, + 'auto_login' => 0, + 'auto_login_method' => '', + 'client_id' => '', + 'client_secret' => '', + 'domain' => '', + 'form_title' => '', 'form_desc' => '', - 'show_icon' => 0, - 'icon_url' => '', - 'redirect_referer' => 0, - 'ip_range_check' => 0, - 'ip_ranges' => '', + 'show_icon' => 0, + 'icon_url' => '', + // 'redirect_referer' => 0, + 'ip_range_check' => 0, + 'ip_ranges' => '', 'wp_login_form' => 0, 'wp_login_btn_text' => __('Regular Login', WPA0_LANG) - ); - } + ); + } } \ No newline at end of file diff --git a/readme.txt b/readme.txt new file mode 100644 index 00000000..22c8887a --- /dev/null +++ b/readme.txt @@ -0,0 +1,21 @@ +=== Wordpress Auth0 Integration === +Tags: Login, oauth, authentication, facebook, google +Tested up to: 3.9 +License: MIT +License URI: https://github.com/auth0/wp-auth0/blob/master/LICENSE.md + +Provides Single Sing On to your wordpress site. You can use different auth providers as facebook, google, twitter, active directory, etc + +== Description == +This plugins allows you to extend the default user implementation and use the service provided by www.auth0.com + +You can make your users to login with facebook, google, linkedin, etc by a click of a button + + +== Installation == + +1. Upload the entire `wp-auth0` folder to the `/wp-content/plugins/` directory. +1. Activate the plugin through the 'Plugins' menu in WordPress. +1. In `settings` - `Auth0 Settings` edit the *Domain*, *Client ID* and *Client Secret* from your auth0 dashboard +1. Go to your auth0 dashboard, edit your application and add this to the available callbacks http:///index.php?auth0=1 + diff --git a/templates/login-auto.php b/templates/login-auto.php index 4f0bef92..c69ae98b 100755 --- a/templates/login-auto.php +++ b/templates/login-auto.php @@ -1,21 +1,23 @@ + if(empty($login_method)): ?>
- + \ No newline at end of file diff --git a/templates/login-form.php b/templates/login-form.php index ba11b13a..0071970d 100755 --- a/templates/login-form.php +++ b/templates/login-form.php @@ -1,59 +1,59 @@ -

+

-