Skip to content
Closed
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
50 changes: 46 additions & 4 deletions src/MSALWrapper/PCAWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ namespace Microsoft.Authentication.MSALWrapper
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -83,14 +85,24 @@ public async Task<TokenResult> GetTokenSilentAsync(IEnumerable<string> scopes, I
/// <inheritdoc/>
public async Task<TokenResult> GetTokenInteractiveAsync(IEnumerable<string> scopes, IAccount account, CancellationToken cancellationToken)
{
AuthenticationResult result = await this.pca
var builder = this.pca
.AcquireTokenInteractive(scopes)
.WithEmbeddedWebViewOptions(new EmbeddedWebViewOptions()
{
Title = this.PromptHint,
})
.WithUseEmbeddedWebView(this.UseEmbeddedWebView)
.WithAccount(account)
.WithAccount(account);

if (!this.UseEmbeddedWebView && RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
builder = builder.WithSystemWebViewOptions(new SystemWebViewOptions
{
OpenBrowserAsync = OpenBrowserOnLinuxAsync,
});
}

AuthenticationResult result = await builder
.ExecuteAsync(cancellationToken)
.ConfigureAwait(false);
return this.TokenResultOrNull(result);
Expand All @@ -99,14 +111,24 @@ public async Task<TokenResult> GetTokenInteractiveAsync(IEnumerable<string> scop
/// <inheritdoc/>
public async Task<TokenResult> GetTokenInteractiveAsync(IEnumerable<string> scopes, string claims, CancellationToken cancellationToken)
{
AuthenticationResult result = await this.pca
var builder = this.pca
.AcquireTokenInteractive(scopes)
.WithEmbeddedWebViewOptions(new EmbeddedWebViewOptions()
{
Title = this.PromptHint,
})
.WithUseEmbeddedWebView(this.UseEmbeddedWebView)
.WithClaims(claims)
.WithClaims(claims);

if (!this.UseEmbeddedWebView && RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
builder = builder.WithSystemWebViewOptions(new SystemWebViewOptions
{
OpenBrowserAsync = OpenBrowserOnLinuxAsync,
});
}

AuthenticationResult result = await builder
.ExecuteAsync(cancellationToken)
.ConfigureAwait(false);
return this.TokenResultOrNull(result);
Expand Down Expand Up @@ -164,6 +186,26 @@ public async Task RemoveAsync(IAccount account)
await this.pca.RemoveAsync(account);
}

private static Task OpenBrowserOnLinuxAsync(Uri uri)
{
string browser = Environment.GetEnvironmentVariable("BROWSER");
if (!string.IsNullOrEmpty(browser))
{
Process.Start(new ProcessStartInfo(browser, uri.AbsoluteUri)
{
UseShellExecute = false,
});
return Task.CompletedTask;
}

// $BROWSER not set — fall back to default .NET behavior (UseShellExecute tries xdg-open etc.)
Process.Start(new ProcessStartInfo(uri.AbsoluteUri)
{
UseShellExecute = true,
});
return Task.CompletedTask;
}

private TokenResult TokenResultOrNull(AuthenticationResult result)
{
if (result == null || string.IsNullOrEmpty(result.AccessToken))
Expand Down
Loading