diff options
Diffstat (limited to 'Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs')
| -rw-r--r-- | Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs b/Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs index 0d8b2ccfc..2006b492f 100644 --- a/Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs +++ b/Software/Visual_Studio/Azure/Tango.AzureUtils/AzureUtilsAuthenticationFactory.cs @@ -3,8 +3,10 @@ using Microsoft.Azure.Management.ResourceManager.Fluent; using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http; using System.Text; using System.Threading.Tasks; +using Tango.AzureUtils.Web; namespace Tango.AzureUtils { @@ -19,13 +21,18 @@ namespace Tango.AzureUtils SubscriptionID = "10c8aa60-3b15-4e0d-b412-6aeef90e5e91" }; - public static void SetCredentials(AzureUtilsCredentials credentials) + public static void SetGlobalCredentials(AzureUtilsCredentials credentials) { _credentials = credentials; } public static Task<IAzure> AuthenticateOrGetAsync() { + if (_credentials == null) + { + throw new NullReferenceException("Credentials were not set."); + } + if (_azure == null) { return Task.Factory.StartNew<IAzure>(() => @@ -46,7 +53,45 @@ namespace Tango.AzureUtils } } - public static AzureUtilsCredentials GetCredentials() + public static Task<IAzure> AuthenticateOrGetAsync(AzureUtilsCredentials credentials) + { + return Task.Factory.StartNew<IAzure>(() => + { + var creds = SdkContext.AzureCredentialsFactory.FromServicePrincipal( + credentials.ClientID, + credentials.ClientSecret, + credentials.TenantID, + AzureEnvironment.AzureGlobalCloud); + + _azure = Azure.Authenticate(creds).WithSubscription(credentials.SubscriptionID); + return _azure; + }); + } + + public static async Task<IAzure> AuthenticateOrGetAsync(String gatewayUrl, String email, String password) + { + using (var http = new HttpClient()) + { + AzureUtilsWebClient client = new AzureUtilsWebClient(gatewayUrl, http); + var response = await client.LoginAsync(new LoginRequest() + { + Email = email, + Password = password + }); + + http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", response.AccessToken); + var c = await client.GetCredentialsAsync(); + return await AuthenticateOrGetAsync(new AzureUtilsCredentials() + { + ClientID = c.ClientID, + ClientSecret = c.ClientSecret, + TenantID = c.TenantID, + SubscriptionID = c.SubscriptionID + }); + } + } + + public static AzureUtilsCredentials GetGlobalCredentials() { return _credentials; } |
