Skip to content

Commit

Permalink
added --list-tenants internal command + unit test (#1538)
Browse files Browse the repository at this point in the history
* added --list-tenants internal command + unit test

* cleaning up imports.

* PR fixes
  • Loading branch information
deepchoudhery authored Mar 30, 2021
1 parent 4d0fcb6 commit be2470e
Show file tree
Hide file tree
Showing 22 changed files with 344 additions and 68 deletions.
21 changes: 21 additions & 0 deletions All.sln
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.MsIdentity
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-msidentity", "tools\dotnet-msidentity\dotnet-msidentity.csproj", "{1DB98E3A-0749-4ACE-9EF2-EF6BC2C6648B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.MsIdentity.UnitTests.Tests", "test\MsIdentityScaffolding\Microsoft.DotNet.MsIdentity.UnitTests.Tests\Microsoft.DotNet.MsIdentity.UnitTests.Tests.csproj", "{0711777C-64C2-49DC-A9CE-67BD641AA7A6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
debug_x86|Any CPU = debug_x86|Any CPU
Expand Down Expand Up @@ -705,6 +707,24 @@ Global
{1DB98E3A-0749-4ACE-9EF2-EF6BC2C6648B}.Release|x64.Build.0 = Release|Any CPU
{1DB98E3A-0749-4ACE-9EF2-EF6BC2C6648B}.Release|x86.ActiveCfg = Release|Any CPU
{1DB98E3A-0749-4ACE-9EF2-EF6BC2C6648B}.Release|x86.Build.0 = Release|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.debug_x86|Any CPU.ActiveCfg = Debug|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.debug_x86|Any CPU.Build.0 = Debug|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.debug_x86|x64.ActiveCfg = Debug|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.debug_x86|x64.Build.0 = Debug|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.debug_x86|x86.ActiveCfg = Debug|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.debug_x86|x86.Build.0 = Debug|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.Debug|x64.ActiveCfg = Debug|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.Debug|x64.Build.0 = Debug|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.Debug|x86.ActiveCfg = Debug|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.Debug|x86.Build.0 = Debug|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.Release|Any CPU.Build.0 = Release|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.Release|x64.ActiveCfg = Release|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.Release|x64.Build.0 = Release|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.Release|x86.ActiveCfg = Release|Any CPU
{0711777C-64C2-49DC-A9CE-67BD641AA7A6}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -747,6 +767,7 @@ Global
{35BA8FDE-4AF3-4EF5-94B3-123B4702AF7E} = {3354705C-5C05-41C3-95E1-31A45141A6C2}
{6B863A66-CFFC-42B4-9316-13B168CB6C62} = {49D385E7-A045-4C54-868F-CEA8F67EDBA8}
{1DB98E3A-0749-4ACE-9EF2-EF6BC2C6648B} = {4B56CCB9-2693-492A-A0C5-B4D15DDF00A4}
{0711777C-64C2-49DC-A9CE-67BD641AA7A6} = {49D385E7-A045-4C54-868F-CEA8F67EDBA8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {26BCDDB3-5505-4903-9D87-C942ED0D03E6}
Expand Down
7 changes: 4 additions & 3 deletions MSIdentityScaffolding.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
"solution": {
"path": "All.sln",
"projects": [
"src\\MsIdentityScaffolding\\Microsoft.DotNet.MsIdentity\\Microsoft.DotNet.MsIdentity.csproj",
"tools\\dotnet-msidentity\\dotnet-msidentity.csproj",
"test\\MsIdentityScaffolding\\Microsoft.DotNet.MsIdentity.Tests\\Microsoft.DotNet.MsIdentity.Tests.csproj"
"src\\MsIdentityScaffolding\\Microsoft.DotNet.MsIdentity\\Microsoft.DotNet.MsIdentity.csproj",
"test\\MsIdentityScaffolding\\Microsoft.DotNet.MsIdentity.Tests\\Microsoft.DotNet.MsIdentity.Tests.csproj",
"test\\MsIdentityScaffolding\\Microsoft.DotNet.MsIdentity.UnitTests.Tests\\Microsoft.DotNet.MsIdentity.UnitTests.Tests.csproj",
"tools\\dotnet-msidentity\\dotnet-msidentity.csproj"
]
}
}
5 changes: 2 additions & 3 deletions scripts/install-msidentity.cmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
set VERSION=0.0.1-dev
set DEFAULT_NUPKG_PATH=%userprofile%/.nuget/packages
set SRC_DIR=%cd%
set NUPKG=artifacts/packages/Debug/Shipping/

Expand All @@ -8,8 +7,8 @@ call rd /Q /S artifacts

call dotnet build MsIdentityScaffolding.slnf
call dotnet pack MsIdentityScaffolding.slnf
call dotnet tool uninstall -g dotnet-msidentity
call dotnet tool uninstall -g Microsoft.dotnet-msidentity

call cd %SRC_DIR%/%NUPKG%
call dotnet tool install -g dotnet-msidentity --add-source %SRC_DIR%\%NUPKG% --version %VERSION%
call dotnet tool install -g Microsoft.dotnet-msidentity --add-source %SRC_DIR%\%NUPKG% --version %VERSION%
call cd %SRC_DIR%
4 changes: 2 additions & 2 deletions scripts/install-msidentity.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ NUPKG=artifacts/packages/Debug/Shipping/
pkill -f dotnet
rm -rf artifacts
./build.sh
dotnet tool uninstall -g dotnet-msidentity
dotnet tool uninstall -g Microsoft.dotnet-msidentity
cd $SRC_DIR/$NUPKG
dotnet tool install -g dotnet-msidentity --add-source $SRC_DIR/$NUPKG --version $VERSION
dotnet tool install -g Microsoft.dotnet-msidentity --add-source $SRC_DIR/$NUPKG --version $VERSION
cd "$OLDPWD"
4 changes: 2 additions & 2 deletions scripts/install-scaffold.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ call taskkill /f /im dotnet.exe
call rd /Q /S artifacts
call dotnet build Scaffolding.slnf
call dotnet pack Scaffolding.slnf
call dotnet tool uninstall -g dotnet-scaffold
call dotnet tool uninstall -g Microsoft.dotnet-scaffold

call cd %DEFAULT_NUPKG_PATH%
call rd /Q /S microsoft.visualstudio.web.codegeneration
Expand All @@ -19,5 +19,5 @@ call rd /Q /S microsoft.visualstudio.web.codegeneration.utils
call rd /Q /S microsoft.visualstudio.web.codegenerators.mvc

call cd %SRC_DIR%/%NUPKG%
call dotnet tool install -g dotnet-scaffold --add-source %SRC_DIR%\%NUPKG% --version %VERSION%
call dotnet tool install -g Microsoft.dotnet-scaffold --add-source %SRC_DIR%\%NUPKG% --version %VERSION%
call cd %SRC_DIR%
4 changes: 2 additions & 2 deletions scripts/install-scaffold.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ NUPKG=artifacts/packages/Debug/Shipping/
pkill -f dotnet
rm -rf artifacts
./build.sh
dotnet tool uninstall -g dotnet-scaffold
dotnet tool uninstall -g Microsoft.dotnet-scaffold
cd $DEFAULT_NUPKG_PATH
rm -rf microsoft.visualstudio.web.codegeneration
rm -rf microsoft.visualstudio.web.codegeneration.contracts
Expand All @@ -21,5 +21,5 @@ rm -rf microsoft.visualstudio.web.codegeneration.templating
rm -rf microsoft.visualstudio.web.codegeneration.utils
rm -rf microsoft.visualstudio.web.codegenerators.mvc
cd "$OLDPWD"/$NUPKG
dotnet tool install -g dotnet-scaffold --add-source $SRC_DIR/$NUPKG --version $VERSION
dotnet tool install -g Microsoft.dotnet-scaffold --add-source $SRC_DIR/$NUPKG --version $VERSION
cd "$OLDPWD"
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;
}
}
}
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();
}
}
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; }
}
}
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")]
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ public class Commands
{
public const string LIST_AAD_APPS_COMMAND = "--list-aad-apps";
public const string LIST_SERVICE_PRINCIPALS_COMMAND = "--list-service-principals";
public const string LIST_TENANTS_COMMAND = "--list-tenants";
public const string REGISTER_APPLICATIION_COMMAND = "--register-application";
public const string UPDATE_APPLICATION_COMMAND = "--update-application";
public const string UNREGISTER_COMMAND = "--unregister";
public const string VALIDATE_APP_PARAMS_COMMAND = "--validate-app-params";
}
}
}
Loading

0 comments on commit be2470e

Please sign in to comment.