1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of tenside/core. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Schiffler <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* This project is provided in good faith and hope to be usable by anyone. |
12
|
|
|
* |
13
|
|
|
* @package tenside/core |
14
|
|
|
* @author Christian Schiffler <[email protected]> |
15
|
|
|
* @copyright 2015 Christian Schiffler <[email protected]> |
16
|
|
|
* @license https://github.com/tenside/core/blob/master/LICENSE MIT |
17
|
|
|
* @link https://github.com/tenside/core |
18
|
|
|
* @filesource |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Tenside\CoreBundle\Security; |
22
|
|
|
|
23
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken; |
24
|
|
|
use Symfony\Component\Security\Core\Role\RoleInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This class is a simple implementation of a Javascript Web Token. |
28
|
|
|
*/ |
29
|
|
|
class JavascriptWebToken extends AbstractToken |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* The token. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $token; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The provider key. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $providerKey; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor. |
47
|
|
|
* |
48
|
|
|
* @param string $token The user credentials |
49
|
|
|
* |
50
|
|
|
* @param string $providerKey The provider key |
51
|
|
|
* |
52
|
|
|
* @param string|object $user The user |
53
|
|
|
* |
54
|
|
|
* @param RoleInterface[]|string[] $roles An array of roles |
55
|
|
|
* |
56
|
|
|
* @throws \InvalidArgumentException When the provider key is empty. |
57
|
|
|
*/ |
58
|
|
|
public function __construct($token, $providerKey, $user = 'anon.', array $roles = []) |
59
|
|
|
{ |
60
|
|
|
parent::__construct($roles); |
61
|
|
|
|
62
|
|
|
if (empty($providerKey)) { |
63
|
|
|
throw new \InvalidArgumentException('$providerKey must not be empty.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->setUser($user); |
67
|
|
|
$this->token = $token; |
68
|
|
|
$this->providerKey = $providerKey; |
69
|
|
|
|
70
|
|
|
if ($roles) { |
|
|
|
|
71
|
|
|
$this->setAuthenticated(true); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the provider key. |
77
|
|
|
* |
78
|
|
|
* @return string The provider key |
79
|
|
|
*/ |
80
|
|
|
public function getProviderKey() |
81
|
|
|
{ |
82
|
|
|
return $this->providerKey; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function getCredentials() |
89
|
|
|
{ |
90
|
|
|
return $this->token; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function eraseCredentials() |
97
|
|
|
{ |
98
|
|
|
parent::eraseCredentials(); |
99
|
|
|
|
100
|
|
|
$this->token = null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function serialize() |
107
|
|
|
{ |
108
|
|
|
return serialize([$this->token, $this->providerKey, parent::serialize()]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
|
|
public function unserialize($str) |
115
|
|
|
{ |
116
|
|
|
list($this->token, $this->providerKey, $parentStr) = unserialize($str); |
117
|
|
|
parent::unserialize($parentStr); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.