| | 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.IO; |
| | 7 | | using System.Text.Json; |
| | 8 | | using System.Threading; |
| | 9 | | using System.Threading.Tasks; |
| | 10 | | using Azure.Core.Pipeline; |
| | 11 | | using Microsoft.Identity.Client; |
| | 12 | |
|
| | 13 | | namespace Azure.Identity |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Account information relating to an authentication request. |
| | 17 | | /// </summary> |
| | 18 | | internal class AuthenticationRecord |
| | 19 | | { |
| | 20 | | private const string UsernamePropertyName = "username"; |
| | 21 | | private const string AuthorityPropertyName = "authority"; |
| | 22 | | private const string HomeAccountIdPropertyName = "homeAccountId"; |
| | 23 | | private const string TenantIdPropertyName = "tenantId"; |
| | 24 | | private const string ClientIdPropertyName = "clientId"; |
| | 25 | |
|
| 2 | 26 | | private static readonly JsonEncodedText s_usernamePropertyNameBytes = JsonEncodedText.Encode(UsernamePropertyNam |
| 2 | 27 | | private static readonly JsonEncodedText s_authorityPropertyNameBytes = JsonEncodedText.Encode(AuthorityPropertyN |
| 2 | 28 | | private static readonly JsonEncodedText s_homeAccountIdPropertyNameBytes = JsonEncodedText.Encode(HomeAccountIdP |
| 2 | 29 | | private static readonly JsonEncodedText s_tenantIdPropertyNameBytes = JsonEncodedText.Encode(TenantIdPropertyNam |
| 2 | 30 | | private static readonly JsonEncodedText s_clientIdPropertyNameBytes = JsonEncodedText.Encode(ClientIdPropertyNam |
| | 31 | |
|
| 6 | 32 | | internal AuthenticationRecord() |
| | 33 | | { |
| | 34 | |
|
| 6 | 35 | | } |
| | 36 | |
|
| 40 | 37 | | internal AuthenticationRecord(AuthenticationResult authResult, string clientId) |
| | 38 | | { |
| 40 | 39 | | Username = authResult.Account.Username; |
| 40 | 40 | | Authority = authResult.Account.Environment; |
| 40 | 41 | | AccountId = authResult.Account.HomeAccountId; |
| 40 | 42 | | TenantId = authResult.TenantId; |
| 40 | 43 | | ClientId = clientId; |
| 40 | 44 | | } |
| | 45 | |
|
| 12 | 46 | | internal AuthenticationRecord(string username, string authority, string homeAccountId, string tenantId, string c |
| | 47 | | { |
| | 48 | |
|
| 12 | 49 | | Username = username; |
| 12 | 50 | | Authority = authority; |
| 12 | 51 | | AccountId = new AccountId(homeAccountId); |
| 12 | 52 | | TenantId = tenantId; |
| 12 | 53 | | ClientId = clientId; |
| 12 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// The user principal or service principal name of the account. |
| | 58 | | /// </summary> |
| 94 | 59 | | public string Username { get; private set; } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// The authority host used to authenticate the account. |
| | 63 | | /// </summary> |
| 74 | 64 | | public string Authority { get; private set; } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// A unique identifier of the account. |
| | 68 | | /// </summary> |
| 14 | 69 | | public string HomeAccountId { get => AccountId.Identifier; } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// The tenant the account should authenticate in. |
| | 73 | | /// </summary> |
| 78 | 74 | | public string TenantId { get; private set; } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// The client id of the application which performed the original authentication |
| | 78 | | /// </summary> |
| 70 | 79 | | public string ClientId { get; private set; } |
| | 80 | |
|
| 110 | 81 | | internal AccountId AccountId { get; private set; } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Serializes the <see cref="AuthenticationRecord"/> to the specified <see cref="Stream"/>. |
| | 85 | | /// </summary> |
| | 86 | | /// <param name="stream">The <see cref="Stream"/> which the serialized <see cref="AuthenticationRecord"/> will b |
| | 87 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 88 | | public void Serialize(Stream stream, CancellationToken cancellationToken = default) |
| | 89 | | { |
| 6 | 90 | | if (stream is null) throw new ArgumentNullException(nameof(stream)); |
| | 91 | |
|
| 2 | 92 | | SerializeAsync(stream, false, cancellationToken).EnsureCompleted(); |
| 2 | 93 | | } |
| | 94 | |
|
| | 95 | | /// <summary> |
| | 96 | | /// Serializes the <see cref="AuthenticationRecord"/> to the specified <see cref="Stream"/>. |
| | 97 | | /// </summary> |
| | 98 | | /// <param name="stream">The <see cref="Stream"/> to which the serialized <see cref="AuthenticationRecord"/> wil |
| | 99 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 100 | | public async Task SerializeAsync(Stream stream, CancellationToken cancellationToken = default) |
| | 101 | | { |
| 8 | 102 | | if (stream is null) throw new ArgumentNullException(nameof(stream)); |
| | 103 | |
|
| 4 | 104 | | await SerializeAsync(stream, true, cancellationToken).ConfigureAwait(false); |
| 2 | 105 | | } |
| | 106 | |
|
| | 107 | |
|
| | 108 | | /// <summary> |
| | 109 | | /// Deserializes the <see cref="AuthenticationRecord"/> from the specified <see cref="Stream"/>. |
| | 110 | | /// </summary> |
| | 111 | | /// <param name="stream">The <see cref="Stream"/> from which the serialized <see cref="AuthenticationRecord"/> w |
| | 112 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 113 | | public static AuthenticationRecord Deserialize(Stream stream, CancellationToken cancellationToken = default) |
| | 114 | | { |
| 6 | 115 | | if (stream is null) throw new ArgumentNullException(nameof(stream)); |
| | 116 | |
|
| 2 | 117 | | return DeserializeAsync(stream, false, cancellationToken).EnsureCompleted(); |
| | 118 | | } |
| | 119 | |
|
| | 120 | | /// <summary> |
| | 121 | | /// Deserializes the <see cref="AuthenticationRecord"/> from the specified <see cref="Stream"/>. |
| | 122 | | /// </summary> |
| | 123 | | /// <param name="stream">The <see cref="Stream"/> from which the serialized <see cref="AuthenticationRecord"/> w |
| | 124 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param> |
| | 125 | | public static async Task<AuthenticationRecord> DeserializeAsync(Stream stream, CancellationToken cancellationTok |
| | 126 | | { |
| 8 | 127 | | if (stream is null) throw new ArgumentNullException(nameof(stream)); |
| | 128 | |
|
| 4 | 129 | | return await DeserializeAsync(stream, true, cancellationToken).ConfigureAwait(false); |
| 2 | 130 | | } |
| | 131 | |
|
| | 132 | | private async Task SerializeAsync(Stream stream, bool async, CancellationToken cancellationToken) |
| | 133 | | { |
| 6 | 134 | | using (var json = new Utf8JsonWriter(stream)) |
| | 135 | | { |
| | 136 | |
|
| 6 | 137 | | json.WriteStartObject(); |
| | 138 | |
|
| 6 | 139 | | json.WriteString(s_usernamePropertyNameBytes, Username); |
| | 140 | |
|
| 6 | 141 | | json.WriteString(s_authorityPropertyNameBytes, Authority); |
| | 142 | |
|
| 6 | 143 | | json.WriteString(s_homeAccountIdPropertyNameBytes, HomeAccountId); |
| | 144 | |
|
| 6 | 145 | | json.WriteString(s_tenantIdPropertyNameBytes, TenantId); |
| | 146 | |
|
| 6 | 147 | | json.WriteString(s_clientIdPropertyNameBytes, ClientId); |
| | 148 | |
|
| 6 | 149 | | json.WriteEndObject(); |
| | 150 | |
|
| 6 | 151 | | if (async) |
| | 152 | | { |
| 4 | 153 | | await json.FlushAsync(cancellationToken).ConfigureAwait(false); |
| | 154 | | } |
| | 155 | | else |
| | 156 | | { |
| 2 | 157 | | json.Flush(); |
| | 158 | | } |
| 4 | 159 | | } |
| 4 | 160 | | } |
| | 161 | |
|
| | 162 | | private static async Task<AuthenticationRecord> DeserializeAsync(Stream stream, bool async, CancellationToken ca |
| | 163 | | { |
| 6 | 164 | | var authProfile = new AuthenticationRecord(); |
| | 165 | |
|
| 6 | 166 | | using JsonDocument doc = async ? await JsonDocument.ParseAsync(stream, cancellationToken: cancellationToken) |
| | 167 | |
|
| 48 | 168 | | foreach (JsonProperty prop in doc.RootElement.EnumerateObject()) |
| | 169 | | { |
| 20 | 170 | | switch (prop.Name) |
| | 171 | | { |
| | 172 | | case UsernamePropertyName: |
| 4 | 173 | | authProfile.Username = prop.Value.GetString(); |
| 4 | 174 | | break; |
| | 175 | | case AuthorityPropertyName: |
| 4 | 176 | | authProfile.Authority = prop.Value.GetString(); |
| 4 | 177 | | break; |
| | 178 | | case HomeAccountIdPropertyName: |
| 4 | 179 | | authProfile.AccountId = new AccountId(prop.Value.GetString()); |
| 4 | 180 | | break; |
| | 181 | | case TenantIdPropertyName: |
| 4 | 182 | | authProfile.TenantId = prop.Value.GetString(); |
| 4 | 183 | | break; |
| | 184 | | case ClientIdPropertyName: |
| 4 | 185 | | authProfile.ClientId = prop.Value.GetString(); |
| | 186 | | break; |
| | 187 | | } |
| | 188 | | } |
| | 189 | |
|
| 4 | 190 | | return authProfile; |
| 4 | 191 | | } |
| | 192 | | } |
| | 193 | | } |