1
0
mirror of synced 2024-11-22 05:16:07 +03:00

Add models for simple connection callbacks

* Add models for simple connection callbacks
* Add verifier request and refactor models
This commit is contained in:
RenCurs 2022-03-18 10:47:19 +03:00 committed by GitHub
parent c796400ccb
commit e206a69d4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 187 additions and 0 deletions

View 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);
}
}

View 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);
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View 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));
}
}

View 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));
}
}

View File

@ -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);
}
}