-
Notifications
You must be signed in to change notification settings - Fork 114
/
AuthCodeRepository.php
111 lines (89 loc) · 3 KB
/
AuthCodeRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
namespace Trikoder\Bundle\OAuth2Bundle\League\Repository;
use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException;
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
use Trikoder\Bundle\OAuth2Bundle\Converter\ScopeConverter;
use Trikoder\Bundle\OAuth2Bundle\League\Entity\AuthCode;
use Trikoder\Bundle\OAuth2Bundle\Manager\AuthorizationCodeManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Manager\ClientManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Model\AuthorizationCode;
final class AuthCodeRepository implements AuthCodeRepositoryInterface
{
/**
* @var AuthorizationCodeManagerInterface
*/
private $authorizationCodeManager;
/**
* @var ClientManagerInterface
*/
private $clientManager;
/**
* @var ScopeConverter
*/
private $scopeConverter;
public function __construct(
AuthorizationCodeManagerInterface $authorizationCodeManager,
ClientManagerInterface $clientManager,
ScopeConverter $scopeConverter
) {
$this->authorizationCodeManager = $authorizationCodeManager;
$this->clientManager = $clientManager;
$this->scopeConverter = $scopeConverter;
}
/**
* {@inheritdoc}
*/
public function getNewAuthCode()
{
return new AuthCode();
}
/**
* {@inheritdoc}
*/
public function persistNewAuthCode(AuthCodeEntityInterface $authCode)
{
$authorizationCode = $this->authorizationCodeManager->find($authCode->getIdentifier());
if (null !== $authorizationCode) {
throw UniqueTokenIdentifierConstraintViolationException::create();
}
$authorizationCode = $this->buildAuthorizationCode($authCode);
$this->authorizationCodeManager->save($authorizationCode);
}
/**
* {@inheritdoc}
*/
public function revokeAuthCode($codeId)
{
$authorizationCode = $this->authorizationCodeManager->find($codeId);
if (null === $codeId) {
return;
}
$authorizationCode->revoke();
$this->authorizationCodeManager->save($authorizationCode);
}
/**
* {@inheritdoc}
*/
public function isAuthCodeRevoked($codeId)
{
$authorizationCode = $this->authorizationCodeManager->find($codeId);
if (null === $authorizationCode) {
return true;
}
return $authorizationCode->isRevoked();
}
private function buildAuthorizationCode(AuthCode $authCode): AuthorizationCode
{
$client = $this->clientManager->find($authCode->getClient()->getIdentifier());
$authorizationCode = new AuthorizationCode(
$authCode->getIdentifier(),
$authCode->getExpiryDateTime(),
$client,
$authCode->getUserIdentifier(),
$this->scopeConverter->toDomainArray($authCode->getScopes())
);
return $authorizationCode;
}
}