|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Explicit Architecture POC, |
|
7
|
|
|
* which is created on top of the Symfony Demo application. |
|
8
|
|
|
* |
|
9
|
|
|
* (c) Herberto Graça <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Acme\App\Infrastructure\Auth\Authentication\Oauth; |
|
16
|
|
|
|
|
17
|
|
|
use Acme\App\Core\Port\Persistence\DQL\DqlQueryBuilderInterface; |
|
18
|
|
|
use Acme\App\Core\Port\Persistence\Exception\EmptyQueryResultException; |
|
19
|
|
|
use Acme\App\Core\Port\Persistence\PersistenceServiceInterface; |
|
20
|
|
|
use Acme\App\Core\Port\Persistence\QueryServiceRouterInterface; |
|
21
|
|
|
use Acme\App\Core\SharedKernel\Component\User\Domain\User\UserId; |
|
22
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; |
|
23
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
|
24
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface; |
|
25
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
|
26
|
|
|
use function is_string; |
|
27
|
|
|
|
|
28
|
|
|
final class OauthAccessTokenRepository implements AccessTokenRepositoryInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var DqlQueryBuilderInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $dqlQueryBuilder; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var QueryServiceRouterInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $queryService; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var PersistenceServiceInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $persistenceService; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct( |
|
46
|
|
|
DqlQueryBuilderInterface $dqlQueryBuilder, |
|
47
|
|
|
QueryServiceRouterInterface $queryService, |
|
48
|
|
|
PersistenceServiceInterface $persistenceService |
|
49
|
|
|
) { |
|
50
|
|
|
$this->dqlQueryBuilder = $dqlQueryBuilder; |
|
51
|
|
|
$this->queryService = $queryService; |
|
52
|
|
|
$this->persistenceService = $persistenceService; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param ScopeEntityInterface[] $scopes |
|
57
|
|
|
* @param UserId $userIdentifier |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getNewToken( |
|
60
|
|
|
ClientEntityInterface $clientEntity, |
|
61
|
|
|
array $scopes, |
|
62
|
|
|
$userIdentifier = null |
|
63
|
|
|
): AccessTokenEntityInterface { |
|
64
|
|
|
return new OauthAccessToken( |
|
65
|
|
|
is_string($userIdentifier) ? new UserId($userIdentifier) : $userIdentifier, $clientEntity, $scopes |
|
|
|
|
|
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function persistNewAccessToken(AccessTokenEntityInterface $oauthAccessToken): void |
|
70
|
|
|
{ |
|
71
|
|
|
$this->persistenceService->upsert($oauthAccessToken); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $tokenId |
|
76
|
|
|
*/ |
|
77
|
|
|
public function revokeAccessToken($tokenId): void |
|
78
|
|
|
{ |
|
79
|
|
|
try { |
|
80
|
|
|
$oauthAccessToken = $this->find($tokenId); |
|
81
|
|
|
} catch (EmptyQueryResultException $e) { |
|
82
|
|
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$oauthAccessToken->revoke(); |
|
86
|
|
|
$this->persistenceService->upsert($oauthAccessToken); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string $tokenId |
|
91
|
|
|
*/ |
|
92
|
|
|
public function isAccessTokenRevoked($tokenId): ?bool |
|
93
|
|
|
{ |
|
94
|
|
|
try { |
|
95
|
|
|
return $this->find($tokenId)->isRevoked(); |
|
96
|
|
|
} catch (EmptyQueryResultException $e) { |
|
97
|
|
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function find(string $tokenId): OauthAccessToken |
|
102
|
|
|
{ |
|
103
|
|
|
$dqlQuery = $this->dqlQueryBuilder->create(OauthAccessToken::class) |
|
104
|
|
|
->where('OauthAccessToken.identifier = :identifier') |
|
105
|
|
|
->setParameter('identifier', $tokenId) |
|
106
|
|
|
->build(); |
|
107
|
|
|
|
|
108
|
|
|
return $this->queryService->query($dqlQuery)->getSingleResult(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: