Lines 0.00% 0 / 7
Functions and Methods 0.00% 0 / 2
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
GoogleIdTokenVerifier 0.00% 0 / 7 0.00% 0 / 2 20 0.00% 0 / 1
 __construct 0.00% 0 / 1 0.00% 0 / 1 2
 verify 0.00% 0 / 6 0.00% 0 / 1 12
1<?php
2
3declare(strict_types=1);
4
5namespace App\Security;
6
7use Google\Client;
8use Psr\Cache\CacheItemPoolInterface;
9use Symfony\Component\DependencyInjection\Attribute\AsAlias;
10use Symfony\Component\DependencyInjection\Attribute\Autowire;
11
12#[AsAlias(GoogleIdTokenVerifierInterface::class)]
13final readonly class GoogleIdTokenVerifier implements GoogleIdTokenVerifierInterface
14{
15    public function __construct(
16        #[Autowire(env: 'OAUTH_GOOGLE_ID')] private string $oauthGoogleId,
17        // Google's signing certificates, else every API request downloads them again
18        #[Autowire(service: 'cache.app')] private CacheItemPoolInterface $cache,
19    ) {
20    }
21
22    #[\Override]
23    public function verify(string $idToken): ?array
24    {
25        try {
26            $client = new Client(['client_id' => $this->oauthGoogleId]);
27            $client->setCache($this->cache);
28            $payload = $client->verifyIdToken($idToken);
29        } catch (\UnexpectedValueException|\LogicException|\OutOfBoundsException) {
30            return null;
31        }
32
33        return \is_array($payload) ? $payload : null;
34    }
35}