< Summary

Class:Azure.Identity.AuthenticationRecord
Assembly:Azure.Identity
File(s):C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\AuthenticationRecord.cs
Covered lines:66
Uncovered lines:0
Coverable lines:66
Total lines:193
Line coverage:100% (66 of 66)
Covered branches:24
Total branches:24
Branch coverage:100% (24 of 24)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.cctor()-100%100%
.ctor()-100%100%
.ctor(...)-100%100%
.ctor(...)-100%100%
get_Username()-100%100%
get_Authority()-100%100%
get_HomeAccountId()-100%100%
get_TenantId()-100%100%
get_ClientId()-100%100%
get_AccountId()-100%100%
Serialize(...)-100%100%
SerializeAsync()-100%100%
Deserialize(...)-100%100%
DeserializeAsync()-100%100%
SerializeAsync()-100%100%
DeserializeAsync()-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\identity\Azure.Identity\src\AuthenticationRecord.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.IO;
 7using System.Text.Json;
 8using System.Threading;
 9using System.Threading.Tasks;
 10using Azure.Core.Pipeline;
 11using Microsoft.Identity.Client;
 12
 13namespace 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
 226        private static readonly JsonEncodedText s_usernamePropertyNameBytes = JsonEncodedText.Encode(UsernamePropertyNam
 227        private static readonly JsonEncodedText s_authorityPropertyNameBytes = JsonEncodedText.Encode(AuthorityPropertyN
 228        private static readonly JsonEncodedText s_homeAccountIdPropertyNameBytes = JsonEncodedText.Encode(HomeAccountIdP
 229        private static readonly JsonEncodedText s_tenantIdPropertyNameBytes = JsonEncodedText.Encode(TenantIdPropertyNam
 230        private static readonly JsonEncodedText s_clientIdPropertyNameBytes = JsonEncodedText.Encode(ClientIdPropertyNam
 31
 632        internal AuthenticationRecord()
 33        {
 34
 635        }
 36
 4037        internal AuthenticationRecord(AuthenticationResult authResult, string clientId)
 38        {
 4039            Username = authResult.Account.Username;
 4040            Authority = authResult.Account.Environment;
 4041            AccountId = authResult.Account.HomeAccountId;
 4042            TenantId = authResult.TenantId;
 4043            ClientId = clientId;
 4044        }
 45
 1246        internal AuthenticationRecord(string username, string authority, string homeAccountId, string tenantId, string c
 47        {
 48
 1249            Username = username;
 1250            Authority = authority;
 1251            AccountId = new AccountId(homeAccountId);
 1252            TenantId = tenantId;
 1253            ClientId = clientId;
 1254        }
 55
 56        /// <summary>
 57        /// The user principal or service principal name of the account.
 58        /// </summary>
 9459        public string Username { get; private set; }
 60
 61        /// <summary>
 62        /// The authority host used to authenticate the account.
 63        /// </summary>
 7464        public string Authority { get; private set; }
 65
 66        /// <summary>
 67        /// A unique identifier of the account.
 68        /// </summary>
 1469        public string HomeAccountId { get => AccountId.Identifier; }
 70
 71        /// <summary>
 72        /// The tenant the account should authenticate in.
 73        /// </summary>
 7874        public string TenantId { get; private set; }
 75
 76        /// <summary>
 77        /// The client id of the application which performed the original authentication
 78        /// </summary>
 7079        public string ClientId { get; private set; }
 80
 11081        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        {
 690            if (stream is null) throw new ArgumentNullException(nameof(stream));
 91
 292            SerializeAsync(stream, false, cancellationToken).EnsureCompleted();
 293        }
 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        {
 8102            if (stream is null) throw new ArgumentNullException(nameof(stream));
 103
 4104            await SerializeAsync(stream, true, cancellationToken).ConfigureAwait(false);
 2105        }
 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        {
 6115            if (stream is null) throw new ArgumentNullException(nameof(stream));
 116
 2117            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        {
 8127            if (stream is null) throw new ArgumentNullException(nameof(stream));
 128
 4129            return await DeserializeAsync(stream, true, cancellationToken).ConfigureAwait(false);
 2130        }
 131
 132        private async Task SerializeAsync(Stream stream, bool async, CancellationToken cancellationToken)
 133        {
 6134            using (var json = new Utf8JsonWriter(stream))
 135            {
 136
 6137                json.WriteStartObject();
 138
 6139                json.WriteString(s_usernamePropertyNameBytes, Username);
 140
 6141                json.WriteString(s_authorityPropertyNameBytes, Authority);
 142
 6143                json.WriteString(s_homeAccountIdPropertyNameBytes, HomeAccountId);
 144
 6145                json.WriteString(s_tenantIdPropertyNameBytes, TenantId);
 146
 6147                json.WriteString(s_clientIdPropertyNameBytes, ClientId);
 148
 6149                json.WriteEndObject();
 150
 6151                if (async)
 152                {
 4153                    await json.FlushAsync(cancellationToken).ConfigureAwait(false);
 154                }
 155                else
 156                {
 2157                    json.Flush();
 158                }
 4159            }
 4160        }
 161
 162        private static async Task<AuthenticationRecord> DeserializeAsync(Stream stream, bool async, CancellationToken ca
 163        {
 6164            var authProfile = new AuthenticationRecord();
 165
 6166            using JsonDocument doc = async ? await JsonDocument.ParseAsync(stream, cancellationToken: cancellationToken)
 167
 48168            foreach (JsonProperty prop in doc.RootElement.EnumerateObject())
 169            {
 20170                switch (prop.Name)
 171                {
 172                    case UsernamePropertyName:
 4173                        authProfile.Username = prop.Value.GetString();
 4174                        break;
 175                    case AuthorityPropertyName:
 4176                        authProfile.Authority = prop.Value.GetString();
 4177                        break;
 178                    case HomeAccountIdPropertyName:
 4179                        authProfile.AccountId = new AccountId(prop.Value.GetString());
 4180                        break;
 181                    case TenantIdPropertyName:
 4182                        authProfile.TenantId = prop.Value.GetString();
 4183                        break;
 184                    case ClientIdPropertyName:
 4185                        authProfile.ClientId = prop.Value.GetString();
 186                        break;
 187                }
 188            }
 189
 4190            return authProfile;
 4191        }
 192    }
 193}