-
Notifications
You must be signed in to change notification settings - Fork 696
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to allow users to input password through commandline to s…
…ign command (#1824) ## Bug Fixes: NuGet/Home#5904 Regression: No ## Fix Details: This PR adds support in sign command to allow users to interactively type pfx file password securely. To allow this I have added `IPasswordProvider.cs` and its implementation `ConsolePasswordProvider.cs` that allows `SignCommandRunner` to read password from console in `-NonInteractive` mode. The underlying implementation comes from `IConsole.ReadSecureString`. I have also added some tests using pfx files and done some cleanup in the test code. ## Testing/Validation Tests Added: Yes Validation done: manual testing with pfx files.
- Loading branch information
Ankit Mishra
authored
Nov 29, 2017
1 parent
427fc2c
commit 065262c
Showing
10 changed files
with
435 additions
and
46 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
46 changes: 46 additions & 0 deletions
46
src/NuGet.Clients/NuGet.CommandLine/ConsolePasswordProvider.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,46 @@ | ||
// 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; | ||
using System.Globalization; | ||
using System.Security; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NuGet.Commands.SignCommand; | ||
|
||
namespace NuGet.CommandLine | ||
{ | ||
/// <summary> | ||
/// Allows requesting a user to input their password through Console. | ||
/// </summary> | ||
internal class ConsolePasswordProvider : IPasswordProvider | ||
{ | ||
private IConsole _console; | ||
|
||
public ConsolePasswordProvider(IConsole console) | ||
{ | ||
_console = console ?? throw new ArgumentNullException(nameof(console)); | ||
} | ||
|
||
#if IS_DESKTOP | ||
/// <summary> | ||
/// Requests user to input password and returns it as a SecureString on Console. | ||
/// </summary> | ||
/// <param name="filePath">Path to the file that needs a password to open.</param> | ||
/// <param name="token">Cancellation token.</param> | ||
/// <returns>SecureString containing the user input password. The SecureString should be disposed after use.</returns> | ||
public Task<SecureString> GetPassword(string filePath, CancellationToken token) | ||
{ | ||
token.ThrowIfCancellationRequested(); | ||
|
||
var password = new SecureString(); | ||
|
||
_console.WriteLine(string.Format(CultureInfo.CurrentCulture, NuGetResources.ConsolePasswordProvider_DisplayFile, filePath)); | ||
_console.Write(NuGetResources.ConsolePasswordProvider_PromptForPassword); | ||
_console.ReadSecureString(password); | ||
|
||
return Task.FromResult(password); | ||
} | ||
#endif | ||
} | ||
} |
20 changes: 19 additions & 1 deletion
20
src/NuGet.Clients/NuGet.CommandLine/NuGetResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
24 changes: 24 additions & 0 deletions
24
src/NuGet.Core/NuGet.Commands/SignCommand/IPasswordProvider.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,24 @@ | ||
// 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; | ||
using System.Security; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace NuGet.Commands.SignCommand | ||
{ | ||
public interface IPasswordProvider | ||
{ | ||
// Currently there is no cross platform interactive scenario | ||
#if IS_DESKTOP | ||
/// <summary> | ||
/// Requests user to input password and returns it as a SecureString. | ||
/// </summary> | ||
/// <param name="filePath">Path to the file that needs a password to open.</param> | ||
/// <param name="token">Cancellation token.</param> | ||
/// <returns>SecureString containing the user input password. The SecureString should be disposed after use.</returns> | ||
Task<SecureString> GetPassword(string filePath, CancellationToken token); | ||
#endif | ||
} | ||
} |
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
Oops, something went wrong.