|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Service; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\User; |
|
6
|
|
|
use App\Exception\GoogleException; |
|
7
|
|
|
use App\Exception\GoogleRefreshTokenException; |
|
8
|
|
|
use DateTime; |
|
9
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
10
|
|
|
use Exception; |
|
11
|
|
|
use Google_Client; |
|
12
|
|
|
use Psr\Log\LoggerInterface; |
|
13
|
|
|
|
|
14
|
|
|
class GoogleOAuthService |
|
15
|
|
|
{ |
|
16
|
4 |
|
public function __construct( |
|
17
|
|
|
private Google_Client $googleClient, |
|
|
|
|
|
|
18
|
|
|
private EntityManagerInterface $em, |
|
|
|
|
|
|
19
|
|
|
private LoggerInterface $logger |
|
|
|
|
|
|
20
|
4 |
|
) {} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @throws GoogleException|GoogleRefreshTokenException |
|
24
|
|
|
*/ |
|
25
|
2 |
|
public function refreshAccessTokenIfExpired(User $user): string |
|
26
|
|
|
{ |
|
27
|
2 |
|
$this->googleClient->setAccessToken([ |
|
28
|
2 |
|
'access_token' => $user->getAccessToken(), |
|
29
|
2 |
|
'expires_in' => $user->getAccessTokenExpiresAt()->getTimestamp() - time(), |
|
30
|
2 |
|
'created' => $user->getAccessTokenCreatedAt()->getTimestamp() |
|
31
|
|
|
]); |
|
32
|
|
|
|
|
33
|
2 |
|
if (!$this->googleClient->isAccessTokenExpired()) { |
|
34
|
1 |
|
return $user->getAccessToken(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
try { |
|
38
|
1 |
|
$creds = $this->googleClient->fetchAccessTokenWithRefreshToken($user->getRefreshToken()); |
|
39
|
|
|
} catch (Exception $e) { |
|
40
|
|
|
throw new GoogleException('Could not refresh the token: ' . $e->getMessage()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
if (isset($creds['error'])) { |
|
44
|
|
|
throw new GoogleRefreshTokenException($creds['error_description']); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$user |
|
48
|
1 |
|
->setAccessToken($creds['access_token']) |
|
49
|
1 |
|
->setAccessTokenCreatedAt(new DateTime('@' . $creds['created'])) |
|
50
|
|
|
// ->setAccessTokenExpiresAt(new DateTime(sprintf('+%d seconds', $data['expires_in']))); |
|
51
|
|
|
// Driven by the unit tests: time() function is mockable, new DateTime objects are not |
|
52
|
1 |
|
->setAccessTokenExpiresAt(DateTime::createFromFormat('U', time() + $creds['expires_in']) |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
1 |
|
$this->em->flush(); |
|
56
|
|
|
|
|
57
|
1 |
|
return $creds['access_token']; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
public function revoke(User $user): void |
|
61
|
|
|
{ |
|
62
|
|
|
try { |
|
63
|
2 |
|
$this->googleClient->revokeToken($user->getAccessToken()); |
|
64
|
2 |
|
$this->googleClient->revokeToken($user->getRefreshToken()); |
|
65
|
1 |
|
} catch (Exception $e) { |
|
66
|
1 |
|
$this->logger->warning('Could not revoke at least one token: ' . $e->getMessage()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
$user->setRevokedAt(new DateTime()); |
|
70
|
2 |
|
$this->em->flush(); |
|
71
|
2 |
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.