Skip to content

Commit

Permalink
add more debug logs for passwordless auth
Browse files Browse the repository at this point in the history
  • Loading branch information
cfbao committed Jan 15, 2025
1 parent ae2b49a commit 43be454
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/D2L.Bmx/D2L.Bmx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Release builds (preview or real release) won't have git hash so they look cleaner.
-->
<IncludeSourceRevisionInInformationalVersion Condition="'$(Version)' != ''">false</IncludeSourceRevisionInInformationalVersion>
<Version Condition="'$(Version)' == ''">3.0.0-scratch</Version>
<Version Condition="'$(Version)' == ''">3.999.999-scratch</Version>
<InformationalVersion>v$(Version)</InformationalVersion>
</PropertyGroup>

Expand Down
25 changes: 21 additions & 4 deletions src/D2L.Bmx/OktaAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ string browserPath

try {
sessionId = await GetSessionIdFromBrowserAsync( browserPath, orgUrl );
} catch( TaskCanceledException ) {
} catch( TaskCanceledException ex ) {
if( BmxEnvironment.IsDebug ) {
consoleWriter.WriteWarning( "Okta passwordless authentication timed out." );
consoleWriter.WriteWarning( $"Okta passwordless authentication timed out.\n{ex}" );
}
} catch( Exception ) {
} catch( Exception ex ) {
if( BmxEnvironment.IsDebug ) {
consoleWriter.WriteWarning( "Unknown error occurred while trying Okta passwordless authentication." );
consoleWriter.WriteWarning( $"Unknown error occurred while trying Okta passwordless authentication.\n{ex}" );
}
}

Expand All @@ -154,6 +154,9 @@ The provided Okta user '{providedLogin}' does not match the system configured pa
}

private async Task<string?> GetSessionIdFromBrowserAsync( string browserPath, Uri orgUrl ) {
if( BmxEnvironment.IsDebug ) {
consoleWriter.WriteWarning( $"Launching browser: {browserPath}" );
}
await using var browser = await browserLauncher.LaunchAsync( browserPath );

var sessionIdTcs = new TaskCompletionSource<string?>( TaskCreationOptions.RunContinuationsAsynchronously );
Expand All @@ -167,10 +170,17 @@ The provided Okta user '{providedLogin}' does not match the system configured pa
pageTimer.Elapsed += ( _, _ ) => cancellationTokenSource.Cancel();
pageTimer.Start();

if( BmxEnvironment.IsDebug ) {
consoleWriter.WriteWarning( "Creating new browser tab" );
}
using var page = await browser.NewPageAsync().WaitAsync( cancellationTokenSource.Token );
int attempt = 1;

page.Load += ( _, _ ) => _ = OnPageLoadAsync();

if( BmxEnvironment.IsDebug ) {
consoleWriter.WriteWarning( $"Navigating to {orgUrl}" );
}
await page.GoToAsync( orgUrl.AbsoluteUri ).WaitAsync( cancellationTokenSource.Token );
return await sessionIdTcs.Task;

Expand All @@ -181,13 +191,20 @@ async Task OnPageLoadAsync() {
pageTimer.Start();
}

if( BmxEnvironment.IsDebug ) {
consoleWriter.WriteWarning( $"Browser loaded {page.Url}" );
}

var url = new Uri( page.Url );
if( url.Host == orgUrl.Host ) {
string title = await page.GetTitleAsync().WaitAsync( cancellationTokenSource.Token );
// DSSO can sometimes takes more than one attempt.
// If the path is '/' with 'sign in' in the title, it means DSSO is not available and we should stop retrying.
if( title.Contains( "sign in", StringComparison.OrdinalIgnoreCase ) ) {
if( attempt < 3 && url.AbsolutePath != "/" ) {
if( BmxEnvironment.IsDebug ) {
consoleWriter.WriteWarning( $"Attempt {attempt} failed. Retry..." );
}
attempt++;
await page.GoToAsync( orgUrl.AbsoluteUri ).WaitAsync( cancellationTokenSource.Token );
} else {
Expand Down

0 comments on commit 43be454

Please sign in to comment.