Laravel AT Protocol Client (alpha & unstable)
at dev 1.2 kB view raw
1<?php 2 3namespace SocialDept\AtpClient\Auth; 4 5use Illuminate\Support\Facades\Cache; 6 7class DPoPNonceManager 8{ 9 /** 10 * Get DPoP nonce for PDS endpoint 11 * 12 * Returns cached nonce if available, otherwise empty string. 13 * The first request will fail with use_dpop_nonce error, 14 * and the server will provide a valid nonce in the response. 15 */ 16 public function getNonce(string $pdsEndpoint): string 17 { 18 $cacheKey = 'dpop_nonce:'.md5($pdsEndpoint); 19 20 // Return cached nonce if available, empty string otherwise 21 // Empty nonce triggers use_dpop_nonce error, which is expected 22 return Cache::get($cacheKey, ''); 23 } 24 25 /** 26 * Store nonce returned from server response 27 */ 28 public function storeNonce(string $pdsEndpoint, string $nonce): void 29 { 30 $cacheKey = 'dpop_nonce:'.md5($pdsEndpoint); 31 Cache::put($cacheKey, $nonce, now()->addMinutes(5)); 32 } 33 34 /** 35 * Clear cached nonce (e.g., after nonce error) 36 */ 37 public function clearNonce(string $pdsEndpoint): void 38 { 39 $cacheKey = 'dpop_nonce:'.md5($pdsEndpoint); 40 Cache::forget($cacheKey); 41 } 42}