-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added --list-tenants internal command + unit test (#1538)
* added --list-tenants internal command + unit test * cleaning up imports. * PR fixes
- Loading branch information
1 parent
4d0fcb6
commit be2470e
Showing
22 changed files
with
344 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...Microsoft.DotNet.MsIdentity/DeveloperCredentials/AzureManagementAuthenticationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using Azure.Core; | ||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.DotNet.MsIdentity.DeveloperCredentials | ||
{ | ||
internal class AzureManagementAuthenticationProvider : IAzureManagementAuthenticationProvider | ||
{ | ||
readonly TokenCredential _tokenCredentials; | ||
readonly string[] _initialScopes; | ||
private const string AzureManagementAPIDefault = "https://management.azure.com/.default"; | ||
private const string AzureManagementTenantsAPI = "https://management.azure.com/tenants?api-version=2020-01-01"; | ||
|
||
public AzureManagementAuthenticationProvider(TokenCredential tokenCredentials) | ||
{ | ||
_tokenCredentials = tokenCredentials; | ||
_initialScopes = new string[] { AzureManagementAPIDefault }; | ||
} | ||
|
||
private async Task<HttpRequestMessage> AuthenticateRequestAsync(HttpRequestMessage request) | ||
{ | ||
HttpRequestMessage authenticatedRequest = request; | ||
TokenRequestContext context = new TokenRequestContext(_initialScopes); | ||
AccessToken token = await _tokenCredentials.GetTokenAsync(context, CancellationToken.None); | ||
authenticatedRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.Token); | ||
return authenticatedRequest; | ||
} | ||
|
||
public async Task<string> ListTenantsAsync() | ||
{ | ||
string content = string.Empty; | ||
var httpRequest = new HttpRequestMessage() | ||
{ | ||
RequestUri = new Uri(AzureManagementTenantsAPI), | ||
Method = HttpMethod.Get | ||
}; | ||
|
||
httpRequest = await AuthenticateRequestAsync(httpRequest); | ||
using (var client = new HttpClient()) | ||
{ | ||
var task = await client.SendAsync(httpRequest) | ||
.ContinueWith(async (taskWithMssg) => | ||
{ | ||
var response = taskWithMssg.Result; | ||
response.EnsureSuccessStatusCode(); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
content = await response.Content.ReadAsStringAsync(); | ||
} | ||
}); | ||
await task; | ||
} | ||
return content; | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...icrosoft.DotNet.MsIdentity/DeveloperCredentials/IAzureManagementAuthenticationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.DotNet.MsIdentity.DeveloperCredentials | ||
{ | ||
public interface IAzureManagementAuthenticationProvider | ||
{ | ||
Task<string> ListTenantsAsync(); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions
10
...ityScaffolding/Microsoft.DotNet.MsIdentity/MicrosoftIdentityPlatform/TenantInformation.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Microsoft.DotNet.MsIdentity.MicrosoftIdentityPlatformApplication | ||
{ | ||
internal class TenantInformation | ||
{ | ||
public string? TenantId { get; set; } | ||
public string? DisplayName { get; set; } | ||
public string? DefaultDomain { get; set; } | ||
public string? TenantType { get; set; } | ||
} | ||
} |
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
src/MsIdentityScaffolding/Microsoft.DotNet.MsIdentity/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Reflection; | ||
using System.Resources; | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Microsoft.DotNet.MsIdentity.UnitTests.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] | ||
[assembly: InternalsVisibleTo("dotnet-msidentity, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.