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