Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit 968aa85

Browse files
committed
fixes errors due to dotnet 8 upgrade
1 parent d8758f8 commit 968aa85

File tree

13 files changed

+159
-40
lines changed

13 files changed

+159
-40
lines changed

src/AspNetCore.IdpSample/AspNetCore.IdpSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
66
</PropertyGroup>
77

src/AspNetCore.IdpSample/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void ConfigureServices(IServiceCollection services)
6666
{
6767
sp.BaseUrl = new Uri("https://localhost:5003");
6868
sp.MaxClockSkew = TimeSpan.FromMinutes(2);
69-
sp.AssertionConsumerServiceEndpoint = "/finish";
69+
sp.AssertionConsumerServiceEndpoint = "/saml2p/finish";
7070
sp.AssertionSigningKey = new X509SecurityKey(new X509Certificate2(Convert.FromBase64String(SigningCertificateBase64)));
7171

7272
sp.SupportedBindings.Clear();

src/AspNetCore.SpSample/AspNetCore.SpSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
66
</PropertyGroup>
77

src/AspNetCore.SpSample/Startup.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ public void ConfigureServices(IServiceCollection services)
6262
idp.CanInitiateSso = true;
6363
idp.RequestedAuthnContextClassRef = Saml2pConstants.Classes.Kerberos;
6464
idp.AssertionSigningKeys.Add(new X509SecurityKey(new X509Certificate2(Convert.FromBase64String(SigningCertificateBase64))));
65-
//idp.Events.OnGeneratingRelayState = (provider, context) => new ValueTask();
66-
//idp.Events.OnValidatingToken = ValidatingToken;
67-
//idp.Events.OnValidatingToken += (provider, context) => new ValueTask();
6865
});
6966
})
7067
;

src/Solid.Identity.Protocols.Sam2p.Tests/Solid.Identity.Protocols.Sam2p.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
4+
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
55
<IsPackable>false</IsPackable>
66
</PropertyGroup>
77

src/Solid.Identity.Protocols.Saml2p/Abstractions/ISecurityTokenDescriptorFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Security.Claims;
77
using System.Text;
88
using System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Authentication;
910

1011
namespace Solid.Identity.Protocols.Saml2p.Abstractions
1112
{

src/Solid.Identity.Protocols.Saml2p/Authentication/Saml2pAuthenticationHandler.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,32 @@
1818
using System.Security.Claims;
1919
using Solid.Identity.Protocols.Saml2p.Options;
2020
using Solid.Identity.Protocols.Saml2p.Exceptions;
21+
// ReSharper disable InconsistentNaming
2122

2223
namespace Solid.Identity.Protocols.Saml2p.Authentication
2324
{
2425
internal class Saml2pAuthenticationHandler : RemoteAuthenticationHandler<Saml2pAuthenticationOptions>, IDisposable
2526
{
2627
private Saml2pOptions _saml2p;
27-
private IDisposable _optionsChangeToken;
28+
private readonly IDisposable _optionsChangeToken;
2829

30+
#if NET6_0
2931
public Saml2pAuthenticationHandler(IOptionsMonitor<Saml2pOptions> saml2pOptionsMonitor, IOptionsMonitor<Saml2pAuthenticationOptions> monitor, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
3032
: base(monitor, logger, encoder, clock)
3133
{
3234
_saml2p = saml2pOptionsMonitor.CurrentValue;
3335
_optionsChangeToken = saml2pOptionsMonitor.OnChange((options, _) => _saml2p = options);
3436
}
37+
#else
38+
public Saml2pAuthenticationHandler(IOptionsMonitor<Saml2pOptions> saml2pOptionsMonitor, IOptionsMonitor<Saml2pAuthenticationOptions> monitor, ILoggerFactory logger, UrlEncoder encoder)
39+
: base(monitor, logger, encoder)
40+
{
41+
_saml2p = saml2pOptionsMonitor.CurrentValue;
42+
_optionsChangeToken = saml2pOptionsMonitor.OnChange((options, _) => _saml2p = options);
43+
}
44+
#endif
45+
46+
3547

3648
protected override Task InitializeHandlerAsync()
3749
{

src/Solid.Identity.Protocols.Saml2p/Extensions/ClaimsIdentityExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Runtime.InteropServices;
34
using System.Text;
5+
using Solid.IdentityModel.Xml;
46

57
namespace System.Security.Claims
68
{
@@ -11,5 +13,17 @@ public static bool TryFindFirst(this ClaimsIdentity identity, string type, out C
1113
claim = identity.FindFirst(type);
1214
return claim != null;
1315
}
16+
17+
public static bool TryParseAuthenticationInstant(this ClaimsIdentity identity, out DateTime? instant)
18+
{
19+
var value = identity.FindFirst(ClaimTypes.AuthenticationInstant)?.Value;
20+
if (string.IsNullOrWhiteSpace(value))
21+
return Out.False(out instant);
22+
if (!DateTime.TryParse(value, out var parsed))
23+
return Out.False(out instant);
24+
25+
instant = parsed;
26+
return instant.HasValue;
27+
}
1428
}
1529
}

src/Solid.Identity.Protocols.Saml2p/Factories/AuthnRequestFactory.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ namespace Solid.Identity.Protocols.Saml2p.Factories
1818
/// </summary>
1919
public class AuthnRequestFactory
2020
{
21-
private ISystemClock _systemClock;
22-
private Saml2pOptions _options;
23-
21+
private readonly Saml2pOptions _options;
22+
#if NET6_0
23+
private readonly ISystemClock _systemClock;
2424
/// <summary>
2525
/// Creates an instance of <see cref="AuthnRequestFactory"/>.
2626
/// </summary>
@@ -31,6 +31,19 @@ public AuthnRequestFactory(ISystemClock systemClock, IOptions<Saml2pOptions> opt
3131
_systemClock = systemClock;
3232
_options = options.Value;
3333
}
34+
#else
35+
private readonly TimeProvider _time;
36+
/// <summary>
37+
/// Creates an instance of <see cref="AuthnRequestFactory"/>.
38+
/// </summary>
39+
/// <param name="time">The provider of the current time.</param>
40+
/// <param name="options">The current <see cref="Saml2pOptions" />.</param>
41+
public AuthnRequestFactory(TimeProvider time, IOptions<Saml2pOptions> options)
42+
{
43+
_time = time;
44+
_options = options.Value;
45+
}
46+
#endif
3447

3548
/// <summary>
3649
/// Creates an instance of <see cref="AuthnRequest"/>.
@@ -46,7 +59,7 @@ public async Task<AuthnRequest> CreateAuthnRequestAsync(HttpContext context, ISa
4659
// TODO: have some sort of providername default
4760
ProviderName = idp.ExpectedIssuer ?? _options.DefaultIssuer,
4861
AssertionConsumerServiceUrl = GetAcsUrl(context.Request),
49-
IssueInstant = _systemClock.UtcNow.UtcDateTime,
62+
IssueInstant = GetUtcNow(),
5063
Issuer = idp.ExpectedIssuer ?? _options.DefaultIssuer,
5164
Destination = new Uri(idp.BaseUrl, idp.AcceptSsoEndpoint),
5265
NameIdPolicy = new NameIdPolicy
@@ -82,5 +95,14 @@ private Uri GetAcsUrl(HttpRequest request)
8295
var path = request.PathBase.Add(_options.FinishPath);
8396
return new Uri(baseUrl, path);
8497
}
98+
99+
private DateTime GetUtcNow()
100+
{
101+
#if NET6_0
102+
return _systemClock.UtcNow.UtcDateTime;
103+
#else
104+
return _time.GetUtcNow().UtcDateTime;
105+
#endif
106+
}
85107
}
86108
}

src/Solid.Identity.Protocols.Saml2p/Factories/SecurityTokenDescriptorFactory.cs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ namespace Solid.Identity.Protocols.Saml2p.Factories
2121
{
2222
internal class SecurityTokenDescriptorFactory : ISecurityTokenDescriptorFactory
2323
{
24-
private Saml2pOptions _options;
25-
private IEnumerable<IServiceProviderClaimsProvider> _claimsProviders;
26-
private ILogger<SecurityTokenDescriptorFactory> _logger;
24+
private readonly Saml2pOptions _options;
25+
private readonly IEnumerable<IServiceProviderClaimsProvider> _claimsProviders;
26+
private readonly ILogger<SecurityTokenDescriptorFactory> _logger;
27+
28+
#if NET6_0
2729
private ISystemClock _systemClock;
2830

2931
public SecurityTokenDescriptorFactory(
@@ -37,18 +39,36 @@ public SecurityTokenDescriptorFactory(
3739
_logger = logger;
3840
_systemClock = systemClock;
3941
}
42+
#else
43+
private readonly TimeProvider _time;
4044

41-
public async ValueTask<SecurityTokenDescriptor> CreateSecurityTokenDescriptorAsync(ClaimsIdentity identity, ISaml2pServiceProvider partner)
45+
public SecurityTokenDescriptorFactory(
46+
IOptions<Saml2pOptions> options,
47+
IEnumerable<IServiceProviderClaimsProvider> claimsProviders,
48+
ILogger<SecurityTokenDescriptorFactory> logger,
49+
TimeProvider time)
4250
{
43-
var instant = identity.FindFirst(ClaimTypes.AuthenticationInstant)?.Value;
44-
var issuedAt = _systemClock.UtcNow.DateTime;
45-
if (instant != null && DateTime.TryParse(instant, out var parsed))
46-
issuedAt = parsed;
51+
_options = options.Value;
52+
_claimsProviders = claimsProviders;
53+
_logger = logger;
54+
_time = time;
55+
}
56+
#endif
4757

48-
var issuer = partner.ExpectedIssuer ?? _options.DefaultIssuer;
58+
public async ValueTask<SecurityTokenDescriptor> CreateSecurityTokenDescriptorAsync(ClaimsIdentity identity, ISaml2pServiceProvider partner)
59+
{
60+
var issuedAt = GetUtcNow();
61+
var instant = issuedAt;
62+
if (identity.TryParseAuthenticationInstant(out var parsed))
63+
instant = parsed!.Value;
4964

5065
var lifetime = partner.TokenLifeTime ?? _options.DefaultTokenLifetime;
51-
var tolerence = partner.MaxClockSkew ?? _options.DefaultMaxClockSkew ?? TimeSpan.Zero;
66+
var tolerance = partner.MaxClockSkew ?? _options.DefaultMaxClockSkew ?? TimeSpan.Zero;
67+
var expires = issuedAt
68+
.Add(lifetime)
69+
.Add(tolerance)
70+
;
71+
var issuer = partner.ExpectedIssuer ?? _options.DefaultIssuer;
5272
var claims = new List<Claim>();
5373
foreach (var provider in _claimsProviders)
5474
{
@@ -74,7 +94,7 @@ public async ValueTask<SecurityTokenDescriptor> CreateSecurityTokenDescriptorAsy
7494
claims = claims.Where(c => supported.Contains(c.Type)).ToList();
7595
Trace($"Filtered claims.", claims);
7696

77-
AddRequiredClaims(identity, claims, issuedAt, issuer);
97+
AddRequiredClaims(identity, claims, instant, issuer);
7898

7999
var attributes = claims
80100
.Where(c => c.Type != ClaimTypes.NameIdentifier)
@@ -91,18 +111,13 @@ public async ValueTask<SecurityTokenDescriptor> CreateSecurityTokenDescriptorAsy
91111
attribute.Properties.Add(ClaimProperties.SamlAttributeNameFormat, "urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified");
92112
}
93113

94-
var expires = issuedAt
95-
.Add(lifetime)
96-
.Add(tolerence)
97-
;
98-
99114
var descriptor = new SecurityTokenDescriptor
100115
{
101116
Audience = partner.Id,
102117
Subject = new ClaimsIdentity(claims, "SSO"),
103118
Issuer = issuer,
104119
IssuedAt = issuedAt,
105-
NotBefore = issuedAt.Subtract(tolerence),
120+
NotBefore = issuedAt.Subtract(tolerance),
106121
Expires = expires,
107122
SigningCredentials = GetSigningCredentials(partner),
108123
EncryptingCredentials = GetEncryptingCredentials(partner)
@@ -171,5 +186,14 @@ private void Trace(string prefix, object obj)
171186
if (!_logger.IsEnabled(LogLevel.Trace)) return;
172187
_logger.LogTrace(prefix + Environment.NewLine + "{state}", new WrappedLogMessageState(obj));
173188
}
189+
190+
private DateTime GetUtcNow()
191+
{
192+
#if NET6_0
193+
return _systemClock.UtcNow.UtcDateTime;
194+
#else
195+
return _time.GetUtcNow().UtcDateTime;
196+
#endif
197+
}
174198
}
175199
}

0 commit comments

Comments
 (0)