Skip to content

Commit

Permalink
Fix issue that homeAccountId string is not parsed correctly into Acco…
Browse files Browse the repository at this point in the history
…untId (#13932)
  • Loading branch information
erich-wang authored Sep 9, 2020
1 parent 8c509e0 commit b800a64
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
22 changes: 19 additions & 3 deletions sdk/identity/Azure.Identity/src/AuthenticationRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ internal AuthenticationRecord(AuthenticationResult authResult, string clientId)

internal AuthenticationRecord(string username, string authority, string homeAccountId, string tenantId, string clientId)
{

Username = username;
Authority = authority;
AccountId = new AccountId(homeAccountId);
AccountId = BuildAccountIdFromString(homeAccountId);
TenantId = tenantId;
ClientId = clientId;
}
Expand Down Expand Up @@ -176,7 +175,7 @@ private static async Task<AuthenticationRecord> DeserializeAsync(Stream stream,
authProfile.Authority = prop.Value.GetString();
break;
case HomeAccountIdPropertyName:
authProfile.AccountId = new AccountId(prop.Value.GetString());
authProfile.AccountId = BuildAccountIdFromString(prop.Value.GetString());
break;
case TenantIdPropertyName:
authProfile.TenantId = prop.Value.GetString();
Expand All @@ -189,5 +188,22 @@ private static async Task<AuthenticationRecord> DeserializeAsync(Stream stream,

return authProfile;
}

private static AccountId BuildAccountIdFromString(string homeAccountId)
{
//For the Microsoft identity platform (formerly named Azure AD v2.0), the identifier is the concatenation of
// Microsoft.Identity.Client.AccountId.ObjectId and Microsoft.Identity.Client.AccountId.TenantId separated by a dot.
var homeAccountSegments = homeAccountId.Split('.');
AccountId accountId;
if (homeAccountSegments.Length == 2)
{
accountId = new AccountId(homeAccountId, homeAccountSegments[0], homeAccountSegments[1]);
}
else
{
accountId = new AccountId(homeAccountId);
}
return accountId;
}
}
}
27 changes: 26 additions & 1 deletion sdk/identity/Azure.Identity/tests/AuthenticationRecordTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.Identity.Client;

using NUnit.Framework;

namespace Azure.Identity.Tests
Expand All @@ -14,6 +17,20 @@ public class AuthenticationRecordTests
{
private const int TestBufferSize = 512;

[Test]
public void AuthenticationRecordConstructor()
{
var record = new AuthenticationRecord(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
$"{Guid.NewGuid()}.{Guid.NewGuid()}", Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

IAccount account = (AuthenticationAccount)record;
Assert.NotNull(account.Username);
Assert.NotNull(account.Environment);
Assert.NotNull(account.HomeAccountId.Identifier);
Assert.NotNull(account.HomeAccountId.ObjectId);
Assert.NotNull(account.HomeAccountId.TenantId);
}

[Test]
public void SerializeDeserializeInputChecks()
{
Expand All @@ -28,23 +45,31 @@ public void SerializeDeserializeInputChecks()
[Test]
public async Task SerializeDeserializeAsync()
{
var expRecord = new AuthenticationRecord(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
var expRecord = new AuthenticationRecord(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), $"{Guid.NewGuid()}.{Guid.NewGuid()}", Guid.NewGuid().ToString(), Guid.NewGuid().ToString());

byte[] buff = new byte[TestBufferSize];

var stream = new MemoryStream(buff);

await expRecord.SerializeAsync(stream);
IAccount expAccount = (AuthenticationAccount)expRecord;

stream = new MemoryStream(buff, 0, (int)stream.Position);

var actRecord = await AuthenticationRecord.DeserializeAsync(stream);
IAccount actAccount = (AuthenticationAccount)actRecord;

Assert.AreEqual(expRecord.Username, actRecord.Username);
Assert.AreEqual(expRecord.Authority, actRecord.Authority);
Assert.AreEqual(expRecord.HomeAccountId, actRecord.HomeAccountId);
Assert.AreEqual(expRecord.TenantId, actRecord.TenantId);
Assert.AreEqual(expRecord.ClientId, actRecord.ClientId);

Assert.AreEqual(expAccount.Username, actAccount.Username);
Assert.AreEqual(expAccount.Environment, actAccount.Environment);
Assert.AreEqual(expAccount.HomeAccountId.Identifier, actAccount.HomeAccountId.Identifier);
Assert.AreEqual(expAccount.HomeAccountId.ObjectId, actAccount.HomeAccountId.ObjectId);
Assert.AreEqual(expAccount.HomeAccountId.TenantId, actAccount.HomeAccountId.TenantId);
}

[Test]
Expand Down

0 comments on commit b800a64

Please sign in to comment.