1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
6
|
|
|
use Doctrine\ORM\UnitOfWork; |
7
|
|
|
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface; |
8
|
|
|
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; |
9
|
|
|
use OAuth\AccessToken; |
10
|
|
|
use OAuth\RefreshToken; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class RefreshTokenRepository |
14
|
|
|
* @package OAuth\Repository |
15
|
|
|
*/ |
16
|
|
|
class RefreshTokenRepository extends EntityRepository implements RefreshTokenRepositoryInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @return RefreshToken |
20
|
|
|
*/ |
21
|
|
|
public function getNewRefreshToken() |
22
|
|
|
{ |
23
|
|
|
return new RefreshToken(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param RefreshTokenEntityInterface $refreshTokenEntity |
28
|
|
|
* @return RefreshTokenEntityInterface |
29
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
30
|
|
|
*/ |
31
|
|
|
public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntity) |
32
|
|
|
{ |
33
|
|
|
$accessToken = $refreshTokenEntity->getAccessToken(); |
34
|
|
|
if ($this->_em->getUnitOfWork()->getEntityState($accessToken) !== UnitOfWork::STATE_MANAGED) { |
35
|
|
|
/** @var AccessToken $accessToken */ |
36
|
|
|
$accessToken = $this->_em->merge($accessToken); |
37
|
|
|
$refreshTokenEntity->setAccessToken($accessToken); |
38
|
|
|
} |
39
|
|
|
$this->_em->persist($refreshTokenEntity); |
40
|
|
|
$this->_em->flush(); |
41
|
|
|
|
42
|
|
|
return $refreshTokenEntity; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $tokenId |
47
|
|
|
* @return bool |
48
|
|
|
* @throws \Doctrine\ORM\ORMException |
49
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
50
|
|
|
* @throws \Doctrine\ORM\TransactionRequiredException |
51
|
|
|
*/ |
52
|
|
|
public function revokeRefreshToken($tokenId) |
53
|
|
|
{ |
54
|
|
|
$token = $this->_em->find(RefreshToken::class, $tokenId); |
55
|
|
|
if ($token instanceof RefreshTokenEntityInterface) { |
|
|
|
|
56
|
|
|
$this->_em->remove($token); |
57
|
|
|
$this->_em->flush(); |
58
|
|
|
|
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $tokenId |
67
|
|
|
* @return bool |
68
|
|
|
* @throws \Doctrine\ORM\ORMException |
69
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
70
|
|
|
* @throws \Doctrine\ORM\TransactionRequiredException |
71
|
|
|
*/ |
72
|
|
|
public function isRefreshTokenRevoked($tokenId) |
73
|
|
|
{ |
74
|
|
|
$token = $this->_em->find(RefreshToken::class, $tokenId); |
75
|
|
|
if ($token instanceof RefreshTokenEntityInterface) { |
|
|
|
|
76
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
} |