public static function getUser($jwt, $encodedJWT)
 {
     global $wpdb;
     $sql = 'SELECT u.*
             FROM ' . $wpdb->auth0_user . ' a
             JOIN ' . $wpdb->users . ' u ON a.wp_id = u.id
             WHERE a.auth0_id = %s;';
     $userRow = $wpdb->get_row($wpdb->prepare($sql, $jwt->sub));
     if (is_null($userRow)) {
         $domain = WP_Auth0_Options::get('domain');
         $response = WP_Auth0_Api_Client::get_user($domain, $encodedJWT, $jwt->sub);
         if ($response['response']['code'] != 200) {
             return null;
         }
         $creator = new WP_Auth0_UserCreator();
         if ($creator->tokenHasRequiredScopes($jwt)) {
             $auth0User = $jwt;
         } else {
             $auth0User = json_decode($response['body']);
         }
         try {
             $user_id = $creator->create($auth0User, $encodedJWT);
             do_action('auth0_user_login', $user_id, $response, true, $encodedJWT, null);
             return new WP_User($user_id);
         } catch (WP_Auth0_CouldNotCreateUserException $e) {
             return null;
         } catch (WP_Auth0_RegistrationNotEnabledException $e) {
             return null;
         }
         return null;
     } elseif ($userRow instanceof WP_Error) {
         self::insertAuth0Error('findAuth0User', $userRow);
         return null;
     } else {
         $user = new WP_User();
         $user->init($userRow);
         do_action('auth0_user_login', $user->ID, $response, false, $encodedJWT, null);
         return $user;
     }
 }
Ejemplo n.º 2
0
 public static function login_user($userinfo, $id_token, $access_token)
 {
     // If the userinfo has no email or an unverified email, and in the options we require a verified email
     // notify the user he cant login until he does so.
     $requires_verified_email = WP_Auth0_Options::get('requires_verified_email');
     if ($requires_verified_email == 1) {
         if (empty($userinfo->email)) {
             $msg = __('This account does not have an email associated. Please login with a different provider.', WPA0_LANG);
             $msg .= '<br/><br/>';
             $msg .= '<a href="' . site_url() . '">' . __('← Go back', WPA0_LANG) . '</a>';
             wp_die($msg);
         }
         if (!$userinfo->email_verified) {
             self::dieWithVerifyEmail($userinfo, $id_token);
         }
     }
     // See if there is a user in the auth0_user table with the user info client id
     $user = self::findAuth0User($userinfo->user_id);
     if (!is_null($user)) {
         // User exists! Log in
         self::updateAuth0Object($userinfo);
         wp_set_auth_cookie($user->ID);
         do_action('auth0_user_login', $user->ID, $userinfo, false, $id_token, $access_token);
         return true;
     } else {
         try {
             $creator = new WP_Auth0_UserCreator();
             $user_id = $creator->create($userinfo, $id_token);
             wp_set_auth_cookie($user_id);
             do_action('auth0_user_login', $user_id, $userinfo, true, $id_token, $access_token);
         } catch (WP_Auth0_CouldNotCreateUserException $e) {
             $msg = __('Error: Could not create user.', WPA0_LANG);
             $msg = ' ' . $e->getMessage();
             $msg .= '<br/><br/>';
             $msg .= '<a href="' . site_url() . '">' . __('← Go back', WPA0_LANG) . '</a>';
             wp_die($msg);
         } catch (WP_Auth0_RegistrationNotEnabledException $e) {
             $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);
         } catch (WP_Auth0_EmailNotVerifiedException $e) {
             self::dieWithVerifyEmail($e->userinfo, $e->id_token);
         }
         return true;
     }
 }