1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Majora\Component\OAuth\Entity; |
4
|
|
|
|
5
|
|
|
use Majora\Component\OAuth\Model\RefreshTokenInterface; |
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
|
|
|
* Class RefreshToken is the default implementation of RefreshTokenInterface |
12
|
|
|
* |
13
|
|
|
* @author Raphael De Freitas <[email protected]> |
14
|
|
|
*/ |
15
|
|
View Code Duplication |
class RefreshToken implements RefreshTokenInterface |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $hash; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
protected $expireIn; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var AccountInterface |
29
|
|
|
*/ |
30
|
|
|
protected $account; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ApplicationInterface |
34
|
|
|
*/ |
35
|
|
|
protected $application; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @see RefreshTokenInterface::__construct() |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
ApplicationInterface $application, |
42
|
|
|
AccountInterface $account = null, |
43
|
|
|
$expireIn = RefreshTokenInterface::DEFAULT_TTL, |
44
|
|
|
$hash = null |
45
|
|
|
) { |
46
|
|
|
$this->application = $application; |
47
|
|
|
$this->account = $account; |
48
|
|
|
$this->expireIn = $expireIn; |
49
|
|
|
|
50
|
|
|
$this->hash = $hash ?: (new MessageDigestPasswordEncoder())->encodePassword( |
51
|
|
|
sprintf('[%s\o/%s]', $application->getSecret(), $account->getPassword() ?: time()), |
|
|
|
|
52
|
|
|
uniqid(mt_rand(), true) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @see RefreshTokenInterface::getHash() |
58
|
|
|
*/ |
59
|
|
|
public function getHash() |
60
|
|
|
{ |
61
|
|
|
return $this->hash; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @see RefreshTokenInterface::getExpireIn() |
66
|
|
|
*/ |
67
|
|
|
public function getExpireIn() |
68
|
|
|
{ |
69
|
|
|
return $this->expireIn; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @see RefreshTokenInterface::getAccount() |
74
|
|
|
*/ |
75
|
|
|
public function getAccount() |
76
|
|
|
{ |
77
|
|
|
return $this->account; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @see RefreshTokenInterface::getApplication() |
82
|
|
|
*/ |
83
|
|
|
public function getApplication() |
84
|
|
|
{ |
85
|
|
|
return $this->application; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @see RefreshTokenInterface::getRoles() |
90
|
|
|
*/ |
91
|
|
|
public function getRoles() |
92
|
|
|
{ |
93
|
|
|
return array_intersect($this->account->getRoles(), $this->application->getRoles()); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.