1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Lookyman\NetteOAuth2Server\Storage\Doctrine\AccessToken; |
5
|
|
|
|
6
|
|
|
use Doctrine\ORM\OptimisticLockException; |
7
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
8
|
|
|
use Kdyby\Doctrine\InvalidStateException; |
9
|
|
|
use Kdyby\Doctrine\QueryException; |
10
|
|
|
use Kdyby\Doctrine\Registry; |
11
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; |
12
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
13
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface; |
14
|
|
|
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; |
15
|
|
|
|
16
|
|
|
class AccessTokenRepository implements AccessTokenRepositoryInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Registry |
20
|
|
|
*/ |
21
|
|
|
private $registry; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Registry $registry |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Registry $registry) |
27
|
|
|
{ |
28
|
|
|
$this->registry = $registry; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param ClientEntityInterface $clientEntity |
33
|
|
|
* @param ScopeEntityInterface[] $scopes |
34
|
|
|
* @param string|null $userIdentifier |
35
|
|
|
* @return AccessTokenEntity |
36
|
|
|
*/ |
37
|
|
|
public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null) |
38
|
|
|
{ |
39
|
|
|
$accessToken = new AccessTokenEntity(); |
40
|
|
|
$accessToken->setClient($clientEntity); |
41
|
|
|
foreach ($scopes as $scope) { |
42
|
|
|
$accessToken->addScope($scope); |
43
|
|
|
} |
44
|
|
|
$accessToken->setUserIdentifier($userIdentifier); |
45
|
|
|
return $accessToken; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param AccessTokenEntityInterface $accessTokenEntity |
50
|
|
|
* @throws ORMInvalidArgumentException |
51
|
|
|
* @throws OptimisticLockException |
52
|
|
|
*/ |
53
|
|
|
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity) |
54
|
|
|
{ |
55
|
|
|
if ($accessTokenEntity instanceof AccessTokenEntity) { |
56
|
|
|
$manager = $this->registry->getManager(); |
57
|
|
|
$manager->persist($accessTokenEntity); |
58
|
|
|
$manager->flush(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $tokenId |
64
|
|
|
* @throws InvalidStateException |
65
|
|
|
* @throws QueryException |
66
|
|
|
* @throws OptimisticLockException |
67
|
|
|
*/ |
68
|
|
|
public function revokeAccessToken($tokenId) |
69
|
|
|
{ |
70
|
|
|
$manager = $this->registry->getManager(); |
71
|
|
|
/** @var AccessTokenEntity|null $accessTokenEntity */ |
72
|
|
|
if ($accessTokenEntity = $manager->getRepository(AccessTokenEntity::class)->fetchOne($this->createQuery()->byIdentifier($tokenId))) { |
73
|
|
|
$accessTokenEntity->setRevoked(true); |
74
|
|
|
$manager->flush(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $tokenId |
80
|
|
|
* @return bool |
81
|
|
|
* @throws InvalidStateException |
82
|
|
|
* @throws QueryException |
83
|
|
|
*/ |
84
|
|
|
public function isAccessTokenRevoked($tokenId) |
85
|
|
|
{ |
86
|
|
|
/** @var AccessTokenEntity|null $accessTokenEntity */ |
87
|
|
|
$accessTokenEntity = $this->registry->getManager()->getRepository(AccessTokenEntity::class)->fetchOne($this->createQuery()->byIdentifier($tokenId)); |
88
|
|
|
return $accessTokenEntity ? $accessTokenEntity->isRevoked() : true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return AccessTokenQuery |
93
|
|
|
*/ |
94
|
|
|
protected function createQuery(): AccessTokenQuery |
95
|
|
|
{ |
96
|
|
|
return new AccessTokenQuery(); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|