-
Notifications
You must be signed in to change notification settings - Fork 673
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8057 from mrclay/token_svc_7824
feature(security): Adds functions to create and validate HMAC tokens
- Loading branch information
Showing
9 changed files
with
246 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
namespace Elgg\Security; | ||
|
||
/** | ||
* Component for creating HMAC tokens | ||
*/ | ||
class Hmac { | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $key; | ||
|
||
/** | ||
* @var callable | ||
*/ | ||
private $comparator; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $data; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $algo; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param string $key HMAC key | ||
* @param callable $comparator Function that returns true if given two equal strings, else false | ||
* @param string[]|string $data HMAC data string (or array of strings) | ||
* @param string $algo Hash algorithm | ||
*/ | ||
public function __construct($key, callable $comparator, $data, $algo = 'sha256') { | ||
$this->key = $key; | ||
$this->comparator = $comparator; | ||
if (!$data) { | ||
throw new \InvalidArgumentException('$data cannot be empty'); | ||
} | ||
if (is_array($data)) { | ||
$data = json_encode(array_map('strval', $data)); | ||
} else { | ||
$data = (string)$data; | ||
} | ||
$this->data = $data; | ||
$this->algo = $algo; | ||
} | ||
|
||
/** | ||
* Get the HMAC token in Base64URL encoding | ||
* | ||
* @return string | ||
*/ | ||
public function getToken() { | ||
$bytes = hash_hmac($this->algo, $this->data, $this->key, true); | ||
return strtr(rtrim(base64_encode($bytes), '='), '+/', '-_'); | ||
} | ||
|
||
/** | ||
* Does the MAC match the given token? | ||
* | ||
* @param string $token HMAC token in Base64URL encoding | ||
* @return bool | ||
*/ | ||
public function matchesToken($token) { | ||
$expected_token = $this->getToken(); | ||
return call_user_func($this->comparator, $expected_token, $token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.