3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * A utility which signs and verifies a list of key-value pairs
21 * FIXME: Add TTL support?
24 * $signer = new CRM_Utils_Signer('myprivatekey', array('param1','param2'));
26 * 'param1' => 'hello',
27 * 'param2' => 'world',
29 * $token = $signer->sign($params);
31 * assertTrue($signer->validate($token, $params));
34 class CRM_Utils_Signer
{
36 * Expected length of the salt
43 * Instantiate a signature-processor
45 * @param string $secret
47 * @param array $paramNames
48 * Array, fields which should be part of the signature.
50 public function __construct($secret, $paramNames) {
51 // ensure consistent serialization of payloads
53 $this->secret
= $secret;
54 $this->paramNames
= $paramNames;
55 // chosen to be valid in URLs but not in salt or md5
56 $this->signDelim
= "_";
57 $this->defaultSalt
= CRM_Utils_String
::createRandom(self
::SALT_LEN
, CRM_Utils_String
::ALPHANUMERIC
);
61 * Generate a signature for a set of key-value pairs
63 * @param array $params
64 * Array, key-value pairs.
66 * the salt (if known) or NULL (for auto-generated).
67 * @return string, the full public token representing the signature
69 public function sign($params, $salt = NULL) {
71 $message['secret'] = $this->secret
;
72 $message['payload'] = [];
74 $message['salt'] = $this->createSalt();
77 $message['salt'] = $salt;
79 // recall: paramNames is pre-sorted for stability
80 foreach ($this->paramNames
as $paramName) {
81 if (isset($params[$paramName])) {
82 if (is_numeric($params[$paramName])) {
83 $params[$paramName] = (string) $params[$paramName];
87 // $paramName is not included or ===NULL
88 $params[$paramName] = '';
90 $message['payload'][$paramName] = $params[$paramName];
92 $token = $message['salt'] . $this->signDelim
. md5(serialize($message));
97 * Determine whether a token represents a proper signature for $params
99 * @param string $token
100 * the full public token representing the signature.
101 * @param array $params
102 * Array, key-value pairs.
105 * @return bool, TRUE iff all $paramNames for the submitted validate($params) and the original sign($params)
107 public function validate($token, $params) {
108 list ($salt, $signature) = explode($this->signDelim
, $token);
109 if (strlen($salt) != self
::SALT_LEN
) {
110 throw new Exception("Token contains invalid salt [" . urlencode($token) . "]");
112 $newToken = $this->sign($params, $salt);
113 return ($token == $newToken);
119 public function createSalt() {
120 // It would be more secure to generate a new value but liable to run this
121 // many times on certain admin pages; so instead we'll re-use the hash.
122 return $this->defaultSalt
;