80 lines
2.2 KiB
PHP
80 lines
2.2 KiB
PHP
|
<?php
|
||
|
|
||
|
/*
|
||
|
* This file is part of the Silex framework.
|
||
|
*
|
||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||
|
*
|
||
|
* This source file is subject to the MIT license that is bundled
|
||
|
* with this source code in the file LICENSE.
|
||
|
*/
|
||
|
|
||
|
namespace Silex\Tests\Provider\SecurityServiceProviderTest;
|
||
|
|
||
|
use Symfony\Component\HttpFoundation\Request;
|
||
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||
|
use Symfony\Component\Security\Core\User\UserInterface;
|
||
|
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
||
|
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
|
||
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||
|
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
||
|
|
||
|
/**
|
||
|
* This class is used to test "guard" authentication with the SecurityServiceProvider.
|
||
|
*/
|
||
|
class TokenAuthenticator extends AbstractGuardAuthenticator
|
||
|
{
|
||
|
public function getCredentials(Request $request)
|
||
|
{
|
||
|
if (!$token = $request->headers->get('X-AUTH-TOKEN')) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
list($username, $secret) = explode(':', $token);
|
||
|
|
||
|
return array(
|
||
|
'username' => $username,
|
||
|
'secret' => $secret,
|
||
|
);
|
||
|
}
|
||
|
|
||
|
public function getUser($credentials, UserProviderInterface $userProvider)
|
||
|
{
|
||
|
return $userProvider->loadUserByUsername($credentials['username']);
|
||
|
}
|
||
|
|
||
|
public function checkCredentials($credentials, UserInterface $user)
|
||
|
{
|
||
|
// This is not a safe way of validating a password.
|
||
|
return $user->getPassword() === $credentials['secret'];
|
||
|
}
|
||
|
|
||
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
|
||
|
{
|
||
|
$data = array(
|
||
|
'message' => strtr($exception->getMessageKey(), $exception->getMessageData()),
|
||
|
);
|
||
|
|
||
|
return new JsonResponse($data, 403);
|
||
|
}
|
||
|
|
||
|
public function start(Request $request, AuthenticationException $authException = null)
|
||
|
{
|
||
|
$data = array(
|
||
|
'message' => 'Authentication Required',
|
||
|
);
|
||
|
|
||
|
return new JsonResponse($data, 401);
|
||
|
}
|
||
|
|
||
|
public function supportsRememberMe()
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
}
|