1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\Repository; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Doctrine\ORM\EntityRepository; |
7
|
|
|
use Exception; |
8
|
|
|
use League\OAuth2\Server\Entities\AuthCodeEntityInterface; |
9
|
|
|
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface; |
10
|
|
|
use OAuth\AuthCode; |
11
|
|
|
use OAuth\Client; |
12
|
|
|
|
13
|
|
|
class AuthCodeRepository extends EntityRepository implements AuthCodeRepositoryInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @return AuthCode |
17
|
|
|
*/ |
18
|
1 |
|
public function getNewAuthCode() |
19
|
|
|
{ |
20
|
1 |
|
return new AuthCode(); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param AuthCodeEntityInterface $authCodeEntity |
25
|
|
|
* @throws \Doctrine\ORM\ORMException |
26
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
27
|
|
|
* @throws \Doctrine\ORM\TransactionRequiredException |
28
|
|
|
*/ |
29
|
1 |
|
public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity) |
30
|
|
|
{ |
31
|
1 |
|
$date = new DateTime(); |
32
|
1 |
|
$date->modify('+24 hours'); |
33
|
1 |
|
$authCodeEntity->setExpiryDateTime($date); |
34
|
|
|
/** @var Client $client */ |
35
|
1 |
|
$client = $this->_em->getRepository(Client::class) |
36
|
1 |
|
->findOneBy(['identifier' => $authCodeEntity->getClient()->getIdentifier()]); |
37
|
1 |
|
$authCodeEntity->setClient($client); |
38
|
1 |
|
$this->_em->persist($authCodeEntity); |
39
|
1 |
|
$this->_em->flush(); |
40
|
1 |
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $codeId |
45
|
|
|
* @throws Exception |
46
|
|
|
*/ |
47
|
1 |
|
public function revokeAuthCode($codeId) |
48
|
|
|
{ |
49
|
|
|
/** @var AuthCode $token */ |
50
|
1 |
|
$code = $this->findOneBy(['identifier' => $codeId]); |
51
|
1 |
|
if(!$code) { |
52
|
|
|
throw new Exception('Token not found', 404); |
53
|
|
|
} |
54
|
1 |
|
$code->setRevoked(true); |
55
|
1 |
|
$this->_em->flush($code); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $codeId |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
1 |
|
public function isAuthCodeRevoked($codeId) |
63
|
|
|
{ |
64
|
|
|
/** @var AuthCode $code */ |
65
|
1 |
|
$code = $this->findOneBy(['identifier' => $codeId]); |
66
|
1 |
|
if (!$code || $code->getExpiryDateTime() < new DateTime() || $code->isRevoked()) { |
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
1 |
|
return false; |
70
|
|
|
} |
71
|
|
|
} |