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\Exception\UnsupportedUserException; |
24
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
25
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
26
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
27
|
|
|
use Tenside\Core\Config\SourceInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* This class validates jwt. |
31
|
|
|
*/ |
32
|
|
|
class UserProviderFromConfig implements UserProviderInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* The config source to use. |
36
|
|
|
* |
37
|
|
|
* @var SourceInterface |
38
|
|
|
*/ |
39
|
|
|
private $configSource; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create a new instance. |
43
|
|
|
* |
44
|
|
|
* @param SourceInterface $config The config source to read the user data from. |
45
|
|
|
*/ |
46
|
|
|
public function __construct(SourceInterface $config) |
47
|
|
|
{ |
48
|
|
|
$this->configSource = $config; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
* |
54
|
|
|
* @throws UsernameNotFoundException When the user is not contained. |
55
|
|
|
*/ |
56
|
|
|
public function loadUserByUsername($username) |
57
|
|
|
{ |
58
|
|
|
if (!($username && $this->configSource->has('auth-password/' . $username))) { |
59
|
|
|
throw new UsernameNotFoundException(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$userData = $this->configSource->get('auth-password/' . $username); |
63
|
|
|
|
64
|
|
|
return new UserInformation(array_merge($userData, ['username' => $username])); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritDoc} |
69
|
|
|
* |
70
|
|
|
* @throws UnsupportedUserException For invalid user types. |
71
|
|
|
*/ |
72
|
|
|
public function refreshUser(UserInterface $user) |
73
|
|
|
{ |
74
|
|
|
if (!$user instanceof UserInformation) { |
75
|
|
|
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->loadUserByUsername($user->getUsername()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritDoc} |
83
|
|
|
*/ |
84
|
|
|
public function supportsClass($class) |
85
|
|
|
{ |
86
|
|
|
return $class === UserInformation::class; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add/update the passed credentials in the database. |
91
|
|
|
* |
92
|
|
|
* @param UserInformation $user The user to add. |
93
|
|
|
* |
94
|
|
|
* @return UserProviderFromConfig |
95
|
|
|
*/ |
96
|
|
|
public function addUser($user) |
97
|
|
|
{ |
98
|
|
|
$this->configSource->set('auth-password/' . $user->getUsername(), $user->values()); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Add the passed credentials in the database. |
105
|
|
|
* |
106
|
|
|
* @param string|UserInformation $user The username to remove. |
107
|
|
|
* |
108
|
|
|
* @return UserProviderFromConfig |
109
|
|
|
*/ |
110
|
|
|
public function removeUser($user) |
111
|
|
|
{ |
112
|
|
|
$username = ($user instanceof UserInformation) ? $user->getUsername() : (string) $user; |
113
|
|
|
|
114
|
|
|
$this->configSource->set('auth-password/' . $username, null); |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|