-
Notifications
You must be signed in to change notification settings - Fork 692
Expand file tree
/
Copy pathApiToken53Authenticator.php
More file actions
68 lines (55 loc) · 2.27 KB
/
ApiToken53Authenticator.php
File metadata and controls
68 lines (55 loc) · 2.27 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Security;
use FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Entity\User;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
class ApiToken53Authenticator extends AbstractAuthenticator
{
protected $headerName = 'x-foo';
protected $tokenValue = 'FOOBAR';
public function authenticate(Request $request): PassportInterface
{
$credentials = $request->headers->get($this->headerName);
if (!$credentials || $credentials !== $this->tokenValue) {
throw new BadCredentialsException();
}
$userBadge = new UserBadge($this->tokenValue, function (): \FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Entity\User {
$user = new User();
$user->username = 'foo';
$user->roles[] = 'ROLE_API';
return $user;
});
return new SelfValidatingPassport($userBadge);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
return null;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
{
return new JsonResponse(null, 401);
}
public function supports(Request $request): ?bool
{
if (!$request->headers->has($this->headerName)) {
return false;
}
return true;
}
}