Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniele Giallonardo committed May 25, 2024
1 parent 3ac699f commit d5d8d97
Show file tree
Hide file tree
Showing 19 changed files with 164 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SPID.AspNetCore.Authentication" Version="3.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.5" />
<PackageReference Include="SPID.AspNetCore.Authentication" Version="3.3.0" />
</ItemGroup>

<!--<ItemGroup>
<Reference Include="SPID.AspNetCore.Authentication">
<HintPath>..\..\..\src\SPID.AspNetCore.Authentication\bin\Debug\net8.0\SPID.AspNetCore.Authentication.dll</HintPath>
</Reference>
</ItemGroup>-->

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<ItemGroup>
<PackageReference Include="IdentityServer4" Version="4.1.2" />

<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="8.0.5" />

<PackageReference Include="SPID.AspNetCore.Authentication" Version="3.2.0" />
<PackageReference Include="SPID.AspNetCore.Authentication" Version="3.3.0" />

</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="8.0.5" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="8.0.2" />
<PackageReference Include="SPID.AspNetCore.Authentication" Version="3.2.0" />
<PackageReference Include="SPID.AspNetCore.Authentication" Version="3.3.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public RedirectContext(
SpidOptions options,
AuthenticationProperties properties,
object signedProtocolMessage)
: base(context, scheme, options, properties)
: base(context, scheme, options, properties)
{
SignedProtocolMessage = signedProtocolMessage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace SPID.AspNetCore.Authentication
{
internal static class LoggingExtensions
{
private static Action<ILogger, Exception> _exceptionProcessingMessage;
private static Action<ILogger, string, Exception> _malformedRedirectUri;
private static Action<ILogger, Exception> _remoteSignOutHandledResponse;
private static Action<ILogger, Exception> _remoteSignOutFailed;
private static Action<ILogger, Exception> _remoteSignOutSkipped;
private static Action<ILogger, Exception> _remoteSignOut;
private static readonly Action<ILogger, Exception> _exceptionProcessingMessage;
private static readonly Action<ILogger, string, Exception> _malformedRedirectUri;
private static readonly Action<ILogger, Exception> _remoteSignOutHandledResponse;
private static readonly Action<ILogger, Exception> _remoteSignOutFailed;
private static readonly Action<ILogger, Exception> _remoteSignOutSkipped;
private static readonly Action<ILogger, Exception> _remoteSignOut;

static LoggingExtensions()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ namespace SPID.AspNetCore.Authentication.Helpers
{
internal static class BusinessValidation
{
public static void Argument<T>(T input, string error, [CallerArgumentExpression("input")] string inputName = "") where T : class
public static void Argument<T>(T input, string error, [CallerArgumentExpression(nameof(input))] string inputName = "") where T : class
{
if (input is string && string.IsNullOrWhiteSpace(input.ToString()) || input == default(T))
{
throw new SpidException(String.Format(ErrorLocalization.ArgumentNull, inputName), error, SpidErrorCode.ArgumentNull);
}
}

public static void ValidationCondition(Func<bool> condition, SpidException error)
Expand Down Expand Up @@ -50,7 +52,8 @@ public static void ValidationNotNullNotWhitespace(string input, SpidException er
}
}

public static T ValidationNotNullNotEmpty<T>(T input, SpidException error) where T : class, new()
public static T ValidationNotNullNotEmpty<T>(T input, SpidException error)
where T : class, new()
{
var instance = new T();
if (input == default(T)) throw error;
Expand Down
39 changes: 20 additions & 19 deletions src/SPID.AspNetCore.Authentication/Helpers/OptionsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,26 @@ internal static class OptionsHelper
internal static SpidConfiguration CreateFromConfiguration(IConfiguration configuration)
{
var section = configuration.GetSection("Spid");
var options = new SpidConfiguration();

options.IsStagingValidatorEnabled = section.GetValue<bool?>("IsStagingValidatorEnabled") ?? false;
options.IsLocalValidatorEnabled = section.GetValue<bool?>("IsLocalValidatorEnabled") ?? false;
options.AllowUnsolicitedLogins = section.GetValue<bool?>("AllowUnsolicitedLogins") ?? false;
options.AssertionConsumerServiceURL = section.GetValue<string>("AssertionConsumerServiceURL");
options.AssertionConsumerServiceIndex = section.GetValue<ushort?>("AssertionConsumerServiceIndex");
options.AttributeConsumingServiceIndex = section.GetValue<ushort?>("AttributeConsumingServiceIndex") ?? 0;
options.CallbackPath = section.GetValue<string>("CallbackPath");
options.IdPRegistryURL = section.GetValue<string>("IdPRegistryURL");
options.EntityId = section.GetValue<string>("EntityId");
options.RemoteSignOutPath = section.GetValue<string>("RemoteSignOutPath");
options.SignOutScheme = section.GetValue<string>("SignOutScheme");
options.UseTokenLifetime = section.GetValue<bool?>("UseTokenLifetime") ?? false;
options.SkipUnrecognizedRequests = section.GetValue<bool?>("SkipUnrecognizedRequests") ?? true;
options.CacheIdpMetadata = section.GetValue<bool?>("CacheIdpMetadata") ?? true;
options.IdpMetadataCacheDurationInMinutes = section.GetValue<int?>("IdpMetadataCacheDurationInMinutes") ?? 1440;
options.RandomIdentityProvidersOrder = section.GetValue<bool?>("RandomIdentityProvidersOrder") ?? false;
options.SecurityLevel = section.GetValue<int?>("SecurityLevel") ?? 2;
var options = new SpidConfiguration
{
IsStagingValidatorEnabled = section.GetValue<bool?>("IsStagingValidatorEnabled") ?? false,
IsLocalValidatorEnabled = section.GetValue<bool?>("IsLocalValidatorEnabled") ?? false,
AllowUnsolicitedLogins = section.GetValue<bool?>("AllowUnsolicitedLogins") ?? false,
AssertionConsumerServiceURL = section.GetValue<string>("AssertionConsumerServiceURL"),
AssertionConsumerServiceIndex = section.GetValue<ushort?>("AssertionConsumerServiceIndex"),
AttributeConsumingServiceIndex = section.GetValue<ushort?>("AttributeConsumingServiceIndex") ?? 0,
CallbackPath = section.GetValue<string>("CallbackPath"),
IdPRegistryURL = section.GetValue<string>("IdPRegistryURL"),
EntityId = section.GetValue<string>("EntityId"),
RemoteSignOutPath = section.GetValue<string>("RemoteSignOutPath"),
SignOutScheme = section.GetValue<string>("SignOutScheme"),
UseTokenLifetime = section.GetValue<bool?>("UseTokenLifetime") ?? false,
SkipUnrecognizedRequests = section.GetValue<bool?>("SkipUnrecognizedRequests") ?? true,
CacheIdpMetadata = section.GetValue<bool?>("CacheIdpMetadata") ?? true,
IdpMetadataCacheDurationInMinutes = section.GetValue<int?>("IdpMetadataCacheDurationInMinutes") ?? 1440,
RandomIdentityProvidersOrder = section.GetValue<bool?>("RandomIdentityProvidersOrder") ?? false,
SecurityLevel = section.GetValue<int?>("SecurityLevel") ?? 2
};
var requestMethodParsed = Enum.TryParse<RequestMethod>(section.GetValue<string?>("RequestMethod"), out var requestMethod);

Check warning on line 34 in src/SPID.AspNetCore.Authentication/Helpers/OptionsHelper.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 34 in src/SPID.AspNetCore.Authentication/Helpers/OptionsHelper.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 34 in src/SPID.AspNetCore.Authentication/Helpers/OptionsHelper.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 34 in src/SPID.AspNetCore.Authentication/Helpers/OptionsHelper.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
options.RequestMethod = requestMethodParsed ? requestMethod : RequestMethod.Post;
options.DefaultLanguage = section.GetValue<string?>("DefaultLanguage") ?? "it";

Check warning on line 36 in src/SPID.AspNetCore.Authentication/Helpers/OptionsHelper.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 36 in src/SPID.AspNetCore.Authentication/Helpers/OptionsHelper.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 36 in src/SPID.AspNetCore.Authentication/Helpers/OptionsHelper.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 36 in src/SPID.AspNetCore.Authentication/Helpers/OptionsHelper.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
Expand Down
3 changes: 1 addition & 2 deletions src/SPID.AspNetCore.Authentication/Helpers/X509Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using SPID.AspNetCore.Authentication.Exceptions;
using SPID.AspNetCore.Authentication.Resources;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
Expand Down Expand Up @@ -83,7 +82,7 @@ public static string CreateSignature(this string payload, X509Certificate2 certi
return Convert.ToBase64String(rsa.SignHash(hash, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1), Base64FormattingOptions.None);
}

public static byte[] ExportPublicKey(this X509Certificate2 cert)
public static byte[] ExportPublicKey(this X509Certificate2 cert)
=> cert.Export(X509ContentType.Cert);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace SPID.AspNetCore.Authentication.Models.ServiceProviders
{
internal class DefaultServiceProvidersFactory : IServiceProvidersFactory
{
public async Task<List<ServiceProvider>> GetServiceProviders()
public async Task<List<ServiceProvider>> GetServiceProviders()
=> await Task.FromResult(new List<ServiceProvider>());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using SPID.AspNetCore.Authentication.Helpers;
using SPID.AspNetCore.Authentication.Saml;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;

namespace SPID.AspNetCore.Authentication.Models.ServiceProviders
Expand Down
6 changes: 3 additions & 3 deletions src/SPID.AspNetCore.Authentication/Models/SpidOptions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using SPID.AspNetCore.Authentication.Events;
using SPID.AspNetCore.Authentication.Helpers;
using SPID.AspNetCore.Authentication.Models.ServiceProviders;
Expand All @@ -11,6 +10,7 @@
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -172,7 +172,7 @@ private async Task<List<IdentityProvider>> FetchIdentityProvidersFromRegistry(Ht
var json = await client.GetStringAsync(IdPRegistryURL);
if (!string.IsNullOrWhiteSpace(json))
{
var registryItems = JsonConvert.DeserializeObject<List<IdPRegistryName>>(json);
var registryItems = JsonSerializer.Deserialize<List<IdPRegistryName>>(json);
return registryItems
.Select(s => new IdentityProvider()
{
Expand All @@ -192,7 +192,7 @@ private async Task<List<IdentityProvider>> FetchIdentityProvidersFromRegistry(Ht
})
.ToList();
}
return new List<IdentityProvider>();
return [];
}

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions src/SPID.AspNetCore.Authentication/Mvc/EidasButtonTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ namespace SPID.AspNetCore.Authentication
{
public class EidasButtonTagHelper : TagHelper
{
private static Dictionary<EidasButtonType, string> _serializedCircleImagesSVG = new Dictionary<EidasButtonType, string>();
private static Dictionary<EidasButtonType, string> _serializedCircleImagesPNG = new Dictionary<EidasButtonType, string>();
private static object _lockobj = new object();
private static readonly Dictionary<EidasButtonType, string> _serializedCircleImagesSVG = new Dictionary<EidasButtonType, string>();
private static readonly Dictionary<EidasButtonType, string> _serializedCircleImagesPNG = new Dictionary<EidasButtonType, string>();
private static readonly object _lockobj = new object();

private static Dictionary<EidasButtonSize, (string ShortClassName, string LongClassName)> _classNames = new()
private static readonly Dictionary<EidasButtonSize, (string ShortClassName, string LongClassName)> _classNames = new()
{
{ EidasButtonSize.Small, ("s", "small") },
{ EidasButtonSize.Medium, ("m", "medium") },
{ EidasButtonSize.Large, ("l", "large") },
{ EidasButtonSize.ExtraLarge, ("xl", "xlarge") }
};

IUrlHelper _urlHelper;
readonly IUrlHelper _urlHelper;

public EidasButtonTagHelper(IUrlHelper urlHelper)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace SPID.AspNetCore.Authentication
public class EidasCSSTagHelper : TagHelper
{
private static string _css;
private static object _lockobj = new object();
private static readonly object _lockobj = new object();

public override void Process(TagHelperContext context, TagHelperOutput output)
{
Expand Down
2 changes: 1 addition & 1 deletion src/SPID.AspNetCore.Authentication/Mvc/SpidCSSTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace SPID.AspNetCore.Authentication
public class SpidCSSTagHelper : TagHelper
{
private static string _css;
private static object _lockobj = new object();
private static readonly object _lockobj = new object();

public override void Process(TagHelperContext context, TagHelperOutput output)
{
Expand Down
2 changes: 1 addition & 1 deletion src/SPID.AspNetCore.Authentication/Mvc/SpidJSTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace SPID.AspNetCore.Authentication
public class SpidJSTagHelper : TagHelper
{
private static string _js;
private static object _lockobj = new object();
private static readonly object _lockobj = new object();

public override void Process(TagHelperContext context, TagHelperOutput output)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
<PackageProjectUrl>https://github.com/italia/spid-aspnetcore</PackageProjectUrl>
<PackageIcon>spid-nuget.png</PackageIcon>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageVersion>3.2.0</PackageVersion>
<Version>3.2.0</Version>
<AssemblyVersion>3.2.0</AssemblyVersion>
<FileVersion>3.2.0</FileVersion>
<InformationalVersion>3.2.0</InformationalVersion>
<PackageVersion>3.3.0</PackageVersion>
<Version>3.3.0</Version>
<AssemblyVersion>3.3.0</AssemblyVersion>
<FileVersion>3.3.0</FileVersion>
<InformationalVersion>3.3.0</InformationalVersion>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/italia/spid-aspnetcore</RepositoryUrl>
Expand All @@ -34,17 +34,14 @@

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="8.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.4" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="7.0.12" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.12" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="6.0.21" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.21" />
</ItemGroup>

<ItemGroup>
Expand Down
16 changes: 8 additions & 8 deletions src/SPID.AspNetCore.Authentication/Saml/SamlHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace SPID.AspNetCore.Authentication.Saml
{
internal static class SamlHandler
{
private static readonly Dictionary<Type, XmlSerializer> serializers = new Dictionary<Type, XmlSerializer>
private static readonly Dictionary<Type, XmlSerializer> serializers = new()
{
{ typeof(AuthnRequestType), new XmlSerializer(typeof(AuthnRequestType)) },
{ typeof(ResponseType), new XmlSerializer(typeof(ResponseType)) },
Expand All @@ -25,7 +25,7 @@ internal static class SamlHandler
{ typeof(SP.EntityDescriptorType), new XmlSerializer(typeof(SP.EntityDescriptorType)) },
{ typeof(SPAv29.EntityDescriptorType), new XmlSerializer(typeof(SPAv29.EntityDescriptorType)) },
};
private static readonly List<string> listAuthRefValid = new List<string>
private static readonly List<string> listAuthRefValid = new()
{
SamlConst.SpidL + "1",
SamlConst.SpidL + "2",
Expand Down Expand Up @@ -110,14 +110,14 @@ public static AuthnRequestType GetAuthnRequest(string requestId,
{
Comparison = AuthnContextComparisonType.minimum,
ComparisonSpecified = true,
Items = new string[1]
{
Items =
[
SamlConst.SpidL + securityLevel
},
ItemsElementName = new ItemsChoiceType7[1]
{
],
ItemsElementName =
[
ItemsChoiceType7.AuthnContextClassRef
}
]
}
};

Expand Down
Loading

0 comments on commit d5d8d97

Please sign in to comment.