mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-14 09:02:12 +00:00
[API] Change endpoint for requesting customer authentication token
This commit is contained in:
parent
b0e1c284e8
commit
05ff93ffb0
10 changed files with 145 additions and 169 deletions
|
|
@ -58,7 +58,7 @@ security:
|
|||
stateless: true
|
||||
entry_point: jwt
|
||||
json_login:
|
||||
check_path: "%sylius.security.new_api_shop_route%/authentication-token"
|
||||
check_path: "%sylius.security.new_api_shop_route%/customers/token"
|
||||
username_path: email
|
||||
password_path: password
|
||||
success_handler: lexik_jwt_authentication.handler.authentication_success
|
||||
|
|
@ -118,5 +118,5 @@ security:
|
|||
- { path: "%sylius.security.new_api_admin_regex%/.*", role: ROLE_API_ACCESS }
|
||||
- { path: "%sylius.security.new_api_admin_route%/administrators/token", role: PUBLIC_ACCESS }
|
||||
- { path: "%sylius.security.new_api_user_account_regex%/.*", role: ROLE_USER }
|
||||
- { path: "%sylius.security.new_api_shop_route%/authentication-token", role: PUBLIC_ACCESS }
|
||||
- { path: "%sylius.security.new_api_shop_route%/customers/token", role: PUBLIC_ACCESS }
|
||||
- { path: "%sylius.security.new_api_shop_regex%/.*", role: PUBLIC_ACCESS }
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ use Symfony\Component\HttpFoundation\Response;
|
|||
|
||||
final class ApiPlatformSecurityClient implements ApiSecurityClientInterface
|
||||
{
|
||||
/** @var array<string, string|object> $request */
|
||||
/** @var array<string, string|object> */
|
||||
private array $request = [];
|
||||
|
||||
public function __construct(
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ final class LoginContext implements Context
|
|||
{
|
||||
$this->shopAuthenticationTokenClient->request(
|
||||
'POST',
|
||||
sprintf('%s/shop/authentication-token', $this->apiUrlPrefix),
|
||||
sprintf('%s/shop/customers/token', $this->apiUrlPrefix),
|
||||
[],
|
||||
[],
|
||||
['CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'],
|
||||
|
|
|
|||
|
|
@ -13,16 +13,24 @@ declare(strict_types=1);
|
|||
|
||||
namespace Sylius\Bundle\ApiBundle\OpenApi\Documentation;
|
||||
|
||||
use ApiPlatform\OpenApi\Model\Operation;
|
||||
use ApiPlatform\OpenApi\Model\PathItem;
|
||||
use ApiPlatform\OpenApi\Model\RequestBody;
|
||||
use ApiPlatform\OpenApi\OpenApi;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
final class CustomerDocumentationModifier implements DocumentationModifierInterface
|
||||
{
|
||||
public function __construct(private readonly string $apiRoute)
|
||||
{
|
||||
}
|
||||
|
||||
public function modify(OpenApi $docs): OpenApi
|
||||
{
|
||||
$docs = $this->updateVerifiedPropertyType($docs);
|
||||
$docs = $this->updateCustomerStatisticsExampleResponse($docs);
|
||||
|
||||
return $docs;
|
||||
return $this->applyCustomerTokenDocumentation($docs);
|
||||
}
|
||||
|
||||
private function updateVerifiedPropertyType(OpenApi $docs): OpenApi
|
||||
|
|
@ -118,4 +126,132 @@ final class CustomerDocumentationModifier implements DocumentationModifierInterf
|
|||
|
||||
return $docs->withComponents($components);
|
||||
}
|
||||
|
||||
private function applyCustomerTokenDocumentation(OpenApi $docs): OpenApi
|
||||
{
|
||||
$components = $docs->getComponents();
|
||||
$schemas = $components->getSchemas();
|
||||
|
||||
$schemas['Customer-shop.customer.token.read'] = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'token' => [
|
||||
'type' => 'string',
|
||||
'readOnly' => true,
|
||||
],
|
||||
'customer' => [
|
||||
'type' => 'string',
|
||||
'readOnly' => true,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$schemas['Customer-shop.customer.token.read.unauthorized'] = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'code' => [
|
||||
'type' => 'integer',
|
||||
'readOnly' => true,
|
||||
],
|
||||
'message' => [
|
||||
'type' => 'string',
|
||||
'readOnly' => true,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$schemas['Customer-shop.customer.token.read.bad-request'] = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'type' => [
|
||||
'type' => 'string',
|
||||
'readOnly' => true,
|
||||
],
|
||||
'title' => [
|
||||
'type' => 'string',
|
||||
'readOnly' => true,
|
||||
],
|
||||
'status' => [
|
||||
'type' => 'integer',
|
||||
'readOnly' => true,
|
||||
],
|
||||
'detail' => [
|
||||
'type' => 'string',
|
||||
'readOnly' => true,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$schemas['Customer-shop.customer.token.create'] = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'email' => [
|
||||
'type' => 'string',
|
||||
'example' => 'shop@example.com',
|
||||
],
|
||||
'password' => [
|
||||
'type' => 'string',
|
||||
'example' => 'sylius',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$components = $components->withSchemas($schemas);
|
||||
$docs = $docs->withComponents($components);
|
||||
|
||||
$docs->getPaths()->addPath(
|
||||
$this->apiRoute . '/shop/customers/token',
|
||||
new PathItem(
|
||||
post: new Operation(
|
||||
operationId: 'postCredentialsItem',
|
||||
tags: ['Customer'],
|
||||
responses: [
|
||||
Response::HTTP_OK => [
|
||||
'description' => 'JWT token retrieval succeeded',
|
||||
'content' => [
|
||||
'application/json' => [
|
||||
'schema' => [
|
||||
'$ref' => '#/components/schemas/Customer-shop.customer.token.read',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
Response::HTTP_UNAUTHORIZED => [
|
||||
'description' => 'JWT token retrieval failed due to invalid credentials',
|
||||
'content' => [
|
||||
'application/json' => [
|
||||
'schema' => [
|
||||
'$ref' => '#/components/schemas/Customer-shop.customer.token.read.unauthorized',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
Response::HTTP_BAD_REQUEST => [
|
||||
'description' => 'JWT token retrieval failed due to invalid request',
|
||||
'content' => [
|
||||
'application/json' => [
|
||||
'schema' => [
|
||||
'$ref' => '#/components/schemas/Customer-shop.customer.token.read.bad-request',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
summary: 'Retrieves the JWT token.',
|
||||
requestBody: new RequestBody(
|
||||
description: 'Retrieves the JWT token.',
|
||||
content: new \ArrayObject([
|
||||
'application/json' => [
|
||||
'schema' => [
|
||||
'$ref' => '#/components/schemas/Customer-shop.customer.token.create',
|
||||
],
|
||||
],
|
||||
]),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $docs;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,155 +0,0 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Sylius package.
|
||||
*
|
||||
* (c) Sylius Sp. z o.o.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Sylius\Bundle\ApiBundle\OpenApi\Documentation;
|
||||
|
||||
use ApiPlatform\OpenApi\Model\Operation;
|
||||
use ApiPlatform\OpenApi\Model\PathItem;
|
||||
use ApiPlatform\OpenApi\Model\RequestBody;
|
||||
use ApiPlatform\OpenApi\OpenApi;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
final class ShopAuthenticationTokenDocumentationModifier implements DocumentationModifierInterface
|
||||
{
|
||||
public function __construct(private readonly string $apiRoute)
|
||||
{
|
||||
}
|
||||
|
||||
public function modify(OpenApi $docs): OpenApi
|
||||
{
|
||||
$components = $docs->getComponents();
|
||||
$schemas = $components->getSchemas();
|
||||
|
||||
$schemas['Customer-shop.authentication-token.read'] = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'token' => [
|
||||
'type' => 'string',
|
||||
'readOnly' => true,
|
||||
],
|
||||
'customer' => [
|
||||
'type' => 'string',
|
||||
'readOnly' => true,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$schemas['Customer-shop.authentication-token.read.unauthorized'] = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'code' => [
|
||||
'type' => 'integer',
|
||||
'readOnly' => true,
|
||||
],
|
||||
'message' => [
|
||||
'type' => 'string',
|
||||
'readOnly' => true,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$schemas['Customer-shop.authentication-token.read.bad-request'] = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'type' => [
|
||||
'type' => 'string',
|
||||
'readOnly' => true,
|
||||
],
|
||||
'title' => [
|
||||
'type' => 'string',
|
||||
'readOnly' => true,
|
||||
],
|
||||
'status' => [
|
||||
'type' => 'integer',
|
||||
'readOnly' => true,
|
||||
],
|
||||
'detail' => [
|
||||
'type' => 'string',
|
||||
'readOnly' => true,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$schemas['Customer-shop.authentication-token.create'] = [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'email' => [
|
||||
'type' => 'string',
|
||||
'example' => 'shop@example.com',
|
||||
],
|
||||
'password' => [
|
||||
'type' => 'string',
|
||||
'example' => 'sylius',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$components = $components->withSchemas($schemas);
|
||||
$docs = $docs->withComponents($components);
|
||||
|
||||
$docs->getPaths()->addPath(
|
||||
$this->apiRoute . '/shop/authentication-token',
|
||||
new PathItem(
|
||||
post: new Operation(
|
||||
operationId: 'postCredentialsItem',
|
||||
tags: ['Customer'],
|
||||
responses: [
|
||||
Response::HTTP_OK => [
|
||||
'description' => 'JWT token retrieval succeeded',
|
||||
'content' => [
|
||||
'application/json' => [
|
||||
'schema' => [
|
||||
'$ref' => '#/components/schemas/Customer-shop.authentication-token.read',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
Response::HTTP_UNAUTHORIZED => [
|
||||
'description' => 'JWT token retrieval failed due to invalid credentials',
|
||||
'content' => [
|
||||
'application/json' => [
|
||||
'schema' => [
|
||||
'$ref' => '#/components/schemas/Customer-shop.authentication-token.read.unauthorized',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
Response::HTTP_BAD_REQUEST => [
|
||||
'description' => 'JWT token retrieval failed due to invalid request',
|
||||
'content' => [
|
||||
'application/json' => [
|
||||
'schema' => [
|
||||
'$ref' => '#/components/schemas/Customer-shop.authentication-token.read.bad-request',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
summary: 'Retrieves the JWT token.',
|
||||
requestBody: new RequestBody(
|
||||
description: 'Retrieves the JWT token.',
|
||||
content: new \ArrayObject([
|
||||
'application/json' => [
|
||||
'schema' => [
|
||||
'$ref' => '#/components/schemas/Customer-shop.authentication-token.create',
|
||||
],
|
||||
],
|
||||
]),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return $docs;
|
||||
}
|
||||
}
|
||||
|
|
@ -74,12 +74,8 @@
|
|||
<tag name="sylius.open_api.modifier" />
|
||||
</service>
|
||||
|
||||
<service id="Sylius\Bundle\ApiBundle\OpenApi\Documentation\ShopAuthenticationTokenDocumentationModifier">
|
||||
<argument>%sylius.security.new_api_route%</argument>
|
||||
<tag name="sylius.open_api.modifier" />
|
||||
</service>
|
||||
|
||||
<service id="Sylius\Bundle\ApiBundle\OpenApi\Documentation\CustomerDocumentationModifier">
|
||||
<argument>%sylius.security.new_api_route%</argument>
|
||||
<tag name="sylius.open_api.modifier" />
|
||||
</service>
|
||||
|
||||
|
|
|
|||
|
|
@ -322,7 +322,7 @@ final class CustomersTest extends JsonApiTestCase
|
|||
$customer = $fixtures['customer_tony'];
|
||||
|
||||
$this->requestDelete(
|
||||
sprintf('/api/v2/admin/customers/%s/user' , $customer->getId()),
|
||||
sprintf('/api/v2/admin/customers/%s/user', $customer->getId()),
|
||||
);
|
||||
|
||||
$this->assertResponseCode($this->client->getResponse(), Response::HTTP_NO_CONTENT);
|
||||
|
|
|
|||
|
|
@ -186,7 +186,7 @@ final class PaymentMethodsTest extends JsonApiTestCase
|
|||
/** @var PaymentMethodInterface $paymentMethod */
|
||||
$paymentMethod = $fixtures['payment_method_cash_on_delivery'];
|
||||
|
||||
$this->requestDelete(uri: '/api/v2/admin/payment-methods/'. $paymentMethod->getCode());
|
||||
$this->requestDelete(uri: '/api/v2/admin/payment-methods/' . $paymentMethod->getCode());
|
||||
|
||||
$this->assertResponseCode($this->client->getResponse(), Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ declare(strict_types=1);
|
|||
namespace Sylius\Tests\Api\Admin;
|
||||
|
||||
use Sylius\Component\Product\Model\ProductAssociationTypeInterface;
|
||||
use Sylius\Component\Product\Model\ProductAssociationTypeTranslation;
|
||||
use Sylius\Component\Product\Model\ProductAssociationTypeTranslationInterface;
|
||||
use Sylius\Tests\Api\JsonApiTestCase;
|
||||
use Sylius\Tests\Api\Utils\AdminUserLoginTrait;
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ final class CustomersTest extends JsonApiTestCase
|
|||
|
||||
$this->client->request(
|
||||
method: 'POST',
|
||||
uri: '/api/v2/shop/authentication-token',
|
||||
uri: '/api/v2/shop/customers/token',
|
||||
server: self::CONTENT_TYPE_HEADER,
|
||||
content: json_encode([
|
||||
'email' => 'oliver@doe.com',
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue