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\TokenInterface; |
9
|
|
|
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Abstract token class. |
13
|
|
|
*/ |
14
|
|
|
abstract class Token implements TokenInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
protected $id; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $hash; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $expireIn; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AccountInterface |
33
|
|
|
*/ |
34
|
|
|
protected $account; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ApplicationInterface |
38
|
|
|
*/ |
39
|
|
|
protected $application; |
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
|
|
|
) { |
50
|
|
|
$this->application = $application; |
51
|
|
|
$this->account = $account; |
52
|
|
|
$this->expireIn = $expireIn; |
53
|
|
|
|
54
|
|
|
$this->hash = $hash ?: (new MessageDigestPasswordEncoder())->encodePassword( |
55
|
|
|
sprintf('[%s\o/%s]', $application->getSecret(), $account->getPassword() ?: time()), |
|
|
|
|
56
|
|
|
uniqid(mt_rand(), true) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getId() |
61
|
|
|
{ |
62
|
|
|
return $this->id; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @see AccessTokenInterface::getHash() |
67
|
|
|
*/ |
68
|
|
|
public function getHash() |
69
|
|
|
{ |
70
|
|
|
return $this->hash; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @see AccessTokenInterface::getExpireIn() |
75
|
|
|
*/ |
76
|
|
|
public function getExpireIn() |
77
|
|
|
{ |
78
|
|
|
return $this->expireIn; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @see AccessTokenInterface::getAccount() |
83
|
|
|
*/ |
84
|
|
|
public function getAccount() |
85
|
|
|
{ |
86
|
|
|
return $this->account; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @see AccessTokenInterface::getApplication() |
91
|
|
|
*/ |
92
|
|
|
public function getApplication() |
93
|
|
|
{ |
94
|
|
|
return $this->application; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @see AccessTokenInterface::getRoles() |
99
|
|
|
*/ |
100
|
|
|
public function getRoles() |
101
|
|
|
{ |
102
|
|
|
return array_intersect($this->account->getRoles(), $this->application->getRoles()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: