-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathSecurityTokenValueResolver.php
50 lines (42 loc) · 1.56 KB
/
SecurityTokenValueResolver.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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Http\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
/**
* @author Konstantin Myakshin <[email protected]>
*/
final class SecurityTokenValueResolver implements ValueResolverInterface
{
public function __construct(private readonly TokenStorageInterface $tokenStorage)
{
}
/**
* @return TokenInterface[]
*/
public function resolve(Request $request, ArgumentMetadata $argument): array
{
if (!($type = $argument->getType()) || (TokenInterface::class !== $type && !is_subclass_of($type, TokenInterface::class))) {
return [];
}
if (null !== $token = $this->tokenStorage->getToken()) {
return [$token];
}
if ($argument->isNullable()) {
return [];
}
throw new HttpException(Response::HTTP_UNAUTHORIZED, 'A security token is required but the token storage is empty.');
}
}