33 lines
853 B
PHP
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);
|
|
}
|
|
}
|