Skip to content

Commit

Permalink
Minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfeeddeadbeef committed Apr 10, 2022
1 parent 8e4fae1 commit be9d81f
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 27 deletions.
12 changes: 11 additions & 1 deletion src/TBC.OpenBanking.Jws.Http/src/JwsMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ private async Task<HttpRequestMessage> ProcessRequestAsync(
if (request.Content is not null)
{
// This is ugly, but there's no better way (so far):

#if NET5_0_OR_GREATER
httpData.Body = await request.Content!.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false);
#else
httpData.Body = await request.Content!.ReadAsByteArrayAsync().ConfigureAwait(false);
#endif

httpData.AppendHeaders(request.Content!.Headers, true);
}
Expand Down Expand Up @@ -191,7 +196,7 @@ private async Task<HttpResponseMessage> ProcessResponseAsync(
{
var httpData = new HttpResponseData
{
StatusCode = ((uint)response.StatusCode).ToString()
StatusCode = ((uint)response.StatusCode).ToString(System.Globalization.CultureInfo.InvariantCulture)
};

httpData.AppendHeaders(response.Headers, true);
Expand All @@ -201,7 +206,12 @@ private async Task<HttpResponseMessage> ProcessResponseAsync(
httpData.AppendHeaders(response.Content!.Headers, true);

// This is ugly, but there's no better way (so far):

#if NET5_0_OR_GREATER
httpData.Body = await response.Content!.ReadAsByteArrayAsync(cancellationToken).ConfigureAwait(false);
#else
httpData.Body = await response.Content!.ReadAsByteArrayAsync().ConfigureAwait(false);
#endif
}

this.verifier!.VerifySignature(httpData, DateTime.Now);
Expand Down
6 changes: 4 additions & 2 deletions src/TBC.OpenBanking.Jws/src/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Reviewed")]
[assembly: SuppressMessage("CodeQuality", "IDE0079:Remove unnecessary suppression", Justification = "Reviewed")]
[assembly: SuppressMessage("CodeQuality", "IDE0079:Remove unnecessary suppression", Justification = "Reviewed", Scope = "module")]
[assembly: SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Reviewed", Scope = "module")]
[assembly: SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Reviewed", Scope = "module")]
[assembly: SuppressMessage("Performance", "CA1848:Use the LoggerMessage delegates", Justification = "Reviewed", Scope = "module")]
4 changes: 2 additions & 2 deletions src/TBC.OpenBanking.Jws/src/HttpMessageData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace TBC.OpenBanking.Jws;
/// </summary>
public abstract class HttpMessageData
{
protected readonly Dictionary<string, string> headers = new(StringComparer.OrdinalIgnoreCase);
protected readonly Dictionary<string, string> _headers = new(StringComparer.OrdinalIgnoreCase);

public enum HeaderNecessity
{
Expand Down Expand Up @@ -85,7 +85,7 @@ public enum HeaderNecessity
/// </summary>
public IDictionary<string, string> Headers
{
get => headers;
get => _headers;
}

public void AddHeader(string name, string value)
Expand Down
2 changes: 2 additions & 0 deletions src/TBC.OpenBanking.Jws/src/HttpResponseData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public override void CheckMandatoryHeaders()

public override string ComposeHeadersForSignature(IList<string> headers, IDictionary<string, string> additionalHeaders = null)
{
_ = headers ?? throw new ArgumentNullException(nameof(headers));

var sb = new StringBuilder();
foreach (var hn in headers)
{
Expand Down
30 changes: 13 additions & 17 deletions src/TBC.OpenBanking.Jws/src/Internals/WebEncoders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,21 +420,17 @@ private static int GetNumBase64PaddingCharsInString(string str)

private static int GetNumBase64PaddingCharsToAddForDecode(int inputLength)
{
switch (inputLength % 4)
{
case 0:
return 0;
case 2:
return 2;
case 3:
return 1;
default:
throw new FormatException(
string.Format(
CultureInfo.CurrentCulture,
EncoderResources.WebEncoders_MalformedInput,
inputLength));
}
return (inputLength % 4) switch
{
0 => 0,
2 => 2,
3 => 1,
_ => throw new FormatException(
string.Format(
CultureInfo.CurrentCulture,
EncoderResources.WebEncoders_MalformedInput,
inputLength)),
};
}

private static void ValidateParameters(int bufferLength, string inputName, int offset, int count)
Expand Down Expand Up @@ -469,12 +465,12 @@ internal static class EncoderResources
/// <summary>
/// Invalid {0}, {1} or {2} length.
/// </summary>
internal static readonly string WebEncoders_InvalidCountOffsetOrLength = "Invalid {0}, {1} or {2} length.";
internal const string WebEncoders_InvalidCountOffsetOrLength = "Invalid {0}, {1} or {2} length.";

/// <summary>
/// Malformed input: {0} is an invalid input length.
/// </summary>
internal static readonly string WebEncoders_MalformedInput = "Malformed input: {0} is an invalid input length.";
internal const string WebEncoders_MalformedInput = "Malformed input: {0} is an invalid input length.";

/// <summary>
/// Invalid {0}, {1} or {2} length.
Expand Down
2 changes: 2 additions & 0 deletions src/TBC.OpenBanking.Jws/src/ProtectedHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public X509Certificate2 DecodeCertificate(string encodedCertificate)
/// <returns></returns>
public string EncodeCertificate(X509Certificate2 cert)
{
_ = cert ?? throw new ArgumentNullException(nameof(cert));

byte[] rawData = cert.Export(X509ContentType.Cert);
return Convert.ToBase64String(rawData);
}
Expand Down
2 changes: 2 additions & 0 deletions src/TBC.OpenBanking.Jws/src/SupportedAlgorithms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public static class SupportedAlgorithms
/// <returns></returns>
static public ISigner CreateSigner(X509Certificate2 cert, string alg)
{
_ = cert ?? throw new ArgumentNullException(nameof(cert));

if (!cert.HasPrivateKey)
throw new ArgumentOutOfRangeException(nameof(cert), "Private key is missing");

Expand Down
18 changes: 13 additions & 5 deletions src/TBC.OpenBanking.Jws/src/TBC.OpenBanking.Jws.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>true</IsPackable>
<PackageId>TBC.OpenBanking.Jws</PackageId>
<Title>TBC Open Banking JSON Web Signature</Title>
Expand All @@ -24,6 +25,7 @@
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<DebugType>embedded</DebugType>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest-all</AnalysisLevel>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
Expand All @@ -40,16 +42,18 @@
<DisableImplicitComponentsAnalyzers>true</DisableImplicitComponentsAnalyzers>
<EnableNETAnalyzers>false</EnableNETAnalyzers>
<RunAnalyzers>false</RunAnalyzers>
<RestoreDisableParallel>true</RestoreDisableParallel>
<MinVerVerbosity>detailed</MinVerVerbosity>
</PropertyGroup>

<ItemGroup>
<Using Include="System.Globalization.CultureInfo" Alias="CultureInfo" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MinVer" Version="2.5.0" Condition=" '$(Configuration)' == 'Release' ">
<PackageReference Include="MinVer" Version="3.1.0" Condition=" '$(Configuration)' == 'Release' ">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down Expand Up @@ -78,7 +82,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.SourceLink.AzureDevOpsServer.Git" Version="1.1.1" Condition=" '$(Configuration)|$(TF_BUILD)' == 'Release|true' ">
<PackageReference Include="Microsoft.SourceLink.AzureDevOpsServer.Git" Version="1.1.1" Condition=" '$(Configuration)|$(TF_BUILD)' == 'Release|True' ">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand All @@ -89,6 +93,10 @@
<AssemblyMetadata Include="BuildId" Value="$(BuildId)" Condition=" $(BuildId) != '' " />
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
<InternalsVisibleTo Include="$(AssemblyName).IntegrationTests" />
<AssemblyAttribute Include="System.CLSCompliant">
<_Parameter1>false</_Parameter1>
<_Parameter1_IsLiteral>true</_Parameter1_IsLiteral>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
Expand All @@ -98,7 +106,7 @@
<Target Name="TbcJws_CalculateAssemblyVersions" AfterTargets="MinVer">
<PropertyGroup>
<AssemblyVersion>$(MinVerMajor).$(MinVerMinor).$(MinVerPatch).0</AssemblyVersion>
<InformationalVersion Condition=" '$(GITHUB_ACTIONS)' == 'true' ">$(MinVerVersion)+$(GITHUB_SHA)</InformationalVersion>
<InformationalVersion Condition=" '$(GITHUB_ACTIONS)' == 'true' ">$(MinVerVersion)</InformationalVersion>
</PropertyGroup>
</Target>

Expand Down

0 comments on commit be9d81f

Please sign in to comment.