New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PLAT-10050:adding support in L3 CDN tokenizer #8700
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
83b7117
Merge branch 'Orion-15.5.0-SUP-18972' of https://github.com/kaltura/s…
ravitshalem 4b37227
PLAT-10050:adding support in L3 CDN tokenizer
ravitshalem bfd2f16
Merge branch 'Orion-15.5.0' of https://github.com/kaltura/server into…
ravitshalem 9f2f10d
Merge branch 'Orion-15.5.0-PLAT-10050' of https://github.com/kaltura/…
ravitshalem b546761
small fix
ravitshalem 880bc94
small fix
ravitshalem f05335e
PLAT-10050:code review remarks
ravitshalem 5470aac
PLAT-10050:adding notes
ravitshalem bac8482
PLAT-10050:small fix
ravitshalem 3d6e4b6
PLAT-10050:code review remarks
ravitshalem File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
177 changes: 177 additions & 0 deletions
177
alpha/apps/kaltura/lib/storage/urlTokenizers/kL3UrlTokenizer.php
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,177 @@ | ||
<?php | ||
|
||
class kL3UrlTokenizer extends kUrlTokenizer | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $gen = ''; | ||
|
||
const FLAVOR_ID = 'flavorId'; | ||
const URL = 'url'; | ||
const NVA = 'nva'; | ||
const DIRS = 'dirs'; | ||
const SECURE_HASH_ALGO ='sha1'; | ||
const TOKEN = 'token'; | ||
const HASH = 'hash'; | ||
const SLASH = '/'; | ||
const EQUAL = '='; | ||
const AMPERSAND = '&'; | ||
const QUESTION_MARK = '?'; | ||
const TILDA = '~'; | ||
|
||
/** | ||
* @param string $url | ||
* @param string $urlPrefix | ||
* @return string | ||
*/ | ||
public function tokenizeSingleUrl($url, $urlPrefix = null) | ||
{ | ||
$url = self::removeTokenWord($url); | ||
list($urlToToken, $restUrl) = self::splitUrlFlavorParts($url); | ||
return $this->calculateToken($urlToToken) . $restUrl; | ||
} | ||
|
||
/** | ||
* @param string $baseUrl | ||
* @param array $flavors | ||
*/ | ||
public function tokenizeMultiUrls(&$baseUrl, &$flavors) | ||
{ | ||
$flavorRestUrl = ''; | ||
$commonPartUrl = ''; | ||
if (count($flavors) > 1) | ||
{ | ||
if (isset($flavors[0][self::URL])) | ||
{ | ||
$url = self::removeTokenWord($flavors[0][self::URL]); | ||
list($commonPartUrl, $flavorRestUrl) = self::splitUrlPartsFlavorsList($url); | ||
} | ||
} | ||
else if (count($flavors) == 1) | ||
{ | ||
if (isset($flavors[0][self::URL])) | ||
{ | ||
$url = self::removeTokenWord($flavors[0][self::URL]); | ||
list($commonPartUrl,$flavorRestUrl) = self::splitUrlFlavorParts($url); | ||
} | ||
} | ||
$urlTokenized = $this->calculateToken($commonPartUrl); | ||
self::setFlavorsUrlWithToken($flavors, $urlTokenized, $flavorRestUrl); | ||
} | ||
|
||
public static function setFlavorsUrlWithToken(&$flavors, $urlTokenized, $flavorRestUrl) | ||
{ | ||
if ($urlTokenized === '') | ||
{ | ||
return; | ||
} | ||
foreach($flavors as $flavorKey => $flavor) | ||
{ | ||
if (isset($flavor[self::URL])) | ||
{ | ||
if ($flavorRestUrl !== '') | ||
{ | ||
$restUrl = $flavorRestUrl; | ||
} | ||
else | ||
{ | ||
$restUrl = self::getRestUrl($flavor); | ||
} | ||
$flavors[$flavorKey][self::URL] = $urlTokenized . $restUrl; | ||
} | ||
} | ||
} | ||
|
||
public static function getRestUrl($flavor) | ||
{ | ||
$restUrl = ''; | ||
$flavorIdPos = strpos($flavor[self::URL], self::FLAVOR_ID . self::SLASH); | ||
if ($flavorIdPos !== false) | ||
{ | ||
$restUrl = substr($flavor[self::URL], $flavorIdPos + strlen(self::FLAVOR_ID . self::SLASH)); | ||
} | ||
return $restUrl; | ||
} | ||
|
||
public function calculateToken($urlToToken) | ||
{ | ||
if ($urlToToken === '') | ||
{ | ||
return ''; | ||
} | ||
$nva = time() + $this->window; | ||
$path = self::SLASH . trim($urlToToken, self::SLASH) . self::SLASH; | ||
$dirs = substr_count($path, self::SLASH) - 1; | ||
$tokenParams = self::NVA . self::EQUAL . $nva . self::AMPERSAND . self::DIRS . self::EQUAL . $dirs; | ||
$uri = $path. self::QUESTION_MARK . $tokenParams; | ||
$hash = $this->gen . substr(hash_hmac(self::SECURE_HASH_ALGO, $uri, $this->key), 0, 20); | ||
$tokenParams .= self::AMPERSAND . self::HASH . self::EQUAL . $hash; | ||
$token = str_replace(self::AMPERSAND, self::TILDA , $tokenParams); | ||
return self::TOKEN . self::EQUAL . $token . $path; | ||
} | ||
|
||
public static function removeTokenWord($url) | ||
{ | ||
$tokenPos = strpos($url, self::SLASH . self::TOKEN); | ||
if ($tokenPos !== false) | ||
{ | ||
$url = substr($url, $tokenPos + strlen(self::SLASH . self::TOKEN)); | ||
} | ||
return $url; | ||
} | ||
|
||
/* | ||
* In this case the flavor url has one flavor, | ||
* the tokenize url should be till the last slash Occurrence | ||
* splitting url into $urlToTokenize and $restUrl | ||
* $urlToTokenize: until the last slash Occurrence | ||
* $restUrl: from the last slash Occurrence | ||
*/ | ||
public static function splitUrlFlavorParts($url) | ||
{ | ||
$lastSlashOccurrence = strrpos ($url, self::SLASH) + 1; | ||
if ($lastSlashOccurrence === false) | ||
{ | ||
return array('', ''); | ||
} | ||
$restUrl = substr($url, $lastSlashOccurrence); | ||
$urlToTokenize = substr($url, 0, $lastSlashOccurrence); | ||
return array($urlToTokenize, $restUrl); | ||
} | ||
|
||
/* | ||
* In this case the flavor url has several flavors, | ||
* the tokenize url should be till the 'flavorId/' | ||
* splitting url into $commonPartUrl and $restUrl | ||
* $commonPartUrl: until the 'flavorId/' | ||
* $restUrl: from the 'flavorId/' | ||
*/ | ||
public static function splitUrlPartsFlavorsList($url) | ||
{ | ||
$commonPartUrl = $url; | ||
$flavorIdPos = strpos($url, self::FLAVOR_ID . self::SLASH); | ||
if ($flavorIdPos !== false) | ||
{ | ||
$commonPartUrl = substr($url, 0, $flavorIdPos) . self::FLAVOR_ID . self::SLASH; | ||
} | ||
$restUrl = ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This var is not really needed |
||
return array($commonPartUrl, $restUrl); | ||
} | ||
|
||
/** | ||
* @return string $gen | ||
*/ | ||
public function getGen() | ||
{ | ||
return $this->gen; | ||
} | ||
|
||
/** | ||
* @param string $gen | ||
*/ | ||
public function setGen($gen) | ||
{ | ||
$this->gen = $gen; | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
/** | ||
* @package api | ||
* @subpackage objects | ||
*/ | ||
class KalturaUrlTokenizerL3 extends KalturaUrlTokenizer | ||
{ | ||
/** | ||
* gen | ||
* | ||
* @var string | ||
*/ | ||
public $gen; | ||
|
||
private static $map_between_objects = array | ||
( | ||
"gen" | ||
); | ||
|
||
public function getMapBetweenObjects ( ) | ||
{ | ||
return array_merge ( parent::getMapBetweenObjects() , self::$map_between_objects ); | ||
} | ||
|
||
public function toObject($dbObject = null, $skip = array()) | ||
{ | ||
if (is_null($dbObject)) | ||
$dbObject = new kL3UrlTokenizer(); | ||
|
||
return parent::toObject($dbObject, $skip); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will never get here since you are adding +1 to the strpos