Skip to content

Commit

Permalink
Upgrade to .NET 8
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Chang <[email protected]>
  • Loading branch information
mocsharp committed Dec 20, 2023
1 parent 758ccea commit 879bd0b
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 71 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "6.0.100",
"version": "8.0.100",
"rollForward": "latestFeature"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task InvokeAsync(HttpContext httpContext)
}
try
{
var authHeader = AuthenticationHeaderValue.Parse(httpContext.Request.Headers["Authorization"]);
var authHeader = AuthenticationHeaderValue.Parse(httpContext.Request.Headers.Authorization!);
if (authHeader.Scheme == "Basic")
{
var credentialBytes = Convert.FromBase64String(authHeader.Parameter ?? "");
Expand Down
5 changes: 2 additions & 3 deletions src/Authentication/Middleware/BypassAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ public class BypassAuthenticationHandler : AuthenticationHandler<AuthenticationS
public BypassAuthenticationHandler(
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock)
: base(options, logger, encoder, clock)
UrlEncoder encoder)
: base(options, logger, encoder)
{
}

Expand Down
29 changes: 9 additions & 20 deletions src/Authentication/Monai.Deploy.Security.Authentication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
~ limitations under the License.
-->
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>Monai.Deploy.Security.Authentication</RootNamespace>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
Expand All @@ -26,12 +25,10 @@
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\LICENSE" Link="LICENSE" PackagePath="" Pack="true" />
<None Include="..\..\third-party-licenses.md" Link="third-party-licenses.md" PackagePath="" Pack="true" />
</ItemGroup>

<PropertyGroup>
<PackageId>Monai.Deploy.Security</PackageId>
<Version>0.1.0</Version>
Expand All @@ -45,45 +42,37 @@
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="..\.sonarlint\project-monai_monai-deploy-security\CSharp\SonarLint.xml" Link="SonarLint.xml" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\AssemblyInfo.cs" Link="AssemblyInfo.cs" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Tests\**" />
<EmbeddedResource Remove="Tests\**" />
<None Remove="Tests\**" />
</ItemGroup>

<ItemGroup>
<None Remove="example.json" />
</ItemGroup>

<ItemGroup>
<Content Include="example.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Ardalis.GuardClauses" Version="4.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.15" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="6.0.0" />
<PackageReference Include="Ardalis.GuardClauses" Version="4.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="8.0.0" />
</ItemGroup>

</Project>
</Project>
16 changes: 8 additions & 8 deletions src/Authentication/Tests/BasicAuthorizationMiddlewareTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public partial class BasicAuthorizationMiddlewareTest
[Fact]
public async Task GivenConfigurationFileToBypassAuthentication_ExpectToBypassAuthentication()
{
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basicbypass.json")).StartAsync().ConfigureAwait(false);
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basicbypass.json")).StartAsync().ConfigureAwait(true);

var server = host.GetTestServer();
server.BaseAddress = new Uri("https://example.com/");

var client = server.CreateClient();
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false);
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(true);

Assert.True(responseMessage.IsSuccessStatusCode);
}
Expand All @@ -47,43 +47,43 @@ public async Task GivenConfigurationFileToBypassAuthentication_ExpectToBypassAut
[Fact]
public async Task GivenConfigurationFileWithBasicConfigured_WhenUserIsNotAuthenticated_ExpectToDenyRequest()
{
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(false);
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(true);

var server = host.GetTestServer();
server.BaseAddress = new Uri("https://example.com/");

var client = server.CreateClient();
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false);
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(true);

Assert.Equal(HttpStatusCode.Unauthorized, responseMessage.StatusCode);
}

[Fact]
public async Task GivenConfigurationFileWithBasicConfigured_WhenUserIsAuthenticated_ExpectToAllowRequest()
{
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(false);
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(true);

var server = host.GetTestServer();
server.BaseAddress = new Uri("https://example.com/");

var client = server.CreateClient();
client.DefaultRequestHeaders.Add("Authorization", $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes("user:pass"))}");
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false);
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(true);

Assert.Equal(HttpStatusCode.OK, responseMessage.StatusCode);
}

[Fact]
public async Task GivenConfigurationFileWithBasicConfigured_WhenHeaderIsInvalid_ExpectToDenyRequest()
{
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(false);
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(true);

var server = host.GetTestServer();
server.BaseAddress = new Uri("https://example.com/");

var client = server.CreateClient();
client.DefaultRequestHeaders.Add("Authorization", $"BasicBad {Convert.ToBase64String(Encoding.UTF8.GetBytes("user:pass"))}");
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false);
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(true);

Assert.Equal(HttpStatusCode.Unauthorized, responseMessage.StatusCode);
}
Expand Down
38 changes: 19 additions & 19 deletions src/Authentication/Tests/EndpointAuthorizationMiddlewareTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,34 @@ public async Task GivenConfigurationFilesIsBad_ExpectExceptionToBeThrown(string
{
await Assert.ThrowsAsync<InvalidOperationException>(async () =>
{
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer(configFile)).StartAsync().ConfigureAwait(false);
}).ConfigureAwait(false);
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer(configFile)).StartAsync().ConfigureAwait(true);
}).ConfigureAwait(true);
}

[Fact]
public async Task GivenConfigurationFileToBypassAuthentication_ExpectToBypassAuthentication()
{
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.bypassd.json")).StartAsync().ConfigureAwait(false);
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.bypassd.json")).StartAsync().ConfigureAwait(true);

var server = host.GetTestServer();
server.BaseAddress = new Uri("https://example.com/");

var client = server.CreateClient();
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false);
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(true);

Assert.True(responseMessage.IsSuccessStatusCode);
}

[Fact]
public async Task GivenConfigurationFileWithOpenIdConfigured_WhenUserIsNotAuthenticated_ExpectToDenyRequest()
{
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.auth.json")).StartAsync().ConfigureAwait(false);
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.auth.json")).StartAsync().ConfigureAwait(true);

var server = host.GetTestServer();
server.BaseAddress = new Uri("https://example.com/");

var client = server.CreateClient();
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false);
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(true);

Assert.Equal(HttpStatusCode.Unauthorized, responseMessage.StatusCode);
}
Expand All @@ -78,7 +78,7 @@ public async Task GivenConfigurationFileWithOpenIdConfigured_WhenUserIsNotAuthen
[InlineData("role-with-test")]
public async Task GivenConfigurationFileWithOpenIdConfigured_WhenUserIsAuthenticated_ExpectToServeTheRequest(string role)
{
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.auth.json")).StartAsync().ConfigureAwait(false);
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.auth.json")).StartAsync().ConfigureAwait(true);

var server = host.GetTestServer();
server.BaseAddress = new Uri("https://example.com/");
Expand All @@ -87,11 +87,11 @@ public async Task GivenConfigurationFileWithOpenIdConfigured_WhenUserIsAuthentic

var client = server.CreateClient();
client.DefaultRequestHeaders.Add("Authorization", $"{JwtBearerDefaults.AuthenticationScheme} {token}");
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false);
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(true);

Assert.Equal(HttpStatusCode.OK, responseMessage.StatusCode);

var data = await responseMessage.Content.ReadFromJsonAsync<List<string>>().ConfigureAwait(false);
var data = await responseMessage.Content.ReadFromJsonAsync<List<string>>().ConfigureAwait(true);

Assert.NotNull(data);
Assert.Collection(data,
Expand All @@ -104,7 +104,7 @@ public async Task GivenConfigurationFileWithOpenIdConfigured_WhenUserIsAuthentic
[InlineData("role-without-test")]
public async Task GivenConfigurationFileWithOpenIdConfigured_WhenUserIsAuthenticatedWithoutProperRoles_ExpectToDenyRequest(string role)
{
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.auth.json")).StartAsync().ConfigureAwait(false);
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.auth.json")).StartAsync().ConfigureAwait(true);

var server = host.GetTestServer();
server.BaseAddress = new Uri("https://example.com/");
Expand All @@ -113,7 +113,7 @@ public async Task GivenConfigurationFileWithOpenIdConfigured_WhenUserIsAuthentic

var client = server.CreateClient();
client.DefaultRequestHeaders.Add("Authorization", $"{JwtBearerDefaults.AuthenticationScheme} {token}");
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false);
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(true);

Assert.Equal(HttpStatusCode.Forbidden, responseMessage.StatusCode);
}
Expand All @@ -122,7 +122,7 @@ public async Task GivenConfigurationFileWithOpenIdConfigured_WhenUserIsAuthentic
[InlineData("role-with-test")]
public async Task GivenConfigurationFileWithOpenIdConfigured_WhenUserProvidesAnExpiredToken_ExpectToDenyRequest(string role)
{
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.auth.json")).StartAsync().ConfigureAwait(false);
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.auth.json")).StartAsync().ConfigureAwait(true);

var server = host.GetTestServer();
server.BaseAddress = new Uri("https://example.com/");
Expand All @@ -131,7 +131,7 @@ public async Task GivenConfigurationFileWithOpenIdConfigured_WhenUserProvidesAnE

var client = server.CreateClient();
client.DefaultRequestHeaders.Add("Authorization", $"{JwtBearerDefaults.AuthenticationScheme} {token}");
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false);
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(true);

Assert.Equal(HttpStatusCode.Unauthorized, responseMessage.StatusCode);
}
Expand All @@ -140,43 +140,43 @@ public async Task GivenConfigurationFileWithOpenIdConfigured_WhenUserProvidesAnE
[Fact]
public async Task GivenConfigurationFileWithBasicConfigured_WhenUserIsNotAuthenticated_ExpectToDenyRequest()
{
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(false);
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(true);

var server = host.GetTestServer();
server.BaseAddress = new Uri("https://example.com/");

var client = server.CreateClient();
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false);
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(true);

Assert.Equal(HttpStatusCode.Unauthorized, responseMessage.StatusCode);
}

[Fact]
public async Task GivenConfigurationFileWithBasicConfigured_WhenUserIsAuthenticated_ExpectToAllowRequest()
{
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(false);
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(true);

var server = host.GetTestServer();
server.BaseAddress = new Uri("https://example.com/");

var client = server.CreateClient();
client.DefaultRequestHeaders.Add("Authorization", $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes("user:pass"))}");
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false);
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(true);

Assert.Equal(HttpStatusCode.OK, responseMessage.StatusCode);
}

[Fact]
public async Task GivenConfigurationFileWithBasicConfigured_WhenHeaderIsInvalid_ExpectToDenyRequest()
{
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(false);
using var host = await new HostBuilder().ConfigureWebHost(SetupWebServer("test.basic.json")).StartAsync().ConfigureAwait(true);

var server = host.GetTestServer();
server.BaseAddress = new Uri("https://example.com/");

var client = server.CreateClient();
client.DefaultRequestHeaders.Add("Authorization", $"BasicBad {Convert.ToBase64String(Encoding.UTF8.GetBytes("user:pass"))}");
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(false);
var responseMessage = await client.GetAsync("api/Test").ConfigureAwait(true);

Assert.Equal(HttpStatusCode.Unauthorized, responseMessage.StatusCode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,29 @@
~ limitations under the License.
-->
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.15" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Monai.Deploy.Security.Authentication.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="test.auth.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
Expand All @@ -56,7 +51,9 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ProjectExtensions><VisualStudio><UserProperties test_1basic_1json__JsonSchema="BasicAuthorizationMiddlewareTestBasicAuthorizationMiddlewareTestBasicAuthorizationMiddlewareTest" /></VisualStudio></ProjectExtensions>

</Project>
<ProjectExtensions>
<VisualStudio>
<UserProperties test_1basic_1json__JsonSchema="BasicAuthorizationMiddlewareTestBasicAuthorizationMiddlewareTestBasicAuthorizationMiddlewareTest" />
</VisualStudio>
</ProjectExtensions>
</Project>
Loading

0 comments on commit 879bd0b

Please sign in to comment.