1
0
mirror of synced 2024-11-25 22:36:07 +03:00

ability to provide custom state instead of generated one

This commit is contained in:
Pavel 2020-10-14 12:24:56 +03:00
parent ac0a3d2bac
commit f928d53fba
5 changed files with 19 additions and 103 deletions

View File

@ -41,58 +41,42 @@ class AuthorizationUriBuilder implements BuilderInterface
private $redirectUri; private $redirectUri;
/** /**
* @var bool $withState * @var string $state
*/ */
private $withState; private $state;
/** /**
* AuthorizationUriBuilder constructor. * AuthorizationUriBuilder constructor.
* *
* @param string $appKey * @param string $appKey
* @param string $redirectUri * @param string $redirectUri
* @param bool $withState Set to true if state should be present in the URI * @param string $state
*
* It doesn't violate SRP because this class doesn't do anything besides URI generation.
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/ */
public function __construct(string $appKey, string $redirectUri, bool $withState = false) public function __construct(string $appKey, string $redirectUri, string $state = '')
{ {
$this->appKey = $appKey; $this->appKey = $appKey;
$this->redirectUri = $redirectUri; $this->redirectUri = $redirectUri;
$this->withState = $withState; $this->state = $state;
} }
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function build(): AuthorizationUri public function build(): string
{
$state = $this->withState ? uniqid('aeauth', true) : null;
return new AuthorizationUri(
self::AUTHORIZE_URI . '?' . http_build_query($this->getParams($state)),
$state
);
}
/**
* @param string|null $state
*
* @return array
*/
private function getParams(?string $state): array
{ {
if (empty($this->redirectUri)) { if (empty($this->redirectUri)) {
throw new BadMethodCallException('Redirect URI should not be empty'); throw new BadMethodCallException('Redirect URI should not be empty');
} }
return array_filter([ $address = array_filter([
'client_id' => $this->appKey, 'client_id' => $this->appKey,
'response_type' => 'code', 'response_type' => 'code',
'redirect_uri' => $this->redirectUri, 'redirect_uri' => $this->redirectUri,
'sp' => 'ae', 'sp' => 'ae',
'state' => $state, 'state' => $this->state,
'view' => 'web' 'view' => 'web'
]); ]);
return self::AUTHORIZE_URI . '?' . http_build_query($address);
} }
} }

View File

@ -1,60 +0,0 @@
<?php
/**
* PHP version 7.3
*
* @category AuthorizationUri
* @package RetailCrm\Component
* @author RetailCRM <integration@retailcrm.ru>
* @license http://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see http://help.retailcrm.ru
*/
namespace RetailCrm\Component;
/**
* Class AuthorizationUri
*
* @category AuthorizationUri
* @package RetailCrm\Component
* @author RetailDriver LLC <integration@retailcrm.ru>
* @license https://retailcrm.ru Proprietary
* @link http://retailcrm.ru
* @see https://help.retailcrm.ru
*/
class AuthorizationUri
{
/** @var string $address */
private $address;
/** @var string $state */
private $state;
/**
* AuthorizationUri constructor.
*
* @param string $address
* @param string|null $state
*/
public function __construct(string $address, ?string $state)
{
$this->address = $address;
$this->state = $state;
}
/**
* @return string
*/
public function getAddress(): string
{
return $this->address;
}
/**
* @return string
*/
public function getState(): string
{
return $this->state;
}
}

View File

@ -42,15 +42,11 @@ interface TopClientInterface
public function getServiceLocator(): ServiceLocator; public function getServiceLocator(): ServiceLocator;
/** /**
* @param bool $withState * @param string $state
* *
* @return BuilderInterface * @return BuilderInterface
*
* $withState is passed to AuthorizationUriBuilder.
* @see AuthorizationUriBuilder::__construct
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/ */
public function getAuthorizationUriBuilder(bool $withState = false): BuilderInterface; public function getAuthorizationUriBuilder(string $state = ''): BuilderInterface;
/** /**
* Send TOP request * Send TOP request

View File

@ -202,17 +202,13 @@ class TopClient implements TopClientInterface
} }
/** /**
* @param bool $withState * @param string $state
* *
* @return BuilderInterface * @return BuilderInterface
*
* $withState is passed to AuthorizationUriBuilder.
* @see AuthorizationUriBuilder::__construct
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/ */
public function getAuthorizationUriBuilder(bool $withState = false): BuilderInterface public function getAuthorizationUriBuilder(string $state = ''): BuilderInterface
{ {
return new AuthorizationUriBuilder($this->appData->getAppKey(), $this->appData->getAppSecret(), $withState); return new AuthorizationUriBuilder($this->appData->getAppKey(), $this->appData->getAppSecret(), $state);
} }
/** /**

View File

@ -30,11 +30,11 @@ class AuthorizationUriBuilderTest extends TestCase
public function testBuild() public function testBuild()
{ {
$appData = $this->getEnvAppData(); $appData = $this->getEnvAppData();
$builder = new AuthorizationUriBuilder($appData->getAppKey(), $appData->getRedirectUri(), true); $builder = new AuthorizationUriBuilder($appData->getAppKey(), $appData->getRedirectUri(), 'state');
$result = $builder->build(); $result = $builder->build();
self::assertNotFalse(strpos($result->getAddress(), $appData->getAppKey())); self::assertNotFalse(strpos($result, $appData->getAppKey()));
self::assertNotFalse(strpos($result->getAddress(), urlencode($appData->getRedirectUri()))); self::assertNotFalse(strpos($result, urlencode($appData->getRedirectUri())));
self::assertNotEmpty($result->getState()); self::assertNotFalse(strpos($result, urlencode('state')));
} }
} }