Example #1
0
 public static function init_auth0()
 {
     global $wp_query;
     if (!isset($wp_query->query_vars['auth0'])) {
         return;
     }
     if ($wp_query->query_vars['auth0'] == 'implicit') {
         self::implicitLogin();
     }
     if ($wp_query->query_vars['auth0'] != '1') {
         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));
     $domain = WP_Auth0_Options::get('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));
     }
     $response = WP_Auth0_Api_Client::get_token($domain, $client_id, $client_secret, 'authorization_code', array('redirect_uri' => home_url(), 'code' => $code));
     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/>';
         $msg .= '<a href="' . wp_login_url() . '">' . __('← Login', WPA0_LANG) . '</a>';
         wp_die($msg);
     }
     $data = json_decode($response['body']);
     if (isset($data->access_token)) {
         // Get the user information
         $response = WP_Auth0_Api_Client::get_user_info($domain, $data->access_token);
         if ($response instanceof WP_Error) {
             self::insertAuth0Error('init_auth0_userinfo', $response);
             error_log($response->get_error_message());
             $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);
         }
         $userinfo = json_decode($response['body']);
         if (self::login_user($userinfo, $data->id_token, $data->access_token)) {
             if ($stateFromGet !== null && isset($stateFromGet->interim) && $stateFromGet->interim) {
                 include WPA0_PLUGIN_DIR . 'templates/login-interim.php';
                 exit;
             } else {
                 if ($stateFromGet !== null && isset($stateFromGet->redirect_to)) {
                     $redirectURL = $stateFromGet->redirect_to;
                 } else {
                     $redirectURL = WP_Auth0_Options::get('default_login_redirection');
                 }
                 wp_safe_redirect($redirectURL);
             }
         }
     } 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;
     }
     exit;
 }