|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Helper class for credential obfuscation functionality. |
| 4 | + * |
| 5 | + * @package Classifai |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Classifai\Providers; |
| 9 | + |
| 10 | +use Classifai\Providers\ProviderProfiles; |
| 11 | + |
| 12 | +if ( ! defined( 'ABSPATH' ) ) { |
| 13 | + exit; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * CredentialObfuscator class. |
| 18 | + * |
| 19 | + * Handles obfuscation of sensitive credentials when displayed in the admin UI |
| 20 | + * and preserves original values when obfuscated values are submitted. |
| 21 | + * |
| 22 | + * @since x.x.x |
| 23 | + */ |
| 24 | +class CredentialObfuscator { |
| 25 | + |
| 26 | + /** |
| 27 | + * Number of characters to show at the beginning of obfuscated values. |
| 28 | + * |
| 29 | + * @var int |
| 30 | + */ |
| 31 | + const VISIBLE_PREFIX_LENGTH = 8; |
| 32 | + |
| 33 | + /** |
| 34 | + * Minimum number of consecutive asterisks to detect an obfuscated value. |
| 35 | + * |
| 36 | + * @var int |
| 37 | + */ |
| 38 | + const MIN_ASTERISKS_TO_DETECT = 3; |
| 39 | + |
| 40 | + /** |
| 41 | + * Obfuscate a credential value. |
| 42 | + * |
| 43 | + * Returns first N characters followed by asterisks. |
| 44 | + * Example: "sk-abc123xyz789" becomes "sk-abc************" |
| 45 | + * |
| 46 | + * @since x.x.x |
| 47 | + * |
| 48 | + * @param string $value The credential value to obfuscate. |
| 49 | + * @return string The obfuscated value, or original if too short. |
| 50 | + */ |
| 51 | + public static function obfuscate( string $value ): string { |
| 52 | + if ( empty( $value ) ) { |
| 53 | + return $value; |
| 54 | + } |
| 55 | + |
| 56 | + $length = strlen( $value ); |
| 57 | + |
| 58 | + // If the value is too short, just return asterisks. |
| 59 | + if ( $length <= self::VISIBLE_PREFIX_LENGTH && $length <= self::MIN_ASTERISKS_TO_DETECT ) { |
| 60 | + return str_repeat( '*', self::MIN_ASTERISKS_TO_DETECT ); |
| 61 | + } elseif ( $length <= self::VISIBLE_PREFIX_LENGTH ) { |
| 62 | + return str_repeat( '*', $length ); |
| 63 | + } |
| 64 | + |
| 65 | + $prefix = substr( $value, 0, self::VISIBLE_PREFIX_LENGTH ); |
| 66 | + $asterisks = str_repeat( '*', $length - self::VISIBLE_PREFIX_LENGTH ); |
| 67 | + |
| 68 | + // If we don't have enough asterisks, add more. |
| 69 | + if ( strlen( $asterisks ) < self::MIN_ASTERISKS_TO_DETECT ) { |
| 70 | + $asterisks = str_repeat( '*', self::MIN_ASTERISKS_TO_DETECT ); |
| 71 | + } |
| 72 | + |
| 73 | + return $prefix . $asterisks; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Check if a value appears to be obfuscated. |
| 78 | + * |
| 79 | + * Detects values containing 3 or more consecutive asterisks. |
| 80 | + * |
| 81 | + * @since x.x.x |
| 82 | + * |
| 83 | + * @param string $value The value to check. |
| 84 | + * @return bool True if the value appears to be obfuscated. |
| 85 | + */ |
| 86 | + public static function is_obfuscated( string $value ): bool { |
| 87 | + if ( empty( $value ) ) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + $pattern = '/\*{' . self::MIN_ASTERISKS_TO_DETECT . ',}/'; |
| 92 | + return (bool) preg_match( $pattern, $value ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Check if a field should be obfuscated. |
| 97 | + * |
| 98 | + * Returns false for non-sensitive fields like authenticated, endpoint_url, etc. |
| 99 | + * |
| 100 | + * @since x.x.x |
| 101 | + * |
| 102 | + * @param string $field The field name to check. |
| 103 | + * @param string $profile_id The profile ID. |
| 104 | + * @return bool True if the field should be obfuscated. |
| 105 | + */ |
| 106 | + public static function should_obfuscate_field( string $field, string $profile_id ): bool { |
| 107 | + $sensitive_fields = ProviderProfiles::get_sensitive_fields( $profile_id ); |
| 108 | + return in_array( $field, $sensitive_fields, true ); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Obfuscate credential fields for a specific Provider. |
| 113 | + * |
| 114 | + * Uses ProviderProfiles to determine which fields are credentials. |
| 115 | + * |
| 116 | + * @since x.x.x |
| 117 | + * |
| 118 | + * @param string $provider_id The Provider ID (e.g., 'openai_chatgpt'). |
| 119 | + * @param array $settings The Provider settings array. |
| 120 | + * @return array The settings with credentials obfuscated. |
| 121 | + */ |
| 122 | + public static function obfuscate_provider_settings( string $provider_id, array $settings ): array { |
| 123 | + $profile_id = ProviderProfiles::get_profile_for_provider( $provider_id ); |
| 124 | + |
| 125 | + if ( ! $profile_id ) { |
| 126 | + return $settings; |
| 127 | + } |
| 128 | + |
| 129 | + $credential_fields = ProviderProfiles::get_credential_fields( $profile_id ); |
| 130 | + |
| 131 | + foreach ( $credential_fields as $field ) { |
| 132 | + if ( |
| 133 | + isset( $settings[ $field ] ) && |
| 134 | + is_string( $settings[ $field ] ) && |
| 135 | + self::should_obfuscate_field( $field, $profile_id ) |
| 136 | + ) { |
| 137 | + $settings[ $field ] = self::obfuscate( $settings[ $field ] ); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return $settings; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Obfuscate all Provider credentials in Feature settings. |
| 146 | + * |
| 147 | + * Iterates through all Provider settings in the Feature and obfuscates |
| 148 | + * their credential fields. |
| 149 | + * |
| 150 | + * @since x.x.x |
| 151 | + * |
| 152 | + * @param array $settings The complete Feature settings array. |
| 153 | + * @return array The settings with all Provider credentials obfuscated. |
| 154 | + */ |
| 155 | + public static function obfuscate_feature_settings( array $settings ): array { |
| 156 | + $profiles = ProviderProfiles::get_all_profiles(); |
| 157 | + |
| 158 | + // Get all Provider IDs from profiles. |
| 159 | + $all_provider_ids = []; |
| 160 | + foreach ( $profiles as $profile ) { |
| 161 | + $all_provider_ids = array_merge( $all_provider_ids, $profile['provider_ids'] ); |
| 162 | + } |
| 163 | + |
| 164 | + // Obfuscate credentials for each Provider that has settings. |
| 165 | + foreach ( $settings as $key => $value ) { |
| 166 | + if ( is_array( $value ) && in_array( $key, $all_provider_ids, true ) ) { |
| 167 | + $settings[ $key ] = self::obfuscate_provider_settings( $key, $value ); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + return $settings; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Merge new credentials with existing credentials, preserving originals when obfuscated. |
| 176 | + * |
| 177 | + * If a new value is obfuscated, use the existing value instead. |
| 178 | + * This prevents obfuscated placeholder values from being saved to the database. |
| 179 | + * |
| 180 | + * @since x.x.x |
| 181 | + * |
| 182 | + * @param array $new_settings The new settings being saved. |
| 183 | + * @param array $existing_settings The current saved settings. |
| 184 | + * @param string $provider_id The Provider ID. |
| 185 | + * @return array The merged settings. |
| 186 | + */ |
| 187 | + public static function merge_credentials( array $new_settings, array $existing_settings, string $provider_id ): array { |
| 188 | + $profile_id = ProviderProfiles::get_profile_for_provider( $provider_id ); |
| 189 | + |
| 190 | + if ( ! $profile_id ) { |
| 191 | + return $new_settings; |
| 192 | + } |
| 193 | + |
| 194 | + $credential_fields = ProviderProfiles::get_credential_fields( $profile_id ); |
| 195 | + |
| 196 | + foreach ( $credential_fields as $field ) { |
| 197 | + // Skip non-sensitive fields. |
| 198 | + if ( ! self::should_obfuscate_field( $field, $profile_id ) ) { |
| 199 | + continue; |
| 200 | + } |
| 201 | + |
| 202 | + // If the new value is obfuscated, preserve the existing value. |
| 203 | + if ( |
| 204 | + isset( $new_settings[ $field ] ) && |
| 205 | + is_string( $new_settings[ $field ] ) && |
| 206 | + self::is_obfuscated( $new_settings[ $field ] ) && |
| 207 | + isset( $existing_settings[ $field ] ) |
| 208 | + ) { |
| 209 | + $new_settings[ $field ] = $existing_settings[ $field ]; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + return $new_settings; |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Merge credentials for all Providers in Feature settings. |
| 218 | + * |
| 219 | + * Iterates through all Provider settings and preserves existing credentials |
| 220 | + * when obfuscated values are submitted. This ensures switching Providers |
| 221 | + * doesn't save obfuscated values for inactive Providers. |
| 222 | + * |
| 223 | + * @since x.x.x |
| 224 | + * |
| 225 | + * @param array $new_settings The new Feature settings being saved. |
| 226 | + * @param array $existing_settings The current saved Feature settings. |
| 227 | + * @return array The settings with all Provider credentials properly merged. |
| 228 | + */ |
| 229 | + public static function merge_all_provider_credentials( array $new_settings, array $existing_settings ): array { |
| 230 | + $profiles = ProviderProfiles::get_all_profiles(); |
| 231 | + |
| 232 | + // Get all Provider IDs from profiles. |
| 233 | + $all_provider_ids = []; |
| 234 | + foreach ( $profiles as $profile ) { |
| 235 | + $all_provider_ids = array_merge( $all_provider_ids, $profile['provider_ids'] ); |
| 236 | + } |
| 237 | + |
| 238 | + // Merge credentials for each Provider that has settings. |
| 239 | + foreach ( $new_settings as $key => $value ) { |
| 240 | + if ( is_array( $value ) && in_array( $key, $all_provider_ids, true ) ) { |
| 241 | + $new_settings[ $key ] = self::merge_credentials( |
| 242 | + $value, |
| 243 | + $existing_settings[ $key ] ?? [], |
| 244 | + $key |
| 245 | + ); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + return $new_settings; |
| 250 | + } |
| 251 | +} |
0 commit comments