Skip to content

Commit ed6fce5

Browse files
Mixed Reality Authentication: This change builds and tests the shared code (#18397)
This change does the following: - Added a new Share.Azure.MixedReality.Authentication.Tests project to test the shared code. I did find 1 bug that presented when compiling with .NET 5.0 due to better nullable annotations. I had to pull the shared code in as source because I couldn't get `InternalsVisisbleTo` to work in this repo. - This change adds a project file to build the Mixed Reality Authentication shared code to ensure it stays healthy. - It took a while to figure out how to add a project that wasn't going to be packaged and shipped, but could also access shared code and what not. You'll see me defining the `AzureCoreSharedSources` property manually because that property is only set for official client libraries which have certain criteria like starting with "Azure.".
1 parent 769e473 commit ed6fce5

14 files changed

+243
-3
lines changed

sdk/mixedreality/Azure.MixedReality.Authentication/Azure.MixedReality.Authentication.sln

+13-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ VisualStudioVersion = 16.0.29709.97
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.MixedReality.Authentication", "src\Azure.MixedReality.Authentication.csproj", "{E33D09D9-D809-472C-82E6-6A26BDB86FC2}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.MixedReality.Authentication.Tests", "tests\Azure.MixedReality.Authentication.Tests.csproj", "{4F476D56-DDE7-43D3-8CB4-BA1E77F5A300}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.MixedReality.Authentication.Tests", "tests\Azure.MixedReality.Authentication.Tests\Azure.MixedReality.Authentication.Tests.csproj", "{4F476D56-DDE7-43D3-8CB4-BA1E77F5A300}"
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Core.TestFramework", "..\..\core\Azure.Core.TestFramework\src\Azure.Core.TestFramework.csproj", "{8052009B-2126-44A3-88CD-4F3B17894C64}"
1111
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared.Azure.MixedReality.Authentication", "shared\Shared.Azure.MixedReality.Authentication.csproj", "{BB9881FB-C892-4EFA-93F0-3A1B4DE018F6}"
13+
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared.Azure.MixedReality.Authentication.Tests", "tests\Shared.Azure.MixedReality.Authentication.Tests\Shared.Azure.MixedReality.Authentication.Tests.csproj", "{A128CD7B-EABC-48C7-BAD2-216007EF130D}"
15+
EndProject
1216
Global
1317
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1418
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +31,14 @@ Global
2731
{8052009B-2126-44A3-88CD-4F3B17894C64}.Debug|Any CPU.Build.0 = Debug|Any CPU
2832
{8052009B-2126-44A3-88CD-4F3B17894C64}.Release|Any CPU.ActiveCfg = Release|Any CPU
2933
{8052009B-2126-44A3-88CD-4F3B17894C64}.Release|Any CPU.Build.0 = Release|Any CPU
34+
{BB9881FB-C892-4EFA-93F0-3A1B4DE018F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35+
{BB9881FB-C892-4EFA-93F0-3A1B4DE018F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
36+
{BB9881FB-C892-4EFA-93F0-3A1B4DE018F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
37+
{BB9881FB-C892-4EFA-93F0-3A1B4DE018F6}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{A128CD7B-EABC-48C7-BAD2-216007EF130D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{A128CD7B-EABC-48C7-BAD2-216007EF130D}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{A128CD7B-EABC-48C7-BAD2-216007EF130D}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{A128CD7B-EABC-48C7-BAD2-216007EF130D}.Release|Any CPU.Build.0 = Release|Any CPU
3042
EndGlobalSection
3143
GlobalSection(SolutionProperties) = preSolution
3244
HideSolutionNode = FALSE

sdk/mixedreality/Azure.MixedReality.Authentication/shared/AuthenticationEndpoint.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static Uri ConstructFromDomain(string accountDomain)
1717
{
1818
Argument.AssertNotNullOrWhiteSpace(accountDomain, nameof(accountDomain));
1919

20-
if (!Uri.TryCreate($"https://sts.{accountDomain}", UriKind.Absolute, out Uri result))
20+
if (!Uri.TryCreate($"https://sts.{accountDomain}", UriKind.Absolute, out Uri? result))
2121
{
2222
throw new ArgumentException("The value could not be used to construct a valid endpoint.", nameof(accountDomain));
2323
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<IsPackable>false</IsPackable>
6+
<LangVersion>latest</LangVersion>
7+
<Nullable>Enable</Nullable>
8+
<AzureCoreSharedSources>$(RepoRoot)/sdk/core/Azure.Core/src/Shared/</AzureCoreSharedSources>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\src\Azure.MixedReality.Authentication.csproj" />
13+
</ItemGroup>
14+
15+
<!-- Shared source from Azure.Core -->
16+
<ItemGroup>
17+
<Compile Include="$(AzureCoreSharedSources)Argument.cs" Link="Shared\%(RecursiveDir)\%(Filename)%(Extension)" />
18+
</ItemGroup>
19+
20+
</Project>

sdk/mixedreality/Azure.MixedReality.Authentication/tests/Azure.MixedReality.Authentication.Tests.csproj renamed to sdk/mixedreality/Azure.MixedReality.Authentication/tests/Azure.MixedReality.Authentication.Tests/Azure.MixedReality.Authentication.Tests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313

1414
<ItemGroup>
1515
<ProjectReference Include="$(AzureCoreTestFramework)" />
16-
<ProjectReference Include="..\src\Azure.MixedReality.Authentication.csproj" />
16+
<ProjectReference Include="..\..\src\Azure.MixedReality.Authentication.csproj" />
1717
</ItemGroup>
1818
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using NUnit.Framework;
6+
7+
namespace Azure.MixedReality.Authentication.Tests
8+
{
9+
public class AuthenticationEndpointTests
10+
{
11+
[Test]
12+
public void ConstructFromDomain()
13+
{
14+
Uri expected = new Uri("https://sts.eastus2.mixedreality.com");
15+
Uri actual = AuthenticationEndpoint.ConstructFromDomain("eastus2.mixedreality.com");
16+
17+
Assert.AreEqual(expected, actual);
18+
}
19+
20+
[Test]
21+
public void ConstructFromDomainWithInvalidParameters()
22+
{
23+
ArgumentException ex = Assert.Throws<ArgumentNullException>(() => AuthenticationEndpoint.ConstructFromDomain(null!));
24+
Assert.AreEqual("accountDomain", ex.ParamName);
25+
26+
ex = Assert.Throws<ArgumentException>(() => AuthenticationEndpoint.ConstructFromDomain(string.Empty));
27+
Assert.AreEqual("accountDomain", ex.ParamName);
28+
29+
ex = Assert.Throws<ArgumentException>(() => AuthenticationEndpoint.ConstructFromDomain(" "));
30+
Assert.AreEqual("accountDomain", ex.ParamName);
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Azure.Core;
7+
using NUnit.Framework;
8+
9+
namespace Azure.MixedReality.Authentication.Tests
10+
{
11+
public class MixedRealityAccountKeyCredentialTests
12+
{
13+
private const string ExpectedTestToken = "87e9abb1-79b9-4502-bbae-cfae8c610f23:my_account_key";
14+
15+
private static readonly Guid s_testAccountId = Guid.Parse("87e9abb1-79b9-4502-bbae-cfae8c610f23");
16+
17+
private static readonly string s_testAccountKey = "my_account_key";
18+
19+
private static readonly AzureKeyCredential s_testKeyCredential = new AzureKeyCredential(s_testAccountKey);
20+
21+
[Test]
22+
public void Create()
23+
{
24+
new MixedRealityAccountKeyCredential(s_testAccountId, s_testAccountKey);
25+
new MixedRealityAccountKeyCredential(s_testAccountId, s_testKeyCredential);
26+
}
27+
28+
[Test]
29+
public void CreateWithInvalidParameters()
30+
{
31+
ArgumentException ex = Assert.Throws<ArgumentException>(() => new MixedRealityAccountKeyCredential(Guid.Empty, s_testAccountKey));
32+
Assert.AreEqual("accountId", ex.ParamName);
33+
34+
ex = Assert.Throws<ArgumentNullException>(() => new MixedRealityAccountKeyCredential(s_testAccountId, (string)null!));
35+
Assert.AreEqual("key", ex.ParamName);
36+
37+
ex = Assert.Throws<ArgumentException>(() => new MixedRealityAccountKeyCredential(s_testAccountId, ""));
38+
Assert.AreEqual("key", ex.ParamName);
39+
40+
ex = Assert.Throws<ArgumentNullException>(() => new MixedRealityAccountKeyCredential(s_testAccountId, (AzureKeyCredential)null!));
41+
Assert.AreEqual("keyCredential", ex.ParamName);
42+
}
43+
44+
[Test]
45+
public void GetToken()
46+
{
47+
MixedRealityAccountKeyCredential credential = new MixedRealityAccountKeyCredential(s_testAccountId, s_testKeyCredential);
48+
AccessToken token = credential.GetToken(default, default);
49+
Assert.AreEqual(ExpectedTestToken, token.Token);
50+
Assert.AreEqual(DateTimeOffset.MaxValue, token.ExpiresOn);
51+
}
52+
53+
[Test]
54+
public async Task GetTokenAsync()
55+
{
56+
MixedRealityAccountKeyCredential credential = new MixedRealityAccountKeyCredential(s_testAccountId, s_testKeyCredential);
57+
AccessToken token = await credential.GetTokenAsync(default, default);
58+
Assert.AreEqual(ExpectedTestToken, token.Token);
59+
Assert.AreEqual(DateTimeOffset.MaxValue, token.ExpiresOn);
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using Azure.Core;
6+
using NUnit.Framework;
7+
8+
namespace Azure.MixedReality.Authentication.Tests
9+
{
10+
public class MixedRealityTokenCredentialTests
11+
{
12+
private static readonly Guid s_testAccountId = Guid.Parse("87e9abb1-79b9-4502-bbae-cfae8c610f23");
13+
14+
private static readonly Uri s_testEndpoint = new Uri("https://sts.my.mixedreality.endpoint.com");
15+
16+
[Test]
17+
public void GetMixedRealityCredential()
18+
{
19+
MixedRealityAccountKeyCredential credential = new MixedRealityAccountKeyCredential(s_testAccountId, "my_account_key");
20+
TokenCredential actual = MixedRealityTokenCredential.GetMixedRealityCredential(s_testAccountId, s_testEndpoint, credential);
21+
22+
Assert.AreNotEqual(credential, actual);
23+
}
24+
25+
[Test]
26+
public void GetMixedRealityCredentialWithStatic()
27+
{
28+
StaticAccessTokenCredential credential = new StaticAccessTokenCredential(default);
29+
TokenCredential actual = MixedRealityTokenCredential.GetMixedRealityCredential(s_testAccountId, s_testEndpoint, credential);
30+
31+
Assert.AreEqual(credential, actual);
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<IsClientLibrary>true</IsClientLibrary>
4+
<IsDataPlaneProject>true</IsDataPlaneProject>
5+
<IsTestProject>true</IsTestProject>
6+
<Nullable>Enable</Nullable>
7+
</PropertyGroup>
8+
9+
<Import Project="$(RepoEngPath)\Directory.Build.Data.props" />
10+
11+
<PropertyGroup>
12+
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
13+
<RootNamespace>Azure.MixedReality.Authentication.Tests</RootNamespace>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="Azure.Identity" />
18+
<PackageReference Include="NUnit" />
19+
<PackageReference Include="NUnit3TestAdapter" />
20+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
21+
<PackageReference Include="Moq" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\..\src\Azure.MixedReality.Authentication.csproj" />
26+
</ItemGroup>
27+
28+
<!-- Shared source from Azure.Core -->
29+
<ItemGroup>
30+
<Compile Include="$(AzureCoreSharedSources)Argument.cs" Link="Shared\%(RecursiveDir)\%(Filename)%(Extension)" />
31+
</ItemGroup>
32+
33+
<!-- Shared source from Azure.MixedReality.Authentication -->
34+
<ItemGroup>
35+
<Compile Include="$(AzureMixedRealityAuthenticationSharedSources)\*.cs" Link="Shared\%(RecursiveDir)\%(Filename)%(Extension)" />
36+
</ItemGroup>
37+
38+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Azure.Core;
7+
using NUnit.Framework;
8+
9+
namespace Azure.MixedReality.Authentication.Tests
10+
{
11+
public class StaticAccessTokenCredentialTests
12+
{
13+
private const string ExpectedTestToken = "my_access_token";
14+
15+
private static readonly AccessToken s_fakeAccessToken = new AccessToken(ExpectedTestToken, DateTimeOffset.MaxValue);
16+
17+
[Test]
18+
public void Create()
19+
{
20+
new StaticAccessTokenCredential(s_fakeAccessToken);
21+
}
22+
23+
[Test]
24+
public void GetToken()
25+
{
26+
StaticAccessTokenCredential credential = new StaticAccessTokenCredential(s_fakeAccessToken);
27+
AccessToken token = credential.GetToken(default, default);
28+
Assert.AreEqual(ExpectedTestToken, token.Token);
29+
Assert.AreEqual(DateTimeOffset.MaxValue, token.ExpiresOn);
30+
}
31+
32+
[Test]
33+
public async Task GetTokenAsync()
34+
{
35+
StaticAccessTokenCredential credential = new StaticAccessTokenCredential(s_fakeAccessToken);
36+
AccessToken token = await credential.GetTokenAsync(default, default);
37+
Assert.AreEqual(ExpectedTestToken, token.Token);
38+
Assert.AreEqual(DateTimeOffset.MaxValue, token.ExpiresOn);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)