feat: google login
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user