Add models for simple connection callbacks
* Add models for simple connection callbacks * Add verifier request and refactor models
This commit is contained in:
parent
c796400ccb
commit
e206a69d4a
15
src/Component/SimpleConnection/RequestVerifier.php
Normal file
15
src/Component/SimpleConnection/RequestVerifier.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace RetailCrm\Api\Component\SimpleConnection;
|
||||
|
||||
use RetailCrm\Api\Model\Callback\Entity\SimpleConnection\RequestProperty\RequestConnectionRegister;
|
||||
|
||||
class RequestVerifier
|
||||
{
|
||||
public function verify(string $secret, RequestConnectionRegister $registerRequest): bool
|
||||
{
|
||||
$hash = TokenCreator::create($registerRequest->apiKey, $secret);
|
||||
|
||||
return hash_equals($hash, $registerRequest->token);
|
||||
}
|
||||
}
|
13
src/Component/SimpleConnection/TokenCreator.php
Normal file
13
src/Component/SimpleConnection/TokenCreator.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace RetailCrm\Api\Component\SimpleConnection;
|
||||
|
||||
class TokenCreator
|
||||
{
|
||||
public const ALGO = 'sha256';
|
||||
|
||||
public static function create(string $apiKey, string $secret): string
|
||||
{
|
||||
return hash_hmac(self::ALGO, $apiKey, $secret);
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace RetailCrm\Api\Model\Callback\Entity\SimpleConnection\RequestProperty;
|
||||
|
||||
use RetailCrm\Api\Component\Serializer\Annotation as JMS;
|
||||
|
||||
class RequestConnectionRegister
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @JMS\Type("string")
|
||||
* @JMS\SerializedName("systemUrl")
|
||||
*/
|
||||
public $systemUrl;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @JMS\Type("string")
|
||||
* @JMS\SerializedName("apiKey")
|
||||
*/
|
||||
public $apiKey;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @JMS\Type("string")
|
||||
* @JMS\SerializedName("token")
|
||||
*/
|
||||
public $token;
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace RetailCrm\Api\Model\Callback\Response\SimpleConnection;
|
||||
|
||||
use RetailCrm\Api\Component\Serializer\Annotation as JMS;
|
||||
|
||||
class ConnectionConfigResponse
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*
|
||||
* @JMS\Type("bool")
|
||||
* @JMS\SerializedName("success")
|
||||
*/
|
||||
public $success;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*
|
||||
* @JMS\Type("array")
|
||||
* @JMS\SerializedName("scopes")
|
||||
*/
|
||||
public $scopes;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @JMS\Type("string")
|
||||
* @JMS\SerializedName("registerUrl")
|
||||
*/
|
||||
public $registerUrl;
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace RetailCrm\Api\Model\Callback\Response\SimpleConnection;
|
||||
|
||||
use RetailCrm\Api\Component\Serializer\Annotation as JMS;
|
||||
|
||||
class ConnectionRegisterResponse
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*
|
||||
* @JMS\Type("bool")
|
||||
* @JMS\SerializedName("success")
|
||||
*/
|
||||
public $success;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @JMS\Type("string")
|
||||
* @JMS\SerializedName("accountUrl")
|
||||
*/
|
||||
public $accountUrl;
|
||||
}
|
26
tests/src/Component/SimpleConnection/RequestVerifierTest.php
Normal file
26
tests/src/Component/SimpleConnection/RequestVerifierTest.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace RetailCrm\Tests\Component\SimpleConnection;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RetailCrm\Api\Component\SimpleConnection\RequestVerifier;
|
||||
use RetailCrm\Api\Model\Callback\Entity\SimpleConnection\RequestProperty\RequestConnectionRegister;
|
||||
|
||||
class RequestVerifierTest extends TestCase
|
||||
{
|
||||
public function testVerify(): void
|
||||
{
|
||||
$secret = 'secret';
|
||||
$apiKey = 'api_key';
|
||||
$token = hash_hmac('sha256', $apiKey, $secret);
|
||||
|
||||
$verifier = new RequestVerifier();
|
||||
$request = new RequestConnectionRegister();
|
||||
|
||||
$request->systemUrl = 'url';
|
||||
$request->apiKey = $apiKey;
|
||||
$request->token = $token;
|
||||
|
||||
static::assertTrue($verifier->verify($secret, $request));
|
||||
}
|
||||
}
|
19
tests/src/Component/SimpleConnection/TokenCreatorTest.php
Normal file
19
tests/src/Component/SimpleConnection/TokenCreatorTest.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace RetailCrm\Tests\Component\SimpleConnection;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RetailCrm\Api\Component\SimpleConnection\TokenCreator;
|
||||
|
||||
class TokenCreatorTest extends TestCase
|
||||
{
|
||||
public function testCreate(): void
|
||||
{
|
||||
$secret = 'secret';
|
||||
$apiKey = 'api_key';
|
||||
|
||||
$hash = hash_hmac('sha256', $apiKey, $secret);
|
||||
|
||||
static::assertEquals($hash, TokenCreator::create($apiKey, $secret));
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace RetailCrm\Tests\Model\Callback\Entity\SimpleConnection\RequestProperty;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RetailCrm\Api\Factory\SerializerFactory;
|
||||
use RetailCrm\Api\Model\Callback\Entity\SimpleConnection\RequestProperty\RequestConnectionRegister;
|
||||
|
||||
class RequestConnectionRegisterTest extends TestCase
|
||||
{
|
||||
public function testDeserialize(): void
|
||||
{
|
||||
$requestData = [
|
||||
'systemUrl' => 'test_url',
|
||||
'apiKey' => 'test_key',
|
||||
'token' => 'token'
|
||||
];
|
||||
|
||||
/** @var RequestConnectionRegister $request */
|
||||
$request = SerializerFactory::create()->fromArray($requestData, RequestConnectionRegister::class);
|
||||
|
||||
static::assertEquals($requestData['systemUrl'], $request->systemUrl);
|
||||
static::assertEquals($requestData['apiKey'], $request->apiKey);
|
||||
static::assertEquals($requestData['token'], $request->token);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user