-
Notifications
You must be signed in to change notification settings - Fork 5
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
Improve question MFA prompt #458
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c7385ba
Prompt with question text for security question MFA
boarnoah a9a716f
Mask input for security question MFA
boarnoah 3b50357
De-serialize MFA factors to different types (#459)
boarnoah bcd9b18
Use a different separator now that MFA factors have pretty names
boarnoah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ internal interface IConsolePrompter { | |
string PromptAccount( string[] accounts ); | ||
string PromptRole( string[] roles ); | ||
OktaMfaFactor SelectMfa( OktaMfaFactor[] mfaOptions ); | ||
string GetMfaResponse( string mfaInputPrompt ); | ||
string GetMfaResponse( string mfaInputPrompt, bool maskInput ); | ||
} | ||
|
||
internal class ConsolePrompter : IConsolePrompter { | ||
|
@@ -64,73 +64,9 @@ string IConsolePrompter.PromptUser( bool allowEmptyInput ) { | |
} | ||
|
||
string IConsolePrompter.PromptPassword( string user, string org ) { | ||
Func<char> readKey; | ||
if( IS_WINDOWS ) { | ||
// On Windows, Console.ReadKey calls native console API, and will fail without a console attached | ||
if( Console.IsInputRedirected ) { | ||
Console.Error.WriteLine( """ | ||
====== WARNING ====== | ||
Input to BMX is redirected. Password input may be displayed on screen! | ||
If you're using mintty (with Git Bash, Cygwin, MSYS2 etc.), consider switching | ||
to Windows Terminal for a better experience. | ||
If you must use mintty, prefix your bmx command with 'winpty '. | ||
===================== | ||
""" ); | ||
readKey = () => (char)_stdinReader.Read(); | ||
} else { | ||
readKey = () => Console.ReadKey( intercept: true ).KeyChar; | ||
} | ||
} else { | ||
readKey = () => (char)_stdinReader.Read(); | ||
} | ||
|
||
Console.Error.WriteLine( $"{ParameterDescriptions.Org}: {org}" ); | ||
Console.Error.WriteLine( $"{ParameterDescriptions.User}: {user}" ); | ||
Console.Error.Write( $"{ParameterDescriptions.Password}: " ); | ||
|
||
string? originalTerminalSettings = null; | ||
try { | ||
if( !IS_WINDOWS ) { | ||
originalTerminalSettings = GetCurrentTerminalSettings(); | ||
EnableTerminalRawMode(); | ||
} | ||
|
||
var passwordBuilder = new StringBuilder(); | ||
while( true ) { | ||
char key = readKey(); | ||
|
||
if( key == CTRL_C ) { | ||
// Ctrl+C should terminate the program. | ||
// Using an empty string as the exception message because this message is displayed to the user, | ||
// but the user doesn't need to see anything when they themselves ended the program. | ||
throw new BmxException( string.Empty ); | ||
} | ||
if( key == '\n' || key == '\r' ) { | ||
// when the terminal is in raw mode, writing \r is needed to start the new line properly | ||
Console.Error.Write( "\r\n" ); | ||
return passwordBuilder.ToString(); | ||
} | ||
|
||
if( key == CTRL_U ) { | ||
string moveLeftString = new( '\b', passwordBuilder.Length ); | ||
string emptyString = new( ' ', passwordBuilder.Length ); | ||
Console.Error.Write( moveLeftString + emptyString + moveLeftString ); | ||
passwordBuilder.Clear(); | ||
} else | ||
// The backspace key is received as the DEL character in raw mode | ||
if( ( key == '\b' || key == DEL ) && passwordBuilder.Length > 0 ) { | ||
Console.Error.Write( "\b \b" ); | ||
passwordBuilder.Length--; | ||
} else if( !char.IsControl( key ) ) { | ||
Console.Error.Write( '*' ); | ||
passwordBuilder.Append( key ); | ||
} | ||
} | ||
} finally { | ||
if( !IS_WINDOWS && !string.IsNullOrEmpty( originalTerminalSettings ) ) { | ||
SetTerminalSettings( originalTerminalSettings ); | ||
} | ||
} | ||
return GetMaskedInput( $"{ParameterDescriptions.Password}: " ); | ||
} | ||
|
||
int? IConsolePrompter.PromptDuration() { | ||
|
@@ -194,12 +130,12 @@ OktaMfaFactor IConsolePrompter.SelectMfa( OktaMfaFactor[] mfaOptions ) { | |
} | ||
|
||
if( mfaOptions.Length == 1 ) { | ||
Console.Error.WriteLine( $"MFA method: {mfaOptions[0].Provider}-{mfaOptions[0].FactorType}" ); | ||
Console.Error.WriteLine( $"MFA method: {mfaOptions[0].Provider} : {mfaOptions[0].FactorName}" ); | ||
return mfaOptions[0]; | ||
} | ||
|
||
for( int i = 0; i < mfaOptions.Length; i++ ) { | ||
Console.Error.WriteLine( $"[{i + 1}] {mfaOptions[i].Provider}-{mfaOptions[i].FactorType}" ); | ||
Console.Error.WriteLine( $"[{i + 1}] {mfaOptions[i].Provider} : {mfaOptions[i].FactorName}" ); | ||
} | ||
Console.Error.Write( "Select an available MFA option: " ); | ||
if( !int.TryParse( _stdinReader.ReadLine(), out int index ) || index > mfaOptions.Length || index < 1 ) { | ||
|
@@ -208,16 +144,90 @@ OktaMfaFactor IConsolePrompter.SelectMfa( OktaMfaFactor[] mfaOptions ) { | |
return mfaOptions[index - 1]; | ||
} | ||
|
||
string IConsolePrompter.GetMfaResponse( string mfaInputPrompt ) { | ||
Console.Error.Write( $"{mfaInputPrompt}: " ); | ||
string? mfaInput = _stdinReader.ReadLine(); | ||
string IConsolePrompter.GetMfaResponse( string mfaInputPrompt, bool maskInput ) { | ||
string? mfaInput; | ||
|
||
if( maskInput ) { | ||
mfaInput = GetMaskedInput( $"{mfaInputPrompt}: " ); | ||
} else { | ||
Console.Error.Write( $"{mfaInputPrompt}: " ); | ||
mfaInput = _stdinReader.ReadLine(); | ||
} | ||
|
||
if( mfaInput is not null ) { | ||
return mfaInput; | ||
} | ||
throw new BmxException( "Invalid MFA Input" ); | ||
} | ||
|
||
private string GetMaskedInput( string prompt ) { | ||
Func<char> readKey; | ||
if( IS_WINDOWS ) { | ||
// On Windows, Console.ReadKey calls native console API, and will fail without a console attached | ||
if( Console.IsInputRedirected ) { | ||
Console.Error.WriteLine( """ | ||
====== WARNING ====== | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is going to pop this twice once for password + security question, in this case. But meh, that is a very niche edge case |
||
Input to BMX is redirected. Input may be displayed on screen! | ||
If you're using mintty (with Git Bash, Cygwin, MSYS2 etc.), consider switching | ||
to Windows Terminal for a better experience. | ||
If you must use mintty, prefix your bmx command with 'winpty '. | ||
===================== | ||
""" ); | ||
readKey = () => (char)_stdinReader.Read(); | ||
} else { | ||
readKey = () => Console.ReadKey( intercept: true ).KeyChar; | ||
} | ||
} else { | ||
readKey = () => (char)_stdinReader.Read(); | ||
} | ||
|
||
Console.Error.Write( prompt ); | ||
|
||
string? originalTerminalSettings = null; | ||
try { | ||
if( !IS_WINDOWS ) { | ||
originalTerminalSettings = GetCurrentTerminalSettings(); | ||
EnableTerminalRawMode(); | ||
} | ||
|
||
var passwordBuilder = new StringBuilder(); | ||
while( true ) { | ||
char key = readKey(); | ||
|
||
if( key == CTRL_C ) { | ||
// Ctrl+C should terminate the program. | ||
// Using an empty string as the exception message because this message is displayed to the user, | ||
// but the user doesn't need to see anything when they themselves ended the program. | ||
throw new BmxException( string.Empty ); | ||
} | ||
if( key == '\n' || key == '\r' ) { | ||
// when the terminal is in raw mode, writing \r is needed to start the new line properly | ||
Console.Error.Write( "\r\n" ); | ||
return passwordBuilder.ToString(); | ||
} | ||
|
||
if( key == CTRL_U ) { | ||
string moveLeftString = new( '\b', passwordBuilder.Length ); | ||
string emptyString = new( ' ', passwordBuilder.Length ); | ||
Console.Error.Write( moveLeftString + emptyString + moveLeftString ); | ||
passwordBuilder.Clear(); | ||
} else | ||
// The backspace key is received as the DEL character in raw mode | ||
if( ( key == '\b' || key == DEL ) && passwordBuilder.Length > 0 ) { | ||
Console.Error.Write( "\b \b" ); | ||
passwordBuilder.Length--; | ||
} else if( !char.IsControl( key ) ) { | ||
Console.Error.Write( '*' ); | ||
passwordBuilder.Append( key ); | ||
} | ||
} | ||
} finally { | ||
if( !IS_WINDOWS && !string.IsNullOrEmpty( originalTerminalSettings ) ) { | ||
SetTerminalSettings( originalTerminalSettings ); | ||
} | ||
} | ||
} | ||
|
||
private static string GetCurrentTerminalSettings() { | ||
var startInfo = new ProcessStartInfo( "stty" ); | ||
startInfo.ArgumentList.Add( "-g" ); | ||
|
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored this masked input read to be re-usable (for GetMfaResponse)