Skip to content

Commit 3853c7b

Browse files
author
Daniele Giallonardo
committed
Bug fix gestione prefisso codice fiscale e caricamento certificato
#2 #3
1 parent 7cee362 commit 3853c7b

8 files changed

Lines changed: 109 additions & 51 deletions

File tree

CIE.AspNetCore.Authentication/CIE.AspNetCore.Authentication/CIE.AspNetCore.Authentication.csproj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
<PackageTags>cie;aspnetcore;authentication</PackageTags>
1212
<PackageProjectUrl>https://github.com/italia/cie-aspnetcore</PackageProjectUrl>
1313
<PackageLicenseExpression>MIT</PackageLicenseExpression>
14-
<PackageVersion>1.1.0</PackageVersion>
15-
<Version>1.1.0</Version>
16-
<AssemblyVersion>1.1.0</AssemblyVersion>
17-
<FileVersion>1.1.0</FileVersion>
18-
<InformationalVersion>1.1.0</InformationalVersion>
14+
<PackageVersion>1.1.2</PackageVersion>
15+
<Version>1.1.2</Version>
16+
<AssemblyVersion>1.1.2</AssemblyVersion>
17+
<FileVersion>1.1.2</FileVersion>
18+
<InformationalVersion>1.1.2</InformationalVersion>
1919
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
2020
</PropertyGroup>
2121

@@ -31,9 +31,10 @@
3131
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
3232
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.2.0" />
3333
<PackageReference Include="Microsoft.AspNetCore.Razor" Version="2.2.0" />
34-
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="5.0.5" />
34+
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="5.0.9" />
3535
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
3636
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
37+
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="5.0.9" />
3738
<PackageReference Include="System.Security.Cryptography.Xml" Version="5.0.0" />
3839
</ItemGroup>
3940

CIE.AspNetCore.Authentication/CIE.AspNetCore.Authentication/CieHandler.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using Microsoft.AspNetCore.Authentication;
2-
using Microsoft.AspNetCore.Http;
3-
using Microsoft.Extensions.Logging;
4-
using Microsoft.Extensions.Options;
5-
using Microsoft.Extensions.Primitives;
6-
using CIE.AspNetCore.Authentication.Events;
1+
using CIE.AspNetCore.Authentication.Events;
72
using CIE.AspNetCore.Authentication.Helpers;
83
using CIE.AspNetCore.Authentication.Models;
94
using CIE.AspNetCore.Authentication.Resources;
105
using CIE.AspNetCore.Authentication.Saml;
6+
using Microsoft.AspNetCore.Authentication;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.Logging;
9+
using Microsoft.Extensions.Options;
10+
using Microsoft.Extensions.Primitives;
1111
using System;
1212
using System.Collections.Concurrent;
1313
using System.Collections.Generic;
@@ -332,17 +332,30 @@ private void AdjustAuthenticationPropertiesDates(AuthenticationProperties proper
332332
}
333333
}
334334

335+
private string GetAttributeValue(ResponseType response, string attributeName)
336+
=> response.GetAssertion()?
337+
.GetAttributeStatement()?
338+
.GetAttributes()?
339+
.FirstOrDefault(x => attributeName.Equals(x.Name) || attributeName.Equals(x.FriendlyName))?
340+
.GetAttributeValue()?
341+
.Trim() ?? string.Empty;
342+
343+
private string RemoveFiscalNumberPrefix(string fiscalNumber)
344+
=> fiscalNumber?
345+
.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries)
346+
.LastOrDefault() ?? string.Empty;
347+
335348
private (ClaimsPrincipal principal, DateTimeOffset? validFrom, DateTimeOffset? validTo) CreatePrincipal(ResponseType idpAuthnResponse)
336349
{
337350
var claims = new Claim[]
338351
{
339352
new Claim( ClaimTypes.NameIdentifier, idpAuthnResponse.GetAssertion().GetAttributeStatement().GetAttributes().FirstOrDefault(x => SamlConst.fiscalNumber.Equals(x.Name) || SamlConst.fiscalNumber.Equals(x.FriendlyName))?.GetAttributeValue()?.Trim()?.Replace("TINIT-", "") ?? string.Empty),
340-
new Claim( CieClaimTypes.Name, idpAuthnResponse.GetAssertion().GetAttributeStatement().GetAttributes().FirstOrDefault(x => SamlConst.name.Equals(x.Name) || SamlConst.name.Equals(x.FriendlyName))?.GetAttributeValue()?.Trim() ?? string.Empty),
341-
new Claim( CieClaimTypes.FamilyName, idpAuthnResponse.GetAssertion().GetAttributeStatement().GetAttributes().FirstOrDefault(x => SamlConst.familyName.Equals(x.Name) || SamlConst.familyName.Equals(x.FriendlyName))?.GetAttributeValue()?.Trim() ?? string.Empty),
342-
new Claim( CieClaimTypes.FiscalNumber, idpAuthnResponse.GetAssertion().GetAttributeStatement().GetAttributes().FirstOrDefault(x => SamlConst.fiscalNumber.Equals(x.Name) || SamlConst.fiscalNumber.Equals(x.FriendlyName))?.GetAttributeValue()?.Trim()?.Replace("TINIT-", "") ?? string.Empty),
343-
new Claim( CieClaimTypes.DateOfBirth, idpAuthnResponse.GetAssertion().GetAttributeStatement().GetAttributes().FirstOrDefault(x => SamlConst.dateOfBirth.Equals(x.Name) || SamlConst.dateOfBirth.Equals(x.FriendlyName))?.GetAttributeValue()?.Trim() ?? string.Empty),
353+
new Claim( CieClaimTypes.Name.Value, GetAttributeValue(idpAuthnResponse, SamlConst.name)),
354+
new Claim( CieClaimTypes.FamilyName.Value, GetAttributeValue(idpAuthnResponse, SamlConst.familyName)),
355+
new Claim( CieClaimTypes.FiscalNumber.Value, RemoveFiscalNumberPrefix(GetAttributeValue(idpAuthnResponse, SamlConst.fiscalNumber))),
356+
new Claim( CieClaimTypes.DateOfBirth.Value, GetAttributeValue(idpAuthnResponse, SamlConst.dateOfBirth)),
344357
};
345-
var identity = new ClaimsIdentity(claims, Scheme.Name, SamlConst.fiscalNumber, null);
358+
var identity = new ClaimsIdentity(claims, Scheme.Name, Options.PrincipalNameClaimType.Value, null);
346359

347360
var returnedPrincipal = new ClaimsPrincipal(identity);
348361
return (returnedPrincipal, new DateTimeOffset(idpAuthnResponse.IssueInstant), new DateTimeOffset(idpAuthnResponse.GetAssertion().Subject.GetSubjectConfirmation().SubjectConfirmationData.NotOnOrAfter));

CIE.AspNetCore.Authentication/CIE.AspNetCore.Authentication/Extensions/CieExtensions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using CIE.AspNetCore.Authentication.Helpers;
99
using CIE.AspNetCore.Authentication.Models;
1010
using System;
11+
using System.Security.Claims;
1112

1213
namespace CIE.AspNetCore.Authentication
1314
{
@@ -62,5 +63,27 @@ public static AuthenticationBuilder AddCie(this AuthenticationBuilder builder, s
6263
builder.Services.AddOptions<CieConfiguration>().Configure(o => OptionsHelper.LoadFromConfiguration(o, configuration));
6364
return builder.AddRemoteScheme<CieOptions, CieHandler>(authenticationScheme, displayName, configureOptions);
6465
}
66+
67+
/// <summary>
68+
/// Finds the first value.
69+
/// </summary>
70+
/// <param name="principal">The principal.</param>
71+
/// <param name="claimType">Type of the claim.</param>
72+
/// <returns></returns>
73+
public static string FindFirstValue(this ClaimsPrincipal principal, CieClaimTypes claimType)
74+
{
75+
return principal.FindFirstValue(claimType.Value);
76+
}
77+
78+
/// <summary>
79+
/// Finds the first.
80+
/// </summary>
81+
/// <param name="principal">The principal.</param>
82+
/// <param name="claimType">Type of the claim.</param>
83+
/// <returns></returns>
84+
public static Claim FindFirst(this ClaimsPrincipal principal, CieClaimTypes claimType)
85+
{
86+
return principal.FindFirst(claimType.Value);
87+
}
6588
}
6689
}

CIE.AspNetCore.Authentication/CIE.AspNetCore.Authentication/Helpers/OptionsHelper.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using Microsoft.Extensions.Configuration;
2-
using CIE.AspNetCore.Authentication.Helpers;
3-
using CIE.AspNetCore.Authentication.Models;
4-
using System.Linq;
1+
using CIE.AspNetCore.Authentication.Models;
2+
using Microsoft.Extensions.Configuration;
53
using System.Security.Cryptography.X509Certificates;
64

75
namespace CIE.AspNetCore.Authentication.Helpers
@@ -14,7 +12,8 @@ internal static CieConfiguration CreateFromConfiguration(IConfiguration configur
1412
var options = new CieConfiguration();
1513

1614
var identityProviderSection = section.GetSection("Provider");
17-
if (identityProviderSection != null) {
15+
if (identityProviderSection != null)
16+
{
1817
options.IdentityProvider = new IdentityProvider()
1918
{
2019
Method = identityProviderSection.GetValue<RequestMethod>("Method"),
@@ -31,7 +30,7 @@ internal static CieConfiguration CreateFromConfiguration(IConfiguration configur
3130
SecurityLevel = identityProviderSection.GetValue<int?>("SecurityLevel") ?? 3,
3231
};
3332
}
34-
33+
3534
options.AllowUnsolicitedLogins = section.GetValue<bool?>("AllowUnsolicitedLogins") ?? false;
3635
options.AssertionConsumerServiceIndex = section.GetValue<ushort?>("AssertionConsumerServiceIndex") ?? 0;
3736
options.AttributeConsumingServiceIndex = section.GetValue<ushort?>("AttributeConsumingServiceIndex") ?? 0;
@@ -56,10 +55,11 @@ internal static CieConfiguration CreateFromConfiguration(IConfiguration configur
5655
var findValue = storeConfiguration.GetValue<string>("FindValue");
5756
var validOnly = storeConfiguration.GetValue<bool>("validOnly");
5857
options.Certificate = X509Helpers.GetCertificateFromStore(
59-
StoreLocation.CurrentUser, StoreName.My,
60-
X509FindType.FindBySubjectName,
58+
location,
59+
name,
60+
findType,
6161
findValue,
62-
validOnly: false);
62+
validOnly);
6363
}
6464
else if (certificateSource == "File")
6565
{
@@ -68,7 +68,7 @@ internal static CieConfiguration CreateFromConfiguration(IConfiguration configur
6868
var password = storeConfiguration.GetValue<string>("Password");
6969
options.Certificate = X509Helpers.GetCertificateFromFile(path, password);
7070
}
71-
else if(certificateSource == "Raw")
71+
else if (certificateSource == "Raw")
7272
{
7373
var storeConfiguration = certificateSection.GetSection("Raw");
7474
var certificate = storeConfiguration.GetValue<string>("Certificate");
Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Collections.Generic;
42

53
namespace CIE.AspNetCore.Authentication.Models
64
{
75
public sealed class CieClaimTypes
86
{
9-
public static string Name = nameof(Name);
10-
public static string FamilyName = nameof(FamilyName);
11-
public static string FiscalNumber = nameof(FiscalNumber);
12-
public static string DateOfBirth = nameof(DateOfBirth);
7+
private static Dictionary<string, CieClaimTypes> _types = new Dictionary<string, CieClaimTypes>() {
8+
{ nameof(Name), new CieClaimTypes(nameof(Name)) },
9+
{ nameof(FamilyName), new CieClaimTypes(nameof(FamilyName)) },
10+
{ nameof(FiscalNumber), new CieClaimTypes(nameof(FiscalNumber)) },
11+
{ nameof(DateOfBirth), new CieClaimTypes(nameof(DateOfBirth)) },
12+
{ nameof(RawFiscalNumber), new CieClaimTypes(nameof(RawFiscalNumber)) },
13+
};
14+
15+
private CieClaimTypes(string value)
16+
{
17+
Value = value;
18+
}
19+
20+
public string Value { get; private set; }
21+
22+
public static CieClaimTypes Name { get { return _types[nameof(Name)]; } }
23+
public static CieClaimTypes FamilyName { get { return _types[nameof(FamilyName)]; } }
24+
public static CieClaimTypes FiscalNumber { get { return _types[nameof(FiscalNumber)]; } }
25+
public static CieClaimTypes DateOfBirth { get { return _types[nameof(DateOfBirth)]; } }
26+
public static CieClaimTypes RawFiscalNumber { get { return _types[nameof(RawFiscalNumber)]; } }
1327
}
1428
}

CIE.AspNetCore.Authentication/CIE.AspNetCore.Authentication/Models/CieOptions.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using Microsoft.AspNetCore.Authentication;
1+
using CIE.AspNetCore.Authentication.Events;
2+
using CIE.AspNetCore.Authentication.Helpers;
3+
using Microsoft.AspNetCore.Authentication;
24
using Microsoft.AspNetCore.Http;
35
using Microsoft.Extensions.Configuration;
4-
using CIE.AspNetCore.Authentication.Events;
5-
using CIE.AspNetCore.Authentication.Helpers;
6-
using System.Collections.Generic;
7-
using System.Linq;
86
using System.Security.Cryptography.X509Certificates;
97

108
namespace CIE.AspNetCore.Authentication.Models
@@ -124,14 +122,22 @@ public override void Validate()
124122
/// </value>
125123
public bool CacheIdpMetadata { get; set; }
126124

125+
/// <summary>
126+
/// Gets or sets the type of the principal name claim.
127+
/// </summary>
128+
/// <value>
129+
/// The type of the principal name claim.
130+
/// </value>
131+
public CieClaimTypes PrincipalNameClaimType { get; set; } = CieClaimTypes.FiscalNumber;
132+
127133
public void LoadFromConfiguration(IConfiguration configuration)
128134
{
129135
var conf = OptionsHelper.CreateFromConfiguration(configuration);
130136
IdentityProvider = conf.IdentityProvider;
131137
AllowUnsolicitedLogins = conf.AllowUnsolicitedLogins;
132138
AssertionConsumerServiceIndex = conf.AssertionConsumerServiceIndex;
133139
AttributeConsumingServiceIndex = conf.AttributeConsumingServiceIndex;
134-
CallbackPath = conf.CallbackPath.HasValue ? conf.CallbackPath: CallbackPath;
140+
CallbackPath = conf.CallbackPath.HasValue ? conf.CallbackPath : CallbackPath;
135141
EntityId = conf.EntityId;
136142
RemoteSignOutPath = conf.RemoteSignOutPath.HasValue ? conf.RemoteSignOutPath : RemoteSignOutPath;
137143
SignOutScheme = conf.SignOutScheme;

CIE.AspNetCore.Authentication/CIE.AspNetCore.Authentication/Saml/SamlTypesExtensions.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,46 @@ namespace CIE.AspNetCore.Authentication.Saml
77
{
88
internal static class SamlTypesExtensions
99
{
10-
public static AssertionType GetAssertion(this ResponseType input)
10+
public static AssertionType GetAssertion(this ResponseType input)
1111
=> input.Items?.FirstOrDefault() as AssertionType;
1212

13-
public static AttributeStatementType GetAttributeStatement(this AssertionType input)
13+
public static AttributeStatementType GetAttributeStatement(this AssertionType input)
1414
=> input.Items?.FirstOrDefault(s => s is AttributeStatementType) as AttributeStatementType;
1515

16-
public static IEnumerable<AttributeType> GetAttributes(this AttributeStatementType input)
16+
public static IEnumerable<AttributeType> GetAttributes(this AttributeStatementType input)
1717
=> input.Items?.Cast<AttributeType>();
1818

19-
public static AuthnStatementType GetAuthnStatement(this AssertionType input)
19+
public static AuthnStatementType GetAuthnStatement(this AssertionType input)
2020
=> input.Items?.FirstOrDefault(s => s is AuthnStatementType) as AuthnStatementType;
2121

22-
public static SubjectConfirmationType GetSubjectConfirmation(this SubjectType input)
22+
public static SubjectConfirmationType GetSubjectConfirmation(this SubjectType input)
2323
=> input.Items?.FirstOrDefault(s => s is SubjectConfirmationType) as SubjectConfirmationType;
2424

2525
public static string GetAttributeValue(this AttributeType input)
2626
=> (input.AttributeValue?.FirstOrDefault()) switch
2727
{
2828
string stringResult => stringResult,
2929
XmlNode[] xmlNodeResult => xmlNodeResult?.FirstOrDefault()?.Value,
30+
DateTime dateTimeResult => dateTimeResult.ToString("yyyy-MM-dd"),
3031
_ => null,
3132
};
3233

33-
public static X509DataType GetX509Data(this KeyInfoType input)
34+
public static X509DataType GetX509Data(this KeyInfoType input)
3435
=> input.Items?.FirstOrDefault(s => s is X509DataType) as X509DataType;
3536

3637
public static byte[] GetRawX509Certificate(this X509DataType input)
3738
=> input.Items?.FirstOrDefault() as byte[];
3839

39-
public static string GetBase64X509Certificate(this X509DataType input)
40+
public static string GetBase64X509Certificate(this X509DataType input)
4041
=> Convert.ToBase64String(input.Items?.FirstOrDefault() as byte[]);
4142

42-
public static NameIDType GetNameID(this SubjectType input)
43+
public static NameIDType GetNameID(this SubjectType input)
4344
=> input.Items?.FirstOrDefault(s => s is NameIDType) as NameIDType;
4445

45-
public static AudienceRestrictionType GetAudienceRestriction(this ConditionsType input)
46+
public static AudienceRestrictionType GetAudienceRestriction(this ConditionsType input)
4647
=> input.Items?.FirstOrDefault(s => s is AudienceRestrictionType) as AudienceRestrictionType;
4748

48-
public static string GetAuthnContextClassRef(this AuthnContextType input)
49+
public static string GetAuthnContextClassRef(this AuthnContextType input)
4950
=> input.ItemsElementName?.Contains(ItemsChoiceType5.AuthnContextClassRef) ?? false
5051
? input.Items[Array.IndexOf(input.ItemsElementName, ItemsChoiceType5.AuthnContextClassRef)] as string
5152
: null;

CIE.AspNetCore.Authentication/CIE.AspNetCore.WebApp/CIE.AspNetCore.WebApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.5" />
10+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="5.0.9" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

0 commit comments

Comments
 (0)