| | | 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 | | /// A <see cref="TokenCredential"/> implementation which authenticates a user using the device code flow, and provid |
| | | 15 | | /// For more information on the device code authentication flow see https://github.com/AzureAD/microsoft-authenticat |
| | | 16 | | /// </summary> |
| | | 17 | | public class DeviceCodeCredential : TokenCredential |
| | | 18 | | { |
| | | 19 | | private readonly MsalPublicClient _client = null; |
| | | 20 | | private readonly CredentialPipeline _pipeline; |
| | | 21 | | private AuthenticationRecord _record = null; |
| | | 22 | | private readonly string _clientId; |
| | | 23 | | private readonly Func<DeviceCodeInfo, CancellationToken, Task> _deviceCodeCallback; |
| | | 24 | | private bool _disableAutomaticAuthentication = false; |
| | | 25 | | private const string AuthenticationRequiredMessage = "Interactive authentication is needed to acquire token. Cal |
| | | 26 | | private const string NoDefaultScopeMessage = "Authenticating in this environment requires specifying a TokenRequ |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Protected constructor for mocking |
| | | 30 | | /// </summary> |
| | 32 | 31 | | protected DeviceCodeCredential() |
| | | 32 | | { |
| | | 33 | | |
| | 32 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Creates a new DeviceCodeCredential with the specified options, which will authenticate users with the specif |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="deviceCodeCallback">The callback to be executed to display the device code to the user</param> |
| | | 40 | | /// <param name="clientId">The client id of the application to which the users will authenticate</param> |
| | | 41 | | /// <param name="options">The client options for the newly created DeviceCodeCredential</param> |
| | | 42 | | public DeviceCodeCredential(Func<DeviceCodeInfo, CancellationToken, Task> deviceCodeCallback, string clientId, T |
| | 16 | 43 | | : this(deviceCodeCallback, null, clientId, options, null) |
| | | 44 | | { |
| | | 45 | | |
| | 16 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Creates a new DeviceCodeCredential with the specified options, which will authenticate users with the specif |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="deviceCodeCallback">The callback to be executed to display the device code to the user</param> |
| | | 52 | | /// <param name="tenantId">The tenant id of the application to which users will authenticate. This can be null |
| | | 53 | | /// <param name="clientId">The client id of the application to which the users will authenticate</param> |
| | | 54 | | /// <param name="options">The client options for the newly created DeviceCodeCredential</param> |
| | | 55 | | public DeviceCodeCredential(Func<DeviceCodeInfo, CancellationToken, Task> deviceCodeCallback, string tenantId, s |
| | 4 | 56 | | : this(deviceCodeCallback, tenantId, clientId, options, null) |
| | | 57 | | { |
| | 4 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Creates a new DeviceCodeCredential with the specified options, which will authenticate users using the devi |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="deviceCodeCallback">The callback to be executed to display the device code to the user.</param> |
| | | 64 | | /// <param name="options">The client options for the newly created <see cref="DeviceCodeCredential"/>.</param> |
| | | 65 | | internal DeviceCodeCredential(Func<DeviceCodeInfo, CancellationToken, Task> deviceCodeCallback, DeviceCodeCreden |
| | 4 | 66 | | : this(deviceCodeCallback, options?.TenantId, options?.ClientId, options, null) |
| | | 67 | | { |
| | 4 | 68 | | _disableAutomaticAuthentication = options?.DisableAutomaticAuthentication ?? false; |
| | 4 | 69 | | _record = options?.AuthenticationRecord; |
| | 4 | 70 | | } |
| | | 71 | | |
| | | 72 | | internal DeviceCodeCredential(Func<DeviceCodeInfo, CancellationToken, Task> deviceCodeCallback, string tenantId, |
| | 24 | 73 | | : this(deviceCodeCallback, tenantId, clientId, options, pipeline, null) |
| | | 74 | | { |
| | 24 | 75 | | } |
| | | 76 | | |
| | 32 | 77 | | internal DeviceCodeCredential(Func<DeviceCodeInfo, CancellationToken, Task> deviceCodeCallback, string tenantId, |
| | | 78 | | { |
| | 32 | 79 | | _clientId = clientId ?? throw new ArgumentNullException(nameof(clientId)); |
| | | 80 | | |
| | 32 | 81 | | _deviceCodeCallback = deviceCodeCallback ?? throw new ArgumentNullException(nameof(deviceCodeCallback)); |
| | | 82 | | |
| | 32 | 83 | | _pipeline = pipeline ?? CredentialPipeline.GetInstance(options); |
| | | 84 | | |
| | 32 | 85 | | _client = client ?? new MsalPublicClient(_pipeline, tenantId, clientId, AzureAuthorityHosts.GetDeviceCodeRed |
| | 32 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Interactively authenticates a user via the default browser. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 92 | | /// <returns>The result of the authentication request, containing the acquired <see cref="AccessToken"/>, and th |
| | | 93 | | internal virtual AuthenticationRecord Authenticate(CancellationToken cancellationToken = default) |
| | | 94 | | { |
| | | 95 | | // get the default scope for the authority, throw if no default scope exists |
| | 0 | 96 | | string defaultScope = AzureAuthorityHosts.GetDefaultScope(_pipeline.AuthorityHost) ?? throw new CredentialUn |
| | | 97 | | |
| | 0 | 98 | | return Authenticate(new TokenRequestContext(new string[] { defaultScope }), cancellationToken); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Interactively authenticates a user via the default browser. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 105 | | /// <returns>The <see cref="AuthenticationRecord"/> which can be used to silently authenticate the account on fu |
| | | 106 | | internal virtual async Task<AuthenticationRecord> AuthenticateAsync(CancellationToken cancellationToken = defaul |
| | | 107 | | { |
| | | 108 | | // get the default scope for the authority, throw if no default scope exists |
| | 0 | 109 | | string defaultScope = AzureAuthorityHosts.GetDefaultScope(_pipeline.AuthorityHost) ?? throw new CredentialUn |
| | | 110 | | |
| | 0 | 111 | | return await AuthenticateAsync(new TokenRequestContext(new string[] { defaultScope }), cancellationToken).Co |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Interactively authenticates a user via the default browser. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 118 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | | 119 | | /// <returns>The <see cref="AuthenticationRecord"/> of the authenticated account.</returns> |
| | | 120 | | internal virtual AuthenticationRecord Authenticate(TokenRequestContext requestContext, CancellationToken cancell |
| | | 121 | | { |
| | 0 | 122 | | return AuthenticateImplAsync(false, requestContext, cancellationToken).EnsureCompleted(); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// Interactively authenticates a user via the default browser. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 129 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | | 130 | | /// <returns>The <see cref="AuthenticationRecord"/> of the authenticated account.</returns> |
| | | 131 | | internal virtual async Task<AuthenticationRecord> AuthenticateAsync(TokenRequestContext requestContext, Cancella |
| | | 132 | | { |
| | 0 | 133 | | return await AuthenticateImplAsync(true, requestContext, cancellationToken).ConfigureAwait(false); |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Obtains a token for a user account, authenticating them through the device code authentication flow. This me |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | | 140 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 141 | | /// <returns>An <see cref="AccessToken"/> which can be used to authenticate service client calls.</returns> |
| | | 142 | | public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken = d |
| | | 143 | | { |
| | 16 | 144 | | return GetTokenImplAsync(false, requestContext, cancellationToken).EnsureCompleted(); |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Obtains a token for a user account, authenticating them through the device code authentication flow. This me |
| | | 149 | | /// </summary> |
| | | 150 | | /// <param name="requestContext">The details of the authentication request.</param> |
| | | 151 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | | 152 | | /// <returns>An <see cref="AccessToken"/> which can be used to authenticate service client calls.</returns> |
| | | 153 | | public override async ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken |
| | | 154 | | { |
| | 16 | 155 | | return await GetTokenImplAsync(true, requestContext, cancellationToken).ConfigureAwait(false); |
| | 6 | 156 | | } |
| | | 157 | | |
| | | 158 | | private async Task<AuthenticationRecord> AuthenticateImplAsync(bool async, TokenRequestContext requestContext, C |
| | | 159 | | { |
| | 0 | 160 | | using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope($"{nameof(DeviceCodeCredential)}.{nameo |
| | | 161 | | |
| | | 162 | | try |
| | | 163 | | { |
| | 0 | 164 | | AccessToken token = await GetTokenViaDeviceCodeAsync(requestContext.Scopes, async, cancellationToken).Co |
| | | 165 | | |
| | 0 | 166 | | scope.Succeeded(token); |
| | | 167 | | |
| | 0 | 168 | | return _record; |
| | | 169 | | } |
| | 0 | 170 | | catch (Exception e) |
| | | 171 | | { |
| | 0 | 172 | | throw scope.FailWrapAndThrow(e); |
| | | 173 | | } |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | private async ValueTask<AccessToken> GetTokenImplAsync(bool async, TokenRequestContext requestContext, Cancellat |
| | | 177 | | { |
| | 32 | 178 | | using CredentialDiagnosticScope scope = _pipeline.StartGetTokenScope($"{nameof(DeviceCodeCredential)}.{nameo |
| | | 179 | | |
| | | 180 | | try |
| | | 181 | | { |
| | 32 | 182 | | Exception inner = null; |
| | | 183 | | |
| | 32 | 184 | | if (_record != null) |
| | | 185 | | { |
| | | 186 | | try |
| | | 187 | | { |
| | 0 | 188 | | AuthenticationResult result = await _client.AcquireTokenSilentAsync(requestContext.Scopes, (Auth |
| | | 189 | | |
| | 0 | 190 | | return scope.Succeeded(new AccessToken(result.AccessToken, result.ExpiresOn)); |
| | | 191 | | } |
| | | 192 | | catch (MsalUiRequiredException e) |
| | | 193 | | { |
| | 0 | 194 | | inner = e; |
| | 0 | 195 | | } |
| | | 196 | | } |
| | | 197 | | |
| | 32 | 198 | | if (_disableAutomaticAuthentication) |
| | | 199 | | { |
| | 4 | 200 | | throw new AuthenticationRequiredException(AuthenticationRequiredMessage, requestContext, inner); |
| | | 201 | | } |
| | | 202 | | |
| | 28 | 203 | | return scope.Succeeded(await GetTokenViaDeviceCodeAsync(requestContext.Scopes, async, cancellationToken) |
| | | 204 | | } |
| | 20 | 205 | | catch (Exception e) |
| | | 206 | | { |
| | 20 | 207 | | throw scope.FailWrapAndThrow(e); |
| | | 208 | | } |
| | 12 | 209 | | } |
| | | 210 | | |
| | | 211 | | private async Task<AccessToken> GetTokenViaDeviceCodeAsync(string[] scopes, bool async, CancellationToken cancel |
| | | 212 | | { |
| | 48 | 213 | | AuthenticationResult result = await _client.AcquireTokenWithDeviceCodeAsync(scopes, code => DeviceCodeCallba |
| | | 214 | | |
| | 12 | 215 | | _record = new AuthenticationRecord(result, _clientId); |
| | | 216 | | |
| | 12 | 217 | | return new AccessToken(result.AccessToken, result.ExpiresOn); |
| | 12 | 218 | | } |
| | | 219 | | |
| | | 220 | | private Task DeviceCodeCallback(DeviceCodeResult deviceCode, CancellationToken cancellationToken) |
| | | 221 | | { |
| | 20 | 222 | | return _deviceCodeCallback(new DeviceCodeInfo(deviceCode), cancellationToken); |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | |
| | | 226 | | } |
| | | 227 | | } |