Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

Commit

Permalink
Merge pull request #165 from Azure/new-ams-sdk-2023
Browse files Browse the repository at this point in the history
New ams sdk 2023
  • Loading branch information
xpouyat authored Mar 14, 2023
2 parents 83d2225 + 98d6f72 commit 6713fab
Show file tree
Hide file tree
Showing 122 changed files with 15,226 additions and 18,565 deletions.
83 changes: 57 additions & 26 deletions AMSExplorer/AMSClient/AMSClientV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
//---------------------------------------------------------------------------------------------

using AMSClient;
using AMSExplorer.AMSLogin;
using Microsoft.Azure.Management.Media;
using Microsoft.Azure.Management.Media.Models;
using Azure.ResourceManager;
using Azure.ResourceManager.Media;
using Microsoft.Identity.Client;
using Microsoft.Identity.Client.Extensibility;
using Microsoft.Identity.Client.Broker;
using Microsoft.Rest;
using System;
using System.Diagnostics;
Expand All @@ -34,9 +33,9 @@ namespace AMSExplorer
{
public class AMSClientV3
{
public AzureMediaServicesClient AMSclient;
public MediaServicesAccountResource AMSclient;
public AuthenticationResult authResult;
public CredentialsEntryV3 credentialsEntry;
public CredentialsEntryV4 credentialsEntry;
private Form _form;
public TokenCredentials credentials;
public BearerTokenCredential credentialForArmClient;
Expand All @@ -49,7 +48,7 @@ public class AMSClientV3
private readonly System.Timers.Timer TimerAutoRefreshAuthToken;


public AMSClientV3(AzureEnvironment myEnvironment, string azureSubscriptionId, CredentialsEntryV3 myCredentialsEntry, Form form)
public AMSClientV3(AzureEnvironment myEnvironment, string azureSubscriptionId, CredentialsEntryV4 myCredentialsEntry, Form form)
{
environment = myEnvironment;
_azureSubscriptionId = azureSubscriptionId;
Expand All @@ -59,9 +58,12 @@ public AMSClientV3(AzureEnvironment myEnvironment, string azureSubscriptionId, C
if (!credentialsEntry.UseSPAuth)
{
_appInteract = PublicClientApplicationBuilder.Create(environment.ClientApplicationId)

//.WithAuthority(AzureCloudInstance.AzurePublic, credentialsEntry.AadTenantId)
.WithAuthority(environment.AADSettings.AuthenticationEndpoint + string.Format("{0}", credentialsEntry.AadTenantId ?? "common"))
.WithRedirectUri("http://localhost")
.WithDefaultRedirectUri()
//.WithRedirectUri("http://localhost")
.WithBrokerPreview(true)
.Build();
}
else // SP
Expand All @@ -75,8 +77,6 @@ public AMSClientV3(AzureEnvironment myEnvironment, string azureSubscriptionId, C
scopes = new[] { environment.AADSettings.TokenAudience.ToString() + "/user_impersonation" };
scopes2 = new[] { environment.MediaServicesV2Resource + "/user_impersonation" };
scopes3 = new[] { environment.AADSettings.TokenAudience.ToString() + "/.default" };


// Timer Auto Refresh of Auth token
TimerAutoRefreshAuthToken = new System.Timers.Timer() { AutoReset = false };
}
Expand All @@ -86,7 +86,6 @@ public void SetNewFormParent(Form form)
_form = form;
}


private async void OnTimedEventAuthRefresh(object sender, ElapsedEventArgs e)
{
await ConnectAndGetNewClientV3Async();
Expand All @@ -106,12 +105,13 @@ private async void OnTimedEventAuthRefresh(object sender, ElapsedEventArgs e)
}


public async Task<AzureMediaServicesClient> ConnectAndGetNewClientV3Async(Form callerForm = null)
public async Task<MediaServicesAccountResource> ConnectAndGetNewClientV3Async(Form callerForm = null)
{
if (!credentialsEntry.UseSPAuth)
{
var accounts = await _appInteract.GetAccountsAsync();


try
{
authResult = await _appInteract.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync().ConfigureAwait(false);
Expand All @@ -122,10 +122,21 @@ public async Task<AzureMediaServicesClient> ConnectAndGetNewClientV3Async(Form c
{
try
{
/*
authResult = await _appInteract.AcquireTokenInteractive(scopes)
.WithPrompt(credentialsEntry.PromptUser ? Prompt.ForceLogin : Prompt.SelectAccount)
.WithCustomWebUi(new EmbeddedBrowserCustomWebUI(callerForm ?? _form))
.ExecuteAsync();
*/

if (callerForm != null) // if interactive and not refresh of token
{
authResult = await _appInteract.AcquireTokenInteractive(scopes)
.WithAccount(null) // this already exists in MSAL, but it is more important for WAM
.WithParentActivityOrWindow(callerForm.Handle) // to be able to parent WAM's windows to your app (optional, but highly recommended; not needed on UWP)
.ExecuteAsync();
}


}
catch (MsalException maslException)
Expand Down Expand Up @@ -170,16 +181,33 @@ public async Task<AzureMediaServicesClient> ConnectAndGetNewClientV3Async(Form c
credentials = new TokenCredentials(authResult.AccessToken, "Bearer");
credentialForArmClient = new BearerTokenCredential(authResult.AccessToken);

// new code
var MediaServiceAccount = MediaServicesAccountResource.CreateResourceIdentifier(
subscriptionId: _azureSubscriptionId,
resourceGroupName: credentialsEntry.ResourceGroupName,
accountName: credentialsEntry.AccountName
);
//var credential = new DefaultAzureCredential(includeInteractiveCredentials: true);
var armClient = new ArmClient(credentialForArmClient);


var amsClient = armClient.GetMediaServicesAccountResource(MediaServiceAccount);

AMSclient = await amsClient.GetAsync();


/*
// Getting Media Services account...
AMSclient = new AzureMediaServicesClient(environment.ArmEndpoint, credentials)
{
SubscriptionId = _azureSubscriptionId
};
*/

if (firstTimeAuth)
{
// let's get info on mediaService, specifically to get the location (region)
credentialsEntry.MediaService = await AMSclient.Mediaservices.GetAsync(credentialsEntry.ResourceGroup, credentialsEntry.AccountName);
// credentialsEntry.MediaService = await AMSclient.Mediaservices.GetAsync(credentialsEntry.ResourceGroup, credentialsEntry.AccountName);

// let's refresh the token 3 minutes before it expires
TimerAutoRefreshAuthToken.Interval = (authResult.ExpiresOn.ToUniversalTime() - DateTimeOffset.UtcNow.AddMinutes(3)).TotalMilliseconds;
Expand All @@ -190,7 +218,8 @@ public async Task<AzureMediaServicesClient> ConnectAndGetNewClientV3Async(Form c
}

string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
AMSclient.SetUserAgent("AMSE", version);
//AMSclient.SetUserAgent("AMSE", version);
firstTimeAuth = false;
return AMSclient;
}

Expand All @@ -206,34 +235,36 @@ public static string GetStorageResourceName(string storageId)
return storageId.Split('/')[split.Length - 5];
}

public async Task<Asset> GetAssetAsync(string assetName, CancellationToken token = default)
public async Task<MediaAssetResource> GetAssetAsync(string assetName, CancellationToken token = default)
{
return await AMSclient.Assets.GetAsync(credentialsEntry.ResourceGroup, credentialsEntry.AccountName, assetName, token).ConfigureAwait(false);
return await AMSclient.GetMediaAssetAsync(assetName, token).ConfigureAwait(false);
}

public async Task<Job> GetJobAsync(string transformName, string jobName)
public async Task<MediaJobResource> GetJobAsync(string transformName, string jobName)
{
return await AMSclient.Jobs.GetAsync(credentialsEntry.ResourceGroup, credentialsEntry.AccountName, transformName, jobName).ConfigureAwait(false);
var t = await GetTransformAsync(transformName);
return await t.GetMediaJobAsync(jobName).ConfigureAwait(false);
}

public async Task<Transform> GetTransformAsync(string transformName)
public async Task<MediaTransformResource> GetTransformAsync(string transformName)
{
return await AMSclient.Transforms.GetAsync(credentialsEntry.ResourceGroup, credentialsEntry.AccountName, transformName).ConfigureAwait(false);
return await AMSclient.GetMediaTransformAsync(transformName).ConfigureAwait(false);
}

public async Task<LiveEvent> GetLiveEventAsync(string liveEventName)
public async Task<MediaLiveEventResource> GetLiveEventAsync(string liveEventName)
{
return await AMSclient.LiveEvents.GetAsync(credentialsEntry.ResourceGroup, credentialsEntry.AccountName, liveEventName).ConfigureAwait(false);
return await AMSclient.GetMediaLiveEventAsync(liveEventName).ConfigureAwait(false);
}

public async Task<LiveOutput> GetLiveOutputAsync(string liveEventName, string liveOutputName)
public async Task<MediaLiveOutputResource> GetLiveOutputAsync(string liveEventName, string liveOutputName)
{
return await AMSclient.LiveOutputs.GetAsync(credentialsEntry.ResourceGroup, credentialsEntry.AccountName, liveEventName, liveOutputName).ConfigureAwait(false);
var o = await GetLiveEventAsync(liveEventName);
return await o.GetMediaLiveOutputAsync(liveOutputName).ConfigureAwait(false);
}

public async Task<StreamingEndpoint> GetStreamingEndpointAsync(string seName)
public async Task<StreamingEndpointResource> GetStreamingEndpointAsync(string seName)
{
return await AMSclient.StreamingEndpoints.GetAsync(credentialsEntry.ResourceGroup, credentialsEntry.AccountName, seName).ConfigureAwait(false);
return await AMSclient.GetStreamingEndpointAsync(seName).ConfigureAwait(false);
}
}
}
2 changes: 1 addition & 1 deletion AMSExplorer/AMSClient/AzureEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public AzureEnvironment(AzureEnvType type)
case AzureEnvType.Custom:
DisplayName = "Custom";
ArmEndpoint = null;
ClientApplicationId = string.Empty;
ClientApplicationId = "04b07795-8ddb-461a-bbee-02f9e1bf7b46";
AADSettings = new ActiveDirectoryServiceSettings();
MediaServicesV2Resource = null;
break;
Expand Down
37 changes: 11 additions & 26 deletions AMSExplorer/AMSClient/CredentialsEntryV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
// limitations under the License.
//---------------------------------------------------------------------------------------------

using Microsoft.Azure.Management.Media.Models;
using System;
using System.Security.Cryptography;
using System.Text;

namespace AMSExplorer
{
public class CredentialsEntryV3 : IEquatable<CredentialsEntryV3>
public class CredentialsEntryV4 : IEquatable<CredentialsEntryV4>
{
public MediaService MediaService;
// public MediaServicesAccountData MediaService;

public string ADSPClientId;

Expand All @@ -36,10 +35,16 @@ public class CredentialsEntryV3 : IEquatable<CredentialsEntryV3>
public bool ManualConfig = false;
public bool UseSPAuth = false;
public string Description;
public string AccountName;
public string SubscriptionId;
public string ResourceGroupName;

public CredentialsEntryV3(MediaService mediaService, AzureEnvironment environment, bool promptUser, bool useSPAuth = false, string tenantId = null, bool manualConfig = false, string adSPClientId = null, string clearADSPClientSecret = null)

public CredentialsEntryV4(string accountName, string subscriptionId, string resourceGroupName, AzureEnvironment environment, bool promptUser, bool useSPAuth = false, string tenantId = null, bool manualConfig = false, string adSPClientId = null, string clearADSPClientSecret = null)
{
MediaService = mediaService;
AccountName = accountName;
SubscriptionId = subscriptionId;
ResourceGroupName = resourceGroupName;
Environment = environment;
UseSPAuth = useSPAuth;
PromptUser = promptUser;
Expand All @@ -52,34 +57,14 @@ public CredentialsEntryV3(MediaService mediaService, AzureEnvironment environmen
}
}

public string AccountName => MediaService.Name;

public string ResourceGroup
{
get
{
string[] idParts = MediaService.Id.Split('/');
return idParts[4];
}
}

public string AzureSubscriptionId
{
get
{
string[] idParts = MediaService.Id.Split('/');
return idParts[2];
}
}

// A contract is used to ignore this property when saveing settings to disk
public string ClearADSPClientSecret
{
get => EncryptedADSPClientSecret != null ? DecryptSecret(EncryptedADSPClientSecret) : null;
set => EncryptedADSPClientSecret = (value != null) ? EncryptSecret(value) : null;
}

public bool Equals(CredentialsEntryV3 other)
public bool Equals(CredentialsEntryV4 other)
{
return false;
/* To implement
Expand Down
25 changes: 15 additions & 10 deletions AMSExplorer/AMSExplorer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<Description></Description>
<Copyright>Copyright © 2023</Copyright>
<Company>$(Authors)Microsoft</Company>
<AssemblyVersion>5.6.1.0</AssemblyVersion>
<FileVersion>5.6.1.0</FileVersion>
<AssemblyVersion>5.7.0.0</AssemblyVersion>
<FileVersion>5.7.0.0</FileVersion>
<AppDesignerFolder>Properties</AppDesignerFolder>
<PackageIcon>Azure Explorer.png</PackageIcon>
<PackageIconUrl />
Expand Down Expand Up @@ -88,26 +88,31 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" Version="1.8.0" />
<PackageReference Include="Azure.ResourceManager" Version="1.3.2" />
<PackageReference Include="Azure.ResourceManager.Resources" Version="1.3.1" />
<PackageReference Include="Azure.ResourceManager.Storage" Version="1.1.0" />
<PackageReference Include="Azure.Monitor.Query" Version="1.1.0" />
<PackageReference Include="Azure.ResourceManager" Version="1.4.0" />
<PackageReference Include="Azure.ResourceManager.Media" Version="1.1.0" />
<PackageReference Include="Azure.ResourceManager.Resources" Version="1.4.0" />
<PackageReference Include="Azure.ResourceManager.Storage" Version="1.1.1" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.19.0" />
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.WindowsServer" Version="2.21.0" />
<PackageReference Include="Microsoft.Azure.Management.Media" Version="6.0.0" />
<PackageReference Include="Microsoft.Azure.Management.Storage" Version="25.0.0" />
<PackageReference Include="Microsoft.Azure.Storage.DataMovement" Version="2.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.49.1" />
<PackageReference Include="Microsoft.Identity.Client.Broker" Version="4.50.0-preview" />
<PackageReference Include="Microsoft.Identity.Client.NativeInterop" Version="0.13.6" />
<PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.24" />
<PackageReference Include="Microsoft.Rest.ClientRuntime.Azure.Authentication" Version="2.4.1" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1462.37" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.1" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1587.40" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.27.0" />
<PackageReference Include="System.Linq.Async.Queryable" Version="6.0.1" />
</ItemGroup>

<ItemGroup>
<Compile Update="AssetInfoAudioTrackCreation.cs">
<SubType>Form</SubType>
</Compile>
<Compile Update="Forms-DynamicEncryption\DRM_CreateOrSelectCKPolicy.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
Loading

0 comments on commit 6713fab

Please sign in to comment.