Skip to content

Commit

Permalink
bump version + check if the db connection exists and avoid creating i…
Browse files Browse the repository at this point in the history
…t again
  • Loading branch information
glena committed Mar 16, 2016
1 parent 2be3276 commit 4a4e3c9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
4 changes: 2 additions & 2 deletions WP_Auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: Auth0 for WordPress
* Description: Implements the Auth0 Single Sign On solution into Wordpress
* Version: 2.1.2
* Version: 2.1.3
* Author: Auth0
* Author URI: https://auth0.com
*/
Expand All @@ -12,7 +12,7 @@
define( 'WPA0_PLUGIN_URL', trailingslashit( plugin_dir_url( __FILE__ ) ) );
define( 'WPA0_LANG', 'wp-auth0' );
define( 'AUTH0_DB_VERSION', 4 );
define( 'WPA0_VERSION', '2.1.2' );
define( 'WPA0_VERSION', '2.1.3' );

/**
* Main plugin class
Expand Down
56 changes: 40 additions & 16 deletions lib/initial-setup/WP_Auth0_InitialSetup_Consent.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,25 @@ public function consent_callback($name) {
$client_id = $client_response->client_id;
}

$db_connection_name = 'DB-' . str_replace(' ', '-', get_bloginfo('name'));
$connection_exists = false;
$connection_pwd_policy = false;

$connections = WP_Auth0_Api_Client::search_connection($domain, $app_token);

foreach ($connections as $connection) {

if ( in_array( $client_id, $connection->enabled_clients ) ) {
if ( $connection->strategy === 'auth0' && $should_create_and_update_connection) {
$enabled_clients = array_diff($connection->enabled_clients, array($client_id));
WP_Auth0_Api_Client::update_connection($domain, $app_token, $connection->id, array('enabled_clients' => array_values($enabled_clients)));

if ($db_connection_name === $connection->name) {
$connection_exists = $connection->id;
$connection_pwd_policy = (isset($connection->options) && isset($connection->options->passwordPolicy)) ? $connection->options->passwordPolicy : null;
} else {
$enabled_clients = array_diff($connection->enabled_clients, array($client_id));
WP_Auth0_Api_Client::update_connection($domain, $app_token, $connection->id, array('enabled_clients' => array_values($enabled_clients)));
}

} elseif ($connection->strategy !== 'auth0') {
$this->a0_options->set_connection( "social_{$connection->name}" , 1 );
$this->a0_options->set_connection( "social_{$connection->name}_key" , isset($connection->options->client_id) ? $connection->options->client_id : null );
Expand All @@ -127,20 +138,33 @@ public function consent_callback($name) {
}

if ($should_create_and_update_connection) {
$secret = $this->a0_options->get( 'client_secret' );
$token_id = uniqid();
$migration_token = JWT::encode(array('scope' => 'migration_ws', 'jti' => $token_id), JWT::urlsafeB64Decode( $secret ));
$migration_token_id = $token_id;

$operations = new WP_Auth0_Api_Operations($this->a0_options);
$response = $operations->create_wordpress_connection($this->a0_options->get( 'auth0_app_token' ), $this->hasInternetConnection, $migration_token);

$this->a0_options->set( "db_connection_id" , $response );
$this->a0_options->set( "db_connection_enabled" , $response ? 1 : 0 );
$this->a0_options->set( "migration_ws" , $this->hasInternetConnection );
$this->a0_options->set( "migration_token" , $migration_token );
$this->a0_options->set( "migration_token_id" , $migration_token_id );
$this->a0_options->set( "password_policy" , null );

if ($connection_exists === false) {

$secret = $this->a0_options->get( 'client_secret' );
$token_id = uniqid();
$migration_token = JWT::encode(array('scope' => 'migration_ws', 'jti' => $token_id), JWT::urlsafeB64Decode( $secret ));
$migration_token_id = $token_id;

$operations = new WP_Auth0_Api_Operations($this->a0_options);
$response = $operations->create_wordpress_connection($this->a0_options->get( 'auth0_app_token' ), $this->hasInternetConnection, $migration_token);

$this->a0_options->set( "migration_ws" , $this->hasInternetConnection );
$this->a0_options->set( "migration_token" , $migration_token );
$this->a0_options->set( "migration_token_id" , $migration_token_id );
$this->a0_options->set( "db_connection_enabled" , $response ? 1 : 0 );
$this->a0_options->set( "db_connection_id" , $response );
$this->a0_options->set( "password_policy" , null );

} else {

$this->a0_options->set( "db_connection_enabled" , 1 );
$this->a0_options->set( "db_connection_id" , $connection_exists );
$this->a0_options->set( "password_policy" , $connection_pwd_policy );

}


}


Expand Down

0 comments on commit 4a4e3c9

Please sign in to comment.