/**
  * @covers WindowsAzure\MediaServices\Models\ContentKeyAuthorizationPolicy::getId
  * @covers WindowsAzure\MediaServices\Models\ContentKeyAuthorizationPolicy::setId
  */
 public function testGetSetId()
 {
     // Setup
     $testNameForContentKeyAuthorizationPolicyId = 'content-key-authorization-policy-id-312312';
     $contentKeyAuthorizationPolicy = new ContentKeyAuthorizationPolicy();
     // Test
     $contentKeyAuthorizationPolicy->setId($testNameForContentKeyAuthorizationPolicyId);
     $result = $contentKeyAuthorizationPolicy->getId();
     // Assert
     $this->assertEquals($testNameForContentKeyAuthorizationPolicyId, $result);
 }
 /**
  * @covers WindowsAzure\MediaServices\MediaServicesRestProxy::deleteContentKeyAuthorizationPolicy
  */
 public function testDeleteContentKeyAuthorizationPolicy()
 {
     // Setup
     $countBefore = count($this->restProxy->getContentKeyAuthorizationPolicyList());
     $name = TestResources::MEDIA_SERVICES_CONTENT_KEY_AUTHORIZATION_POLICY_NAME . $this->createSuffix();
     $policy = new ContentKeyAuthorizationPolicy();
     $policy->setName($name);
     $result = $this->restProxy->createContentKeyAuthorizationPolicy($policy);
     $countMiddle = count($this->restProxy->getContentKeyAuthorizationPolicyList());
     // Test
     $this->restProxy->deleteContentKeyAuthorizationPolicy($result);
     $countAfter = count($this->restProxy->getContentKeyAuthorizationPolicyList());
     // Assert
     $this->assertEquals($countMiddle - 1, $countBefore);
     $this->assertEquals($countBefore, $countAfter);
     $this->assertEquals($countMiddle - 1, $countAfter);
 }
 /**
  * Get content key authorization policies list
  *
  * @return array of Models\ContentKeyAuthorizationPolicy
  */
 public function getContentKeyAuthorizationPolicyList()
 {
     $propertyList = $this->_getEntityList("ContentKeyAuthorizationPolicies");
     $result = array();
     foreach ($propertyList as $properties) {
         $result[] = ContentKeyAuthorizationPolicy::createFromOptions($properties);
     }
     return $result;
 }
function addTokenRestrictedAuthorizationPolicy($restProxy, $contentKey, $tokenType, $fairPlayASK, $fairPlayPfxPassword, $fairPlayPfxFile, $fairPlayIV)
{
    // 4.1 Create ContentKeyAuthorizationPolicyRestriction (Token Restricted)
    $tokenRestriction = generateTokenRequirements($tokenType);
    $restriction = new ContentKeyAuthorizationPolicyRestriction();
    $restriction->setName('ContentKey Authorization Policy Restriction');
    $restriction->setKeyRestrictionType(ContentKeyRestrictionType::TOKEN_RESTRICTED);
    $restriction->setRequirements($tokenRestriction);
    // 4.2 Configure FairPlay license options.
    $fairPlayConfiguration = configureFairPlayPolicyOptions($restProxy, $fairPlayASK, $fairPlayPfxPassword, $fairPlayPfxFile, $fairPlayIV);
    // 4.3 Create ContentKeyAuthorizationPolicyOption (FairPlay)
    $fairPlayOption = new ContentKeyAuthorizationPolicyOption();
    $fairPlayOption->setName('Deliver Common CBC Content Key with token restrictions');
    $fairPlayOption->setKeyDeliveryType(ContentKeyDeliveryType::FAIRPLAY);
    $fairPlayOption->setRestrictions(array($restriction));
    $fairPlayOption->setKeyDeliveryConfiguration($fairPlayConfiguration);
    $fairPlayOption = $restProxy->createContentKeyAuthorizationPolicyOption($fairPlayOption);
    // 4.4 Create ContentKeyAuthorizationPolicy
    $ckapolicy = new ContentKeyAuthorizationPolicy();
    $ckapolicy->setName('ContentKey Authorization Policy');
    $ckapolicy = $restProxy->createContentKeyAuthorizationPolicy($ckapolicy);
    // 4.5 Link the ContentKeyAuthorizationPolicyOption to the ContentKeyAuthorizationPolicy
    $restProxy->linkOptionToContentKeyAuthorizationPolicy($fairPlayOption, $ckapolicy);
    // 4.6 Associate the ContentKeyAuthorizationPolicy with the ContentKey
    $contentKey->setAuthorizationPolicyId($ckapolicy->getId());
    $restProxy->updateContentKey($contentKey);
    echo "Added Content Key Authorization Policy: name={$ckapolicy->getName()} id={$ckapolicy->getId()}" . PHP_EOL;
    return $tokenRestriction;
}
function addTokenRestrictedAuthorizationPolicy($restProxy, $contentKey, $tokenType)
{
    // 4.1 Create ContentKeyAuthorizationPolicyRestriction (Token Restricted)
    $tokenRestriction = generateTokenRequirements($tokenType);
    $restriction = new ContentKeyAuthorizationPolicyRestriction();
    $restriction->setName('ContentKey Authorization Policy Restriction');
    $restriction->setKeyRestrictionType(ContentKeyRestrictionType::TOKEN_RESTRICTED);
    $restriction->setRequirements($tokenRestriction);
    // 4.2 Create ContentKeyAuthorizationPolicyOption (AES)
    $option = new ContentKeyAuthorizationPolicyOption();
    $option->setName('ContentKey Authorization Policy Option');
    $option->setKeyDeliveryType(ContentKeyDeliveryType::BASELINE_HTTP);
    $option->setRestrictions(array($restriction));
    $option = $restProxy->createContentKeyAuthorizationPolicyOption($option);
    // 4.3 Create ContentKeyAuthorizationPolicy
    $ckapolicy = new ContentKeyAuthorizationPolicy();
    $ckapolicy->setName('ContentKey Authorization Policy');
    $ckapolicy = $restProxy->createContentKeyAuthorizationPolicy($ckapolicy);
    // 4.4 Link the ContentKeyAuthorizationPolicyOption to the ContentKeyAuthorizationPolicy
    $restProxy->linkOptionToContentKeyAuthorizationPolicy($option, $ckapolicy);
    // 4.7 Associate the ContentKeyAuthorizationPolicy with the ContentKey
    $contentKey->setAuthorizationPolicyId($ckapolicy->getId());
    $restProxy->updateContentKey($contentKey);
    print "Added Content Key Authorization Policy: name={$ckapolicy->getName()} id={$ckapolicy->getId()}\r\n";
    return $tokenRestriction;
}
print "Content Key id={$contentKey->getId()}\r\n";
// 3.4 Associate the ContentKey with the Asset
$restProxy->linkContentKeyToAsset($encodedAsset, $contentKey);
// Phase 4 - Create the ContentKey Authorization Policy
// 4.1 Create ContentKeyAuthorizationPolicyRestriction (Open)
$restriction = new ContentKeyAuthorizationPolicyRestriction();
$restriction->setName('ContentKey Authorization Policy Restriction');
$restriction->setKeyRestrictionType(ContentKeyRestrictionType::OPEN);
// 4.2 Create ContentKeyAuthorizationPolicyOption (AES)
$option = new ContentKeyAuthorizationPolicyOption();
$option->setName('ContentKey Authorization Policy Option');
$option->setKeyDeliveryType(ContentKeyDeliveryType::BASELINE_HTTP);
$option->setRestrictions(array($restriction));
$option = $restProxy->createContentKeyAuthorizationPolicyOption($option);
// 4.3 Create ContentKeyAuthorizationPolicy
$ckapolicy = new ContentKeyAuthorizationPolicy();
$ckapolicy->setName('ContentKey Authorization Policy');
$ckapolicy = $restProxy->createContentKeyAuthorizationPolicy($ckapolicy);
// 4.4 Link the ContentKeyAuthorizationPolicyOption to the ContentKeyAuthorizationPolicy
$restProxy->linkOptionsToContentKeyAuthorizationPolicy($option, $ckapolicy);
// 4.5 Associate the ContentKeyAuthorizationPolicy with the ContentKey
$contentKey->setAuthorizationPolicyId($ckapolicy->getId());
$restProxy->updateContentKey($contentKey);
print "Added Content Key Authorization Policy: name={$ckapolicy->getName()} id={$ckapolicy->getId()}\r\n";
// Phase 5 - Create the AssetDeliveryPolicy
// 5.1 Get the acquisition URL
$acquisitionUrl = $restProxy->getKeyDeliveryUrl($contentKey, ContentKeyDeliveryType::BASELINE_HTTP);
// 5.2 Generate the AssetDeliveryPolicy Configuration Key
$randomKey = Utilities::generateCryptoKey(16);
$configuration = [AssetDeliveryPolicyConfigurationKey::ENVELOPE_KEY_ACQUISITION_URL => $acquisitionUrl, AssetDeliveryPolicyConfigurationKey::ENVELOPE_ENCRYPTION_IV_AS_BASE64 => base64_encode($randomKey)];
$confJson = AssetDeliveryPolicyConfigurationKey::stringifyAssetDeliveryPolicyConfiguartionKey($configuration);
function addTokenRestrictedAuthorizationPolicy($restProxy, $contentKey, $tokenType)
{
    // 4.1 Create ContentKeyAuthorizationPolicyRestriction (Token Restricted)
    $tokenRestriction = generateTokenRequirements($tokenType);
    $restriction = new ContentKeyAuthorizationPolicyRestriction();
    $restriction->setName('ContentKey Authorization Policy Restriction');
    $restriction->setKeyRestrictionType(ContentKeyRestrictionType::TOKEN_RESTRICTED);
    $restriction->setRequirements($tokenRestriction);
    // 4.2 Configure PlayReady and Widevine license templates.
    $playReadyLicenseTemplate = configurePlayReadyLicenseTemplate();
    $widevineLicenseTemplate = configureWidevineLicenseTemplate();
    // 4.3 Create ContentKeyAuthorizationPolicyOption (PlayReady)
    $playReadyOption = new ContentKeyAuthorizationPolicyOption();
    $playReadyOption->setName('PlayReady Authorization Policy Option');
    $playReadyOption->setKeyDeliveryType(ContentKeyDeliveryType::PLAYREADY_LICENSE);
    $playReadyOption->setRestrictions(array($restriction));
    $playReadyOption->setKeyDeliveryConfiguration($playReadyLicenseTemplate);
    $playReadyOption = $restProxy->createContentKeyAuthorizationPolicyOption($playReadyOption);
    // 4.4 Create ContentKeyAuthorizationPolicyOption (Widevine)
    $widevineOption = new ContentKeyAuthorizationPolicyOption();
    $widevineOption->setName('Widevine Authorization Policy Option');
    $widevineOption->setKeyDeliveryType(ContentKeyDeliveryType::WIDEVINE);
    $widevineOption->setRestrictions(array($restriction));
    $widevineOption->setKeyDeliveryConfiguration($widevineLicenseTemplate);
    $widevineOption = $restProxy->createContentKeyAuthorizationPolicyOption($widevineOption);
    // 4.5 Create ContentKeyAuthorizationPolicy
    $ckapolicy = new ContentKeyAuthorizationPolicy();
    $ckapolicy->setName('ContentKey Authorization Policy');
    $ckapolicy = $restProxy->createContentKeyAuthorizationPolicy($ckapolicy);
    // 4.6 Link the ContentKeyAuthorizationPolicyOption to the ContentKeyAuthorizationPolicy
    $restProxy->linkOptionToContentKeyAuthorizationPolicy($playReadyOption, $ckapolicy);
    $restProxy->linkOptionToContentKeyAuthorizationPolicy($widevineOption, $ckapolicy);
    // 4.7 Associate the ContentKeyAuthorizationPolicy with the ContentKey
    $contentKey->setAuthorizationPolicyId($ckapolicy->getId());
    $restProxy->updateContentKey($contentKey);
    print "Added Content Key Authorization Policy: name={$ckapolicy->getName()} id={$ckapolicy->getId()}\r\n";
    return $tokenRestriction;
}
 /**
  * Create ContentKeyAuthorizationPolicy from array
  *
  * @param array $options Array containing values for object properties
  *
  * @return ContentKeyAuthorizationPolicy
  */
 public static function createFromOptions($options)
 {
     $contentKeyAuthorizationPolicy = new ContentKeyAuthorizationPolicy();
     $contentKeyAuthorizationPolicy->fromArray($options);
     return $contentKeyAuthorizationPolicy;
 }