Beispiel #1
0
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "token/asset/{assetId}/key/{keyId}")] HttpRequestMessage req, string assetId, string keyId, TraceWriter log)
        {
            // Create and cache the Media Services credentials in a static class variable
            _cachedCredentials = new MediaServicesCredentials(_mediaServicesAccountName, _mediaServicesAccountKey);

            // Used the cached credentials to create CloudMediaContext
            _context = new CloudMediaContext(_cachedCredentials);

            var asset = _context.Assets.Where(a => a.Id == assetId).FirstOrDefault();

            if (asset == null)
            {
                return(req.CreateResponse(HttpStatusCode.NotFound, $"Asset {assetId} doesn't exist."));
            }

            // Get the raw key value that we'll need to pass to generate the token bec. we specified TokenClaim.ContentKeyIdentifierClaim in during the creation of TokenRestrictionTemplate.
            Guid rawkey = EncryptionUtils.GetKeyIdAsGuid(keyId);

            TokenRestrictionTemplate tokenTemplate = DRMHelper.GenerateTokenRequirements(_tokenPrimaryVerificationKey, _tokenAlternativeVerificationKey, _tokenScope, _tokenIssuer, true);


            string testToken = TokenRestrictionTemplateSerializer.GenerateTestToken(
                tokenTemplate,
                new SymmetricVerificationKey(Convert.FromBase64String(_tokenPrimaryVerificationKey)),
                rawkey,
                DateTime.UtcNow.AddDays(365)
                );

            var tokenResponse = new TokenResponse {
                Token = testToken, TokenBase64 = testToken.Base64Encode()
            };

            return(req.CreateResponse(HttpStatusCode.OK, tokenResponse));
        }
        public static async Task <object> Run([HttpTrigger("post", WebHookType = "genericJson")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info("GetToken requested.");

            var tRequest = await req.Content.ReadAsAsync <TokenRequest>();

            // Sanity checks
            #region Sanity checks
            if (tRequest == null || tRequest.AssetId == null || tRequest.KeyId == null)
            {
                return(req.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    error = "Invalid token request."
                }));
            }
            #endregion

            // Create and cache the Media Services credentials in a static class variable
            _cachedCredentials = new MediaServicesCredentials(_mediaServicesAccountName, _mediaServicesAccountKey);

            // Used the cached credentials to create CloudMediaContext
            _context = new CloudMediaContext(_cachedCredentials);

            var asset = _context.Assets.Where(a => a.Id == tRequest.AssetId).FirstOrDefault();
            if (asset == null)
            {
                return(req.CreateResponse(HttpStatusCode.NotFound, new
                {
                    error = $"Asset {tRequest.AssetId} doesn't exist."
                }));
            }

            // Get the raw key value that we'll need to pass to generate the token bec. we specified TokenClaim.ContentKeyIdentifierClaim in during the creation of TokenRestrictionTemplate.
            Guid rawkey = EncryptionUtils.GetKeyIdAsGuid(tRequest.KeyId);

            TokenRestrictionTemplate tokenTemplate = DRMHelper.GenerateTokenRequirements(_tokenPrimaryVerificationKey, _tokenAlternativeVerificationKey, _tokenScope, _tokenIssuer, true);


            string testToken = TokenRestrictionTemplateSerializer.GenerateTestToken(
                tokenTemplate,
                new SymmetricVerificationKey(Convert.FromBase64String(_tokenPrimaryVerificationKey)),
                rawkey,
                DateTime.UtcNow.AddDays(365)
                );

            var tokenResponse = new TokenResponse {
                Token = testToken, TokenBase64 = testToken.Base64Encode()
            };
            return(req.CreateResponse(HttpStatusCode.OK, tokenResponse));
        }