ability to provide custom state instead of generated one
This commit is contained in:
parent
ac0a3d2bac
commit
f928d53fba
@ -41,58 +41,42 @@ class AuthorizationUriBuilder implements BuilderInterface
|
||||
private $redirectUri;
|
||||
|
||||
/**
|
||||
* @var bool $withState
|
||||
* @var string $state
|
||||
*/
|
||||
private $withState;
|
||||
private $state;
|
||||
|
||||
/**
|
||||
* AuthorizationUriBuilder constructor.
|
||||
*
|
||||
* @param string $appKey
|
||||
* @param string $redirectUri
|
||||
* @param bool $withState Set to true if state should be present in the URI
|
||||
*
|
||||
* It doesn't violate SRP because this class doesn't do anything besides URI generation.
|
||||
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
|
||||
* @param string $state
|
||||
*/
|
||||
public function __construct(string $appKey, string $redirectUri, bool $withState = false)
|
||||
public function __construct(string $appKey, string $redirectUri, string $state = '')
|
||||
{
|
||||
$this->appKey = $appKey;
|
||||
$this->redirectUri = $redirectUri;
|
||||
$this->withState = $withState;
|
||||
$this->state = $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function build(): AuthorizationUri
|
||||
{
|
||||
$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
|
||||
public function build(): string
|
||||
{
|
||||
if (empty($this->redirectUri)) {
|
||||
throw new BadMethodCallException('Redirect URI should not be empty');
|
||||
}
|
||||
|
||||
return array_filter([
|
||||
$address = array_filter([
|
||||
'client_id' => $this->appKey,
|
||||
'response_type' => 'code',
|
||||
'redirect_uri' => $this->redirectUri,
|
||||
'sp' => 'ae',
|
||||
'state' => $state,
|
||||
'state' => $this->state,
|
||||
'view' => 'web'
|
||||
]);
|
||||
|
||||
return self::AUTHORIZE_URI . '?' . http_build_query($address);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -42,15 +42,11 @@ interface TopClientInterface
|
||||
public function getServiceLocator(): ServiceLocator;
|
||||
|
||||
/**
|
||||
* @param bool $withState
|
||||
* @param string $state
|
||||
*
|
||||
* @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
|
||||
|
@ -202,17 +202,13 @@ class TopClient implements TopClientInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $withState
|
||||
* @param string $state
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -30,11 +30,11 @@ class AuthorizationUriBuilderTest extends TestCase
|
||||
public function testBuild()
|
||||
{
|
||||
$appData = $this->getEnvAppData();
|
||||
$builder = new AuthorizationUriBuilder($appData->getAppKey(), $appData->getRedirectUri(), true);
|
||||
$builder = new AuthorizationUriBuilder($appData->getAppKey(), $appData->getRedirectUri(), 'state');
|
||||
$result = $builder->build();
|
||||
|
||||
self::assertNotFalse(strpos($result->getAddress(), $appData->getAppKey()));
|
||||
self::assertNotFalse(strpos($result->getAddress(), urlencode($appData->getRedirectUri())));
|
||||
self::assertNotEmpty($result->getState());
|
||||
self::assertNotFalse(strpos($result, $appData->getAppKey()));
|
||||
self::assertNotFalse(strpos($result, urlencode($appData->getRedirectUri())));
|
||||
self::assertNotFalse(strpos($result, urlencode('state')));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user