public static function login($username, $password) { global $wpdb; $user = $wpdb->get_row($wpdb->prepare("SELECT ID, user_pass\n\t\t\t\tFROM {$wpdb->users}\n\t\t\t\tWHERE user_login = %s OR user_email = %s", $username, $username)); if (isset($user) && wp_check_password($password, $user->user_pass, $user->ID)) { $tokenData = get_user_meta($user->ID, 'bimsie_token', true); if (isset($tokenData) && $tokenData != '' && $tokenData['timestamp'] > time() - Bimsie::$tokenTimeout) { // Token is still valid return BIMsie::updateTokenTimestamp($user->ID); } else { return BIMsie::updateTokenTimestamp($user->ID, BIMSie::generateToken()); } } else { return false; } }
public static function authenticateWithToken($token) { global $wpdb; $response = array('error' => false); if (strlen($token) < 32) { $response['error'] = true; $response['errorType'] = 'UserException'; $response['errorMessage'] = __('Invalid token', 'bim-bcf-management'); } else { $userId = $wpdb->get_var($wpdb->prepare("SELECT user_id\n FROM {$wpdb->usermeta}\n WHERE meta_key LIKE '_bcf_viewer_token_%%' AND meta_value COLLATE utf8_bin LIKE %s", $token)); if ($userId != '') { $timestamp = get_user_meta($userId, '_bcf_viewer_timestamp', true); if ($timestamp > time()) { // Token is valid $serverId = $wpdb->get_var($wpdb->prepare("SELECT meta_key\n FROM {$wpdb->usermeta}\n WHERE meta_key LIKE '_bcf_viewer_token_%%' AND meta_value COLLATE utf8_bin LIKE %s AND user_id = %d", $token, $userId)); $serverId = str_replace('_bcf_viewer_token_', '', $serverId); $server = BIMsie::getServerById($serverId, $userId); if ($server !== false) { $tokenData = get_user_meta($userId, 'bimsie_token', true); if (isset($tokenData) && $tokenData != '' && $tokenData['timestamp'] > time() - Bimsie::$tokenTimeout) { // Token is still valid $token = BIMsie::updateTokenTimestamp($userId); } else { $token = BIMsie::updateTokenTimestamp($userId, BIMSie::generateToken()); } $response['result'] = array('bimserver_url' => $server['uri'], 'bimserver_username' => $server['username'], 'bimserver_password' => $server['password'], 'bcfserver_token' => $token); } else { $response['error'] = true; $response['errorType'] = 'UserException'; $response['errorMessage'] = __('Invalid token', 'bim-bcf-management'); } } else { $response['error'] = true; $response['errorType'] = 'UserException'; $response['errorMessage'] = __('Expired token', 'bim-bcf-management'); } } else { $response['error'] = true; $response['errorType'] = 'UserException'; $response['errorMessage'] = __('Invalid token', 'bim-bcf-management'); } } return $response; }