private static IContentKey CreateDummyContentKey(ContentKeyType keyType) { return(MediaServicesHelper.CreateContentKey(_context, "Dummy ContentKey", keyType)); }
public static async Task <object> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log) { log.Info($"AMS v2 Function - CreateContentKeyAuthorizationPolicy was triggered!"); string jsonContent = await req.Content.ReadAsStringAsync(); dynamic data = JsonConvert.DeserializeObject(jsonContent); // Validate input objects if (data.assetId == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass assetId in the input object" })); } if (data.contentKeyAuthorizationPolicyId == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass contentKeyAuthorizationPolicyId in the input object" })); } if (data.assetDeliveryPolicyId == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass assetDeliveryPolicyId in the input object" })); } if (data.contentKeyType == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass contentKeyType in the input object" })); } string assetId = data.assetId; string contentKeyAuthorizationPolicyId = data.contentKeyAuthorizationPolicyId; string assetDeliveryPolicyId = data.assetDeliveryPolicyId; string contentKeyTypeName = data.contentKeyType; if (!MediaServicesHelper.AMSContentKeyType.ContainsKey(contentKeyTypeName)) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass a valid contentKeyType in the input object" })); } ContentKeyType contentKeyType = MediaServicesHelper.AMSContentKeyType[contentKeyTypeName]; if (contentKeyType != ContentKeyType.CommonEncryption && contentKeyType != ContentKeyType.CommonEncryptionCbcs && contentKeyType != ContentKeyType.EnvelopeEncryption) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Please pass a valid contentKeyType in the input object" })); } string contentKeyName = null; if (data.contentKeyName != null) { contentKeyName = data.contentKeyName; } MediaServicesCredentials amsCredentials = new MediaServicesCredentials(); IAsset asset = null; IContentKeyAuthorizationPolicy ckaPolicy = null; IAssetDeliveryPolicy adPolicy = null; IContentKey contentKey = null; try { // Load AMS account context log.Info($"Using AMS v2 REST API Endpoint : {amsCredentials.AmsRestApiEndpoint.ToString()}"); AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(amsCredentials.AmsAadTenantDomain, new AzureAdClientSymmetricKey(amsCredentials.AmsClientId, amsCredentials.AmsClientSecret), AzureEnvironments.AzureCloudEnvironment); AzureAdTokenProvider tokenProvider = new AzureAdTokenProvider(tokenCredentials); _context = new CloudMediaContext(amsCredentials.AmsRestApiEndpoint, tokenProvider); // Get the Asset, ContentKeyAuthorizationPolicy, AssetDeliveryPolicy asset = _context.Assets.Where(a => a.Id == assetId).FirstOrDefault(); if (asset == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "Asset not found" })); } ckaPolicy = _context.ContentKeyAuthorizationPolicies.Where(p => p.Id == contentKeyAuthorizationPolicyId).Single(); if (ckaPolicy == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "ContentKeyAuthorizationPolicy not found" })); } adPolicy = _context.AssetDeliveryPolicies.Where(p => p.Id == assetDeliveryPolicyId).Single(); if (adPolicy == null) { return(req.CreateResponse(HttpStatusCode.BadRequest, new { error = "AssetDeliveryPolicy not found" })); } switch (contentKeyType) { case ContentKeyType.CommonEncryption: if (contentKeyName == null) { contentKeyName = "Common Encryption ContentKey"; } contentKey = MediaServicesHelper.CreateContentKey(_context, contentKeyName, ContentKeyType.CommonEncryption); break; case ContentKeyType.CommonEncryptionCbcs: if (contentKeyName == null) { contentKeyName = "Common Encryption CBCS ContentKey"; } contentKey = MediaServicesHelper.CreateContentKey(_context, contentKeyName, ContentKeyType.CommonEncryptionCbcs); break; case ContentKeyType.EnvelopeEncryption: if (contentKeyName == null) { contentKeyName = "Envelope Encryption ContentKey"; } contentKey = MediaServicesHelper.CreateContentKey(_context, contentKeyName, ContentKeyType.EnvelopeEncryption); break; } asset.ContentKeys.Add(contentKey); contentKey.AuthorizationPolicyId = ckaPolicy.Id; contentKey = contentKey.UpdateAsync().Result; asset.DeliveryPolicies.Add(adPolicy); } catch (Exception e) { log.Info($"ERROR: Exception {e}"); return(req.CreateResponse(HttpStatusCode.BadRequest)); } return(req.CreateResponse(HttpStatusCode.OK, new { contentKeyId = contentKey.Id })); }