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
|
|
|
public function setAuth0Data(Token $data) |
30
|
|
|
{ |
31
|
|
|
$this->auth0Data = $data; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getAuth0Data(): ?Token |
35
|
|
|
{ |
36
|
|
|
return $this->auth0Data; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getAccessToken(): ?string |
40
|
|
|
{ |
41
|
|
|
if (null === $this->auth0Data) { |
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->auth0Data->getAccessToken(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getExpiresAt(): ?\DateTimeInterface |
49
|
|
|
{ |
50
|
|
|
if (null === $this->auth0Data) { |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->auth0Data->getExpiresAt(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getUserModel(): ?UserInfo |
58
|
|
|
{ |
59
|
|
|
return $this->userModel; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setUserModel(UserInfo $userModel) |
63
|
|
|
{ |
64
|
|
|
$this->userModel = $userModel; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getCredentials() |
68
|
|
|
{ |
69
|
|
|
return ''; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function __serialize(): array |
76
|
|
|
{ |
77
|
|
|
$user = $this->getUser(); |
78
|
|
|
|
79
|
|
|
return [ |
80
|
|
|
is_object($user) ? clone $user : $user, |
81
|
|
|
is_object($this->userModel) ? clone $this->userModel : $this->userModel, |
82
|
|
|
$this->isAuthenticated(), |
83
|
|
|
$this->getRoles(), |
|
|
|
|
84
|
|
|
$this->getAttributes(), |
85
|
|
|
$this->auth0Data, |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function __unserialize(array $data): void |
93
|
|
|
{ |
94
|
|
|
[$user, $this->userModel, $isAuthenticated, $this->storedRoles, $attributes, $auth0Data] = $data; |
|
|
|
|
95
|
|
|
if ($user) { |
96
|
|
|
$this->setUser($user); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($auth0Data instanceof Token) { |
100
|
|
|
$this->setAuth0Data($auth0Data); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->setAuthenticated($isAuthenticated); |
104
|
|
|
$this->setAttributes($attributes); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getRoleNames(): array |
108
|
|
|
{ |
109
|
|
|
$allRoles = array_merge(parent::getRoleNames(), $this->storedRoles); |
110
|
|
|
$uniqueRoles = []; |
111
|
|
|
|
112
|
|
|
/** @var Role $role */ |
113
|
|
|
foreach ($allRoles as $role) { |
114
|
|
|
$name = is_string($role) ? $role : $role->getRole(); |
115
|
|
|
$uniqueRoles[$name] = true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return array_keys($uniqueRoles); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* This function is deprecated by Symfony 4.3. |
123
|
|
|
* |
124
|
|
|
* @deprecated |
125
|
|
|
*/ |
126
|
|
|
public function getRoles() |
127
|
|
|
{ |
128
|
|
|
// To avoid any Symfony deprecation notices created by Symfony |
129
|
|
|
if (0 === \func_num_args()) { |
130
|
|
|
$parentRoles = parent::getRoles(); |
|
|
|
|
131
|
|
|
} else { |
132
|
|
|
$parentRoles = parent::getRoles(func_get_arg(0)); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$allRoles = array_merge($parentRoles, $this->storedRoles); |
136
|
|
|
$uniqueRoles = []; |
137
|
|
|
|
138
|
|
|
/** @var Role $role */ |
139
|
|
|
foreach ($allRoles as $role) { |
140
|
|
|
$uniqueRoles[$role->getRole()] = $role; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return array_values($uniqueRoles); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This method has been deprecated.