From 2f7293ae0fb52117aaf623353b9db338dc19eb32 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Mon, 8 Jan 2018 11:52:45 -0800 Subject: [PATCH 1/3] Creating and implementing WP_Auth0_Api_Client::signup_user --- lib/WP_Auth0_Api_Client.php | 28 +++++++++++++++++++ .../WP_Auth0_InitialSetup_AdminUser.php | 13 +++------ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/lib/WP_Auth0_Api_Client.php b/lib/WP_Auth0_Api_Client.php index ac15397a..ffb6421f 100755 --- a/lib/WP_Auth0_Api_Client.php +++ b/lib/WP_Auth0_Api_Client.php @@ -191,6 +191,34 @@ public static function create_user( $domain, $jwt, $data ) { return json_decode( $response['body'] ); } + public static function signup_user( $domain, $data ) { + + $endpoint = "https://$domain/dbconnections/signup"; + + $headers = self::get_info_headers(); + + $headers['content-type'] = "application/json"; + + $response = wp_remote_post( $endpoint , array( + 'headers' => $headers, + 'body' => json_encode( $data ) + ) ); + + if ( $response instanceof WP_Error ) { + WP_Auth0_ErrorManager::insert_auth0_error( 'WP_Auth0_Api_Client::signup_user', $response ); + error_log( $response->get_error_message() ); + return false; + } + + if ( $response['response']['code'] != 201 ) { + WP_Auth0_ErrorManager::insert_auth0_error( 'WP_Auth0_Api_Client::signup_user', $response['body'] ); + error_log( $response['body'] ); + return false; + } + + return json_decode( $response['body'] ); + } + public static function get_required_scopes() { return array( 'update:clients', diff --git a/lib/initial-setup/WP_Auth0_InitialSetup_AdminUser.php b/lib/initial-setup/WP_Auth0_InitialSetup_AdminUser.php index e1f0e5b1..a19f3753 100644 --- a/lib/initial-setup/WP_Auth0_InitialSetup_AdminUser.php +++ b/lib/initial-setup/WP_Auth0_InitialSetup_AdminUser.php @@ -20,23 +20,18 @@ public function callback() { $current_user = wp_get_current_user(); - $db_connection_name = $this->a0_options->get( "db_connection_name" ); - $domain = $this->a0_options->get( 'domain' ); - $jwt = $this->a0_options->get( 'auth0_app_token' ); - $data = array( + 'client_id' => $this->a0_options->get( 'client_id' ), 'email' => $current_user->user_email, 'password' => $_POST['admin-password'], - 'connection' => $db_connection_name, - 'email_verified' => true + 'connection' => $this->a0_options->get( "db_connection_name" ) ); - $admin_user = WP_Auth0_Api_Client::create_user( $domain, $jwt, $data ); + $admin_user = WP_Auth0_Api_Client::signup_user( $this->a0_options->get( 'domain' ), $data ); if ( $admin_user === false ) { wp_redirect( admin_url( "admin.php?page=wpa0-setup&step=3&profile=social&result=error" ) ); - } - else { + } else { wp_redirect( admin_url( "admin.php?page=wpa0-setup&step=4&profile=social" ) ); } exit; From 509b42c882bba27144ebd07f9fbaf2089df5f821 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Mon, 8 Jan 2018 12:48:53 -0800 Subject: [PATCH 2/3] check that status code returned in WP_Auth0_Api_Client::signup_user is 200 --- lib/WP_Auth0_Api_Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/WP_Auth0_Api_Client.php b/lib/WP_Auth0_Api_Client.php index ffb6421f..51d22d6e 100755 --- a/lib/WP_Auth0_Api_Client.php +++ b/lib/WP_Auth0_Api_Client.php @@ -210,7 +210,7 @@ public static function signup_user( $domain, $data ) { return false; } - if ( $response['response']['code'] != 201 ) { + if ( $response['response']['code'] !== 200 ) { WP_Auth0_ErrorManager::insert_auth0_error( 'WP_Auth0_Api_Client::signup_user', $response['body'] ); error_log( $response['body'] ); return false; From 2799710b7b44ffcc07bf535ff4be05d89db7e228 Mon Sep 17 00:00:00 2001 From: Josh Cunningham Date: Mon, 8 Jan 2018 14:22:34 -0800 Subject: [PATCH 3/3] better quotes --- lib/WP_Auth0_Api_Client.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/WP_Auth0_Api_Client.php b/lib/WP_Auth0_Api_Client.php index 51d22d6e..a2386bde 100755 --- a/lib/WP_Auth0_Api_Client.php +++ b/lib/WP_Auth0_Api_Client.php @@ -168,10 +168,9 @@ public static function create_user( $domain, $jwt, $data ) { $headers = self::get_info_headers(); $headers['Authorization'] = "Bearer $jwt"; - $headers['content-type'] = "application/json"; + $headers['content-type'] = 'application/json'; $response = wp_remote_post( $endpoint , array( - 'method' => 'POST', 'headers' => $headers, 'body' => json_encode( $data ) ) ); @@ -197,7 +196,7 @@ public static function signup_user( $domain, $data ) { $headers = self::get_info_headers(); - $headers['content-type'] = "application/json"; + $headers['content-type'] = 'application/json'; $response = wp_remote_post( $endpoint , array( 'headers' => $headers,