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

display okta user and org in password prompt / failed auth error message #451

Merged
merged 8 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 4 additions & 2 deletions src/D2L.Bmx/ConsolePrompter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal interface IConsolePrompter {
string PromptOrg( bool allowEmptyInput );
string PromptProfile();
string PromptUser( bool allowEmptyInput );
string PromptPassword();
string PromptPassword( string user, string org );
int? PromptDuration();
string PromptAccount( string[] accounts );
string PromptRole( string[] roles );
Expand Down Expand Up @@ -63,7 +63,7 @@ string IConsolePrompter.PromptUser( bool allowEmptyInput ) {
return user;
}

string IConsolePrompter.PromptPassword() {
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
Expand All @@ -84,6 +84,8 @@ Input to BMX is redirected. Password input may be displayed on screen!
readKey = () => (char)_stdinReader.Read();
}

Console.Error.WriteLine( $"{ParameterDescriptions.Org}: {org}" );
Console.Error.WriteLine( $"{ParameterDescriptions.User}: {user}" );
Console.Error.Write( $"{ParameterDescriptions.Password}: " );

string? originalTerminalSettings = null;
Expand Down
14 changes: 10 additions & 4 deletions src/D2L.Bmx/Okta/OktaApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ Task<AuthenticateResponse> VerifyMfaChallengeResponseAsync(
internal class OktaApi : IOktaApi {
private readonly CookieContainer _cookieContainer;
private readonly HttpClient _httpClient;
private string? _organization;

public OktaApi() {
_cookieContainer = new CookieContainer();
_httpClient = new HttpClient( new HttpClientHandler { CookieContainer = _cookieContainer } );
_httpClient.Timeout = TimeSpan.FromSeconds( 30 );
_httpClient = new HttpClient( new HttpClientHandler { CookieContainer = _cookieContainer } ) {
Timeout = TimeSpan.FromSeconds( 30 )
};
_httpClient.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue( "application/json" ) );
}

void IOktaApi.SetOrganization( string organization ) {
_organization = organization;
if( !organization.Contains( '.' ) ) {
_httpClient.BaseAddress = new Uri( $"https://{organization}.okta.com/api/v1/" );
} else {
Expand Down Expand Up @@ -83,7 +86,11 @@ await resp.Content.ReadAsStreamAsync(),
authnResponse.Embedded.Factors
);
}
throw new BmxException( "Okta authentication failed. Check if org, user and password is correct" );

string org = _organization is not null ? _organization : "unknown";
gord5500 marked this conversation as resolved.
Show resolved Hide resolved
throw new BmxException(
$"Okta authentication for user '{username}' in org '{org}'"
+ "failed. Check if org, user, and password is correct" );
}

async Task IOktaApi.IssueMfaChallengeAsync( string stateToken, string factorId ) {
Expand Down Expand Up @@ -168,7 +175,6 @@ async Task<OktaApp[]> IOktaApi.GetAwsAccountAppsAsync() {

return apps?.Where( app => app.AppName == "amazon_aws" ).ToArray()
?? throw new BmxException( "Error retrieving AWS accounts from Okta." );

}

async Task<string> IOktaApi.GetPageAsync( string samlLoginUrl ) {
Expand Down
2 changes: 1 addition & 1 deletion src/D2L.Bmx/OktaAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool ignoreCache
throw new BmxException( "Okta authentication failed. Please run `bmx login` first." );
}

string password = consolePrompter.PromptPassword();
string password = consolePrompter.PromptPassword( user, org );

var authnResponse = await oktaApi.AuthenticateAsync( user, password );

Expand Down
Loading