-
Notifications
You must be signed in to change notification settings - Fork 4
/
StorageUserProvider.php
54 lines (43 loc) · 1.37 KB
/
StorageUserProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
declare(strict_types=1);
namespace Damax\Bundle\ApiAuthBundle\Security\ApiKey;
use Damax\Bundle\ApiAuthBundle\Key\Storage\KeyNotFound;
use Damax\Bundle\ApiAuthBundle\Key\Storage\Reader as Storage;
use Damax\Bundle\ApiAuthBundle\Security\ApiUser;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;
class StorageUserProvider implements ApiKeyUserProvider
{
private $storage;
public function __construct(Storage $storage)
{
$this->storage = $storage;
}
public function supportsClass($class): bool
{
return ApiUser::class === $class;
}
public function loadUserByUsername($username): UserInterface
{
return new ApiUser($username);
}
public function loadUserByApiKey(string $apiKey): UserInterface
{
try {
$key = $this->storage->get($apiKey);
} catch (KeyNotFound $e) {
throw new InvalidApiKey();
}
if ($key->expired()) {
throw new InvalidApiKey();
}
return $this->loadUserByUsername($key->identity());
}
/**
* @throws UnsupportedUserException
*/
public function refreshUser(UserInterface $user): UserInterface
{
throw new UnsupportedUserException(sprintf('Provider "%s" must be configured as stateless.', __CLASS__));
}
}