|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Majora\Component\OAuth\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Majora\Component\OAuth\Model\AccessTokenInterface; |
|
6
|
|
|
use Majora\Component\OAuth\Model\AccountInterface; |
|
7
|
|
|
use Majora\Component\OAuth\Model\ApplicationInterface; |
|
8
|
|
|
use Majora\Component\OAuth\Model\RefreshTokenInterface; |
|
9
|
|
|
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Access token class. |
|
13
|
|
|
*/ |
|
14
|
|
|
class AccessToken implements AccessTokenInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $hash; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $expireIn; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var AccountInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $account; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ApplicationInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $application; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var RefreshTokenInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $refreshToken; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @see AccessTokenInterface::__construct() |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( |
|
45
|
|
|
ApplicationInterface $application, |
|
46
|
|
|
AccountInterface $account = null, |
|
47
|
|
|
$expireIn = AccessTokenInterface::DEFAULT_TTL, |
|
48
|
|
|
$hash = null, |
|
49
|
|
|
RefreshTokenInterface $refreshToken = null |
|
50
|
|
|
) { |
|
51
|
|
|
$this->application = $application; |
|
52
|
|
|
$this->account = $account; |
|
53
|
|
|
$this->expireIn = $expireIn; |
|
54
|
|
|
$this->refreshToken = $refreshToken; |
|
55
|
|
|
|
|
56
|
|
|
$this->hash = $hash ?: (new MessageDigestPasswordEncoder())->encodePassword( |
|
57
|
|
|
sprintf('[%s\o/%s]', $application->getSecret(), $account->getPassword() ?: time()), |
|
|
|
|
|
|
58
|
|
|
uniqid(mt_rand(), true) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @see AccessTokenInterface::getHash() |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getHash() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->hash; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @see AccessTokenInterface::getExpireIn() |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getExpireIn() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->expireIn; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @see AccessTokenInterface::getAccount() |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getAccount() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->account; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @see AccessTokenInterface::getApplication() |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getApplication() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->application; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @see AccessTokenInterface::getRoles() |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getRoles() |
|
98
|
|
|
{ |
|
99
|
|
|
return array_intersect($this->account->getRoles(), $this->application->getRoles()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @see AccessTokenInterface::getRefreshToken() |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getRefreshToken() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->refreshToken; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: