committed by
GitHub
14 changed files with 625 additions and 12 deletions
@ -0,0 +1,26 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFrameworks>$(NetFrameworkTargetFrameworks);$(NetCoreTargetFrameworks)</TargetFrameworks> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\OpenIddict.Client.SystemNetHttp\OpenIddict.Client.SystemNetHttp.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" /> |
|||
<PackageReference Include="Moq" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' "> |
|||
<Reference Include="System.Net.Http" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<Using Include="OpenIddict.Abstractions" /> |
|||
<Using Include="OpenIddict.Abstractions.OpenIddictConstants" Static="true" /> |
|||
<Using Include="OpenIddict.Abstractions.OpenIddictResources" Alias="SR" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,69 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Client.SystemNetHttp.Tests; |
|||
|
|||
public class OpenIddictClientSystemNetHttpBuilderTests |
|||
{ |
|||
[Fact] |
|||
public void Constructor_ThrowsAnExceptionForNullServices() |
|||
{ |
|||
// Arrange
|
|||
var services = (IServiceCollection) null!; |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => new OpenIddictClientSystemNetHttpBuilder(services)); |
|||
|
|||
Assert.Equal("services", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void EnableSensitivePayloadLogging_IsDisabledByDefault() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
CreateBuilder(services); |
|||
|
|||
// Act
|
|||
var options = GetOptions(services); |
|||
|
|||
// Assert
|
|||
Assert.False(options.EnableSensitivePayloadLogging); |
|||
} |
|||
|
|||
[Fact] |
|||
public void EnableSensitivePayloadLogging_SetsSensitivePayloadLoggingEnabled() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.EnableSensitivePayloadLogging(); |
|||
|
|||
var options = GetOptions(services); |
|||
|
|||
// Assert
|
|||
Assert.True(options.EnableSensitivePayloadLogging); |
|||
} |
|||
|
|||
private static IServiceCollection CreateServices() |
|||
=> new ServiceCollection().AddOptions(); |
|||
|
|||
private static OpenIddictClientSystemNetHttpBuilder CreateBuilder(IServiceCollection services) |
|||
=> new(services); |
|||
|
|||
private static OpenIddictClientSystemNetHttpOptions GetOptions(IServiceCollection services) |
|||
{ |
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptions<OpenIddictClientSystemNetHttpOptions>>(); |
|||
return options.Value; |
|||
} |
|||
} |
|||
@ -0,0 +1,143 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System.Globalization; |
|||
using System.Net; |
|||
using System.Net.Http; |
|||
using System.Text; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using Xunit; |
|||
using static OpenIddict.Client.OpenIddictClientEvents; |
|||
using static OpenIddict.Client.SystemNetHttp.OpenIddictClientSystemNetHttpHandlers; |
|||
|
|||
namespace OpenIddict.Client.SystemNetHttp.Tests; |
|||
|
|||
public class OpenIddictClientSystemNetHttpHandlersTests |
|||
{ |
|||
private const string Payload = "SENSITIVE_PAYLOAD"; |
|||
|
|||
[Theory] |
|||
[InlineData(false)] |
|||
[InlineData(true)] |
|||
public async Task ExtractJsonHttpResponse_OnlyLogsPayloadWhenSensitivePayloadLoggingIsEnabled(bool enabled) |
|||
{ |
|||
// Arrange
|
|||
var logger = new Mock<ILogger<OpenIddictClientDispatcher>>(); |
|||
|
|||
using var response = new HttpResponseMessage(HttpStatusCode.OK) |
|||
{ |
|||
// Note: the payload is deliberately malformed to trigger the JSON deserialization error.
|
|||
Content = new StringContent("{\"access_token\":\"" + Payload + "\"", Encoding.UTF8, "application/json") |
|||
}; |
|||
|
|||
var context = new PrepareDeviceAuthorizationRequestContext(CreateTransaction(logger.Object, response)); |
|||
var handler = new ExtractJsonHttpResponse<PrepareDeviceAuthorizationRequestContext>(CreateOptions(enabled)); |
|||
|
|||
// Act
|
|||
await handler.HandleAsync(context); |
|||
|
|||
// Assert
|
|||
Assert.True(context.IsRejected); |
|||
Assert.Equal(Errors.ServerError, context.Error); |
|||
|
|||
logger.Verify(logger => logger.Log( |
|||
LogLevel.Error, |
|||
It.Is<EventId>(id => id.Id == 6183), |
|||
It.Is<It.IsAnyType>((state, type) => Convert.ToString(state, CultureInfo.InvariantCulture)!.Contains(Payload) == enabled), |
|||
It.IsAny<Exception>(), |
|||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()), Times.Once()); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(false)] |
|||
[InlineData(true)] |
|||
public async Task ValidateHttpResponse_OnlyLogsPayloadOfGenericErrorsWhenSensitivePayloadLoggingIsEnabled(bool enabled) |
|||
{ |
|||
// Arrange
|
|||
var logger = new Mock<ILogger<OpenIddictClientDispatcher>>(); |
|||
|
|||
using var response = new HttpResponseMessage(HttpStatusCode.InternalServerError) |
|||
{ |
|||
Content = new StringContent(Payload, Encoding.UTF8, "text/plain") |
|||
}; |
|||
|
|||
var context = new PrepareDeviceAuthorizationRequestContext(CreateTransaction(logger.Object, response)); |
|||
var handler = new ValidateHttpResponse<PrepareDeviceAuthorizationRequestContext>(CreateOptions(enabled)); |
|||
|
|||
// Act
|
|||
await handler.HandleAsync(context); |
|||
|
|||
// Assert
|
|||
Assert.True(context.IsRejected); |
|||
Assert.Equal(Errors.ServerError, context.Error); |
|||
|
|||
logger.Verify(logger => logger.Log( |
|||
LogLevel.Error, |
|||
It.Is<EventId>(id => id.Id == 6184), |
|||
It.Is<It.IsAnyType>((state, type) => Convert.ToString(state, CultureInfo.InvariantCulture)!.Contains(Payload) == enabled), |
|||
It.IsAny<Exception>(), |
|||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()), Times.Once()); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(false)] |
|||
[InlineData(true)] |
|||
public async Task ValidateHttpResponse_OnlyLogsPayloadOfUnsupportedResponsesWhenSensitivePayloadLoggingIsEnabled(bool enabled) |
|||
{ |
|||
// Arrange
|
|||
var logger = new Mock<ILogger<OpenIddictClientDispatcher>>(); |
|||
|
|||
using var response = new HttpResponseMessage(HttpStatusCode.OK) |
|||
{ |
|||
Content = new StringContent(Payload, Encoding.UTF8, "text/plain") |
|||
}; |
|||
|
|||
var context = new PrepareDeviceAuthorizationRequestContext(CreateTransaction(logger.Object, response)); |
|||
var handler = new ValidateHttpResponse<PrepareDeviceAuthorizationRequestContext>(CreateOptions(enabled)); |
|||
|
|||
// Act
|
|||
await handler.HandleAsync(context); |
|||
|
|||
// Assert
|
|||
Assert.True(context.IsRejected); |
|||
Assert.Equal(Errors.ServerError, context.Error); |
|||
|
|||
logger.Verify(logger => logger.Log( |
|||
LogLevel.Error, |
|||
It.Is<EventId>(id => id.Id == 6185), |
|||
It.Is<It.IsAnyType>((state, type) => Convert.ToString(state, CultureInfo.InvariantCulture)!.Contains(Payload) == enabled), |
|||
It.IsAny<Exception>(), |
|||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()), Times.Once()); |
|||
} |
|||
|
|||
private static IOptionsMonitor<OpenIddictClientSystemNetHttpOptions> CreateOptions(bool enabled) |
|||
=> Mock.Of<IOptionsMonitor<OpenIddictClientSystemNetHttpOptions>>(monitor => |
|||
monitor.CurrentValue == new OpenIddictClientSystemNetHttpOptions |
|||
{ |
|||
EnableSensitivePayloadLogging = enabled |
|||
}); |
|||
|
|||
private static OpenIddictClientTransaction CreateTransaction( |
|||
ILogger<OpenIddictClientDispatcher> logger, HttpResponseMessage response) |
|||
{ |
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(logger); |
|||
|
|||
var transaction = new OpenIddictClientTransaction |
|||
{ |
|||
CancellationToken = CancellationToken.None, |
|||
Options = new OpenIddictClientOptions(), |
|||
ServiceProvider = services.BuildServiceProvider() |
|||
}; |
|||
|
|||
transaction.Properties[typeof(HttpResponseMessage).FullName!] = response; |
|||
|
|||
return transaction; |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFrameworks>$(NetFrameworkTargetFrameworks);$(NetCoreTargetFrameworks)</TargetFrameworks> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\OpenIddict.Validation.SystemNetHttp\OpenIddict.Validation.SystemNetHttp.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" /> |
|||
<PackageReference Include="Moq" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' "> |
|||
<Reference Include="System.Net.Http" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<Using Include="OpenIddict.Abstractions" /> |
|||
<Using Include="OpenIddict.Abstractions.OpenIddictConstants" Static="true" /> |
|||
<Using Include="OpenIddict.Abstractions.OpenIddictResources" Alias="SR" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,69 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Xunit; |
|||
|
|||
namespace OpenIddict.Validation.SystemNetHttp.Tests; |
|||
|
|||
public class OpenIddictValidationSystemNetHttpBuilderTests |
|||
{ |
|||
[Fact] |
|||
public void Constructor_ThrowsAnExceptionForNullServices() |
|||
{ |
|||
// Arrange
|
|||
var services = (IServiceCollection) null!; |
|||
|
|||
// Act and assert
|
|||
var exception = Assert.Throws<ArgumentNullException>(() => new OpenIddictValidationSystemNetHttpBuilder(services)); |
|||
|
|||
Assert.Equal("services", exception.ParamName); |
|||
} |
|||
|
|||
[Fact] |
|||
public void EnableSensitivePayloadLogging_IsDisabledByDefault() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
CreateBuilder(services); |
|||
|
|||
// Act
|
|||
var options = GetOptions(services); |
|||
|
|||
// Assert
|
|||
Assert.False(options.EnableSensitivePayloadLogging); |
|||
} |
|||
|
|||
[Fact] |
|||
public void EnableSensitivePayloadLogging_SetsSensitivePayloadLoggingEnabled() |
|||
{ |
|||
// Arrange
|
|||
var services = CreateServices(); |
|||
var builder = CreateBuilder(services); |
|||
|
|||
// Act
|
|||
builder.EnableSensitivePayloadLogging(); |
|||
|
|||
var options = GetOptions(services); |
|||
|
|||
// Assert
|
|||
Assert.True(options.EnableSensitivePayloadLogging); |
|||
} |
|||
|
|||
private static IServiceCollection CreateServices() |
|||
=> new ServiceCollection().AddOptions(); |
|||
|
|||
private static OpenIddictValidationSystemNetHttpBuilder CreateBuilder(IServiceCollection services) |
|||
=> new(services); |
|||
|
|||
private static OpenIddictValidationSystemNetHttpOptions GetOptions(IServiceCollection services) |
|||
{ |
|||
var provider = services.BuildServiceProvider(); |
|||
var options = provider.GetRequiredService<IOptions<OpenIddictValidationSystemNetHttpOptions>>(); |
|||
return options.Value; |
|||
} |
|||
} |
|||
@ -0,0 +1,143 @@ |
|||
/* |
|||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
|
|||
* See https://github.com/openiddict/openiddict-core for more information concerning
|
|||
* the license and the contributors participating to this project. |
|||
*/ |
|||
|
|||
using System.Globalization; |
|||
using System.Net; |
|||
using System.Net.Http; |
|||
using System.Text; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.Extensions.Options; |
|||
using Moq; |
|||
using Xunit; |
|||
using static OpenIddict.Validation.OpenIddictValidationEvents; |
|||
using static OpenIddict.Validation.SystemNetHttp.OpenIddictValidationSystemNetHttpHandlers; |
|||
|
|||
namespace OpenIddict.Validation.SystemNetHttp.Tests; |
|||
|
|||
public class OpenIddictValidationSystemNetHttpHandlersTests |
|||
{ |
|||
private const string Payload = "SENSITIVE_PAYLOAD"; |
|||
|
|||
[Theory] |
|||
[InlineData(false)] |
|||
[InlineData(true)] |
|||
public async Task ExtractJsonHttpResponse_OnlyLogsPayloadWhenSensitivePayloadLoggingIsEnabled(bool enabled) |
|||
{ |
|||
// Arrange
|
|||
var logger = new Mock<ILogger<OpenIddictValidationDispatcher>>(); |
|||
|
|||
using var response = new HttpResponseMessage(HttpStatusCode.OK) |
|||
{ |
|||
// Note: the payload is deliberately malformed to trigger the JSON deserialization error.
|
|||
Content = new StringContent("{\"access_token\":\"" + Payload + "\"", Encoding.UTF8, "application/json") |
|||
}; |
|||
|
|||
var context = new PrepareConfigurationRequestContext(CreateTransaction(logger.Object, response)); |
|||
var handler = new ExtractJsonHttpResponse<PrepareConfigurationRequestContext>(CreateOptions(enabled)); |
|||
|
|||
// Act
|
|||
await handler.HandleAsync(context); |
|||
|
|||
// Assert
|
|||
Assert.True(context.IsRejected); |
|||
Assert.Equal(Errors.ServerError, context.Error); |
|||
|
|||
logger.Verify(logger => logger.Log( |
|||
LogLevel.Error, |
|||
It.Is<EventId>(id => id.Id == 6183), |
|||
It.Is<It.IsAnyType>((state, type) => Convert.ToString(state, CultureInfo.InvariantCulture)!.Contains(Payload) == enabled), |
|||
It.IsAny<Exception>(), |
|||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()), Times.Once()); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(false)] |
|||
[InlineData(true)] |
|||
public async Task ValidateHttpResponse_OnlyLogsPayloadOfGenericErrorsWhenSensitivePayloadLoggingIsEnabled(bool enabled) |
|||
{ |
|||
// Arrange
|
|||
var logger = new Mock<ILogger<OpenIddictValidationDispatcher>>(); |
|||
|
|||
using var response = new HttpResponseMessage(HttpStatusCode.InternalServerError) |
|||
{ |
|||
Content = new StringContent(Payload, Encoding.UTF8, "text/plain") |
|||
}; |
|||
|
|||
var context = new PrepareConfigurationRequestContext(CreateTransaction(logger.Object, response)); |
|||
var handler = new ValidateHttpResponse<PrepareConfigurationRequestContext>(CreateOptions(enabled)); |
|||
|
|||
// Act
|
|||
await handler.HandleAsync(context); |
|||
|
|||
// Assert
|
|||
Assert.True(context.IsRejected); |
|||
Assert.Equal(Errors.ServerError, context.Error); |
|||
|
|||
logger.Verify(logger => logger.Log( |
|||
LogLevel.Error, |
|||
It.Is<EventId>(id => id.Id == 6184), |
|||
It.Is<It.IsAnyType>((state, type) => Convert.ToString(state, CultureInfo.InvariantCulture)!.Contains(Payload) == enabled), |
|||
It.IsAny<Exception>(), |
|||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()), Times.Once()); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(false)] |
|||
[InlineData(true)] |
|||
public async Task ValidateHttpResponse_OnlyLogsPayloadOfUnsupportedResponsesWhenSensitivePayloadLoggingIsEnabled(bool enabled) |
|||
{ |
|||
// Arrange
|
|||
var logger = new Mock<ILogger<OpenIddictValidationDispatcher>>(); |
|||
|
|||
using var response = new HttpResponseMessage(HttpStatusCode.OK) |
|||
{ |
|||
Content = new StringContent(Payload, Encoding.UTF8, "text/plain") |
|||
}; |
|||
|
|||
var context = new PrepareConfigurationRequestContext(CreateTransaction(logger.Object, response)); |
|||
var handler = new ValidateHttpResponse<PrepareConfigurationRequestContext>(CreateOptions(enabled)); |
|||
|
|||
// Act
|
|||
await handler.HandleAsync(context); |
|||
|
|||
// Assert
|
|||
Assert.True(context.IsRejected); |
|||
Assert.Equal(Errors.ServerError, context.Error); |
|||
|
|||
logger.Verify(logger => logger.Log( |
|||
LogLevel.Error, |
|||
It.Is<EventId>(id => id.Id == 6185), |
|||
It.Is<It.IsAnyType>((state, type) => Convert.ToString(state, CultureInfo.InvariantCulture)!.Contains(Payload) == enabled), |
|||
It.IsAny<Exception>(), |
|||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()), Times.Once()); |
|||
} |
|||
|
|||
private static IOptionsMonitor<OpenIddictValidationSystemNetHttpOptions> CreateOptions(bool enabled) |
|||
=> Mock.Of<IOptionsMonitor<OpenIddictValidationSystemNetHttpOptions>>(monitor => |
|||
monitor.CurrentValue == new OpenIddictValidationSystemNetHttpOptions |
|||
{ |
|||
EnableSensitivePayloadLogging = enabled |
|||
}); |
|||
|
|||
private static OpenIddictValidationTransaction CreateTransaction( |
|||
ILogger<OpenIddictValidationDispatcher> logger, HttpResponseMessage response) |
|||
{ |
|||
var services = new ServiceCollection(); |
|||
services.AddSingleton(logger); |
|||
|
|||
var transaction = new OpenIddictValidationTransaction |
|||
{ |
|||
CancellationToken = CancellationToken.None, |
|||
Options = new OpenIddictValidationOptions(), |
|||
ServiceProvider = services.BuildServiceProvider() |
|||
}; |
|||
|
|||
transaction.Properties[typeof(HttpResponseMessage).FullName!] = response; |
|||
|
|||
return transaction; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue