/
AccessTokenRepository.php
85 lines (74 loc) · 2.36 KB
/
AccessTokenRepository.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
<?php
namespace Concrete\Core\Entity\OAuth;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Entities\ScopeEntityInterface;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
class AccessTokenRepository extends EntityRepository implements AccessTokenRepositoryInterface
{
/**
* Create a new access token
*
* @param ClientEntityInterface $clientEntity
* @param ScopeEntityInterface[] $scopes
* @param mixed $userIdentifier
*
* @return AccessTokenEntityInterface
*/
public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null)
{
$token = new AccessToken();
$token->setUserIdentifier($userIdentifier);
return $token;
}
/**
* Persists a new access token to permanent storage.
*
* @param AccessTokenEntityInterface $accessTokenEntity
* @throws \Exception
*/
public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEntity)
{
$this->getEntityManager()->transactional(function(EntityManagerInterface $em) use ($accessTokenEntity) {
$em->persist($accessTokenEntity);
});
}
/**
* Revoke an access token.
*
* @param string $tokenId
* @throws \Exception
*/
public function revokeAccessToken($tokenId)
{
if ($token = $this->find($tokenId)) {
$this->getEntityManager()->transactional(function(EntityManagerInterface $em) use ($token) {
$em->remove($token);
});
}
}
/**
* Check if the access token has been revoked.
*
* @param string $tokenId
*
* @return bool Return true if this token has been revoked
*/
public function isAccessTokenRevoked($tokenId)
{
/** @var \Concrete\Core\Entity\OAuth\AccessToken $token */
$token = $this->find($tokenId);
if (!$token) {
// The token was manually removed.
return true;
}
$now = new \DateTime('now');
// If we have a token and it has expired...
if ($token && $token->getExpiryDateTime() < $now) {
return true;
}
return false;
}
}