Skip to content

Commit 6ddc84a

Browse files
authored
Merge pull request #214 from auth0/dev
refactor error handling, fix rules creation with site name, fix SLO
2 parents 44a43e3 + 8a6adbe commit 6ddc84a

File tree

8 files changed

+48
-56
lines changed

8 files changed

+48
-56
lines changed

WP_Auth0.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function check_signup_status() {
137137

138138
$operations->disable_signup_wordpress_connection( $app_token, !$is_wp_registration_enabled );
139139

140-
$rule_name = WP_Auth0_RulesLib::$disable_social_signup['name'];
140+
$rule_name = WP_Auth0_RulesLib::$disable_social_signup['name'] . '-' . get_bloginfo('name');
141141

142142
$rule_script = WP_Auth0_RulesLib::$disable_social_signup['script'];
143143
$rule_script = str_replace( 'REPLACE_WITH_YOUR_CLIENT_ID', $this->a0_options->get( 'client_id' ), $rule_script );

lib/WP_Auth0_Api_Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,13 @@ public static function create_rule( $domain, $app_token, $name, $script, $enable
329329
) );
330330

331331
if ( $response instanceof WP_Error ) {
332-
WP_Auth0_ErrorManager::insert_auth0_error( 'WP_Auth0_Api_Client::create_rule', $response );
332+
WP_Auth0_ErrorManager::insert_auth0_error( 'WP_Auth0_Api_Client::create_rule ' . $name, $response );
333333
error_log( $response->get_error_message() );
334334
return false;
335335
}
336336

337337
if ( $response['response']['code'] != 201 ) {
338-
WP_Auth0_ErrorManager::insert_auth0_error( 'WP_Auth0_Api_Client::create_rule', $response['body'] );
338+
WP_Auth0_ErrorManager::insert_auth0_error( 'WP_Auth0_Api_Client::create_rule ' . $name, $response['body'] );
339339
error_log( $response['body'] );
340340
return false;
341341
}

lib/WP_Auth0_Api_Operations.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function disable_signup_wordpress_connection( $app_token, $disable_signup
1414
$connections = WP_Auth0_Api_Client::search_connection( $domain, $app_token, 'auth0' );
1515

1616
foreach ( $connections as $connection ) {
17+
1718
if ( in_array( $client_id, $connection->enabled_clients ) ) {
1819
$connection->options->disable_signup = $disable_signup;
1920
$connection_id = $connection->id;
@@ -25,6 +26,7 @@ public function disable_signup_wordpress_connection( $app_token, $disable_signup
2526
WP_Auth0_Api_Client::update_connection( $domain, $app_token, $connection_id, $connection );
2627
}
2728
}
29+
2830
}
2931

3032
public function update_wordpress_connection( $app_token, $connection_id, $password_policy, $migration_token ) {

lib/WP_Auth0_LoginManager.php

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,28 @@ public function login_auto() {
147147
public function init_auth0() {
148148
global $wp_query;
149149

150-
// WP_Auth0_Seeder::get_me(100);
151-
// exit;
152-
153150
if ( $this->query_vars( 'auth0' ) === null ) {
154151
return;
155152
}
156153

157-
if ( $this->query_vars( 'auth0' ) === 'implicit' ) {
158-
$this->implicit_login();
159-
} else {
160-
$this->redirect_login();
154+
try {
155+
if ( $this->query_vars( 'auth0' ) === 'implicit' ) {
156+
$this->implicit_login();
157+
} else {
158+
$this->redirect_login();
159+
}
160+
} catch (WP_Auth0_LoginFlowValidationException $e) {
161+
162+
$msg = __( 'There was a problem with your log in', WPA0_LANG );
163+
$msg .= ' '. $e->getMessage();
164+
$msg .= '<br/><br/>';
165+
$msg .= '<a href="' . wp_login_url() . '">' . __( '← Login', WPA0_LANG ) . '</a>';
166+
wp_die( $msg );
167+
168+
} catch (Exception $e) {
169+
161170
}
171+
162172
}
163173

164174
public function redirect_login() {
@@ -169,19 +179,11 @@ public function redirect_login() {
169179
}
170180

171181
if ( $this->query_vars( 'error_description' ) !== null && $this->query_vars( 'error_description' ) !== '' ) {
172-
$msg = __( 'There was a problem with your log in:', WPA0_LANG );
173-
$msg .= ' '.$this->query_vars( 'error_description' );
174-
$msg .= '<br/><br/>';
175-
$msg .= '<a href="' . wp_login_url() . '">' . __( '← Login', WPA0_LANG ) . '</a>';
176-
wp_die( $msg );
182+
throw new WP_Auth0_LoginFlowValidationException( $this->query_vars( 'error_description' ) );
177183
}
178184

179185
if ( $this->query_vars( 'error' ) !== null && trim( $this->query_vars( 'error' ) ) !== '' ) {
180-
$msg = __( 'There was a problem with your log in:', WPA0_LANG );
181-
$msg .= ' '.$this->query_vars( 'error' );
182-
$msg .= '<br/><br/>';
183-
$msg .= '<a href="' . wp_login_url() . '">' . __( '← Login', WPA0_LANG ) . '</a>';
184-
wp_die( $msg );
186+
throw new WP_Auth0_LoginFlowValidationException( $this->query_vars( 'error' ) );
185187
}
186188

187189
$code = $this->query_vars( 'code' );
@@ -195,13 +197,13 @@ public function redirect_login() {
195197
$client_secret = $this->a0_options->get( 'client_secret' );
196198

197199
if ( empty( $client_id ) ) {
198-
wp_die( __( 'Error: Your Auth0 Client ID has not been entered in the Auth0 SSO plugin settings.', WPA0_LANG ) );
200+
throw new WP_Auth0_LoginFlowValidationException( __( 'Error: Your Auth0 Client ID has not been entered in the Auth0 SSO plugin settings.', WPA0_LANG ) );
199201
}
200202
if ( empty( $client_secret ) ) {
201-
wp_die( __( 'Error: Your Auth0 Client Secret has not been entered in the Auth0 SSO plugin settings.', WPA0_LANG ) );
203+
throw new WP_Auth0_LoginFlowValidationException( __( 'Error: Your Auth0 Client Secret has not been entered in the Auth0 SSO plugin settings.', WPA0_LANG ) );
202204
}
203205
if ( empty( $domain ) ) {
204-
wp_die( __( 'Error: No Domain defined in Wordpress Administration!', WPA0_LANG ) );
206+
throw new WP_Auth0_LoginFlowValidationException( __( 'Error: No Domain defined in Wordpress Administration!', WPA0_LANG ) );
205207
}
206208

207209
$response = WP_Auth0_Api_Client::get_token( $domain, $client_id, $client_secret, 'authorization_code', array(
@@ -213,10 +215,8 @@ public function redirect_login() {
213215
WP_Auth0_ErrorManager::insert_auth0_error( 'init_auth0_oauth/token', $response );
214216

215217
error_log( $response->get_error_message() );
216-
$msg = __( 'Sorry. There was a problem logging you in.', WPA0_LANG );
217-
$msg .= '<br/><br/>';
218-
$msg .= '<a href="' . wp_login_url() . '">' . __( '← Login', WPA0_LANG ) . '</a>';
219-
wp_die( $msg );
218+
219+
throw new WP_Auth0_LoginFlowValidationException( $response->get_error_message() );
220220
}
221221

222222
$data = json_decode( $response['body'] );
@@ -233,10 +233,8 @@ public function redirect_login() {
233233
WP_Auth0_ErrorManager::insert_auth0_error( 'init_auth0_userinfo', $response );
234234

235235
error_log( $response->get_error_message() );
236-
$msg = __( 'There was a problem with your log in.', WPA0_LANG );
237-
$msg .= '<br/><br/>';
238-
$msg .= '<a href="' . wp_login_url() . '">' . __( '← Login', WPA0_LANG ) . '</a>';
239-
wp_die( $msg );
236+
237+
throw new WP_Auth0_LoginFlowValidationException( );
240238
}
241239

242240
$userinfo = json_decode( $response['body'] );
@@ -261,9 +259,8 @@ public function redirect_login() {
261259
WP_Auth0_ErrorManager::insert_auth0_error( 'init_auth0_oauth/token', $error );
262260

263261
$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 );
264-
$msg .= '<br/><br/>';
265-
$msg .= '<a href="' . wp_login_url() . '">' . __( '← Login', WPA0_LANG ) . '</a>';
266-
wp_die( $msg );
262+
263+
throw new WP_Auth0_LoginFlowValidationException( $msg );
267264
} else {
268265
$error = '';
269266
$description = '';
@@ -323,14 +320,11 @@ public function implicit_login() {
323320
}
324321

325322
} catch( UnexpectedValueException $e ) {
326-
327323
WP_Auth0_ErrorManager::insert_auth0_error( 'implicit_login', $e );
328324

329325
error_log( $e->getMessage() );
330-
$msg = __( 'Sorry. There was a problem logging you in.', WPA0_LANG );
331-
$msg .= '<br/><br/>';
332-
$msg .= '<a href="' . wp_login_url() . '">' . __( '← Login', WPA0_LANG ) . '</a>';
333-
wp_die( $msg );
326+
327+
throw new WP_Auth0_LoginFlowValidationException( );
334328
}
335329
}
336330

@@ -340,13 +334,12 @@ public function login_user( $userinfo, $id_token, $access_token ) {
340334
$requires_verified_email = $this->a0_options->get( 'requires_verified_email' );
341335
$remember_users_session = $this->a0_options->get( 'remember_users_session' );
342336

337+
343338
if ( ! $this->ignore_unverified_email && 1 == $requires_verified_email ) {
344339
if ( empty( $userinfo->email ) ) {
345340
$msg = __( 'This account does not have an email associated, as required by your site administrator.', WPA0_LANG );
346-
$msg .= '<br/><br/>';
347-
$msg .= '<a href="' . home_url() . '">' . __( '← Go back', WPA0_LANG ) . '</a>';
348341

349-
wp_die( $msg );
342+
throw new WP_Auth0_LoginFlowValidationException( 'PEPE' );
350343
}
351344

352345
if ( ! $userinfo->email_verified ) {
@@ -399,16 +392,9 @@ public function login_user( $userinfo, $id_token, $access_token ) {
399392
do_action( 'auth0_user_login' , $user_id, $userinfo, true, $id_token, $access_token );
400393
}
401394
catch ( WP_Auth0_CouldNotCreateUserException $e ) {
402-
$msg = __( 'Error: Could not create user.', WPA0_LANG );
403-
$msg = ' ' . $e->getMessage();
404-
$msg .= '<br/><br/>';
405-
$msg .= '<a href="' . home_url() . '">' . __( '← Go back', WPA0_LANG ) . '</a>';
406-
wp_die( $msg );
395+
throw new WP_Auth0_LoginFlowValidationException( $e->getMessage() );
407396
} catch ( WP_Auth0_RegistrationNotEnabledException $e ) {
408-
$msg = __( 'Error: Could not create user. The registration process is not available. Please contact your site’s administrator.', WPA0_LANG );
409-
$msg .= '<br/><br/>';
410-
$msg .= '<a href="' . home_url() . '">' . __( '← Go back', WPA0_LANG ) . '</a>';
411-
wp_die( $msg );
397+
throw new WP_Auth0_LoginFlowValidationException( 'Could not create user. The registration process is not available. Please contact your site’s administrator.' );
412398
} catch ( WP_Auth0_EmailNotVerifiedException $e ) {
413399
$this->dieWithVerifyEmail( $e->userinfo, $e->id_token );
414400
}

lib/admin/WP_Auth0_Admin_Features.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ public function basic_validation( $old_options, $input ) {
160160

161161
public function sso_validation( $old_options, $input ) {
162162
$input['sso'] = ( isset( $input['sso'] ) ? $input['sso'] : 0 );
163+
163164
if ( $old_options['sso'] != $input['sso'] && 1 == $input['sso'] ) {
164165
if ( false === WP_Auth0_Api_Client::update_client( $input['domain'], $input['auth0_app_token'], $input['client_id'], $input['sso'] == 1 ) ) {
165166

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
class WP_Auth0_LoginFlowValidationException extends Exception {}

lib/initial-setup/WP_Auth0_InitialSetup_Rules.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ public function callback() {
4343

4444
$mfa_script = WP_Auth0_RulesLib::$google_MFA['script'];
4545
$mfa_script = str_replace( 'REPLACE_WITH_YOUR_CLIENT_ID', $client_id, $mfa_script );
46-
$input = $this->rule_validation( $old_options, $input, 'mfa', WP_Auth0_RulesLib::$google_MFA['name'], $mfa_script );
46+
$input = $this->rule_validation( $old_options, $input, 'mfa', WP_Auth0_RulesLib::$google_MFA['name'] . '-' . get_bloginfo('name'), $mfa_script );
4747

48-
$input = $this->rule_validation( $old_options, $input, 'geo_rule', WP_Auth0_RulesLib::$geo['name'], WP_Auth0_RulesLib::$geo['script'] );
48+
$input = $this->rule_validation( $old_options, $input, 'geo_rule', WP_Auth0_RulesLib::$geo['name'] . '-' . get_bloginfo('name'), WP_Auth0_RulesLib::$geo['script'] );
4949

50-
$input = $this->rule_validation( $old_options, $input, 'income_rule', WP_Auth0_RulesLib::$income['name'], WP_Auth0_RulesLib::$income['script'] );
50+
$input = $this->rule_validation( $old_options, $input, 'income_rule', WP_Auth0_RulesLib::$income['name'] . '-' . get_bloginfo('name'), WP_Auth0_RulesLib::$income['script'] );
5151

5252
$fullcontact_script = WP_Auth0_RulesLib::$fullcontact['script'];
5353
$fullcontact_script = str_replace( 'REPLACE_WITH_YOUR_CLIENT_ID', $input['fullcontact_apikey'], $fullcontact_script );
54-
$input = $this->rule_validation( $old_options, $input, 'fullcontact', WP_Auth0_RulesLib::$fullcontact['name'], $fullcontact_script );
54+
$input = $this->rule_validation( $old_options, $input, 'fullcontact', WP_Auth0_RulesLib::$fullcontact['name'] . '-' . get_bloginfo('name'), $fullcontact_script );
5555

5656
$this->a0_options->set( 'fullcontact_apikey', $input['fullcontact_apikey'] );
5757

templates/auth0-singlelogout-handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<script type="text/javascript">
33
(function(){
44

5-
var uuids = '<?php echo $profile->user_id; ?>';
5+
var uuids = '<?php echo $user_profile->user_id; ?>';
66
document.addEventListener("DOMContentLoaded", function() {
77
var lock = new Auth0Lock('<?php echo $client_id; ?>', '<?php echo $domain; ?>');
88
lock.$auth0.getSSOData(function(err, data) {

0 commit comments

Comments
 (0)