1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\Auth0Bundle\Security\Authentication\Token; |
4
|
|
|
|
5
|
|
|
use Happyr\Auth0Bundle\Model\Authentication\UserProfile\UserInfo; |
6
|
|
|
use Happyr\Auth0Bundle\Model\Authorization\Token\Token; |
7
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken; |
8
|
|
|
use Symfony\Component\Security\Core\Role\Role; |
9
|
|
|
|
10
|
|
|
class SSOToken extends AbstractToken |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Token|null |
14
|
|
|
*/ |
15
|
|
|
private $auth0Data; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $storedRoles = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The user model for the API. |
24
|
|
|
* |
25
|
|
|
* @var mixed |
26
|
|
|
*/ |
27
|
|
|
private $userModel; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Token $data |
31
|
|
|
* |
32
|
|
|
* @return SSOToken |
33
|
|
|
*/ |
34
|
|
|
public function setAuth0Data(Token $data) |
35
|
|
|
{ |
36
|
|
|
$this->auth0Data = $data; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return Token|null |
43
|
|
|
*/ |
44
|
|
|
public function getAuth0Data() |
45
|
|
|
{ |
46
|
|
|
return $this->auth0Data; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function getAccessToken() |
53
|
|
|
{ |
54
|
|
|
if (null === $this->auth0Data) { |
55
|
|
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->auth0Data->getAccessToken(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function getExpiresAt() |
65
|
|
|
{ |
66
|
|
|
if (null === $this->auth0Data) { |
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->auth0Data->getExpiresAt(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
public function getUserModel() |
77
|
|
|
{ |
78
|
|
|
return $this->userModel; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param UserInfo $userModel |
83
|
|
|
* |
84
|
|
|
* @return SSOToken |
85
|
|
|
*/ |
86
|
|
|
public function setUserModel(UserInfo $userModel) |
87
|
|
|
{ |
88
|
|
|
$this->userModel = $userModel; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getCredentials() |
94
|
|
|
{ |
95
|
|
|
return ''; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function serialize() |
102
|
|
|
{ |
103
|
|
|
$user = $this->getUser(); |
104
|
|
|
|
105
|
|
|
return serialize( |
106
|
|
|
[ |
107
|
|
|
is_object($user) ? clone $user : $user, |
108
|
|
|
is_object($this->userModel) ? clone $this->userModel : $this->userModel, |
109
|
|
|
$this->isAuthenticated(), |
110
|
|
|
$this->getRoles(), |
111
|
|
|
$this->getAttributes(), |
112
|
|
|
$this->auth0Data, |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function unserialize($serialized) |
121
|
|
|
{ |
122
|
|
|
list($user, $this->userModel, $isAuthenticated, $this->storedRoles, $attributes, $auth0Data) = unserialize($serialized); |
123
|
|
|
if ($user) { |
124
|
|
|
$this->setUser($user); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($auth0Data instanceof Token) { |
128
|
|
|
$this->setAuth0Data($auth0Data); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->setAuthenticated($isAuthenticated); |
132
|
|
|
$this->setAttributes($attributes); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getRoleNames(): array |
136
|
|
|
{ |
137
|
|
|
$allRoles = array_merge(parent::getRoleNames(), $this->storedRoles); |
|
|
|
|
138
|
|
|
$uniqueRoles = []; |
139
|
|
|
|
140
|
|
|
/** @var Role $role */ |
141
|
|
|
foreach ($allRoles as $role) { |
142
|
|
|
$name = is_string($role) ? $role : $role->getRole(); |
143
|
|
|
$uniqueRoles[$name] = true; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return array_keys($uniqueRoles); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* This function is deprecated by Symfony 4.3. |
152
|
|
|
*/ |
153
|
|
|
public function getRoles() |
154
|
|
|
{ |
155
|
|
|
$allRoles = array_merge(parent::getRoles(), $this->storedRoles); |
156
|
|
|
$uniqueRoles = []; |
157
|
|
|
|
158
|
|
|
/** @var Role $role */ |
159
|
|
|
foreach ($allRoles as $role) { |
160
|
|
|
$uniqueRoles[$role->getRole()] = $role; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return array_values($uniqueRoles); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: