| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using Azure.Core; |
| | 5 | | using Azure.Core.Pipeline; |
| | 6 | | using Microsoft.Identity.Client; |
| | 7 | | using System; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | |
|
| | 11 | | namespace Azure.Identity |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Enables authentication to Azure Active Directory using a client secret that was generated for an App Registratio |
| | 15 | | /// to configure a client secret can be found here: |
| | 16 | | /// https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-configure-app-access-web-apis#add-cre |
| | 17 | | /// </summary> |
| | 18 | | public class ClientSecretCredential : TokenCredential |
| | 19 | | { |
| | 20 | | private readonly MsalConfidentialClient _client; |
| | 21 | | private readonly CredentialPipeline _pipeline; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Gets the Azure Active Directory tenant (directory) Id of the service principal |
| | 25 | | /// </summary> |
| 4 | 26 | | internal string TenantId { get; } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Gets the client (application) ID of the service principal |
| | 30 | | /// </summary> |
| 4 | 31 | | internal string ClientId { get; } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Gets the client secret that was generated for the App Registration used to authenticate the client. |
| | 35 | | /// </summary> |
| 4 | 36 | | internal string ClientSecret { get; } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Protected constructor for mocking. |
| | 40 | | /// </summary> |
| 16 | 41 | | protected ClientSecretCredential() |
| | 42 | | { |
| 16 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Creates an instance of the ClientSecretCredential with the details needed to authenticate against Azure Acti |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="tenantId">The Azure Active Directory tenant (directory) Id of the service principal.</param> |
| | 49 | | /// <param name="clientId">The client (application) ID of the service principal</param> |
| | 50 | | /// <param name="clientSecret">A client secret that was generated for the App Registration used to authenticate |
| | 51 | | public ClientSecretCredential(string tenantId, string clientId, string clientSecret) |
| 12 | 52 | | : this(tenantId, clientId, clientSecret, null, null, null) |
| | 53 | | { |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Creates an instance of the ClientSecretCredential with the details needed to authenticate against Azure Acti |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="tenantId">The Azure Active Directory tenant (directory) Id of the service principal.</param> |
| | 60 | | /// <param name="clientId">The client (application) ID of the service principal</param> |
| | 61 | | /// <param name="clientSecret">A client secret that was generated for the App Registration used to authenticate |
| | 62 | | /// <param name="options">Options that allow to configure the management of the requests sent to the Azure Activ |
| | 63 | | internal ClientSecretCredential(string tenantId, string clientId, string clientSecret, ClientSecretCredentialOpt |
| 0 | 64 | | : this(tenantId, clientId, clientSecret, options, null, null) |
| | 65 | | { |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Creates an instance of the ClientSecretCredential with the details needed to authenticate against Azure Acti |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="tenantId">The Azure Active Directory tenant (directory) Id of the service principal.</param> |
| | 72 | | /// <param name="clientId">The client (application) ID of the service principal</param> |
| | 73 | | /// <param name="clientSecret">A client secret that was generated for the App Registration used to authenticate |
| | 74 | | /// <param name="options">Options that allow to configure the management of the requests sent to the Azure Activ |
| | 75 | | public ClientSecretCredential(string tenantId, string clientId, string clientSecret, TokenCredentialOptions opti |
| 16 | 76 | | : this(tenantId, clientId, clientSecret, options, null, null) |
| | 77 | | { |
| 16 | 78 | | } |
| | 79 | |
|
| 48 | 80 | | internal ClientSecretCredential(string tenantId, string clientId, string clientSecret, TokenCredentialOptions op |
| | 81 | | { |
| 48 | 82 | | TenantId = tenantId ?? throw new ArgumentNullException(nameof(tenantId)); |
| | 83 | |
|
| 44 | 84 | | ClientId = clientId ?? throw new ArgumentNullException(nameof(clientId)); |
| | 85 | |
|
| 40 | 86 | | ClientSecret = clientSecret ?? throw new ArgumentNullException(nameof(clientSecret)); |
| | 87 | |
|
| 36 | 88 | | _pipeline = pipeline ?? CredentialPipeline.GetInstance(options); |
| | 89 | |
|
| 36 | 90 | | _client = client ?? new MsalConfidentialClient(_pipeline, tenantId, clientId, clientSecret, options as IToke |
| 36 | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Obtains a token from the Azure Active Directory service, using the specified client secret to authenticate. |
| | 95 | | /// </summary> |
| | 96 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | 97 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 98 | | /// <returns>An <see cref="AccessToken"/> which can be used to authenticate service client calls.</returns> |
| | 99 | | public override async ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken |
| | 100 | | { |
| 26 | 101 | | using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope("ClientSecretCredential.GetToken", requ |
| | 102 | |
|
| | 103 | | try |
| | 104 | | { |
| 26 | 105 | | AuthenticationResult result = await _client.AcquireTokenForClientAsync(requestContext.Scopes, true, canc |
| | 106 | |
|
| 14 | 107 | | return scope.Succeeded(new AccessToken(result.AccessToken, result.ExpiresOn)); |
| | 108 | | } |
| 12 | 109 | | catch (Exception e) |
| | 110 | | { |
| 12 | 111 | | throw scope.FailWrapAndThrow(e); |
| | 112 | | } |
| 14 | 113 | | } |
| | 114 | |
|
| | 115 | | /// <summary> |
| | 116 | | /// Obtains a token from the Azure Active Directory service, using the specified client secret to authenticate. |
| | 117 | | /// </summary> |
| | 118 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | 119 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 120 | | /// <returns>An <see cref="AccessToken"/> which can be used to authenticate service client calls.</returns> |
| | 121 | | public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken = d |
| | 122 | | { |
| 10 | 123 | | using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope("ClientSecretCredential.GetToken", requ |
| | 124 | |
|
| | 125 | | try |
| | 126 | | { |
| 10 | 127 | | AuthenticationResult result = _client.AcquireTokenForClientAsync(requestContext.Scopes, false, cancellat |
| | 128 | |
|
| 2 | 129 | | return scope.Succeeded(new AccessToken(result.AccessToken, result.ExpiresOn)); |
| | 130 | | } |
| 8 | 131 | | catch (Exception e) |
| | 132 | | { |
| 8 | 133 | | throw scope.FailWrapAndThrow(e); |
| | 134 | | } |
| 2 | 135 | | } |
| | 136 | | } |
| | 137 | | } |