-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/acp 4074/acp 4403 invalid checkout with paypalexpress in glue…
… does not produce helpful errors (#19) * ACP-4403 Added Request validation against OpenApi schema.
- Loading branch information
Showing
12 changed files
with
572 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ jobs: | |
fail-fast: false | ||
matrix: | ||
php-version: [ | ||
'8.1', | ||
'8.2', | ||
] | ||
steps: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,20 +5,24 @@ info: | |
name: Spryker | ||
url: 'https://spryker.com/app-composition-platform/' | ||
email: [email protected] | ||
title: 'Hello World' | ||
title: 'AppKernel API' | ||
license: | ||
name: MIT | ||
url: 'https://opensource.org/licenses/MIT' | ||
servers: | ||
- url: 'http://glue-backend.hello-world.spryker.local/' | ||
- url: 'http://glue-backend.app-kernel.spryker.local' | ||
description: 'Local development endpoint' | ||
- url: 'https://api.hello-world-testing.aop.demo-spryker.com/' | ||
- url: 'https://api.app-kernel-testing.aop.demo-spryker.com' | ||
description: 'Testing' | ||
- url: 'https://api.hello-world-staging.aop.demo-spryker.com/' | ||
- url: 'https://api.app-kernel-staging.aop.demo-spryker.com' | ||
description: 'Staging' | ||
paths: | ||
'/private/configure': | ||
post: | ||
operationId: 'configure' | ||
summary: 'Saves or updates Hello World App configuration between Tenants and this App.' | ||
security: | ||
- Bearer: [ ] | ||
parameters: | ||
- $ref: '#/components/parameters/tenantIdentifier' | ||
requestBody: | ||
|
@@ -49,8 +53,11 @@ paths: | |
type: string | ||
'/private/disconnect': | ||
post: | ||
operationId: 'disconnect' | ||
summary: 'Disconnects this App from a Tenants Application. | ||
Finds configuration and removes it.' | ||
security: | ||
- Bearer: [ ] | ||
parameters: | ||
- $ref: '#/components/parameters/tenantIdentifier' | ||
responses: | ||
|
@@ -69,6 +76,13 @@ paths: | |
schema: | ||
type: string | ||
components: | ||
securitySchemes: | ||
Bearer: | ||
type: http | ||
scheme: bearer | ||
bearerFormat: JWT | ||
description: >- | ||
Enter the token with the `Bearer ` prefix, e.g. "Bearer abcde12345". | ||
schemas: | ||
ConfigurationApiRequest: | ||
properties: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Spryker/Glue/AppKernel/Plugin/GlueApplication/AppGlueRequestSchemaValidatorPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace Spryker\Glue\AppKernel\Plugin\GlueApplication; | ||
|
||
use Generated\Shared\Transfer\GlueRequestTransfer; | ||
use Generated\Shared\Transfer\GlueRequestValidationTransfer; | ||
use Spryker\Glue\GlueApplicationExtension\Dependency\Plugin\RequestValidatorPluginInterface; | ||
use Spryker\Glue\Kernel\AbstractPlugin; | ||
|
||
/** | ||
* @method \Spryker\Glue\AppKernel\AppKernelFactory getFactory() | ||
*/ | ||
class AppGlueRequestSchemaValidatorPlugin extends AbstractPlugin implements RequestValidatorPluginInterface | ||
{ | ||
public function validate(GlueRequestTransfer $glueRequestTransfer): GlueRequestValidationTransfer | ||
{ | ||
return $this->getFactory()->createOpenApiRequestSchemaValidator()->validate($glueRequestTransfer); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/Spryker/Glue/AppKernel/Validator/OpenApiRequestSchemaValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace Spryker\Glue\AppKernel\Validator; | ||
|
||
use Generated\Shared\Transfer\GlueErrorTransfer; | ||
use Generated\Shared\Transfer\GlueRequestTransfer; | ||
use Generated\Shared\Transfer\GlueRequestValidationTransfer; | ||
use GuzzleHttp\Psr7\Request; | ||
use League\OpenAPIValidation\PSR7\Exception\Validation\InvalidBody; | ||
use League\OpenAPIValidation\PSR7\ValidatorBuilder; | ||
use Spryker\Glue\AppKernel\AppKernelConfig; | ||
use Spryker\Shared\Log\LoggerTrait; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Throwable; | ||
|
||
class OpenApiRequestSchemaValidator | ||
{ | ||
use LoggerTrait; | ||
|
||
public function __construct(protected AppKernelConfig $appKernelConfig) | ||
{ | ||
} | ||
|
||
public function validate(GlueRequestTransfer $glueRequestTransfer): GlueRequestValidationTransfer | ||
{ | ||
$glueRequestValidationTransfer = new GlueRequestValidationTransfer(); | ||
$glueRequestValidationTransfer->setIsValid(true); | ||
|
||
$openApiSchemaPath = $this->appKernelConfig->getOpenApiSchemaPath(); | ||
|
||
if ($openApiSchemaPath === null || $openApiSchemaPath === '' || $openApiSchemaPath === '0') { | ||
return $glueRequestValidationTransfer; | ||
} | ||
|
||
// Converting the HTTP request to PSR7 request | ||
$psr7Request = new Request( | ||
$glueRequestTransfer->getMethod() ?? '', | ||
$glueRequestTransfer->getPath() ?? '', | ||
$glueRequestTransfer->getMeta(), | ||
$glueRequestTransfer->getContent(), | ||
); | ||
|
||
// Validate the request | ||
$validator = (new ValidatorBuilder()) | ||
->fromYamlFile($openApiSchemaPath) | ||
->getRequestValidator(); | ||
|
||
try { | ||
$validator->validate($psr7Request); | ||
} catch (Throwable $throwable) { | ||
$this->getLogger()->error( | ||
$this->getMessageFromThrowable($throwable), | ||
$glueRequestTransfer->getMeta(), | ||
); | ||
|
||
$glueErrorTransfer = new GlueErrorTransfer(); | ||
$glueErrorTransfer | ||
->setMessage($this->getMessageFromThrowable($throwable)); | ||
|
||
$glueRequestValidationTransfer | ||
->setIsValid(false) | ||
->addError($glueErrorTransfer) | ||
->setStatus(Response::HTTP_BAD_REQUEST); | ||
|
||
return $glueRequestValidationTransfer; | ||
} | ||
|
||
return $glueRequestValidationTransfer; | ||
} | ||
|
||
protected function getMessageFromThrowable(Throwable $throwable): string | ||
{ | ||
return match (get_class($throwable)) { | ||
InvalidBody::class => $throwable->getVerboseMessage(), | ||
default => $throwable->getMessage(), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.