forked from symfony/demo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTokenAuthenticator.php
87 lines (71 loc) · 2.5 KB
/
TokenAuthenticator.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace AppBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
/**
* Example authenticator that reads from a header
*/
class TokenAuthenticator extends AbstractGuardAuthenticator
{
public function getCredentials(Request $request)
{
$token = $request->headers->get('X-AUTH-TOKEN');
// no token? Don't do anything :D
if (!$token) {
return;
}
return [
'token' => $token,
];
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
$token = $credentials['token'];
// this authenticator is configured to always receive our UserProvider
/** @var UserProvider $userProvider */
$user = $userProvider->loadUserByToken($token);
// we could just return null, but this allows us to control the message a bit more
if (!$user) {
throw new AuthenticationCredentialsNotFoundException();
}
return $user;
}
public function checkCredentials($credentials, UserInterface $user)
{
// we're saying that the token is *always* ok
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
// on success, just let the request keep going!
return null;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
$data = array(
// you might translate this message
'message' => $exception->getMessageKey()
);
return new Response(json_encode($data), 403);
}
/**
* Called when authentication is needed, but it's not sent
*/
public function start(Request $request, AuthenticationException $authException = null)
{
$data = array(
// you might translate this message
'message' => 'Authentication Required'
);
return new Response(json_encode($data), 401);
}
public function supportsRememberMe()
{
return false;
}
}