/**
  * @covers WindowsAzure\MediaServices\Models\ContentKeyAuthorizationPolicyRestriction::getRequirements
  * @covers WindowsAzure\MediaServices\Models\ContentKeyAuthorizationPolicyRestriction::setRequirements
  */
 public function testGetSetRequirements()
 {
     // Setup
     $testNameForContentKeyAuthorizationRestriction = 'test requirements';
     $contentKeyAuthorizationRestriction = new ContentKeyAuthorizationPolicyRestriction();
     // Test
     $contentKeyAuthorizationRestriction->setRequirements($testNameForContentKeyAuthorizationRestriction);
     $result = $contentKeyAuthorizationRestriction->getRequirements();
     // Assert
     $this->assertEquals($testNameForContentKeyAuthorizationRestriction, $result);
 }
 public function testCreateContentKeyAuthorizationPolicyOptionForWidevine()
 {
     // Setup Token
     $widevine = new WidevineMessage();
     $widevine->allowed_track_types = AllowedTrackTypes::SD_HD;
     $contentKeySpecs = new ContentKeySpecs();
     $contentKeySpecs->required_output_protection = new RequiredOutputProtection();
     $contentKeySpecs->required_output_protection->hdcp = Hdcp::HDCP_NONE;
     $contentKeySpecs->security_level = 1;
     $contentKeySpecs->track_type = 'SD';
     $widevine->content_key_specs = array($contentKeySpecs);
     $policyOverrides = new \stdClass();
     $policyOverrides->can_play = true;
     $policyOverrides->can_persist = true;
     $policyOverrides->can_renew = false;
     $widevine->policy_overrides = $policyOverrides;
     $jsonWidevine = WidevineMessageSerializer::serialize($widevine);
     $template = new TokenRestrictionTemplate(TokenType::SWT);
     $template->setPrimaryVerificationKey(new SymmetricVerificationKey());
     $template->setAlternateVerificationKeys(array(new SymmetricVerificationKey()));
     $template->setAudience('http://sampleaudience/');
     $template->setIssuer('http://sampleissuerurl/');
     $claims = array();
     $claims[] = new TokenClaim(TokenClaim::CONTENT_KEY_ID_CLAIM_TYPE);
     $claims[] = new TokenClaim('Rental', 'true');
     $template->setRequiredClaims($claims);
     $serializedTemplate = TokenRestrictionTemplateSerializer::serialize($template);
     // Setup Options
     $name = TestResources::MEDIA_SERVICES_CONTENT_KEY_AUTHORIZATION_OPTIONS_NAME . $this->createSuffix();
     $restrictionName = TestResources::MEDIA_SERVICES_CONTENT_KEY_AUTHORIZATION_POLICY_RESTRICTION_NAME . $this->createSuffix();
     $restriction = new ContentKeyAuthorizationPolicyRestriction();
     $restriction->setName($restrictionName);
     $restriction->setKeyRestrictionType(ContentKeyRestrictionType::TOKEN_RESTRICTED);
     $restriction->setRequirements($serializedTemplate);
     $restrictions = array($restriction);
     $options = new ContentKeyAuthorizationPolicyOption();
     $options->setName($name);
     $options->setKeyDeliveryType(ContentKeyDeliveryType::WIDEVINE);
     $options->setKeyDeliveryConfiguration($jsonWidevine);
     $options->setRestrictions($restrictions);
     // Test
     $result = $this->createContentKeyAuthorizationPolicyOption($options);
     // Retrieve the CKAPO again.
     $result = $this->restProxy->getContentKeyAuthorizationPolicyOption($result->getId());
     // Assert Options
     $this->assertEquals($options->getName(), $result->getName());
     $this->assertEquals($options->getKeyDeliveryType(), $result->getKeyDeliveryType());
     $this->assertJsonStringEqualsJsonString($jsonWidevine, $result->getKeyDeliveryConfiguration());
     $actualWidevine = WidevineMessageSerializer::deserialize($result->getKeyDeliveryConfiguration());
     $this->assertEqualsWidevineMessage($widevine, $actualWidevine);
     return $result->getId();
 }
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;
}
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 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;
}