// ============================================ // GIT PUSHER - LICENSE VALIDATION (RSA) // Version 2.1 - 100% Client-Side Validation // ============================================ // Configuration const LICENSE_CONFIG = { storageKey: 'git_pusher_license', usageKey: 'git_pusher_usage', version: '2.1.0' }; // ============================================ // CLÉ PUBLIQUE RSA // ============================================ // Cette clé est générée par le vendeur avec license_generator_rsa.py // Commande: python3 license_generator_rsa.py export-key const PUBLIC_KEY_PEM = `-----BEGIN PUBLIC KEY----- MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA7H8v243PKpy4ZcGUI3YX EiAZaK+VwWSDywCwBOMOJBA5slWorP78cME0bIphrNRvTlA9xpuo0a+8V+VFMb3+ Uw9AhDtuJKRIEgwJixm/mkKcbjwqSHPmnyBBHPBBX7lO/q2wsEX0y3O/NujByqc3 dVsB0VVhMFJmgyR3dVy6ZQITgEu/NGs9v/jUc5IT1YzVmOCcL8BZrjlGiF0AXeS3 /U8khq7wEx5OilhXC7i8w6urd9c4Djjg583WsGtDKk0aZ6xvnfYpmgfTzaFIrUkS afTxbcZ1h0N3lN9MBvaLbgAui5RgdlbJlbGsgl3uAa9R9xZk+rqTh8VBLVq+KW5I a6aYOVterUf2hz/hUkNjM8Rolv4/3PQX0mGu6fa4fwoxmjlSUEVxVFh7TdCE/WHj 3kAOybZXWJnws/++urqijP5SmYxyCaVlYAoutdWmz1tTrSXOh74qrou2wv3C8Dmo 8ccVznAhdhHcVs7MSl9Qbyw1fsi1117WigUGkPE5Cxjlrl8EcBQg3G5x91ER95JM O0SjyhDborT+oMq9947ZL35VllzkKbBELbhDnogXmDMrI3Ij1UBmCtSOZzOLhyHD FmGf5AB1LWbxcgrzOMcTLoAHduaDalZCzmW4WdV4313CqeawEfqJVj8BJ+0VEFdb RDk4ZzHpOaGAuCJjN3AuxO8CAwEAAQ== -----END PUBLIC KEY-----`; // ============================================ // UTILITAIRES CRYPTO // ============================================ /** * Convertir une chaîne PEM en ArrayBuffer */ function pemToArrayBuffer(pem) { const b64 = pem .replace(/-----BEGIN PUBLIC KEY-----/, '') .replace(/-----END PUBLIC KEY-----/, '') .replace(/\s/g, ''); const binary = atob(b64); const bytes = new Uint8Array(binary.length); for (let i = 0; i < binary.length; i++) { bytes[i] = binary.charCodeAt(i); } return bytes.buffer; } /** * Importer la clé publique RSA */ async function importPublicKey() { try { const keyData = pemToArrayBuffer(PUBLIC_KEY_PEM); const key = await crypto.subtle.importKey( 'spki', keyData, { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' }, false, ['verify'] ); return key; } catch (error) { console.error('Erreur import clé publique:', error); return null; } } /** * Vérifier la signature RSA PKCS#1 v1.5 */ async function verifySignature(data, signatureB64, publicKey) { try { const signature = Uint8Array.from(atob(signatureB64), c => c.charCodeAt(0)); const dataBuffer = new TextEncoder().encode(data); const isValid = await crypto.subtle.verify( 'RSASSA-PKCS1-v1_5', publicKey, signature, dataBuffer ); return isValid; } catch (error) { console.error('Erreur vérification signature:', error); return false; } } // ============================================ // FONCTIONS UTILITAIRES // ============================================ /** * Décoder Base64 */ function base64Decode(str) { try { return decodeURIComponent(atob(str).split('').map(function(c) { return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); }).join('')); } catch (e) { return atob(str); } } /** * Obtenir le hostname actuel (depuis l'URL) */ function getCurrentHostname() { return window.location.hostname.toLowerCase(); } /** * Obtenir le vrai hostname Splunk via l'API REST */ async function getSplunkHostname() { try { // Méthode 1: Via server/info const response = await fetch('/en-US/splunkd/__raw/services/server/info?output_mode=json', { credentials: 'include' }); if (response.ok) { const data = await response.json(); const serverName = data.entry?.[0]?.content?.serverName; if (serverName) { console.log('Hostname Splunk (server/info):', serverName); return serverName.toLowerCase(); } } } catch (e) { console.log('Méthode server/info échouée:', e); } try { // Méthode 2: Via server/settings const response2 = await fetch('/en-US/splunkd/__raw/services/server/settings?output_mode=json', { credentials: 'include' }); if (response2.ok) { const data2 = await response2.json(); const serverName = data2.entry?.[0]?.content?.serverName; if (serverName) { console.log('Hostname Splunk (server/settings):', serverName); return serverName.toLowerCase(); } } } catch (e) { console.log('Méthode server/settings échouée:', e); } // Fallback: hostname de l'URL (pas idéal) console.warn('Impossible de récupérer le hostname Splunk, utilisation URL:', getCurrentHostname()); return getCurrentHostname(); } /** * Calculer les jours restants */ function daysRemaining(expiryDate) { const expiry = new Date(expiryDate); const now = new Date(); const diff = expiry - now; return Math.ceil(diff / (1000 * 60 * 60 * 24)); } // ============================================ // PARSING DE LICENCE // ============================================ /** * Parser le contenu d'un fichier .lic */ function parseLicenseFile(content) { try { const lines = content.trim().split('\n'); let payloadB64 = null; for (const line of lines) { const trimmed = line.trim(); if (trimmed && !trimmed.startsWith('#')) { payloadB64 = trimmed; break; } } if (!payloadB64) { return { error: 'Payload non trouvé dans le fichier' }; } // Décoder le payload const payloadJson = base64Decode(payloadB64); const payload = JSON.parse(payloadJson); if (!payload.license || !payload.signature) { return { error: 'Format de licence invalide' }; } // Décoder les données de licence const licenseJson = base64Decode(payload.license); const licenseData = JSON.parse(licenseJson); return { success: true, licenseJson: licenseJson, licenseData: licenseData, signatureB64: payload.signature, rawPayload: payloadB64 }; } catch (error) { console.error('Erreur parsing licence:', error); return { error: 'Erreur de lecture du fichier de licence' }; } } // ============================================ // VALIDATION DE LICENCE // ============================================ /** * Valider une licence complète (signature RSA + hostname + expiration) */ async function validateLicense(licenseContent = null) { try { // Si pas de contenu fourni, charger depuis le localStorage let parsed; if (licenseContent) { parsed = parseLicenseFile(licenseContent); } else { const stored = localStorage.getItem(LICENSE_CONFIG.storageKey); if (!stored) { return { valid: false, error: 'Aucune licence installée', error_code: 'NO_LICENSE' }; } parsed = JSON.parse(stored); } if (parsed.error) { return { valid: false, error: parsed.error, error_code: 'PARSE_ERROR' }; } const { licenseJson, licenseData, signatureB64 } = parsed; // DEBUG: Afficher les données console.log('=== DEBUG VALIDATION LICENCE ==='); console.log('License JSON:', licenseJson); console.log('License JSON length:', licenseJson.length); console.log('Signature B64:', signatureB64.substring(0, 50) + '...'); console.log('Signature B64 length:', signatureB64.length); // 1. Vérifier la signature RSA console.log('Vérification de la signature RSA...'); const publicKey = await importPublicKey(); if (!publicKey) { console.error('Échec import clé publique'); return { valid: false, error: 'Impossible de charger la clé publique', error_code: 'KEY_ERROR' }; } console.log('Clé publique importée avec succès'); const signatureValid = await verifySignature(licenseJson, signatureB64, publicKey); console.log('Résultat vérification signature:', signatureValid); if (!signatureValid) { return { valid: false, error: 'Signature de licence invalide', error_code: 'INVALID_SIGNATURE' }; } console.log('✓ Signature RSA valide'); // 2. Vérifier le hostname const expectedHostname = (licenseData.hostname || '').toLowerCase(); const currentHostname = await getSplunkHostname(); console.log(`Hostname attendu: "${expectedHostname}", actuel: "${currentHostname}"`); // Permettre une correspondance partielle ou exacte if (expectedHostname && expectedHostname !== currentHostname) { // Vérifier si c'est une correspondance partielle (le hostname peut être un FQDN) if (!currentHostname.includes(expectedHostname) && !expectedHostname.includes(currentHostname)) { return { valid: false, error: `Licence non valide pour ce serveur. Attendu: ${expectedHostname}, Actuel: ${currentHostname}`, error_code: 'HOSTNAME_MISMATCH', expected_hostname: expectedHostname, current_hostname: currentHostname }; } console.log('✓ Hostname valide (correspondance partielle)'); } else { console.log('✓ Hostname valide (exact)'); } // 3. Vérifier la date d'expiration const expiryDate = licenseData.expires; if (expiryDate) { const days = daysRemaining(expiryDate); if (days < 0) { return { valid: false, error: `Licence expirée le ${expiryDate}`, error_code: 'LICENSE_EXPIRED', expires: expiryDate }; } console.log(`✓ Licence valide (${days} jours restants)`); } // Licence valide ! return { valid: true, license_id: licenseData.license_id, type: licenseData.type, type_name: licenseData.type_name, customer: licenseData.customer, hostname: expectedHostname, issued: licenseData.issued, expires: expiryDate, days_remaining: daysRemaining(expiryDate), limits: licenseData.limits || {}, features: licenseData.features || [] }; } catch (error) { console.error('Erreur validation licence:', error); return { valid: false, error: error.message, error_code: 'VALIDATION_ERROR' }; } } /** * Vérifier si une fonctionnalité est disponible */ async function hasFeature(featureName) { const validation = await validateLicense(); if (!validation.valid) return false; return validation.features.includes(featureName); } // ============================================ // GESTION DU STOCKAGE // ============================================ /** * Sauvegarder une licence validée */ async function saveLicense(licenseContent) { try { // Parser le fichier const parsed = parseLicenseFile(licenseContent); if (parsed.error) { return { success: false, error: parsed.error }; } // Valider la licence avant de sauvegarder const validation = await validateLicense(licenseContent); if (!validation.valid) { return { success: false, error: validation.error, error_code: validation.error_code }; } // Sauvegarder dans localStorage localStorage.setItem(LICENSE_CONFIG.storageKey, JSON.stringify(parsed)); console.log('✓ Licence sauvegardée avec succès'); return { success: true, license: validation }; } catch (error) { console.error('Erreur sauvegarde licence:', error); return { success: false, error: error.message }; } } /** * Supprimer la licence */ function removeLicense() { localStorage.removeItem(LICENSE_CONFIG.storageKey); localStorage.removeItem(LICENSE_CONFIG.usageKey); console.log('Licence supprimée'); } /** * Récupérer les infos de licence (sans revalider la signature) */ function getLicenseInfo() { try { const stored = localStorage.getItem(LICENSE_CONFIG.storageKey); if (!stored) return null; const parsed = JSON.parse(stored); return parsed.licenseData; } catch { return null; } } // ============================================ // GESTION DES LIMITES D'UTILISATION // ============================================ /** * Obtenir les stats d'utilisation */ function getUsageStats() { try { const stored = localStorage.getItem(LICENSE_CONFIG.usageKey); if (stored) { return JSON.parse(stored); } } catch {} return { total_pushes: 0, pushes_today: 0, last_push_date: null }; } /** * Incrémenter le compteur d'utilisation */ function incrementUsage() { const stats = getUsageStats(); const today = new Date().toISOString().split('T')[0]; // Reset si nouveau jour if (stats.last_push_date !== today) { stats.pushes_today = 0; stats.last_push_date = today; } stats.total_pushes = (stats.total_pushes || 0) + 1; stats.pushes_today = (stats.pushes_today || 0) + 1; localStorage.setItem(LICENSE_CONFIG.usageKey, JSON.stringify(stats)); return stats; } /** * Vérifier les limites avant un push */ async function checkLimits() { const validation = await validateLicense(); if (!validation.valid) { return { allowed: false, error: validation.error, error_code: validation.error_code }; } const limits = validation.limits || {}; const maxPushes = limits.max_pushes_per_day || -1; if (maxPushes > 0) { const stats = getUsageStats(); const today = new Date().toISOString().split('T')[0]; // Reset si nouveau jour let pushesToday = stats.pushes_today || 0; if (stats.last_push_date !== today) { pushesToday = 0; } if (pushesToday >= maxPushes) { return { allowed: false, error: `Limite quotidienne atteinte (${maxPushes} pushes/jour)`, error_code: 'DAILY_LIMIT_REACHED' }; } } return { allowed: true, license_type: validation.type_name, remaining_today: maxPushes > 0 ? maxPushes - getUsageStats().pushes_today : -1 }; } // ============================================ // INTERFACE UTILISATEUR // ============================================ /** * Afficher le badge de licence */ async function updateLicenseBadge() { const container = document.getElementById('license-badge-container'); if (!container) return; const validation = await validateLicense(); let badgeHtml = ''; if (validation.valid) { const daysLeft = validation.days_remaining; let badgeClass = 'license-badge-valid'; let icon = '✓'; if (daysLeft <= 7) { badgeClass = 'license-badge-expiring'; icon = '⚠️'; } else if (daysLeft <= 30) { badgeClass = 'license-badge-warning'; icon = '⏳'; } badgeHtml = `
Chargement...
| ID | ${validation.license_id} |
| Type | ${validation.type_name} |
| Client | ${validation.customer?.name || 'N/A'} |
| ${validation.customer?.email || 'N/A'} | |
| Hostname | ${validation.hostname} |
| Émise le | ${validation.issued} |
| Expire le | ${validation.expires} |
| Jours restants | ${validation.days_remaining} |
| Apps max | ${maxApps} |
| Pushes/jour | ${maxPushes} |