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

Mixed Reality Authentication: This change builds and tests the shared code #18397

Merged
3 commits merged into from
Feb 5, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ VisualStudioVersion = 16.0.29709.97
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.MixedReality.Authentication", "src\Azure.MixedReality.Authentication.csproj", "{E33D09D9-D809-472C-82E6-6A26BDB86FC2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.MixedReality.Authentication.Tests", "tests\Azure.MixedReality.Authentication.Tests.csproj", "{4F476D56-DDE7-43D3-8CB4-BA1E77F5A300}"
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}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Core.TestFramework", "..\..\core\Azure.Core.TestFramework\src\Azure.Core.TestFramework.csproj", "{8052009B-2126-44A3-88CD-4F3B17894C64}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared.Azure.MixedReality.Authentication", "shared\Shared.Azure.MixedReality.Authentication.csproj", "{BB9881FB-C892-4EFA-93F0-3A1B4DE018F6}"
EndProject
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +31,14 @@ Global
{8052009B-2126-44A3-88CD-4F3B17894C64}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8052009B-2126-44A3-88CD-4F3B17894C64}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8052009B-2126-44A3-88CD-4F3B17894C64}.Release|Any CPU.Build.0 = Release|Any CPU
{BB9881FB-C892-4EFA-93F0-3A1B4DE018F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB9881FB-C892-4EFA-93F0-3A1B4DE018F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB9881FB-C892-4EFA-93F0-3A1B4DE018F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB9881FB-C892-4EFA-93F0-3A1B4DE018F6}.Release|Any CPU.Build.0 = Release|Any CPU
{A128CD7B-EABC-48C7-BAD2-216007EF130D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A128CD7B-EABC-48C7-BAD2-216007EF130D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A128CD7B-EABC-48C7-BAD2-216007EF130D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A128CD7B-EABC-48C7-BAD2-216007EF130D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static Uri ConstructFromDomain(string accountDomain)
{
Argument.AssertNotNullOrWhiteSpace(accountDomain, nameof(accountDomain));

if (!Uri.TryCreate($"https://sts.{accountDomain}", UriKind.Absolute, out Uri result))
if (!Uri.TryCreate($"https://sts.{accountDomain}", UriKind.Absolute, out Uri? result))
{
throw new ArgumentException("The value could not be used to construct a valid endpoint.", nameof(accountDomain));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<Nullable>Enable</Nullable>
<AzureCoreSharedSources>$(RepoRoot)/sdk/core/Azure.Core/src/Shared/</AzureCoreSharedSources>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\Azure.MixedReality.Authentication.csproj" />
</ItemGroup>

<!-- Shared source from Azure.Core -->
<ItemGroup>
<Compile Include="$(AzureCoreSharedSources)Argument.cs" Link="Shared\%(RecursiveDir)\%(Filename)%(Extension)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@

<ItemGroup>
<ProjectReference Include="$(AzureCoreTestFramework)" />
<ProjectReference Include="..\src\Azure.MixedReality.Authentication.csproj" />
<ProjectReference Include="..\..\src\Azure.MixedReality.Authentication.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using NUnit.Framework;

namespace Azure.MixedReality.Authentication.Tests
{
public class AuthenticationEndpointTests
{
[Test]
public void ConstructFromDomain()
{
Uri expected = new Uri("https://sts.eastus2.mixedreality.com");
Uri actual = AuthenticationEndpoint.ConstructFromDomain("eastus2.mixedreality.com");

Assert.AreEqual(expected, actual);
}

[Test]
public void ConstructFromDomainWithInvalidParameters()
{
ArgumentException ex = Assert.Throws<ArgumentNullException>(() => AuthenticationEndpoint.ConstructFromDomain(null!));
Assert.AreEqual("accountDomain", ex.ParamName);

ex = Assert.Throws<ArgumentException>(() => AuthenticationEndpoint.ConstructFromDomain(string.Empty));
Assert.AreEqual("accountDomain", ex.ParamName);

ex = Assert.Throws<ArgumentException>(() => AuthenticationEndpoint.ConstructFromDomain(" "));
Assert.AreEqual("accountDomain", ex.ParamName);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading.Tasks;
using Azure.Core;
using NUnit.Framework;

namespace Azure.MixedReality.Authentication.Tests
{
public class MixedRealityAccountKeyCredentialTests
{
private const string ExpectedTestToken = "87e9abb1-79b9-4502-bbae-cfae8c610f23:my_account_key";

private static readonly Guid s_testAccountId = Guid.Parse("87e9abb1-79b9-4502-bbae-cfae8c610f23");

private static readonly string s_testAccountKey = "my_account_key";

private static readonly AzureKeyCredential s_testKeyCredential = new AzureKeyCredential(s_testAccountKey);

[Test]
public void Create()
{
new MixedRealityAccountKeyCredential(s_testAccountId, s_testAccountKey);
new MixedRealityAccountKeyCredential(s_testAccountId, s_testKeyCredential);
}

[Test]
public void CreateWithInvalidParameters()
{
ArgumentException ex = Assert.Throws<ArgumentException>(() => new MixedRealityAccountKeyCredential(Guid.Empty, s_testAccountKey));
Assert.AreEqual("accountId", ex.ParamName);

ex = Assert.Throws<ArgumentNullException>(() => new MixedRealityAccountKeyCredential(s_testAccountId, (string)null!));
Assert.AreEqual("key", ex.ParamName);

ex = Assert.Throws<ArgumentException>(() => new MixedRealityAccountKeyCredential(s_testAccountId, ""));
Assert.AreEqual("key", ex.ParamName);

ex = Assert.Throws<ArgumentNullException>(() => new MixedRealityAccountKeyCredential(s_testAccountId, (AzureKeyCredential)null!));
Assert.AreEqual("keyCredential", ex.ParamName);
}

[Test]
public void GetToken()
{
MixedRealityAccountKeyCredential credential = new MixedRealityAccountKeyCredential(s_testAccountId, s_testKeyCredential);
AccessToken token = credential.GetToken(default, default);
Assert.AreEqual(ExpectedTestToken, token.Token);
Assert.AreEqual(DateTimeOffset.MaxValue, token.ExpiresOn);
}

[Test]
public async Task GetTokenAsync()
{
MixedRealityAccountKeyCredential credential = new MixedRealityAccountKeyCredential(s_testAccountId, s_testKeyCredential);
AccessToken token = await credential.GetTokenAsync(default, default);
Assert.AreEqual(ExpectedTestToken, token.Token);
Assert.AreEqual(DateTimeOffset.MaxValue, token.ExpiresOn);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Azure.Core;
using NUnit.Framework;

namespace Azure.MixedReality.Authentication.Tests
{
public class MixedRealityTokenCredentialTests
{
private static readonly Guid s_testAccountId = Guid.Parse("87e9abb1-79b9-4502-bbae-cfae8c610f23");

private static readonly Uri s_testEndpoint = new Uri("https://sts.my.mixedreality.endpoint.com");

[Test]
public void GetMixedRealityCredential()
{
MixedRealityAccountKeyCredential credential = new MixedRealityAccountKeyCredential(s_testAccountId, "my_account_key");
TokenCredential actual = MixedRealityTokenCredential.GetMixedRealityCredential(s_testAccountId, s_testEndpoint, credential);

Assert.AreNotEqual(credential, actual);
}

[Test]
public void GetMixedRealityCredentialWithStatic()
{
StaticAccessTokenCredential credential = new StaticAccessTokenCredential(default);
TokenCredential actual = MixedRealityTokenCredential.GetMixedRealityCredential(s_testAccountId, s_testEndpoint, credential);

Assert.AreEqual(credential, actual);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<IsClientLibrary>true</IsClientLibrary>
<IsDataPlaneProject>true</IsDataPlaneProject>
<IsTestProject>true</IsTestProject>
<Nullable>Enable</Nullable>
</PropertyGroup>

<Import Project="$(RepoEngPath)\Directory.Build.Data.props" />

<PropertyGroup>
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
<RootNamespace>Azure.MixedReality.Authentication.Tests</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Identity" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Azure.MixedReality.Authentication.csproj" />
</ItemGroup>

<!-- Shared source from Azure.Core -->
<ItemGroup>
<Compile Include="$(AzureCoreSharedSources)Argument.cs" Link="Shared\%(RecursiveDir)\%(Filename)%(Extension)" />
</ItemGroup>

<!-- Shared source from Azure.MixedReality.Authentication -->
<ItemGroup>
<Compile Include="$(AzureMixedRealityAuthenticationSharedSources)\*.cs" Link="Shared\%(RecursiveDir)\%(Filename)%(Extension)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Threading.Tasks;
using Azure.Core;
using NUnit.Framework;

namespace Azure.MixedReality.Authentication.Tests
{
public class StaticAccessTokenCredentialTests
{
private const string ExpectedTestToken = "my_access_token";

private static readonly AccessToken s_fakeAccessToken = new AccessToken(ExpectedTestToken, DateTimeOffset.MaxValue);

[Test]
public void Create()
{
new StaticAccessTokenCredential(s_fakeAccessToken);
}

[Test]
public void GetToken()
{
StaticAccessTokenCredential credential = new StaticAccessTokenCredential(s_fakeAccessToken);
AccessToken token = credential.GetToken(default, default);
Assert.AreEqual(ExpectedTestToken, token.Token);
Assert.AreEqual(DateTimeOffset.MaxValue, token.ExpiresOn);
}

[Test]
public async Task GetTokenAsync()
{
StaticAccessTokenCredential credential = new StaticAccessTokenCredential(s_fakeAccessToken);
AccessToken token = await credential.GetTokenAsync(default, default);
Assert.AreEqual(ExpectedTestToken, token.Token);
Assert.AreEqual(DateTimeOffset.MaxValue, token.ExpiresOn);
}
}
}