Protéger l’export en Python
Enregistrez l’exemple sous report.py. Renseignez l’identifiant du produit et ses clés publiques de confiance depuis la console. Préparez deux licences de test du même produit. Seule la première accorde le droit export.
import os
import sys
from pathlib import Path
from licentivo import Client
PRODUCT_ID = "YOUR_PRODUCT_ID"
TRUSTED_KEYS = {"YOUR_KEY_ID": "YOUR_PUBLIC_KEY"}
def main(args):
if len(args) != 1:
print("Usage: LicenseReport <output.csv>", file=sys.stderr)
return 64
output = Path(args[0])
try:
with Client.open_product(
PRODUCT_ID, TRUSTED_KEYS, os.getenv("LICENTIVO_LICENSE_KEY", "")
) as client:
client.run_feature(
"export", lambda: output.write_text("item,total\nreports,1\n", encoding="utf-8")
)
except Exception as error:
print(error, file=sys.stderr)
return 2
print(output)
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))Créer l’exécutable Windows
Exécutez ces commandes PowerShell sur une machine Windows x64 équipée de Python 3.12. Le dossier dist contient l’unique exécutable à remettre au testeur.
py -3.12 -m venv .venv
.\.venv\Scripts\python.exe -m pip install "licentivo-sdk==0.11.8" "PyInstaller==6.22.3"
.\.venv\Scripts\python.exe -m PyInstaller --clean --noconfirm --onefile --name LicenseReport report.pyActiver l’application distribuée
Copiez uniquement LicenseReport.exe dans un dossier vide sur une machine de test Windows x64 sans Python. Exécutez les commandes dans ce dossier et saisissez la première clé dans l’invite masquée. L’export crée first.csv.
$key = Read-Host 'Test license key' -AsSecureString
try {
$env:LICENTIVO_LICENSE_KEY = [System.Net.NetworkCredential]::new('', $key).Password
.\LicenseReport.exe first.csv
$LASTEXITCODE
} finally {
Remove-Item Env:LICENTIVO_LICENSE_KEY -ErrorAction SilentlyContinue
$key.Dispose()
}
Test-Path -LiteralPath .\first.csvRedémarrer sous le même compte Windows
Relancez l’exécutable après avoir supprimé la variable contenant la clé. Un nouveau rapport confirme que le SDK a restauré les identifiants enregistrés pour ce compte Windows.
.\LicenseReport.exe restart.csv
$LASTEXITCODE
Get-Content -LiteralPath .\restart.csvTester une licence sans droit d’export
Passez à un nouveau profil Windows. Répétez l’activation avec la seconde clé et le nom de sortie denied.csv. Attendez un code de sortie 2 et aucun fichier. Choisissez un nom inédit pour chaque essai.
La compilation locale sous Windows x64 a utilisé CPython 3.12.10. Seul l’exécutable a été copié dans un dossier vide. Il a chargé Python depuis son propre dossier d’extraction et produit un rapport à l’activation et au redémarrage. Sans droit d’export, il a renvoyé 2 sans créer de fichier.
# In the fresh profile, supply the second key.
$key = Read-Host 'Test license key' -AsSecureString
try {
$env:LICENTIVO_LICENSE_KEY = [System.Net.NetworkCredential]::new('', $key).Password
.\LicenseReport.exe denied.csv
$LASTEXITCODE
} finally {
Remove-Item Env:LICENTIVO_LICENSE_KEY -ErrorAction SilentlyContinue
$key.Dispose()
}
Test-Path -LiteralPath .\denied.csv