|
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, $this->auth0Data) = unserialize($serialized); |
|
123
|
|
|
if ($user) { |
|
124
|
|
|
$this->setUser($user); |
|
125
|
|
|
} |
|
126
|
|
|
$this->setAuthenticated($isAuthenticated); |
|
127
|
|
|
$this->setAttributes($attributes); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getRoles() |
|
131
|
|
|
{ |
|
132
|
|
|
$allRoles = array_merge(parent::getRoles(), $this->storedRoles); |
|
133
|
|
|
$uniqueRoles = []; |
|
134
|
|
|
|
|
135
|
|
|
/** @var Role $role */ |
|
136
|
|
|
foreach ($allRoles as $role) { |
|
137
|
|
|
$uniqueRoles[$role->getRole()] = $role; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return array_values($uniqueRoles); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|