@anonympins/fingerprint 0.3.7 → 0.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,9 @@ declare(strict_types=1);
4
4
 
5
5
  namespace Anonympins\Fingerprint\Challenge;
6
6
 
7
+ use Anonympins\Fingerprint\Store\StoreManager;
7
8
  use Anonympins\Fingerprint\Utils\BigInt;
9
+ use Anonympins\Fingerprint\Utils\RequestUtils;
8
10
 
9
11
  /**
10
12
  * Classe utilitaire pour la génération et la vérification des challenges Proof-of-Work.
@@ -34,11 +36,52 @@ class ChallengeUtils
34
36
  }
35
37
 
36
38
  /**
37
- * Vérifie si un ticket de passage est valide.
39
+ * Vérifie si un ticket de passage est valide (supporte les tickets opaques via store et le fallback legacy).
38
40
  */
39
- public static function isTicketValid(?string $ip, ?string $ticket): bool
40
- {
41
- if (empty($ip) || empty($ticket) || !str_contains($ticket, ':')) {
41
+ public static function isTicketValid(
42
+ ?string $ip,
43
+ ?string $ticket,
44
+ string $deviceId = '',
45
+ string $deviceHash = '',
46
+ bool $allowCrossNetworkRoaming = false
47
+ ): bool {
48
+ if (empty($ip) || empty($ticket)) {
49
+ return false;
50
+ }
51
+
52
+ $store = StoreManager::getStore();
53
+ $ticketData = $store->get("ticket:{$ticket}");
54
+
55
+ if ($ticketData !== null) {
56
+ $expiry = $ticketData['expiry'] ?? null;
57
+ $originalIp = $ticketData['originalIp'] ?? null;
58
+ $storedDeviceId = $ticketData['deviceId'] ?? '';
59
+ $storedDeviceHash = $ticketData['deviceHash'] ?? '';
60
+
61
+ if (!$expiry || (int)floor(microtime(true) * 1000) > (int)$expiry) {
62
+ $store->delete("ticket:{$ticket}");
63
+ return false;
64
+ }
65
+
66
+ if ($ip === $originalIp) {
67
+ return true;
68
+ }
69
+
70
+ $currentSubnet = RequestUtils::getIpSubnet($ip);
71
+ $originalSubnet = RequestUtils::getIpSubnet($originalIp);
72
+ if ($currentSubnet !== null && $originalSubnet !== null && $currentSubnet === $originalSubnet) {
73
+ return true;
74
+ }
75
+
76
+ if (!$allowCrossNetworkRoaming) {
77
+ return false;
78
+ }
79
+
80
+ return !empty($deviceId) && $deviceId === $storedDeviceId && !empty($deviceHash) && $deviceHash === $storedDeviceHash;
81
+ }
82
+
83
+ // Fallback rétrocompatible pour les anciens tickets signés (sans état)
84
+ if (!str_contains($ticket, ':')) {
42
85
  return false;
43
86
  }
44
87
 
@@ -87,14 +130,16 @@ class ChallengeUtils
87
130
 
88
131
  /**
89
132
  * Vérifie une solution de PoW CPU et génère un ticket si elle est valide.
90
- * @return string|null Le ticket en cas de succès, sinon null.
133
+ * @return string|null Le ticket opaque en cas de succès, sinon null.
91
134
  */
92
135
  public static function verifyCpuTargetPoWAndGenerateTicket(
93
136
  string $clientIp,
94
137
  int $ticketTtl,
95
138
  string $nonce,
96
139
  string $solution,
97
- array $challengeContext
140
+ array $challengeContext,
141
+ string $deviceId = '',
142
+ string $deviceHash = ''
98
143
  ): ?string {
99
144
  $cpuTargetHex = $challengeContext['cpuTarget'] ?? null;
100
145
  $baseBlock = $challengeContext['baseBlock'] ?? null;
@@ -114,9 +159,20 @@ class ChallengeUtils
114
159
 
115
160
  if ($isValid) {
116
161
  error_log('[FP Server Verify] CPU PoW verification PASSED.');
162
+
163
+ // Génération d'un jeton opaque et unique
164
+ $ticketId = bin2hex(random_bytes(16));
117
165
  $expiry = (int)floor(microtime(true) * 1000) + $ticketTtl;
118
- $signature = hash_hmac('sha256', "{$clientIp}:{$expiry}", self::getPowSecret());
119
- return "{$expiry}:{$signature}";
166
+
167
+ $store = StoreManager::getStore();
168
+ $store->set("ticket:{$ticketId}", [
169
+ 'expiry' => $expiry,
170
+ 'originalIp' => $clientIp,
171
+ 'deviceId' => $deviceId,
172
+ 'deviceHash' => $deviceHash
173
+ ], (int)ceil($ticketTtl / 1000));
174
+
175
+ return $ticketId;
120
176
  }
121
177
 
122
178
  // Log details on failure