Skip to content

Commit

Permalink
Add all getters from BaseRequest to the interface, should have done t…
Browse files Browse the repository at this point in the history
…his a while ago...
  • Loading branch information
Pete Burkindine committed Mar 13, 2017
1 parent 2359de4 commit d8dd333
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
37 changes: 21 additions & 16 deletions src/Request/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Application
{
// Constants

const ERROR_APPLICATION_ID_NOT_STRING = 'The provided value for the Alexa application ID was not a string';
const ERROR_APPLICATION_ID_NOT_MATCHED = 'The application ID \'%\' found in the request does not match ' .
'any of the expected application IDs.';

Expand All @@ -27,66 +28,70 @@ class Application
// Fields

/**
* @var array[string]
* @var string
*
* @Assert\Type("array")
* @Assert\Type("string")
* @Assert\NotBlank
*/
private $expectedApplicationIds;
private $applicationId;


// Hooks

/**
* Application constructor.
*
* @param $expectedApplicationIds
* @param string $applicationId
* @param \HTMLPurifier $purifier
*/
public function __construct(array $expectedApplicationIds, \HTMLPurifier $purifier)
public function __construct($applicationId, \HTMLPurifier $purifier)
{
// Set purifier
$this->setPurifier($purifier);

// Set application IDs
$this->setExpectedApplicationIds($expectedApplicationIds);
$this->setApplicationId($applicationId);
}

// Public Methods

/**
* validateApplicationId()
*
* Confirms the provided application ID is one of the list provided as valid
* Confirms the application ID from the request is one in the list provided as valid
*
* @param $requestApplicationId
* @param array[string] $validApplicationIds
*
* @throws \InvalidArgumentException
*/
public function validateApplicationId($requestApplicationId)
public function validateApplicationId(array $validApplicationIds)
{
if (!in_array($requestApplicationId, $this->getExpectedApplicationIds())) {
if (!in_array($this->getApplicationId(), $validApplicationIds)) {
throw new \InvalidArgumentException(self::ERROR_APPLICATION_ID_NOT_MATCHED);
}
}

// Accessors

/**
* @return array
* @return string
*/
public function getExpectedApplicationIds()
public function getApplicationId()
{
return $this->expectedApplicationIds;
return $this->applicationId;
}

// Mutators

/**
* @param array $expectedApplicationIds
* @param string $applicationId
*/
protected function setExpectedApplicationIds(array $expectedApplicationIds)
protected function setApplicationId($applicationId)
{
$this->expectedApplicationIds = $expectedApplicationIds;
if (!is_string($applicationId)) {
throw new \InvalidArgumentException(self::ERROR_APPLICATION_ID_NOT_STRING);
}

$this->applicationId = $applicationId;
}
}
31 changes: 15 additions & 16 deletions src/Request/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,45 +34,44 @@ class RequestFactory
* Return an instance of the correct type of Request from the raw JSON string
*
* @param string $rawData - The raw POST value, before json_decode
* @param string $applicationId - Your application's ID (from the dev portal)
* @param array[string] $validApplicationIds - Your application's allowed Alexa application IDs (from the dev
* portal for each skill)
* @param Certificate|null $certificate - Override the auto-generated Certificate with your own
* @param Application|null $application - Override the auto-generated Application with your own
* @param \HTMLPurifier|null $purifier
*
* @return \Alexa\Request\Request
* @throws RuntimeException
* @return \Alexa\Request\RequestInterface
* @throws \RuntimeException - If the request type on the Alexa request is not one of the known types
* @throws \InvalidArgumentException - If the application ID on the Alexa request is not one of the provided
* $validApplicationIds
*/
public function fromRawData(
$rawData,
array $expectedApplicationIds,
array $validApplicationIds,
Certificate $certificate = null,
Application $application = null,
\HTMLPurifier $purifier = null
) {
// Generate purifier
if (!$purifier) {
$purifierFactory = new PurifierFactory();
$purifier = $purifierFactory->generatePurifier(PurifierFactory::DEFAULT_CACHE_PATH);
}

// Parse data for construction
$data = json_decode($rawData, true);

// Perform defaults
$application = $application ?: new Application($expectedApplicationIds, $purifier);
// Generate defaults
$application = $application ?: new Application(
$data['session']['application']['applicationId'],
$purifier
);

$certificate = $certificate ?:
new Certificate($_SERVER['HTTP_SIGNATURECERTCHAINURL'], $_SERVER['HTTP_SIGNATURE'], $purifier);

$purifier = $purifier ?: PurifierFactory::generatePurifier(PurifierFactory::DEFAULT_CACHE_PATH);

// Generate base request
/** @var BaseRequest $request */
$request = $this->generateRequest($data, $rawData, $purifier, $certificate, $application);

// Validate received application ID matches client value
$requestApplicationId = isset($data['session']['application']['applicationId']) ?
$data['session']['application']['applicationId'] :
null;
$request->getApplication()->validateApplicationId($requestApplicationId);
$request->getApplication()->validateApplicationId($validApplicationIds);

// Validate that the request signature matches the certificate
$request->getCertificate()->validateRequest($rawData);
Expand Down

0 comments on commit d8dd333

Please sign in to comment.