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

added create-app-registration command #1564

Merged
merged 4 commits into from
Apr 27, 2021
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 @@ -107,17 +107,17 @@ public string? Domain1
/// <summary>
/// The project is a web API.
/// </summary>
public bool IsWebApi { get; set; }
public bool? IsWebApi { get; set; }

/// <summary>
/// The project is a web app.
/// </summary>
public bool IsWebApp { get; set; }
public bool? IsWebApp { get; set; }

/// <summary>
/// The project is a blazor web assembly.
/// </summary>
public bool IsBlazorWasm { get; set; }
public bool? IsBlazorWasm { get; set; }

/// <summary>
/// The app calls Microsoft Graph.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ private static void ProcessProject(

private static void PostProcessWebUris(ProjectAuthenticationSettings projectAuthenticationSettings)
{
bool isBlazorWasm = projectAuthenticationSettings.ApplicationParameters.IsBlazorWasm
&& !projectAuthenticationSettings.ApplicationParameters.IsWebApp;
bool isBlazorWasm = projectAuthenticationSettings.ApplicationParameters.IsBlazorWasm.HasValue &&
projectAuthenticationSettings.ApplicationParameters.IsBlazorWasm.Value &&
projectAuthenticationSettings.ApplicationParameters.IsWebApp.HasValue &&
!projectAuthenticationSettings.ApplicationParameters.IsWebApp.Value;
string callbackPath = projectAuthenticationSettings.ApplicationParameters.CallbackPath ?? "/signin-oidc";
if (isBlazorWasm)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.DotNet.MSIdentity.AuthenticationParameters;
using Microsoft.DotNet.MSIdentity.Project;
using Microsoft.DotNet.MSIdentity.Tool;
using Microsoft.Extensions.Internal;

namespace Microsoft.DotNet.MSIdentity.CodeReaderWriter
{
public static class CodeWriter
{
internal static void WriteConfiguration(Summary summary, IEnumerable<Replacement> replacements, ApplicationParameters reconciledApplicationParameters, bool jsonOutput)
internal static void WriteConfiguration(Summary summary, IEnumerable<Replacement> replacements, ApplicationParameters reconciledApplicationParameters, IConsoleLogger consoleLogger)
{
foreach (var replacementsInFile in replacements.GroupBy(r => r.FilePath))
{
Expand All @@ -23,7 +23,7 @@ internal static void WriteConfiguration(Summary summary, IEnumerable<Replacement
bool updated = false;
foreach (Replacement r in replacementsInFile.OrderByDescending(r => r.Index))
{
string? replaceBy = ComputeReplacement(r.ReplaceBy, reconciledApplicationParameters, jsonOutput);
string? replaceBy = ComputeReplacement(r.ReplaceBy, reconciledApplicationParameters, consoleLogger);
if (replaceBy != null && replaceBy!=r.ReplaceFrom)
{
int index = fileContent.IndexOf(r.ReplaceFrom /*, r.Index*/);
Expand Down Expand Up @@ -51,16 +51,16 @@ internal static void WriteConfiguration(Summary summary, IEnumerable<Replacement
}

//TODO : Add integration tests for testing instead of mocking for unit tests.
public static void AddUserSecrets(bool isB2C, string projectPath, string value, bool jsonOutput)
public static void AddUserSecrets(bool isB2C, string projectPath, string value, IConsoleLogger consoleLogger)
{
//init regardless. If it's already initiated, dotnet-user-secrets confirms it.
InitUserSecrets(projectPath, jsonOutput);
InitUserSecrets(projectPath, consoleLogger);
string section = isB2C ? "AzureADB2C" : "AzureAD";
string key = $"{section}:ClientSecret";
SetUserSecerets(projectPath, key, value, jsonOutput);
SetUserSecerets(projectPath, key, value, consoleLogger);
}

public static void InitUserSecrets(string projectPath, bool jsonOutput)
public static void InitUserSecrets(string projectPath, IConsoleLogger consoleLogger)
{
var errors = new List<string>();
var output = new List<string>();
Expand All @@ -75,11 +75,7 @@ public static void InitUserSecrets(string projectPath, bool jsonOutput)
}

arguments.Add("init");

if (!jsonOutput)
{
Console.Write("\nInitializing User Secrets . . . ");
}
consoleLogger.LogMessage("\nInitializing User Secrets . . . ", LogMessageType.Error);

var result = Command.CreateDotNet(
"user-secrets",
Expand All @@ -90,22 +86,16 @@ public static void InitUserSecrets(string projectPath, bool jsonOutput)

if (result.ExitCode != 0)
{
if (!jsonOutput)
{
Console.Write("FAILED\n");
}
consoleLogger.LogMessage("FAILED\n", LogMessageType.Error, removeNewLine: true);
throw new Exception("Error while running dotnet-user-secrets init");
}
else
{
if (!jsonOutput)
{
Console.Write("SUCCESS\n");
}
consoleLogger.LogMessage("SUCCESS\n", removeNewLine: true);
}
}

public static void AddPackage(string packageName, string packageVersion, string tfm, bool jsonOutput)
public static void AddPackage(string packageName, string packageVersion, string tfm, IConsoleLogger consoleLogger)
{
if (!string.IsNullOrEmpty(packageName) && ((!string.IsNullOrEmpty(packageVersion)) || (!string.IsNullOrEmpty(tfm))))
{
Expand All @@ -130,10 +120,8 @@ public static void AddPackage(string packageName, string packageVersion, string
arguments.Add("-f");
arguments.Add(tfm);
}
if (!jsonOutput)
{
Console.Write($"\nAdding package {packageName} . . . ");
}

consoleLogger.LogMessage($"\nAdding package {packageName} . . . ");

var result = Command.CreateDotNet(
"add",
Expand All @@ -144,19 +132,12 @@ public static void AddPackage(string packageName, string packageVersion, string

if (result.ExitCode != 0)
{
if (!jsonOutput)
{
Console.Write("FAILED\n");
Console.WriteLine($"Failed to add package {packageName}");
}
consoleLogger.LogMessage("FAILED\n", removeNewLine: true);
consoleLogger.LogMessage($"Failed to add package {packageName}");
}
else
{
if (!jsonOutput)
{
Console.Write("SUCCESS\n");
}

consoleLogger.LogMessage("SUCCESS\n");
}
}
}
Expand All @@ -166,7 +147,7 @@ private static bool IsTfmPreRelease(string tfm)
return tfm.Equals("net6.0", StringComparison.OrdinalIgnoreCase);
}

private static void SetUserSecerets(string projectPath, string key, string value, bool jsonOutput)
private static void SetUserSecerets(string projectPath, string key, string value, IConsoleLogger consoleLogger)
{
var errors = new List<string>();
var output = new List<string>();
Expand Down Expand Up @@ -196,14 +177,11 @@ private static void SetUserSecerets(string projectPath, string key, string value
}
else
{
if (!jsonOutput)
{
Console.WriteLine($"\nAdded {key} to user secrets.\n");
}
consoleLogger.LogMessage($"\nAdded {key} to user secrets.\n");
}
}

private static string? ComputeReplacement(string replaceBy, ApplicationParameters reconciledApplicationParameters, bool jsonOutput)
private static string? ComputeReplacement(string replaceBy, ApplicationParameters reconciledApplicationParameters, IConsoleLogger consoleLogger)
{
string? replacement = replaceBy;
switch(replaceBy)
Expand All @@ -212,7 +190,7 @@ private static void SetUserSecerets(string projectPath, string key, string value
string? password = reconciledApplicationParameters.PasswordCredentials.LastOrDefault();
if (!string.IsNullOrEmpty(reconciledApplicationParameters.SecretsId) && !string.IsNullOrEmpty(password))
{
AddUserSecrets(reconciledApplicationParameters.IsB2C, reconciledApplicationParameters.ProjectPath ?? string.Empty, password, jsonOutput);
AddUserSecrets(reconciledApplicationParameters.IsB2C, reconciledApplicationParameters.ProjectPath ?? string.Empty, password, consoleLogger);
}
else
{
Expand Down
Loading