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 --add-client-secret and --update-app-registration #1557

Merged
merged 7 commits into from
Apr 22, 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
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.DotNet.MSIdentity.Project;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.DotNet.MSIdentity.CodeReaderWriter
{
public static class CodeWriter
{
internal static void WriteConfiguration(Summary summary, IEnumerable<Replacement> replacements, ApplicationParameters reconcialedApplicationParameters)
internal static void WriteConfiguration(Summary summary, IEnumerable<Replacement> replacements, ApplicationParameters reconciledApplicationParameters, bool jsonOutput)
{
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, reconcialedApplicationParameters);
string? replaceBy = ComputeReplacement(r.ReplaceBy, reconciledApplicationParameters, jsonOutput);
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)
public static void AddUserSecrets(bool isB2C, string projectPath, string value, bool jsonOutput)
{
//init regardless. If it's already initiated, dotnet-user-secrets confirms it.
InitUserSecrets(projectPath);
InitUserSecrets(projectPath, jsonOutput);
string section = isB2C ? "AzureADB2C" : "AzureAD";
string key = $"{section}:ClientSecret";
SetUserSecerets(projectPath, key, value);
SetUserSecerets(projectPath, key, value, jsonOutput);
}

private static void InitUserSecrets(string projectPath)
public static void InitUserSecrets(string projectPath, bool jsonOutput)
{
var errors = new List<string>();
var output = new List<string>();
Expand All @@ -74,13 +74,13 @@ private static void InitUserSecrets(string projectPath)
arguments.Add(projectPath);
}

Debugger.Launch();
if (arguments.Contains("-uus"))
{
arguments.Add("init");

if (!jsonOutput)
{
Console.Write("\nInitializing User Secrets . . . ");
}

arguments.Add("init");
var result = Command.CreateDotNet(
"user-secrets",
arguments.ToArray())
Expand All @@ -90,11 +90,83 @@ private static void InitUserSecrets(string projectPath)

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

private static void SetUserSecerets(string projectPath, string key, string value)
public static void AddPackage(string packageName, string packageVersion, string tfm, bool jsonOutput)
{
if (!string.IsNullOrEmpty(packageName) && ((!string.IsNullOrEmpty(packageVersion)) || (!string.IsNullOrEmpty(tfm))))
{
var errors = new List<string>();
var output = new List<string>();
var arguments = new List<string>();
arguments.Add("package");
arguments.Add(packageName);
if (!string.IsNullOrEmpty(packageVersion))
{
arguments.Add("-v");
arguments.Add(packageVersion);
}

if (IsTfmPreRelease(tfm))
{
arguments.Add("--prerelease");
}

if (!string.IsNullOrEmpty(tfm))
{
arguments.Add("-f");
arguments.Add(tfm);
}
if (!jsonOutput)
{
Console.Write($"\nAdding package {packageName} . . . ");
}

var result = Command.CreateDotNet(
"add",
arguments.ToArray())
.OnErrorLine(e => errors.Add(e))
.OnOutputLine(o => output.Add(o))
deepchoudhery marked this conversation as resolved.
Show resolved Hide resolved
.Execute();

if (result.ExitCode != 0)
{
if (!jsonOutput)
{
Console.Write("FAILED\n");
Console.WriteLine($"Failed to add package {packageName}");
deepchoudhery marked this conversation as resolved.
Show resolved Hide resolved
}
}
else
{
if (!jsonOutput)
{
Console.Write("SUCCESS\n");
}

}
}
}

private static bool IsTfmPreRelease(string tfm)
{
return tfm.Equals("net6.0", StringComparison.OrdinalIgnoreCase);
deepchoudhery marked this conversation as resolved.
Show resolved Hide resolved
}

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

private static string? ComputeReplacement(string replaceBy, ApplicationParameters reconciledApplicationParameters)
private static string? ComputeReplacement(string replaceBy, ApplicationParameters reconciledApplicationParameters, bool jsonOutput)
{
string? replacement = replaceBy;
switch(replaceBy)
Expand All @@ -137,7 +212,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);
AddUserSecrets(reconciledApplicationParameters.IsB2C, reconciledApplicationParameters.ProjectPath ?? string.Empty, password, jsonOutput);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.DotNet.MSIdentity.AuthenticationParameters;
Expand All @@ -9,7 +9,7 @@ namespace Microsoft.DotNet.MSIdentity.CodeReaderWriter
{
public class ProjectAuthenticationSettings
{
public ProjectAuthenticationSettings(ProjectDescription projectDescription)
public ProjectAuthenticationSettings(ProjectDescription? projectDescription = null)
{
ProjectDescription = projectDescription;
}
Expand All @@ -18,6 +18,6 @@ public ProjectAuthenticationSettings(ProjectDescription projectDescription)

public List<Replacement> Replacements { get; } = new List<Replacement>();

public ProjectDescription ProjectDescription { get; private set; }
public ProjectDescription? ProjectDescription { get; private set; }
}
}
Loading