Skip to content

Commit 5a437c9

Browse files
author
Clément VALENTIN
committed
docs: ajouter la documentation sur le chiffrement des données utilisateurs
1 parent 7e0c0ed commit 5a437c9

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

docs/architecture/encryption.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
sidebar_position: 2
3+
title: Chiffrement des données
4+
description: Système de chiffrement GDPR-compliant pour protéger les données utilisateurs
5+
---
6+
7+
# Chiffrement des données utilisateurs
8+
9+
## Vue d'ensemble
10+
11+
MyElectricalData implémente un système de chiffrement **GDPR-compliant** pour protéger les données sensibles des utilisateurs (consommation, production, contrats). Chaque utilisateur possède une clé de chiffrement unique dérivée de son `client_secret`.
12+
13+
```text
14+
┌─────────────────────────────────────────────────────────────┐
15+
│ FLUX DE CHIFFREMENT │
16+
├─────────────────────────────────────────────────────────────┤
17+
│ │
18+
│ Utilisateur │
19+
│ │ │
20+
│ ▼ │
21+
│ Authentification (JWT ou client_secret) │
22+
│ │ │
23+
│ ▼ │
24+
│ Récupération du client_secret │
25+
│ │ │
26+
│ ▼ │
27+
│ ┌─────────────────────────────────────┐ │
28+
│ │ Dérivation de clé │ │
29+
│ │ │ │
30+
│ │ 1. SHA256(client_secret) │ │
31+
│ │ 2. Base64 URL-safe encode │ │
32+
│ │ 3. Création cipher Fernet │ │
33+
│ └─────────────────────────────────────┘ │
34+
│ │ │
35+
│ ▼ │
36+
│ ┌─────────────────────────────────────┐ │
37+
│ │ Valkey (données chiffrées) │ │
38+
│ │ │ │
39+
│ │ consumption:daily:{pdl}:{date} │ │
40+
│ │ production:daily:{pdl}:{date} │ │
41+
│ │ contract:{pdl} │ │
42+
│ └─────────────────────────────────────┘ │
43+
│ │
44+
└─────────────────────────────────────────────────────────────┘
45+
```
46+
47+
## Algorithme : Fernet
48+
49+
Le système utilise **Fernet** de la bibliothèque `cryptography` Python :
50+
51+
- **AES-128-CBC** pour le chiffrement
52+
- **HMAC-SHA256** pour l'authentification
53+
- **Timestamps** pour la validation temporelle
54+
55+
| Avantage | Description |
56+
| ----------------------- | --------------------------------------------------------------- |
57+
| **Sécurisé par défaut** | Pas de configuration complexe, résistant aux attaques courantes |
58+
| **Authentifié** | HMAC garantit l'intégrité des données |
59+
| **Simple** | API minimaliste, moins de risques d'erreur |
60+
| **Standard** | Utilisé largement dans l'écosystème Python |
61+
62+
## Implémentation
63+
64+
### Fichier principal
65+
66+
`apps/api/src/services/cache.py`
67+
68+
### Dérivation de la clé
69+
70+
```python
71+
def _get_cipher(self, encryption_key: str) -> Fernet:
72+
"""Get Fernet cipher with user's client_secret as key"""
73+
from base64 import urlsafe_b64encode
74+
from hashlib import sha256
75+
76+
key = urlsafe_b64encode(sha256(encryption_key.encode()).digest())
77+
return Fernet(key)
78+
```
79+
80+
### Chiffrement en cache
81+
82+
```python
83+
async def set(self, key: str, value: Any, encryption_key: str, ttl: int | None = None) -> bool:
84+
"""Store encrypted data in Valkey"""
85+
json_data = json.dumps(value)
86+
cipher = self._get_cipher(encryption_key)
87+
encrypted_data = cipher.encrypt(json_data.encode())
88+
await self.redis.setex(key, ttl or self.default_ttl, encrypted_data)
89+
return True
90+
91+
async def get(self, key: str, encryption_key: str) -> Any | None:
92+
"""Retrieve and decrypt data from Valkey"""
93+
encrypted_data = await self.redis.get(key)
94+
if not encrypted_data:
95+
return None
96+
cipher = self._get_cipher(encryption_key)
97+
decrypted_data = cipher.decrypt(encrypted_data)
98+
return json.loads(decrypted_data.decode())
99+
```
100+
101+
## Clés de cache
102+
103+
| Type de données | Clé de cache |
104+
| ------------------------ | --------------------------------------------- |
105+
| Consommation journalière | `consumption:daily:12345678901234:2024-01-15` |
106+
| Production journalière | `production:daily:12345678901234:2024-01-15` |
107+
| Contrat | `contract:12345678901234` |
108+
| Adresse | `address:12345678901234` |
109+
110+
## Propriétés de sécurité
111+
112+
| Propriété | Description |
113+
| ------------------------- | ---------------------------------------------------- |
114+
| **Isolation utilisateur** | Chaque cache est chiffré avec un secret unique |
115+
| **Confidentialité** | Impossible de déchiffrer sans le bon `client_secret` |
116+
| **Intégrité** | HMAC-SHA256 détecte toute modification |
117+
| **GDPR** | Données personnelles chiffrées au repos |
118+
| **TTL automatique** | Expiration après 24h (configurable) |
119+
120+
## Références
121+
122+
- [Fernet specification](https://github.com/fernet/spec/)
123+
- [Cryptography library](https://cryptography.io/)
124+
- Code source : `apps/api/src/services/cache.py`

0 commit comments

Comments
 (0)