Skip to content

Commit

Permalink
hmacMatch renamed to verifyHMAC. Also, refator to receive a string pr…
Browse files Browse the repository at this point in the history
…operty containing the hmac
  • Loading branch information
Grajo committed Jun 30, 2021
1 parent 20c78d8 commit 791435d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Cronofy.php
Original file line number Diff line number Diff line change
Expand Up @@ -1140,15 +1140,15 @@ public function executeBatch(Batch $batch): BatchResult
return $result;
}

public function hmacMatch($params, $body)
public function verifyHMAC($hmac_header, $body)
{
if ($params['hmac'] == null || empty($params['hmac'])) {
if ($hmac_header == null || empty($hmac_header)) {
return false;
}

$digest = hash_hmac('sha256', $body, $this->clientSecret);
$calculated = base64_encode($digest);
$hmac_list = explode(',', $params['hmac']);
$hmac_list = explode(',', $hmac_header);

return in_array($calculated, $hmac_list);
}
Expand Down
12 changes: 6 additions & 6 deletions tests/CronofyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -721,22 +721,22 @@ public function testHmacValidation()

$body = '{"example":"well-known"}';

$actual = $cronofy->hmacMatch(["hmac" => "NDJlMWE1YzcxYjJjMzQzNmIxNTIzNzdhNDU4ZTMwYzQ2N2ZlZTRhMGViOWE4NmNjOWEzOTA2NDBmYjQxZGQ2NA=="], $body);
$actual = $cronofy->verifyHMAC("NDJlMWE1YzcxYjJjMzQzNmIxNTIzNzdhNDU4ZTMwYzQ2N2ZlZTRhMGViOWE4NmNjOWEzOTA2NDBmYjQxZGQ2NA==", $body);
$this->assertTrue($actual);

$actual = $cronofy->hmacMatch(["hmac" => "something-else"], $body);
$actual = $cronofy->verifyHMAC("something-else", $body);
$this->assertFalse($actual);

$actual = $cronofy->hmacMatch(["hmac" => "something-else,NDJlMWE1YzcxYjJjMzQzNmIxNTIzNzdhNDU4ZTMwYzQ2N2ZlZTRhMGViOWE4NmNjOWEzOTA2NDBmYjQxZGQ2NA==,something-else2"], $body);
$actual = $cronofy->verifyHMAC("something-else,NDJlMWE1YzcxYjJjMzQzNmIxNTIzNzdhNDU4ZTMwYzQ2N2ZlZTRhMGViOWE4NmNjOWEzOTA2NDBmYjQxZGQ2NA==,something-else2", $body);
$this->assertTrue($actual);

$actual = $cronofy->hmacMatch(["hmac" => "something-else,something-else2"], $body);
$actual = $cronofy->verifyHMAC("something-else,something-else2", $body);
$this->assertFalse($actual);

$actual = $cronofy->hmacMatch(null, $body);
$actual = $cronofy->verifyHMAC(null, $body);
$this->assertFalse($actual);

$actual = $cronofy->hmacMatch(["hmac" => ""], $body);
$actual = $cronofy->verifyHMAC("", $body);
$this->assertFalse($actual);
}
}

0 comments on commit 791435d

Please sign in to comment.