Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue that homeAccountId string not parsed correctly into AccoundId #13932

Merged
merged 1 commit into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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