1
0
Fork 0
mirror of synced 2025-04-05 06:03:34 +03:00

fix FrontApiClientAuthenticator, add phpdoc

This commit is contained in:
Akolzin Dmitry 2021-02-05 13:28:28 +03:00
parent b6c6dfac2d
commit 6fb4a5d6e1
21 changed files with 213 additions and 2 deletions

2
.gitignore vendored
View file

@ -40,6 +40,7 @@ composer.lock
/app/phpunit.xml
/phpunit.xml
.phpunit.result.cache
test-report.xml
# Build data
/build/
@ -54,3 +55,4 @@ composer.lock
/.web-server-pid
.idea
coverage.xml

View file

@ -5,10 +5,20 @@ namespace RetailCrm\ServiceBundle\ArgumentResolver;
use RetailCrm\ServiceBundle\Exceptions\InvalidRequestArgumentException;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Class AbstractValueResolver
*
* @package RetailCrm\ServiceBundle\ArgumentResolver
*/
abstract class AbstractValueResolver
{
protected $validator;
/**
* AbstractValueResolver constructor.
*
* @param ValidatorInterface $validator
*/
public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
@ -16,6 +26,8 @@ abstract class AbstractValueResolver
/**
* @param object $data
*
* @return void
*/
protected function validate(object $data): void
{

View file

@ -9,11 +9,23 @@ use Symfony\Component\Serializer\SerializerInterface;
use Generator;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* Class CallbackValueResolver
*
* @package RetailCrm\ServiceBundle\ArgumentResolver
*/
class CallbackValueResolver extends AbstractValueResolver implements ArgumentValueResolverInterface
{
private $serializer;
private $requestSchema;
/**
* CallbackValueResolver constructor.
*
* @param SerializerInterface $serializer
* @param ValidatorInterface $validator
* @param array $requestSchema
*/
public function __construct(
SerializerInterface $serializer,
ValidatorInterface $validator,

View file

@ -11,12 +11,25 @@ use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Generator;
/**
* Class ClientValueResolver
*
* @package RetailCrm\ServiceBundle\ArgumentResolver
*/
class ClientValueResolver extends AbstractValueResolver implements ArgumentValueResolverInterface
{
private $serializer;
private $denormalizer;
private $requestSchema;
/**
* ClientValueResolver constructor.
*
* @param ValidatorInterface $validator
* @param SerializerInterface $serializer
* @param DenormalizerInterface $denormalizer
* @param array $requestSchema
*/
public function __construct(
ValidatorInterface $validator,
SerializerInterface $serializer,

View file

@ -5,6 +5,11 @@ namespace RetailCrm\ServiceBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* Class Configuration
*
* @package RetailCrm\ServiceBundle\DependencyInjection
*/
class Configuration implements ConfigurationInterface
{
/**

View file

@ -10,6 +10,11 @@ use RetailCrm\ServiceBundle\Security\FrontApiClientAuthenticator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
/**
* Class RetailCrmServiceExtension
*
* @package RetailCrm\ServiceBundle\DependencyInjection
*/
class RetailCrmServiceExtension extends Extension
{
/**

View file

@ -28,6 +28,9 @@ class InvalidRequestArgumentException extends InvalidArgumentException
$this->validateErrors = $errors;
}
/**
* @return iterable
*/
public function getValidateErrors(): iterable
{
return $this->validateErrors;

View file

@ -2,6 +2,11 @@
namespace RetailCrm\ServiceBundle\Models;
/**
* Class Error
*
* @package RetailCrm\ServiceBundle\Models
*/
class Error
{
/**

0
Resources/doc/index.md Normal file
View file

View file

@ -7,15 +7,32 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\SerializerInterface;
/**
* Class ErrorJsonResponseFactory
*
* @package RetailCrm\ServiceBundle\Response
*/
class ErrorJsonResponseFactory
{
private $serializer;
/**
* ErrorJsonResponseFactory constructor.
*
* @param SerializerInterface $serializer
*/
public function __construct(SerializerInterface $serializer)
{
$this->serializer = $serializer;
}
/**
* @param Error $error
* @param int $statusCode
* @param array $headers
*
* @return Response
*/
public function create(Error $error, int $statusCode = Response::HTTP_BAD_REQUEST, array $headers = []): Response
{
return JsonResponse::fromJsonString(

View file

@ -12,17 +12,30 @@ use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
/**
* Class AbstractClientAuthenticator
*
* @package RetailCrm\ServiceBundle\Security
*/
abstract class AbstractClientAuthenticator extends AbstractGuardAuthenticator
{
public const AUTH_FIELD = 'clientId';
private $errorResponseFactory;
/**
* AbstractClientAuthenticator constructor.
*
* @param ErrorJsonResponseFactory $errorResponseFactory
*/
public function __construct(ErrorJsonResponseFactory $errorResponseFactory)
{
$this->errorResponseFactory = $errorResponseFactory;
}
/**
* {@inheritdoc }
*/
public function start(Request $request, AuthenticationException $authException = null): Response
{
$error = new Error();
@ -31,21 +44,33 @@ abstract class AbstractClientAuthenticator extends AbstractGuardAuthenticator
return $this->errorResponseFactory->create($error,Response::HTTP_UNAUTHORIZED);
}
/**
* {@inheritdoc }
*/
public function getCredentials(Request $request): string
{
return $request->get(static::AUTH_FIELD);
}
/**
* {@inheritdoc }
*/
public function getUser($credentials, UserProviderInterface $userProvider): ?UserInterface
{
return $userProvider->loadUserByUsername($credentials);
}
/**
* {@inheritdoc }
*/
public function checkCredentials($credentials, UserInterface $user): bool
{
return true;
}
/**
* {@inheritdoc }
*/
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
{
$error = new Error();
@ -54,6 +79,9 @@ abstract class AbstractClientAuthenticator extends AbstractGuardAuthenticator
return $this->errorResponseFactory->create($error,Response::HTTP_FORBIDDEN);
}
/**
* {@inheritdoc }
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey): ?Response
{
return null;

View file

@ -4,13 +4,24 @@ namespace RetailCrm\ServiceBundle\Security;
use Symfony\Component\HttpFoundation\Request;
/**
* Class CallbackClientAuthenticator
*
* @package RetailCrm\ServiceBundle\Security
*/
class CallbackClientAuthenticator extends AbstractClientAuthenticator
{
/**
* {@inheritdoc }
*/
public function supports(Request $request): bool
{
return $request->request->has(static::AUTH_FIELD) || $request->query->has(static::AUTH_FIELD);
}
/**
* {@inheritdoc }
*/
public function supportsRememberMe(): bool
{
return false;

View file

@ -6,10 +6,21 @@ use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
/**
* Class FrontApiClientAuthenticator
*
* @package RetailCrm\ServiceBundle\Security
*/
class FrontApiClientAuthenticator extends AbstractClientAuthenticator
{
private $security;
/**
* FrontApiClientAuthenticator constructor.
*
* @param ErrorJsonResponseFactory $errorResponseFactory
* @param Security $security
*/
public function __construct(
ErrorJsonResponseFactory $errorResponseFactory,
Security $security
@ -19,15 +30,21 @@ class FrontApiClientAuthenticator extends AbstractClientAuthenticator
$this->security = $security;
}
/**
* {@inheritdoc }
*/
public function supports(Request $request): bool
{
if ($this->security->getUser()) {
return false;
}
return true;
return $request->request->has(static::AUTH_FIELD);
}
/**
* {@inheritdoc }
*/
public function supportsRememberMe(): bool
{
return true;

View file

@ -14,6 +14,11 @@ use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Validator\Validation;
use Generator;
/**
* Class CallbackValueResolverTest
*
* @package RetailCrm\ServiceBundle\Tests\ArgumentResolver
*/
class CallbackValueResolverTest extends TestCase
{
private $resolver;

View file

@ -14,6 +14,11 @@ use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Validator\Validation;
use Generator;
/**
* Class ClientValueResolverTest
*
* @package RetailCrm\ServiceBundle\Tests\ArgumentResolver
*/
class ClientValueResolverTest extends TestCase
{
private $resolver;

View file

@ -4,6 +4,11 @@ namespace RetailCrm\ServiceBundle\Tests\DataFixtures;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class RequestDto
*
* @package RetailCrm\ServiceBundle\Tests\DataFixtures
*/
class RequestDto
{
/**

View file

@ -4,6 +4,11 @@ namespace RetailCrm\ServiceBundle\Tests\DataFixtures;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Class User
*
* @package RetailCrm\ServiceBundle\Tests\DataFixtures
*/
class User implements UserInterface
{
public function getRoles(): array

View file

@ -6,6 +6,11 @@ use PHPUnit\Framework\TestCase;
use RetailCrm\ServiceBundle\DependencyInjection\Configuration;
use Symfony\Component\Config\Definition\Processor;
/**
* Class ConfigurationTest
*
* @package RetailCrm\ServiceBundle\Tests\DependencyInjection
*/
class ConfigurationTest extends TestCase
{
public function testConfig(): void
@ -34,5 +39,39 @@ class ConfigurationTest extends TestCase
static::assertArrayHasKey('request_schema', $config);
static::assertArrayHasKey('callback', $config['request_schema']);
static::assertArrayHasKey('client', $config['request_schema']);
static::assertEquals(
[
'type' => 'type',
'params' => ['param']
],
$config['request_schema']['callback'][0]
);
static::assertEquals(
[
'type1',
'type2'
],
$config['request_schema']['client']
);
}
public function testPartConfig(): void
{
$processor = new Processor();
$configs = [
[
'request_schema' => [
'client' => [
'type',
]
]
]
];
$config = $processor->processConfiguration(new Configuration(), $configs);
static::assertArrayHasKey('client', $config['request_schema']);
static::assertEquals(['type'], $config['request_schema']['client']);
}
}

View file

@ -11,6 +11,11 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Class CallbackClientAuthenticatorTest
*
* @package RetailCrm\ServiceBundle\Tests\Security
*/
class CallbackClientAuthenticatorTest extends TestCase
{
public function testStart(): void

View file

@ -14,6 +14,11 @@ use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Class FrontApiClientAuthenticatorTest
*
* @package RetailCrm\ServiceBundle\Tests\Security
*/
class FrontApiClientAuthenticatorTest extends TestCase
{
public function testStart(): void
@ -100,7 +105,7 @@ class FrontApiClientAuthenticatorTest extends TestCase
$security->method('getUser')->willReturn(null);
$auth = new FrontApiClientAuthenticator($errorResponseFactory, $security);
$result = $auth->supports(new Request());
$result = $auth->supports(new Request([], [FrontApiClientAuthenticator::AUTH_FIELD => '123']));
static::assertTrue($result);
}

View file

@ -10,9 +10,21 @@
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<coverage>
<exclude>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
<report>
<clover outputFile="coverage.xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="RetailCRM Service Bundle Tests">
<directory>./Tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="test-report.xml"/>
</logging>
</phpunit>