Files
daily-meal-api/app/Services/SocialAuthenticationNonce.php
T
leonm ddc80956a0
CI / 🧪 Tests Laravel (push) Failing after 9s
CI / 🐳 Build & Push Images (push) Has been skipped
feat: google login
2026-08-17 10:06:35 +02:00

33 lines
853 B
PHP

<?php
namespace App\Services;
use App\Enums\SocialProvider;
use Illuminate\Support\Facades\Cache;
class SocialAuthenticationNonce
{
public function issue(SocialProvider $provider): string
{
$nonce = bin2hex(random_bytes(32));
Cache::put($this->cacheKey($provider, $nonce), true, now()->addMinutes(10));
return $nonce;
}
public function consume(SocialProvider $provider, string $nonce, mixed $tokenNonce): bool
{
$nonceWasIssued = Cache::pull($this->cacheKey($provider, $nonce), false);
return is_string($tokenNonce)
&& hash_equals($nonce, $tokenNonce)
&& $nonceWasIssued === true;
}
private function cacheKey(SocialProvider $provider, string $nonce): string
{
return 'social-auth:'.$provider->value.':nonce:'.hash('sha256', $nonce);
}
}