| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Security; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Azure.Core.Pipeline; |
| | | 10 | | using Microsoft.Identity.Client; |
| | | 11 | | using Microsoft.Identity.Client.Extensions.Msal; |
| | | 12 | | |
| | | 13 | | namespace Azure.Identity |
| | | 14 | | { |
| | | 15 | | internal class MsalPublicClient : MsalClientBase<IPublicClientApplication> |
| | | 16 | | { |
| | | 17 | | private readonly string _redirectUrl; |
| | | 18 | | |
| | 100 | 19 | | protected MsalPublicClient() |
| | | 20 | | { |
| | 100 | 21 | | } |
| | | 22 | | |
| | | 23 | | public MsalPublicClient(CredentialPipeline pipeline, string tenantId, string clientId, string redirectUrl, IToke |
| | 248 | 24 | | : base(pipeline, tenantId, clientId, cacheOptions) |
| | | 25 | | { |
| | 248 | 26 | | _redirectUrl = redirectUrl; |
| | 248 | 27 | | } |
| | | 28 | | |
| | | 29 | | protected override Task<IPublicClientApplication> CreateClientAsync() |
| | | 30 | | { |
| | 64 | 31 | | var authorityHost = Pipeline.AuthorityHost; |
| | | 32 | | |
| | 64 | 33 | | var authorityUri = new UriBuilder(authorityHost.Scheme, authorityHost.Host, authorityHost.Port, TenantId ?? |
| | | 34 | | |
| | 64 | 35 | | PublicClientApplicationBuilder pubAppBuilder = PublicClientApplicationBuilder.Create(ClientId).WithAuthority |
| | | 36 | | |
| | 64 | 37 | | if (!string.IsNullOrEmpty(_redirectUrl)) |
| | | 38 | | { |
| | 20 | 39 | | pubAppBuilder = pubAppBuilder.WithRedirectUri(_redirectUrl); |
| | | 40 | | } |
| | | 41 | | |
| | 64 | 42 | | return Task.FromResult(pubAppBuilder.Build()); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | public virtual async Task<IEnumerable<IAccount>> GetAccountsAsync() |
| | | 46 | | { |
| | 0 | 47 | | await EnsureInitializedAsync(true).ConfigureAwait(false); |
| | | 48 | | |
| | 0 | 49 | | return await Client.GetAccountsAsync().ConfigureAwait(false); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | public virtual async Task<AuthenticationResult> AcquireTokenSilentAsync(string[] scopes, IAccount account, bool |
| | | 53 | | { |
| | 0 | 54 | | await EnsureInitializedAsync(async).ConfigureAwait(false); |
| | | 55 | | |
| | 0 | 56 | | return await Client.AcquireTokenSilent(scopes, account).ExecuteAsync(async, cancellationToken).ConfigureAwai |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | public virtual async Task<AuthenticationResult> AcquireTokenInteractiveAsync(string[] scopes, Prompt prompt, boo |
| | | 60 | | { |
| | 0 | 61 | | await EnsureInitializedAsync(async).ConfigureAwait(false); |
| | | 62 | | |
| | 0 | 63 | | return await Client.AcquireTokenInteractive(scopes).WithPrompt(prompt).ExecuteAsync(async, cancellationToken |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | public virtual async Task<AuthenticationResult> AcquireTokenByUsernamePasswordAsync(string[] scopes, string user |
| | | 67 | | { |
| | 12 | 68 | | await EnsureInitializedAsync(async).ConfigureAwait(false); |
| | | 69 | | |
| | 12 | 70 | | return await Client.AcquireTokenByUsernamePassword(scopes, username, password).ExecuteAsync(async, cancellat |
| | 12 | 71 | | } |
| | | 72 | | |
| | | 73 | | public virtual async Task<AuthenticationResult> AcquireTokenWithDeviceCodeAsync(string[] scopes, Func<DeviceCode |
| | | 74 | | { |
| | 20 | 75 | | await EnsureInitializedAsync(async).ConfigureAwait(false); |
| | | 76 | | |
| | 20 | 77 | | return await Client.AcquireTokenWithDeviceCode(scopes, deviceCodeCallback).ExecuteAsync(async, cancellationT |
| | 8 | 78 | | } |
| | | 79 | | |
| | | 80 | | public virtual async Task<AuthenticationResult> AcquireTokenByRefreshToken(string[] scopes, string refreshToken, |
| | | 81 | | { |
| | 64 | 82 | | await EnsureInitializedAsync(async).ConfigureAwait(false); |
| | | 83 | | |
| | 64 | 84 | | return await ((IByRefreshToken)Client).AcquireTokenByRefreshToken(scopes, refreshToken).WithAuthority(azureC |
| | 60 | 85 | | } |
| | | 86 | | } |
| | | 87 | | } |