Skip to content

Commit

Permalink
[wip] Keep old pre-hash string around
Browse files Browse the repository at this point in the history
Signed-off-by: Olivier Mehani <olivier.mehani@learnosity.com>
  • Loading branch information
shtrom committed Mar 5, 2024
1 parent 4601c60 commit df747fb
Showing 1 changed file with 79 additions and 2 deletions.
81 changes: 79 additions & 2 deletions src/Request/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public function __construct(
$this->preHashStringGenerator = $this->preHashStringFactory->getPreHashStringGenerator($service);

// First validate the arguments passed
list ($requestPacket, $securityPacket) = $this->validate($service, $secret, $securityPacket, $requestPacket);
/* list ($requestPacket, $securityPacket) = $this->validate($service, $secret, $securityPacket, $requestPacket); */
list ($requestPacket, $securityPacket) = $this->legacyValidate($service, $secret, $securityPacket, $requestPacket);

if (self::$telemetryEnabled) {
$requestPacket = $this->addMeta($requestPacket);
Expand Down Expand Up @@ -292,6 +293,81 @@ public function generate(bool $encode = true)
return $encode ? Json::encode($output) : $output;
}

public function legacyValidate(string $service, string $secret, $securityPacket, $requestPacket): array
{
if (is_string($requestPacket)) {
$requestPacket = json_decode($requestPacket, true);
$this->requestPassedAsString = true;
}

if (is_null($requestPacket)) {
$requestPacket = [];
}

// In case the user gave us a JSON securityPacket, convert to an array
if (!is_array($securityPacket) && is_string($securityPacket)) {
$securityPacket = json_decode($securityPacket, true);
}

if (empty($service)) {
throw new ValidationException('The `service` argument wasn\'t found or was empty');
} elseif (!in_array(strtolower($service), $this->validServices)) {
throw new ValidationException("The service provided ($service) is not valid");
}

if (empty($securityPacket) || !is_array($securityPacket)) {
throw new ValidationException('The security packet must be an array or a valid JSON string');
}

foreach (array_keys($securityPacket) as $key) {
if (!in_array($key, LegacyPreHashString::$validSecurityKeys)) {
throw new ValidationException('Invalid key found in the security packet: ' . $key);
}
}
if ($service === "questions" && !array_key_exists('user_id', $securityPacket)) {
throw new ValidationException('Questions API requires a `user_id` in the security packet');
}
if (!array_key_exists('timestamp', $securityPacket)) {
$securityPacket['timestamp'] = gmdate('Ymd-Hi');
}

if (empty($secret)) {
throw new ValidationException('The `secret` argument must be a valid string');
}

if (!empty($requestPacket) && !is_array($requestPacket)) {
throw new ValidationException('The request packet must be an array or a valid JSON string');
}

return [$requestPacket, $securityPacket];
}

public function generatePreHashString()
{
$signatureArray = [];

// Create a pre-hash string based on the security credentials
// The order is important
foreach (LegacyPreHashString::$validSecurityKeys as $key) {
if (array_key_exists($key, $this->securityPacket)) {
$signatureArray[] = $this->securityPacket[$key];
}
}

// Add the requestPacket if necessary
if ($this->signRequestData && !empty($this->requestPacket)) {
$signatureArray[] = Json::encode($this->requestPacket);
}

// Add the action if necessary
if (!empty($this->action)) {
$signatureArray[] = $this->action;
}

$preHashString = implode('_', $signatureArray);
return $preHashString;
}

/**
* Generate a signature hash for the request, this includes:
* - the security credentials
Expand All @@ -304,7 +380,8 @@ public function generateSignature(): string
{
$preHashStringGenerator = $this->preHashStringFactory->getPreHashStringGenerator($this->service);

$preHashString = $preHashStringGenerator->getPreHashString($this->securityPacket, $this->requestPacket);
/* $preHashString = $preHashStringGenerator->getPreHashString($this->securityPacket, $this->requestPacket); */
$preHashString = $this->generatePreHashString();

// As we only support v2 from this point onwards
// we do not need to check the version at this point,
Expand Down

0 comments on commit df747fb

Please sign in to comment.