| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Security.Cryptography.X509Certificates; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Microsoft.Identity.Client; |
| | 9 | | using Microsoft.Identity.Client.Extensions.Msal; |
| | 10 | |
|
| | 11 | | namespace Azure.Identity |
| | 12 | | { |
| | 13 | | internal class MsalConfidentialClient : MsalClientBase<IConfidentialClientApplication> |
| | 14 | | { |
| | 15 | | private readonly string _clientSecret; |
| | 16 | | private readonly ClientCertificateCredential.IX509Certificate2Provider _certificateProvider; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// For mocking purposes only. |
| | 20 | | /// </summary> |
| | 21 | | protected MsalConfidentialClient() |
| 32 | 22 | | : base() |
| | 23 | | { |
| 32 | 24 | | } |
| | 25 | |
|
| | 26 | | public MsalConfidentialClient(CredentialPipeline pipeline, string tenantId, string clientId, string clientSecret |
| 20 | 27 | | : base(pipeline, tenantId, clientId, cacheOptions) |
| | 28 | | { |
| 20 | 29 | | _clientSecret = clientSecret; |
| 20 | 30 | | } |
| | 31 | |
|
| | 32 | | public MsalConfidentialClient(CredentialPipeline pipeline, string tenantId, string clientId, ClientCertificateCr |
| 56 | 33 | | : base(pipeline, tenantId, clientId, cacheOptions) |
| | 34 | | { |
| 56 | 35 | | _certificateProvider = certificateProvider; |
| 56 | 36 | | } |
| | 37 | |
|
| | 38 | | protected override async Task<IConfidentialClientApplication> CreateClientAsync() |
| | 39 | | { |
| 68 | 40 | | ConfidentialClientApplicationBuilder confClientBuilder = ConfidentialClientApplicationBuilder.Create(ClientI |
| | 41 | |
|
| 68 | 42 | | if (_clientSecret != null) |
| | 43 | | { |
| 16 | 44 | | confClientBuilder.WithClientSecret(_clientSecret); |
| | 45 | | } |
| | 46 | |
|
| 68 | 47 | | if (_certificateProvider != null) |
| | 48 | | { |
| 52 | 49 | | X509Certificate2 clientCertificate = await _certificateProvider.GetCertificateAsync(true, default).Confi |
| | 50 | |
|
| 36 | 51 | | confClientBuilder.WithCertificate(clientCertificate); |
| | 52 | | } |
| | 53 | |
|
| 52 | 54 | | return confClientBuilder.Build(); |
| | 55 | |
|
| 52 | 56 | | } |
| | 57 | |
|
| | 58 | | public virtual async Task<AuthenticationResult> AcquireTokenForClientAsync(string[] scopes, bool async, Cancella |
| | 59 | | { |
| 100 | 60 | | await EnsureInitializedAsync(async).ConfigureAwait(false); |
| | 61 | |
|
| 68 | 62 | | return await Client.AcquireTokenForClient(scopes).ExecuteAsync(async, cancellationToken).ConfigureAwait(fals |
| 48 | 63 | | } |
| | 64 | | } |
| | 65 | | } |