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
|
|
|
* @author Raphael De Freitas <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractToken implements TokenInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $hash; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $expireIn; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var AccountInterface |
30
|
|
|
*/ |
31
|
|
|
protected $account; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ApplicationInterface |
35
|
|
|
*/ |
36
|
|
|
protected $application; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @see AccessTokenInterface::__construct() |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
ApplicationInterface $application, |
43
|
|
|
AccountInterface $account = null, |
44
|
|
|
$expireIn = AccessTokenInterface::DEFAULT_TTL, |
45
|
|
|
$hash = null |
46
|
|
|
) { |
47
|
|
|
$this->application = $application; |
48
|
|
|
$this->account = $account; |
49
|
|
|
$this->expireIn = $expireIn; |
50
|
|
|
|
51
|
|
|
$this->hash = $hash ?: (new MessageDigestPasswordEncoder())->encodePassword( |
52
|
|
|
sprintf('[%s\o/%s]', $application->getSecret(), $account->getPassword() ?: time()), |
|
|
|
|
53
|
|
|
uniqid(mt_rand(), true) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @see AccessTokenInterface::getHash() |
59
|
|
|
*/ |
60
|
|
|
public function getHash() |
61
|
|
|
{ |
62
|
|
|
return $this->hash; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @see AccessTokenInterface::getExpireIn() |
67
|
|
|
*/ |
68
|
|
|
public function getExpireIn() |
69
|
|
|
{ |
70
|
|
|
return $this->expireIn; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @see AccessTokenInterface::getAccount() |
75
|
|
|
*/ |
76
|
|
|
public function getAccount() |
77
|
|
|
{ |
78
|
|
|
return $this->account; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @see AccessTokenInterface::getApplication() |
83
|
|
|
*/ |
84
|
|
|
public function getApplication() |
85
|
|
|
{ |
86
|
|
|
return $this->application; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @see AccessTokenInterface::getRoles() |
91
|
|
|
*/ |
92
|
|
|
public function getRoles() |
93
|
|
|
{ |
94
|
|
|
return array_intersect($this->account->getRoles(), $this->application->getRoles()); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: