Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f4d55de
Don't show widget when registrations are not allowed. #5
glena Oct 9, 2014
0fbd6e3
Moved errors log to a page and added link to menu and pugin list.
glena Oct 9, 2014
7c68bef
added message when there were any errors in the last month.
glena Oct 10, 2014
6879659
plugin version changed
glena Oct 10, 2014
63a09fd
add nice error message when exchange of token returns 401 #11
glena Oct 10, 2014
13ae30a
Change the way we handle the 401 status code
glena Oct 10, 2014
378dc47
fix
glena Oct 10, 2014
f01a55c
error message updated
glena Oct 10, 2014
042c126
Message where are no errors on the log changed.
glena Oct 10, 2014
29cc639
add error handling with typed api errors
glena Oct 10, 2014
1d31722
Don't show widget when registrations are not allowed. #5
glena Oct 10, 2014
e9ceb24
Show WP Auth0 Logs somewhere so that we can easily diagnose problems…
glena Oct 16, 2014
c910bfa
Show WP Auth0 Logs somewhere so that we can easily diagnose problems…
glena Oct 16, 2014
27f86e7
Don't show widget when registrations are not allowed. #5 (remove th…
glena Oct 16, 2014
1c5039c
add nice error message when exchange of token returns 401 #11 (now i…
glena Oct 16, 2014
7a1e7e5
fix error handling & registration with social network (without email …
glena Oct 16, 2014
32d1258
added A0 icon
glena Oct 16, 2014
5785aeb
Add option to enter custom CSS #21
glena Oct 17, 2014
c490ede
fixed the way it loads the plugin url to show the A0 icon
glena Oct 27, 2014
419c6ba
Added "Changes saved" Label on settings page
glena Oct 27, 2014
6c299f9
Change the text from the current one to There was a problem with your…
glena Oct 27, 2014
41e92fc
Extra enter & Set the options to "Yes", "No", "Default Setting"
glena Oct 27, 2014
3d72b14
Merge pull request #27 from auth0/issue21_customcss
mgonto Oct 29, 2014
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
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
166 changes: 158 additions & 8 deletions WP_Auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 "<div class=\"a0-message\">$message <small onclick=\"jQuery('.a0-message').hide();\">(Close)</small></div>";
echo '<script type="text/javascript">
setTimeout(function(){jQuery(".a0-message").hide();}, 10 * 1000);
</script>';
}
}

// Add settings link on plugin page
public static function wp_add_plugin_settings_link($links) {
$settings_link = '<a href="options-general.php?page=wpa0">Settings</a>';

$settings_link = '<a href="admin.php?page=wpa0-errors">Error Log</a>';
array_unshift($links, $settings_link);

$settings_link = '<a href="admin.php?page=wpa0">Settings</a>';
array_unshift($links, $settings_link);

return $links;
}

Expand All @@ -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' );
}

Expand Down Expand Up @@ -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']);
}
Expand Down Expand Up @@ -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 .= '<br/><br/>';
$msg .= '<a href="' . wp_login_url() . '">' . __('← Login', WPA0_LANG) . '</a>';
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 .= '<br/><br/>';
$msg .= '<a href="' . wp_login_url() . '">' . __('← Login', WPA0_LANG) . '</a>';
wp_die($msg);
}

$code = $wp_query->query_vars['code'];
$state = $wp_query->query_vars['state'];
$stateFromGet = json_decode(stripcslashes($state));
Expand All @@ -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'
);
Expand All @@ -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 .= '<br/><br/>';
Expand All @@ -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 .= '<br/><br/>';
$msg .= '<a href="' . wp_login_url() . '">' . __('← Login', WPA0_LANG) . '</a>';
wp_die($msg);
Expand All @@ -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 .= '<br/><br/>';
$msg .= '<a href="' . wp_login_url() . '">' . __('← Login', WPA0_LANG) . '</a>';
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:<br/>" . $data->error_description;
Expand All @@ -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();
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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 .= '<br/><br/>';
$msg .= '<a href="' . site_url() . '">' . __('← Go back', WPA0_LANG) . '</a>';
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 .= '<br/><br/>';
$msg .= '<a href="' . site_url() . '">' . __('← Go back', WPA0_LANG) . '</a>';
Expand All @@ -407,6 +539,11 @@ private static function login_user( $userinfo, $data ){
$msg .= '<a href="' . site_url() . '">' . __('← Go back', WPA0_LANG) . '</a>';
wp_die($msg);
}
} else {
$msg = __('Error: Could not create user. The registration process is not available.', WPA0_LANG);
$msg .= '<br/><br/>';
$msg .= '<a href="' . site_url() . '">' . __('← Go back', WPA0_LANG) . '</a>';
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
Expand Down Expand Up @@ -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) {
Expand All @@ -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){
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
27 changes: 27 additions & 0 deletions assets/css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Binary file added assets/img/a0icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading