|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Security; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
|
11
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
12
|
|
|
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException; |
|
13
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
14
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
15
|
|
|
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; |
|
16
|
|
|
|
|
17
|
|
|
class ComposerAuthenticator extends AbstractGuardAuthenticator |
|
18
|
|
|
{ |
|
19
|
|
|
private $passwordEncoder; |
|
20
|
|
|
|
|
21
|
|
|
private $manager; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(UserPasswordEncoderInterface $passwordEncoder, DocumentManager $manager) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->passwordEncoder = $passwordEncoder; |
|
26
|
|
|
$this->manager = $manager; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function start(Request $request, AuthenticationException $authException = null) |
|
30
|
|
|
{ |
|
31
|
|
|
$response = new Response(); |
|
32
|
|
|
$response->headers->set('WWW-Authenticate', 'Basic realm="Twity auth"'); |
|
33
|
|
|
$response->setStatusCode(401); |
|
34
|
|
|
|
|
35
|
|
|
return $response; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function supports(Request $request) |
|
39
|
|
|
{ |
|
40
|
|
|
return $request->headers->has('PHP_AUTH_USER') && $request->headers->has('PHP_AUTH_PW'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getCredentials(Request $request) |
|
44
|
|
|
{ |
|
45
|
|
|
return [ |
|
46
|
|
|
'username' => $request->headers->get('PHP_AUTH_USER'), |
|
47
|
|
|
'token' => $request->headers->get('PHP_AUTH_PW'), |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getUser($credentials, UserProviderInterface $userProvider) |
|
52
|
|
|
{ |
|
53
|
|
|
$user = $userProvider->loadUserByUsername($credentials['username']); |
|
54
|
|
|
|
|
55
|
|
|
if (!$user) { |
|
|
|
|
|
|
56
|
|
|
// fail authentication with a custom error |
|
57
|
|
|
throw new CustomUserMessageAuthenticationException('Username could not be found.'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $user; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function checkCredentials($credentials, UserInterface $user) |
|
64
|
|
|
{ |
|
65
|
|
|
return $user->getToken() === $credentials['token']; |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
|
69
|
|
|
{ |
|
70
|
|
|
return new JsonResponse(['message' => 'invalid login or token'], Response::HTTP_UNAUTHORIZED); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
|
74
|
|
|
{ |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function supportsRememberMe() |
|
78
|
|
|
{ |
|
79
|
|
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|