Skip to content

Commit

Permalink
Enable customerizing webview for interactive authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
msJinLei committed Feb 12, 2023
1 parent 41ed5f6 commit c27d328
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class InteractiveBrowserCredential : TokenCredential
internal string[] AdditionallyAllowedTenantIds { get; }
internal string ClientId { get; }
internal string LoginHint { get; }
internal bool? UseEmbeddedWebView { get; }
internal SystemWebViewOptions SystemBrowserOptions { get; }
internal EmbeddedWebViewOptions EmbededBrowserOptions {get;}
internal MsalPublicClient Client { get; }
internal CredentialPipeline Pipeline { get; }
internal bool DisableAutomaticAuthentication { get; }
Expand Down Expand Up @@ -86,6 +89,10 @@ internal InteractiveBrowserCredential(string tenantId, string clientId, TokenCre
Client = client ?? new MsalPublicClient(Pipeline, tenantId, clientId, redirectUrl, options);
AdditionallyAllowedTenantIds = TenantIdResolver.ResolveAddionallyAllowedTenantIds(options?.AdditionallyAllowedTenantsCore);
Record = (options as InteractiveBrowserCredentialOptions)?.AuthenticationRecord;
UseEmbeddedWebView = (options as InteractiveBrowserCredentialOptions)?.UseEmbeddedWebView ?? null;
SystemBrowserOptions = (options as InteractiveBrowserCredentialOptions)?.SystemBrowserOptions ?? null;
EmbededBrowserOptions = (options as InteractiveBrowserCredentialOptions)?.EmbededBrowserOptions ?? null;
//set new added parameter;
}

/// <summary>
Expand Down Expand Up @@ -227,7 +234,7 @@ private async Task<AccessToken> GetTokenViaBrowserLoginAsync(TokenRequestContext

var tenantId = TenantIdResolver.Resolve(TenantId ?? Record?.TenantId, context, AdditionallyAllowedTenantIds);
AuthenticationResult result = await Client
.AcquireTokenInteractiveAsync(context.Scopes, context.Claims, prompt, LoginHint, tenantId, async, cancellationToken)
.AcquireTokenInteractiveAsync(context.Scopes, context.Claims, prompt, LoginHint, tenantId, UseEmbeddedWebView, SystemBrowserOptions, EmbededBrowserOptions, async, cancellationToken)
.ConfigureAwait(false);

Record = new AuthenticationRecord(result, ClientId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Identity.Client;

namespace Azure.Identity
{
Expand Down Expand Up @@ -64,5 +65,21 @@ public string TenantId

/// <inheritdoc/>
public bool DisableInstanceDiscovery { get; set; }

/// <summary>
/// Specifies if the public client application should used an embedded web browser
/// or the system default browser
/// </summary>
public bool? UseEmbeddedWebView { get; set; }

/// <summary>
/// The options for using the system OS browser handle interactive authentication.
/// </summary>
public SystemWebViewOptions SystemBrowserOptions { get; set; }

/// <summary>
/// The options for using the embedded web view for interactive authentication.
/// </summary>
public EmbeddedWebViewOptions EmbededBrowserOptions { get; set; }
}
}
22 changes: 18 additions & 4 deletions sdk/identity/Azure.Identity/src/MsalPublicClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected virtual async ValueTask<AuthenticationResult> AcquireTokenSilentCoreAs
.ConfigureAwait(false);
}

public async ValueTask<AuthenticationResult> AcquireTokenInteractiveAsync(string[] scopes, string claims, Prompt prompt, string loginHint, string tenantId, bool async, CancellationToken cancellationToken)
public async ValueTask<AuthenticationResult> AcquireTokenInteractiveAsync(string[] scopes, string claims, Prompt prompt, string loginHint, string tenantId, bool? useEmbeddedWebView, SystemWebViewOptions systemWebviewOptions, EmbeddedWebViewOptions embeddedWebViewOptions, bool async, CancellationToken cancellationToken)
{
#pragma warning disable AZC0109 // Misuse of 'async' parameter.
if (!async && !IdentityCompatSwitches.DisableInteractiveBrowserThreadpoolExecution)
Expand All @@ -141,7 +141,7 @@ public async ValueTask<AuthenticationResult> AcquireTokenInteractiveAsync(string
#pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult().
return Task.Run(async () =>
{
var result = await AcquireTokenInteractiveCoreAsync(scopes, claims, prompt, loginHint, tenantId, true, cancellationToken).ConfigureAwait(false);
var result = await AcquireTokenInteractiveCoreAsync(scopes, claims, prompt, loginHint, tenantId, useEmbeddedWebView, systemWebviewOptions, embeddedWebViewOptions, true, cancellationToken).ConfigureAwait(false);
LogAccountDetails(result);
return result;
}).GetAwaiter().GetResult();
Expand All @@ -150,12 +150,13 @@ public async ValueTask<AuthenticationResult> AcquireTokenInteractiveAsync(string

AzureIdentityEventSource.Singleton.InteractiveAuthenticationExecutingInline();

var result = await AcquireTokenInteractiveCoreAsync(scopes, claims, prompt, loginHint, tenantId, async, cancellationToken).ConfigureAwait(false);
var result = await AcquireTokenInteractiveCoreAsync(scopes, claims, prompt, loginHint, tenantId, useEmbeddedWebView, systemWebviewOptions, embeddedWebViewOptions, async, cancellationToken).ConfigureAwait(false);
LogAccountDetails(result);
return result;
}

protected virtual async ValueTask<AuthenticationResult> AcquireTokenInteractiveCoreAsync(string[] scopes, string claims, Prompt prompt, string loginHint, string tenantId, bool async, CancellationToken cancellationToken)
protected virtual async ValueTask<AuthenticationResult> AcquireTokenInteractiveCoreAsync(string[] scopes, string claims, Prompt prompt, string loginHint, string tenantId, bool? useEmbeddedWebView, SystemWebViewOptions systemWebviewOptions, EmbeddedWebViewOptions embeddedWebViewOptions,
bool async, CancellationToken cancellationToken)
{
IPublicClientApplication client = await GetClientAsync(async, cancellationToken).ConfigureAwait(false);

Expand All @@ -164,6 +165,7 @@ protected virtual async ValueTask<AuthenticationResult> AcquireTokenInteractiveC
.WithClaims(claims)
.WithPrompt(prompt)
.WithClaims(claims);

if (loginHint != null)
{
builder.WithLoginHint(loginHint);
Expand All @@ -172,6 +174,18 @@ protected virtual async ValueTask<AuthenticationResult> AcquireTokenInteractiveC
{
builder.WithAuthority(Pipeline.AuthorityHost.AbsoluteUri, tenantId);
}
if (useEmbeddedWebView != null)
{
builder.WithUseEmbeddedWebView(useEmbeddedWebView == true);
}
if (systemWebviewOptions != null)
{
builder.WithSystemWebViewOptions(systemWebviewOptions);
}
if (embeddedWebViewOptions != null)
{
builder.WithEmbeddedWebViewOptions(embeddedWebViewOptions);
}
return await builder
.ExecuteAsync(async, cancellationToken)
.ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ protected override ValueTask<AuthenticationResult> AcquireTokenInteractiveCoreAs
Prompt prompt,
string loginHint,
string tenantId,
bool? useEmbeddedWebView,
SystemWebViewOptions systemWebviewOptions,
EmbeddedWebViewOptions embeddedWebViewOptions,
bool async,
CancellationToken cancellationToken)
{
Expand Down

0 comments on commit c27d328

Please sign in to comment.