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

turn Browser non-static #491

Merged
merged 1 commit into from
Oct 22, 2024
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
11 changes: 8 additions & 3 deletions src/D2L.Bmx/Browser.cs → src/D2L.Bmx/BrowserLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

namespace D2L.Bmx;

public static class Browser {
internal interface IBrowserLauncher {
Task<IBrowser> LaunchAsync( string browserPath );
bool TryGetPathToBrowser( [NotNullWhen( returnValue: true )] out string? path );
}

internal class BrowserLauncher : IBrowserLauncher {

// https://github.com/microsoft/playwright/blob/6763d5ab6bd20f1f0fc879537855a26c7644a496/packages/playwright-core/src/server/registry/index.ts#L630
private static readonly string[] WindowsEnvironmentVariables = [
Expand All @@ -26,7 +31,7 @@ public static class Browser {
"/opt/microsoft/msedge/msedge",
];

public static async Task<IBrowser> LaunchBrowserAsync( string browserPath ) {
async Task<IBrowser> IBrowserLauncher.LaunchAsync( string browserPath ) {
var launchOptions = new LaunchOptions {
ExecutablePath = browserPath,
// For whatever reason, with an elevated user, Chromium cannot launch in headless mode without --no-sandbox.
Expand All @@ -37,7 +42,7 @@ public static async Task<IBrowser> LaunchBrowserAsync( string browserPath ) {
return await Puppeteer.LaunchAsync( launchOptions );
}

public static bool TryGetPathToBrowser( [NotNullWhen( returnValue: true )] out string? path ) {
bool IBrowserLauncher.TryGetPathToBrowser( [NotNullWhen( returnValue: true )] out string? path ) {
path = null;
if( OperatingSystem.IsWindows() ) {
foreach( string windowsPartialPath in WindowsPartialPaths ) {
Expand Down
5 changes: 3 additions & 2 deletions src/D2L.Bmx/OktaAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ IOktaAuthenticatedClient Client
internal class OktaAuthenticator(
IOktaClientFactory oktaClientFactory,
IOktaSessionStorage sessionStorage,
IBrowserLauncher browserLauncher,
IConsolePrompter consolePrompter,
IConsoleWriter consoleWriter,
BmxConfig config
Expand Down Expand Up @@ -58,7 +59,7 @@ bool ignoreCache
return new( Org: org, User: user, Client: oktaAuthenticated );
}

if( Browser.TryGetPathToBrowser( out string? browserPath ) ) {
if( browserLauncher.TryGetPathToBrowser( out string? browserPath ) ) {
if( !nonInteractive ) {
Console.Error.WriteLine( "Attempting Okta passwordless authentication..." );
}
Expand Down Expand Up @@ -113,7 +114,7 @@ private bool TryAuthenticateFromCache(
string user,
string browserPath
) {
await using var browser = await Browser.LaunchBrowserAsync( browserPath );
await using var browser = await browserLauncher.LaunchAsync( browserPath );

using var cancellationTokenSource = new CancellationTokenSource( TimeSpan.FromSeconds( 15 ) );
var sessionIdTcs = new TaskCompletionSource<string?>( TaskCreationOptions.RunContinuationsAsynchronously );
Expand Down
3 changes: 3 additions & 0 deletions src/D2L.Bmx/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
var handler = new LoginHandler( new OktaAuthenticator(
new OktaClientFactory(),
new OktaSessionStorage(),
new BrowserLauncher(),
new ConsolePrompter(),
consoleWriter,
config
Expand Down Expand Up @@ -129,6 +130,7 @@
new OktaAuthenticator(
new OktaClientFactory(),
new OktaSessionStorage(),
new BrowserLauncher(),
consolePrompter,
consoleWriter,
config ),
Expand Down Expand Up @@ -183,6 +185,7 @@
new OktaAuthenticator(
new OktaClientFactory(),
new OktaSessionStorage(),
new BrowserLauncher(),
consolePrompter,
consoleWriter,
config ),
Expand Down
Loading