Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 498a386

Browse files
marcerodriguezsergey-shandar
authored andcommitted
Azure AD Support for Azure Media Services (#959)
* Update AMS API Version to 2.17 + Fix integration tests * Add AzureAD support (WIP) * Update samples and unit test to use Azure AD Service Princial authentication * Add User/Pass authentication + Access Token Cache * Update README.md * Fix typo * Fix typo * Remove old unit test + dead code * Updated README document of Azure Media Services samples to include Azure AD authentication scenarios * Fix MediaServicesSettings unit tests * Remove group annotation * Fix AuthenticationFilter unit tests
1 parent 0d3ce5b commit 498a386

38 files changed

+1867
-753
lines changed

README.md

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Microsoft Azure tables, blobs, queues, service bus (queues and topics), service
4040
* deployment: create, get, delete, swap, change configuration, update status, upgrade, rollback
4141
* role instance: reboot, reimage
4242
* REST API Version: 2011-10-01
43-
* Media Services
43+
* Media Services
4444
* Connection
4545
* Ingest asset, upload files
4646
* Encoding / process asset, create job, job templates
@@ -51,7 +51,7 @@ Microsoft Azure tables, blobs, queues, service bus (queues and topics), service
5151
* Live streaming: live encoding and pass-through channels, programs and all their operations
5252
* REST API Version: 2.13
5353

54-
54+
5555
# Getting Started
5656
## Download Source Code
5757

@@ -63,7 +63,7 @@ cd ./azure-sdk-for-php
6363
```
6464

6565
> **Note**
66-
>
66+
>
6767
> The recommended way to resolve dependencies is to install them using the [Composer package manager](http://getcomposer.org).
6868
6969
## Install via Composer
@@ -72,9 +72,9 @@ cd ./azure-sdk-for-php
7272

7373
```json
7474
{
75-
"require": {
75+
"require": {
7676
"microsoft/windowsazure": "^0.5"
77-
}
77+
}
7878
}
7979
```
8080

@@ -94,14 +94,14 @@ cd ./azure-sdk-for-php
9494

9595
## Getting Started
9696

97-
There are four basic steps that have to be performed before you can make a call to any Microsoft Azure API when using the libraries.
97+
There are four basic steps that have to be performed before you can make a call to any Microsoft Azure API when using the libraries.
9898

9999
* First, include the autoloader script:
100100

101101
```PHP
102102
require_once "vendor/autoload.php";
103103
```
104-
104+
105105
* Include the namespaces you are going to use.
106106

107107
To create any Microsoft Azure service client you need to use the **ServicesBuilder** class:
@@ -115,17 +115,17 @@ There are four basic steps that have to be performed before you can make a call
115115
```PHP
116116
use WindowsAzure\Common\ServiceException;
117117
```
118-
119-
* To instantiate the service client you will also need a valid connection string. The format is:
118+
119+
* To instantiate the service client you will also need a valid connection string. The format is:
120120

121121
* For accessing a live storage service (tables, blobs, queues):
122-
122+
123123
```
124124
DefaultEndpointsProtocol=[http|https];AccountName=[yourAccount];AccountKey=[yourKey]
125125
```
126-
126+
127127
* For accessing the emulator storage:
128-
128+
129129
```
130130
UseDevelopmentStorage=true
131131
```
@@ -170,8 +170,19 @@ There are four basic steps that have to be performed before you can make a call
170170
* For Media Services:
171171
172172
```PHP
173-
$mediaServicesRestProxy = ServicesBuilder->getInstance()->createMediaServicesService(new MediaServicesSettings([YourAccountName], [YourPrimaryOrSecondaryAccessKey]));
173+
// 1 - Instantiate the credentials
174+
$credentials = new AzureAdTokenCredentials(
175+
'<tenant domain name>',
176+
new AzureAdClientSymmetricKey('<service principal client id>', '<service principal client key>'),
177+
AzureEnvironments::AZURE_CLOUD_ENVIRONMENT());
178+
179+
// 2 - Instantiate a token provider
180+
$provider = new AzureAdTokenProvider($credentials);
181+
182+
// 3 - Connect to Azure Media Services
183+
$mediaServicesRestProxy = ServicesBuilder::getInstance()->createMediaServicesService(new MediaServicesSettings('<rest api endpoint>', $provider));
174184
```
185+
You can find more examples for Media Services Authentication on the [examples](examples/MediaServices/) folder.
175186
176187
## Table Storage
177188
@@ -250,9 +261,9 @@ The following are examples of common operations performed with the Blob serivce.
250261
```PHP
251262
// OPTIONAL: Set public access policy and metadata.
252263
// Create container options object.
253-
$createContainerOptions = new CreateContainerOptions();
264+
$createContainerOptions = new CreateContainerOptions();
254265

255-
// Set public access policy. Possible values are
266+
// Set public access policy. Possible values are
256267
// PublicAccessType::CONTAINER_AND_BLOBS and PublicAccessType::BLOBS_ONLY.
257268
// CONTAINER_AND_BLOBS: full public read access for container and blob data.
258269
// BLOBS_ONLY: public read access for blobs. Container data not available.
@@ -306,7 +317,7 @@ try {
306317
// List blobs.
307318
$blob_list = $blobRestProxy->listBlobs("mycontainer");
308319
$blobs = $blob_list->getBlobs();
309-
320+
310321
foreach($blobs as $blob)
311322
{
312323
echo $blob->getName().": ".$blob->getUrl()."<br />";
@@ -346,7 +357,7 @@ try {
346357
```
347358

348359
[Error Codes and Messages for Queues](http://msdn.microsoft.com/en-us/library/windowsazure/dd179446.aspx)
349-
360+
350361

351362
### Add a message to a queue
352363

@@ -427,20 +438,20 @@ try {
427438
```
428439

429440
## Service Bus Queues
430-
The current PHP Service Bus APIs only support ACS connection strings. You need to use PowerShell to create a new ACS Service Bus namespace at the present time.
431-
First, make sure you have Azure PowerShell installed, then in a PowerShell command prompt, run
441+
The current PHP Service Bus APIs only support ACS connection strings. You need to use PowerShell to create a new ACS Service Bus namespace at the present time.
442+
First, make sure you have Azure PowerShell installed, then in a PowerShell command prompt, run
432443
```PowerShell
433444
Add-AzureAccount # this will sign you in
434445
New-AzureSBNamespace -CreateACSNamespace $true -Name 'mytestbusname' -Location 'West US' -NamespaceType 'Messaging'
435446
```
436447
If it is sucessful, you will get the connection string in the PowerShell output. If you get connection errors with it and the conection string looks like Endpoint=sb://..., change it to **Endpoint=https://...**
437-
448+
438449
### Create a Queue
439450

440451
```PHP
441452
try {
442453
$queueInfo = new QueueInfo("myqueue");
443-
454+
444455
// Create queue.
445456
$serviceBusRestProxy->createQueue($queueInfo);
446457
} catch(ServiceException $e){
@@ -483,14 +494,14 @@ try {
483494
// Set the receive mode to PeekLock (default is ReceiveAndDelete).
484495
$options = new ReceiveMessageOptions();
485496
$options->setPeekLock(true);
486-
497+
487498
// Receive message.
488499
$message = $serviceBusRestProxy->receiveQueueMessage("myqueue", $options);
489500
echo "Body: ".$message->getBody()."<br />";
490501
echo "MessageID: ".$message->getMessageId()."<br />";
491-
502+
492503
// *** Process message here ***
493-
504+
494505
// Delete message.
495506
$serviceBusRestProxy->deleteMessage($message);
496507
} catch(ServiceException $e){
@@ -505,7 +516,7 @@ try {
505516
### Create a Topic
506517

507518
```PHP
508-
try {
519+
try {
509520
// Create topic.
510521
$topicInfo = new TopicInfo("mytopic");
511522
$serviceBusRestProxy->createTopic($topicInfo);
@@ -553,7 +564,7 @@ try {
553564

554565
The primary way to receive messages from a subscription is to use a **ServiceBusRestProxy->receiveSubscriptionMessage** method. Received messages can work in two different modes: **ReceiveAndDelete** (the default) and **PeekLock** similarly to Service Bus Queues.
555566

556-
The example below demonstrates how a message can be received and processed using **ReceiveAndDelete** mode (the default mode).
567+
The example below demonstrates how a message can be received and processed using **ReceiveAndDelete** mode (the default mode).
557568

558569
```PHP
559570
try {
@@ -562,8 +573,8 @@ try {
562573
$options->setReceiveAndDelete();
563574

564575
// Get message.
565-
$message = $serviceBusRestProxy->receiveSubscriptionMessage("mytopic",
566-
"mysubscription",
576+
$message = $serviceBusRestProxy->receiveSubscriptionMessage("mytopic",
577+
"mysubscription",
567578
$options);
568579
echo "Body: ".$message->getBody()."<br />";
569580
echo "MessageID: ".$message->getMessageId()."<br />";
@@ -578,18 +589,18 @@ try {
578589

579590
### Set-up certificates
580591

581-
You need to create two certificates, one for the server (a .cer file) and one for the client (a .pem file). To create the .pem file using [OpenSSL](http://www.openssl.org), execute this:
592+
You need to create two certificates, one for the server (a .cer file) and one for the client (a .pem file). To create the .pem file using [OpenSSL](http://www.openssl.org), execute this:
582593
```
583594
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
584595
```
585-
To create the .cer certificate, execute this:
596+
To create the .cer certificate, execute this:
586597
```
587598
openssl x509 -inform pem -in mycert.pem -outform der -out mycert.cer
588599
```
589600

590601
### List Available Locations
591602

592-
```PHP
603+
```PHP
593604
$serviceManagementRestProxy->listLocations();
594605
$locations = $result->getLocations();
595606
foreach($locations as $location){
@@ -608,11 +619,11 @@ $options = new CreateStorageServiceOptions();
608619
$options->setLocation('West US');
609620

610621
$result = $serviceManagementRestProxy->createStorageService($name, $label, $options);
611-
```
612-
622+
```
623+
613624
### Create a Cloud Service
614625

615-
A cloud service is also known as a hosted service (from earlier versions of Microsoft Azure). The **createHostedServices** method allows you to create a new hosted service by providing a hosted service name (which must be unique in Microsoft Azure), a label (the base 64-endcoded hosted service name), and a **CreateServiceOptions** object which allows you to set the location *or* the affinity group for your service.
626+
A cloud service is also known as a hosted service (from earlier versions of Microsoft Azure). The **createHostedServices** method allows you to create a new hosted service by providing a hosted service name (which must be unique in Microsoft Azure), a label (the base 64-endcoded hosted service name), and a **CreateServiceOptions** object which allows you to set the location *or* the affinity group for your service.
616627

617628
```PHP
618629
$name = "myhostedservice";
@@ -648,7 +659,7 @@ echo "Operation status: ".$status->getStatus()."<br />";
648659
```
649660

650661
## Media Services
651-
662+
652663
### Create new asset with file
653664

654665
To create an asset with a file you need to create an empty asset, create access policy with write permission, create a locator joining your asset and access policy, perform actual upload and generate file info.
@@ -713,11 +724,11 @@ $streamingUrl = $originLocator->getPath() . '[Manifest file name]' . "/manifest"
713724

714725
### Manage media services entities
715726

716-
Media services CRUD operations are performed through media services rest proxy class. It has methods like “createAsset”, “createLocator”, “createJob” and etc. for entities creations.
727+
Media services CRUD operations are performed through media services rest proxy class. It has methods like “createAsset”, “createLocator”, “createJob” and etc. for entities creations.
717728

718-
To retrieve all entities list you may use methods “getAssetList”, “getAccessPolicyList”, “getLocatorList”, “getJobList” and etc. For getting single entity data use methods “getAsset”, “getJob”, “getTask” and etc. passing the entity identifier or entity data model object with non-empty identifier as a parameter.
729+
To retrieve all entities list you may use methods “getAssetList”, “getAccessPolicyList”, “getLocatorList”, “getJobList” and etc. For getting single entity data use methods “getAsset”, “getJob”, “getTask” and etc. passing the entity identifier or entity data model object with non-empty identifier as a parameter.
719730

720-
Update entities with methods like “updateLocator”, “updateAsset”, “updateAssetFile” and etc. passing the entity data model object as a parameter. It is important to have valid entity identifier specified in data model object.
731+
Update entities with methods like “updateLocator”, “updateAsset”, “updateAssetFile” and etc. passing the entity data model object as a parameter. It is important to have valid entity identifier specified in data model object.
721732

722733
Erase entities with methods like “deleteAsset”, “deleteAccessPolicy”, “deleteJob” and etc. passing the entity identifier or entity data model object with non-empty identifier as a parameter.
723734

examples/MediaServices/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
This folder contains the following Azure Media Service PHP SDK samples:
1+
This folder contains the following Azure Media Services PHP SDK samples:
22

33
* vodworkflow_aes.php: End-to-end VOD workflow that applies AES content protection.
44
* vodworkflow_drm_playready_widevine.php: End-to-end VOD workflow that applies DRM (PlayReady + Widevine) content protection.
55
* vodworkflow_drm_fairplay.php: End-to-end VOD workflow that applies DRM (FairPlay) content protection.
66
* scale_encoding_units.php: Scales the encoding reserved units.
77
* analyticsworkflow_indexer.php: End-to-end analitycs workflow to index a media file.
88
* liveworkflow_features.php: End-to-end live event workflow with configuration options to cover multiple scenarios.
9+
* azuread_userpass.php: Azure AD authentication with user credentials (username/password).
10+
* azuread_symmetrickey.php: Azure AD authentication with service principal (client symmetric key).
11+
* azuread_asymmetrickey: Azure AD authentication with service principal (client certificate).
912
* userconfig.php: Common file used to store the Azure Media Services account credentials to execute all the samples.

examples/MediaServices/analyticsworkflow_indexer.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
use WindowsAzure\Common\ServicesBuilder;
2828
use WindowsAzure\Common\Internal\MediaServicesSettings;
2929
use WindowsAzure\MediaServices\MediaServicesRestProxy;
30+
use WindowsAzure\MediaServices\Authentication\AzureAdTokenCredentials;
31+
use WindowsAzure\MediaServices\Authentication\AzureAdClientSymmetricKey;
32+
use WindowsAzure\MediaServices\Authentication\AzureAdTokenProvider;
33+
use WindowsAzure\MediaServices\Authentication\AzureEnvironments;
3034
use WindowsAzure\MediaServices\Models\Asset;
3135
use WindowsAzure\MediaServices\Models\AccessPolicy;
3236
use WindowsAzure\MediaServices\Models\Locator;
@@ -51,9 +55,12 @@
5155

5256
echo "Azure SDK for PHP - Media Analytics Sample (Indexer)".PHP_EOL;
5357

54-
// 0 - Set up the MediaServicesService object to call into the Media Services REST API.
55-
$restProxy = ServicesBuilder::getInstance()->createMediaServicesService(
56-
new MediaServicesSettings($account, $secret));
58+
// 0 - Instantiate the credentials, the token provider and connect to Azure Media Services
59+
$credentials = new AzureAdTokenCredentials(
60+
$tenant, new AzureAdClientSymmetricKey($clientId, $clientKey),
61+
AzureEnvironments::AZURE_CLOUD_ENVIRONMENT());
62+
$provider = new AzureAdTokenProvider($credentials);
63+
$restProxy = ServicesBuilder::getInstance()->createMediaServicesService(new MediaServicesSettings($restApiEndpoint, $provider));
5764

5865
// 1 - Upload the mezzanine
5966
$sourceAsset = uploadFileAndCreateAsset($restProxy, $mediaFileName);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/**
4+
* LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*
15+
* PHP version 5
16+
*
17+
* @category Microsoft
18+
*
19+
* @author Azure PHP SDK <azurephpsdk@microsoft.com>
20+
* @copyright 2012 Microsoft Corporation
21+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
22+
*
23+
* @link https://github.com/windowsazure/azure-sdk-for-php
24+
*/
25+
require_once __DIR__.'/../../vendor/autoload.php';
26+
27+
use WindowsAzure\Common\ServicesBuilder;
28+
use WindowsAzure\Common\Internal\MediaServicesSettings;
29+
use WindowsAzure\Common\Internal\Utilities;
30+
use WindowsAzure\MediaServices\MediaServicesRestProxy;
31+
use WindowsAzure\MediaServices\Authentication\AzureAdTokenCredentials;
32+
use WindowsAzure\MediaServices\Authentication\AzureAdClientAsymmetricKey;
33+
use WindowsAzure\MediaServices\Authentication\AzureAdTokenProvider;
34+
use WindowsAzure\MediaServices\Authentication\AzureEnvironments;
35+
use WindowsAzure\MediaServices\Models\Asset;
36+
37+
// read user settings from config
38+
include_once 'userconfig.php';
39+
40+
echo "Azure SDK for PHP - AzureAD Asymmetric Key Authentication Sample".PHP_EOL;
41+
42+
// 0 - Open the certificate file
43+
if ((!$cert_store = file_get_contents($pfxFileName)) ||
44+
(!openssl_pkcs12_read($cert_store, $cert_info, $pfxPassword))) {
45+
echo "Error: Unable to read the cert file\n";
46+
exit;
47+
}
48+
49+
// 1 - Instantiate the credentials
50+
$credentials = new AzureAdTokenCredentials(
51+
$tenant,
52+
new AzureAdClientAsymmetricKey($clientId, $cert_info),
53+
AzureEnvironments::AZURE_CLOUD_ENVIRONMENT());
54+
55+
// 2 - Instantiate a token provider
56+
$provider = new AzureAdTokenProvider($credentials);
57+
58+
// 3 - Connect to Azure Media Services
59+
$restProxy = ServicesBuilder::getInstance()->createMediaServicesService(new MediaServicesSettings($restApiEndpoint, $provider));
60+
61+
// 4 - List assets (sample operation)
62+
print('Listing Assets:' . PHP_EOL);
63+
foreach($restProxy->getAssetList() as $asset)
64+
{
65+
print('Asset Id=' . $asset->getId() . ' Name=' . $asset->getName() . PHP_EOL);
66+
}

0 commit comments

Comments
 (0)