Skip to content

Commit

Permalink
AssetBuilder - Switch to JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed Jan 10, 2023
1 parent 80ad12c commit e2edb71
Showing 1 changed file with 9 additions and 46 deletions.
55 changes: 9 additions & 46 deletions Civi/Core/AssetBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,14 @@ public function getUrl($name, $params = []) {
}
else {
return \CRM_Utils_System::url('civicrm/asset/builder', [
// The 'an' and 'ad' provide hints for cache lifespan and debugging/inspection.
'an' => $name,
'ap' => $this->encode($params),
'ad' => $this->digest($name, $params),
'aj' => \Civi::service('crypto.jwt')->encode([
'asset' => [$name, $params],
'exp' => 86400 * (floor(\CRM_Utils_Time::time() / 86400) + 2),
// Caching-friendly TTL -- We want the URL to be stable for a decent amount of time.
]),
], TRUE, NULL, FALSE);
}
}
Expand Down Expand Up @@ -281,7 +286,6 @@ protected function getCacheUrl($fileName = NULL) {
* @return string
*/
protected function digest($name, $params) {
// WISHLIST: For secure digest, generate+persist privatekey & call hash_hmac.
ksort($params);
$digest = md5(
$name .
Expand All @@ -292,40 +296,6 @@ protected function digest($name, $params) {
return $digest;
}

/**
* Encode $params in a format that's optimized for shorter URLs.
*
* @param array $params
* @return string
*/
protected function encode($params) {
if (empty($params)) {
return '';
}

$str = json_encode($params);
if (function_exists('gzdeflate')) {
$str = gzdeflate($str);
}
return base64_encode($str);
}

/**
* @param string $str
* @return array
*/
protected function decode($str) {
if ($str === NULL || $str === FALSE || $str === '') {
return [];
}

$str = base64_decode($str);
if (function_exists('gzdeflate')) {
$str = gzinflate($str);
}
return json_decode($str, TRUE);
}

/**
* @return bool
*/
Expand Down Expand Up @@ -372,16 +342,9 @@ public static function pageRender($get) {
/** @var Assetbuilder $assets */
$assets = \Civi::service('asset_builder');

$expectDigest = $assets->digest($get['an'], $assets->decode($get['ap']));
if ($expectDigest !== $get['ad']) {
return [
'statusCode' => 500,
'mimeType' => 'text/plain',
'content' => 'Invalid digest',
];
}

return $assets->render($get['an'], $assets->decode($get['ap']));
$obj = \Civi::service('crypto.jwt')->decode($get['aj']);
$arr = json_decode(json_encode($obj), TRUE);
return $assets->render($arr['asset'][0], $arr['asset'][1]);
}
catch (UnknownAssetException $e) {
return [
Expand Down

0 comments on commit e2edb71

Please sign in to comment.