Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add instance parameter to Release/7.0 branch #2410

Merged
merged 2 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.DotNet.MSIdentity.DeveloperCredentials
{
public class DeveloperCredentialsReader
{
public TokenCredential GetDeveloperCredentials(string? username, string? currentApplicationTenantId, IConsoleLogger consoleLogger)
public TokenCredential GetDeveloperCredentials(string? username, string? currentApplicationTenantId, string? instance, IConsoleLogger consoleLogger)
{
#if AzureSDK
* Tried but does not work if another tenant than the home tenant id is specified
Expand All @@ -30,6 +30,7 @@ public TokenCredential GetDeveloperCredentials(string? username, string? current
TokenCredential tokenCredential = new MsalTokenCredential(
currentApplicationTenantId,
username,
instance,
consoleLogger);
return tokenCredential;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public class MsalTokenCredential : TokenCredential
public MsalTokenCredential(
string? tenantId,
string? username,
string? instance,
IConsoleLogger consoleLogger)
{
_consoleLogger = consoleLogger;
TenantId = tenantId ?? "organizations"; // MSA-passthrough
Username = username;
Instance = "https://login.microsoftonline.com";
Instance = instance ?? "https://login.microsoftonline.com"; // default instance
}

private IPublicClientApplication? App { get; set; }
Expand Down Expand Up @@ -71,6 +72,7 @@ private async Task<IPublicClientApplication> GetOrCreateApp()
.Build();

App = PublicClientApplicationBuilder.Create(clientId)
.WithAuthority(Instance, TenantId)
.WithRedirectUri(RedirectUri)
.Build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.DotNet.MSIdentity.Properties;
using Microsoft.DotNet.MSIdentity.Shared;
using Microsoft.DotNet.MSIdentity.Tool;
using Microsoft.DotNet.Scaffolding.Shared;
using Microsoft.Graph;

namespace Microsoft.DotNet.MSIdentity.MicrosoftIdentityPlatformApplication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ProjectDescriptionReader(IEnumerable<string> files)
}

// If projectDescription cannot be inferred, default to Web App
return ProjectDescriptions.FirstOrDefault(p => string.Equals(ProjectTypes.WebApp, p.Identifier));
return ProjectDescriptions.FirstOrDefault(p => string.Equals($"{ProjectTypeIdPrefix}{ProjectTypes.WebApp}", p.Identifier));
}

static readonly JsonSerializerOptions serializerOptionsWithComments = new JsonSerializerOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"ProjectRelativeFolder": "",
"BasedOnProjectDescription": "dotnet-web",
"MatchesForProjectType": [
{
"FileRelativePath": "Startup.cs",
"MatchAny": [ ".AddAzureAD", ".AddMicrosoftIdentityWebApp", ".AddMicrosoftIdentityWebAppAuthentication", "Microsoft.Owin" ]
},
{
"FolderRelativePath": "Pages",
"FileRelativePath": "Index.cshtml",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@
<value>Failed to provision Client Application for Blazor WASM hosted project</value>
</data>
<data name="FailedToRetrieveADObjectsError" xml:space="preserve">
<value>Failed to retrieve all Azure AD/AD B2C objects(apps/service principals</value>
<value>Failed to retrieve all Azure AD/AD B2C objects (apps/service principals), exception: {0}</value>
<comment>0 = error message</comment>
</data>
<data name="FailedToRetrieveApplicationParameters" xml:space="preserve">
<value>Failed to retrieve application parameters.</value>
Expand Down Expand Up @@ -347,4 +348,4 @@
<data name="UpdatingProjectPackages" xml:space="preserve">
<value>Updating project packages ...</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ internal static TokenCredential GetTokenCredential(ProvisioningToolOptions provi
return developerCredentialsReader.GetDeveloperCredentials(
provisioningToolOptions.Username,
currentApplicationTenantId ?? provisioningToolOptions.TenantId,
provisioningToolOptions.Instance,
consoleLogger);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -66,17 +65,17 @@ public GraphObjectRetriever(GraphServiceClient graphServiceClient, IConsoleLogge
nextPage = null;
}
}
catch (ServiceException)
catch (ServiceException se)
{
nextPage = null;
_consoleLogger.LogFailureAndExit(Resources.FailedToRetrieveADObjectsError);
_consoleLogger.LogFailureAndExit(string.Format(Resources.FailedToRetrieveADObjectsError, se.Message));
}
}
}
}
catch (ServiceException)
catch (ServiceException se)
{
_consoleLogger.LogFailureAndExit(Resources.FailedToRetrieveADObjectsError);
_consoleLogger.LogFailureAndExit(string.Format(Resources.FailedToRetrieveADObjectsError, se.Message));
}

return graphObjectsList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public MsAADTool(string commandName, ProvisioningToolOptions provisioningToolOpt
ProvisioningToolOptions = provisioningToolOptions;
CommandName = commandName;
ConsoleLogger = new ConsoleLogger(CommandName, ProvisioningToolOptions.Json);
TokenCredential = new MsalTokenCredential(ProvisioningToolOptions.TenantId, ProvisioningToolOptions.Username, ConsoleLogger);
TokenCredential = new MsalTokenCredential(ProvisioningToolOptions.TenantId, ProvisioningToolOptions.Username, ProvisioningToolOptions.Instance, ConsoleLogger);
GraphServiceClient = new GraphServiceClient(new TokenCredentialAuthenticationProvider(TokenCredential));
AzureManagementAPI = new AzureManagementAuthenticationProvider(TokenCredential);
GraphObjectRetriever = new GraphObjectRetriever(GraphServiceClient, ConsoleLogger);
Expand Down Expand Up @@ -91,13 +91,9 @@ internal async Task<string> PrintApplicationsList()

internal async Task<IList<Application>> GetApplicationsAsync()
{
var graphObjectsList = await GraphObjectRetriever.GetGraphObjects();
if (graphObjectsList is null)
{
ConsoleLogger.LogFailureAndExit(Resources.FailedToRetrieveADObjectsError);
}

IList<Application> applicationList = new List<Application>();

var graphObjectsList = await GraphObjectRetriever.GetGraphObjects(); // Will exit early if call fails
foreach (var graphObj in graphObjectsList!)
{
if (graphObj is Application app)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public string ProjectTypeIdentifier
/// </summary>
public string? TenantId { get; set; }

/// <summary>
/// URL that indicates a directory that MSAL can request tokens from.
/// e.g. https://login.microsoftonline.com/, https://login.microsoftonline.us/
/// </summary>
public string? Instance { get; set; }

/// <summary>
/// Required for the creation of a B2C application.
/// Represents the sign-up/sign-in user flow.
Expand Down
26 changes: 18 additions & 8 deletions tools/dotnet-msidentity/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ internal static Command ListAADAppsCommand() =>
name: Commands.LIST_AAD_APPS_COMMAND,
description: "Lists AAD Applications for a given tenant/username.\n")
{
TenantOption(), UsernameOption(), JsonOption()
TenantOption(), UsernameOption(), InstanceOption(), JsonOption()
};

internal static Command ListServicePrincipalsCommand() =>
new Command(
name: Commands.LIST_SERVICE_PRINCIPALS_COMMAND,
description: "Lists AAD Service Principals.\n")
{
TenantOption(), UsernameOption(), JsonOption()
TenantOption(), UsernameOption(), InstanceOption(), JsonOption()
};

internal static Command ListTenantsCommand() =>
Expand All @@ -204,7 +204,7 @@ internal static Command CreateClientSecretCommand() =>
name: Commands.ADD_CLIENT_SECRET,
description: "Create client secret for an Azure AD or AD B2C app registration.\n")
{
TenantOption(), UsernameOption(), JsonOption(), ClientIdOption(), ProjectFilePathOption(), UpdateUserSecretsOption()
TenantOption(), UsernameOption(), InstanceOption(), JsonOption(), ClientIdOption(), ProjectFilePathOption(), UpdateUserSecretsOption()
};

internal static Command RegisterApplicationCommand() =>
Expand All @@ -213,7 +213,7 @@ internal static Command RegisterApplicationCommand() =>
description: "Register an Azure AD or Azure AD B2C app registration in Azure and update the project." +
"\n\t- Updates the appsettings.json file.\n")
{
TenantOption(), UsernameOption(), JsonOption(), ClientIdOption(), ClientSecretOption(), HostedAppIdUriOption(), ApiClientIdOption(), SusiPolicyIdOption(), ProjectFilePathOption()
TenantOption(), UsernameOption(), InstanceOption(), JsonOption(), ClientIdOption(), ClientSecretOption(), HostedAppIdUriOption(), ApiClientIdOption(), SusiPolicyIdOption(), ProjectFilePathOption()
};

internal static Command UpdateProjectCommand() =>
Expand All @@ -224,23 +224,23 @@ internal static Command UpdateProjectCommand() =>
"\n\t- Updates the Startup.cs file." +
"\n\t- Updates the user secrets.\n")
{
TenantOption(), UsernameOption(), ClientIdOption(), JsonOption(), ProjectFilePathOption(), ConfigUpdateOption(), CodeUpdateOption(), PackagesUpdateOption(), CallsGraphOption(), CallsDownstreamApiOption(), UpdateUserSecretsOption(), RedirectUriOption(), SusiPolicyIdOption()
TenantOption(), UsernameOption(), InstanceOption(), ClientIdOption(), JsonOption(), ProjectFilePathOption(), ConfigUpdateOption(), CodeUpdateOption(), PackagesUpdateOption(), CallsGraphOption(), CallsDownstreamApiOption(), UpdateUserSecretsOption(), RedirectUriOption(), SusiPolicyIdOption()
};

internal static Command UpdateAppRegistrationCommand() =>
new Command(
name: Commands.UPDATE_APP_REGISTRATION_COMMAND,
description: "Update an Azure AD/AD B2C app registration in Azure.\n")
{
TenantOption(), UsernameOption(), JsonOption(), HostedAppIdUriOption(), ClientIdOption(), RedirectUriOption(), EnableIdTokenOption(), EnableAccessToken(), ClientProjectOption(), ApiScopesOption()
TenantOption(), UsernameOption(), InstanceOption(), JsonOption(), HostedAppIdUriOption(), ClientIdOption(), RedirectUriOption(), EnableIdTokenOption(), EnableAccessToken(), ClientProjectOption(), ApiScopesOption()
};

internal static Command CreateAppRegistrationCommand() =>
new Command(
name: Commands.CREATE_APP_REGISTRATION_COMMAND,
description: "Create an Azure AD/AD B2C app registration in Azure.\n")
{
TenantOption(), UsernameOption(), JsonOption(), AppDisplayName(), ProjectFilePathOption(), ProjectType(), ClientProjectOption()
TenantOption(), UsernameOption(), InstanceOption(), JsonOption(), AppDisplayName(), ProjectFilePathOption(), ProjectType(), ClientProjectOption()
};

internal static Command UnregisterApplicationCommand() =>
Expand All @@ -250,7 +250,7 @@ internal static Command UnregisterApplicationCommand() =>
description: "Unregister an Azure AD or Azure AD B2C app registration in Azure." +
"\n\t- Updates the appsettings.json file.\n")
{
TenantOption(), UsernameOption(), JsonOption(), HostedAppIdUriOption(), ProjectFilePathOption(), ClientIdOption()
TenantOption(), UsernameOption(), InstanceOption(), JsonOption(), HostedAppIdUriOption(), ProjectFilePathOption(), ClientIdOption()
};

private static Option JsonOption() =>
Expand Down Expand Up @@ -435,5 +435,15 @@ private static Option UsernameOption() =>
{
IsRequired = false
};


private static Option InstanceOption() =>
new Option<string>(
aliases: new[] { "-i", "--instance" },
description: "Instance where the Azure AD or Azure AD B2C tenant is located.\n" +
"If not specified, will default to https://login.microsoftonline.com/")
{
IsRequired = false
};
}
}