Refactored from ISystemClock to TimeProvider (#14963)

* Removed obsolete ISystemClock. The SecurityStampValidator provides the TimeProvider.

* Removed obsolete ISystemClock.

* Refactored obsolete ISystemClock with TimeProvider
This commit is contained in:
Andreas Zerbst
2023-10-12 13:58:51 +02:00
committed by GitHub
parent 6fb2550690
commit ace8d80183
5 changed files with 20 additions and 21 deletions

View File

@@ -13,8 +13,8 @@ public class BackOfficeSecurityStampValidator : SecurityStampValidator<BackOffic
{
public BackOfficeSecurityStampValidator(
IOptions<BackOfficeSecurityStampValidatorOptions> options,
BackOfficeSignInManager signInManager, ISystemClock clock, ILoggerFactory logger)
: base(options, signInManager, clock, logger)
BackOfficeSignInManager signInManager, ILoggerFactory logger)
: base(options, signInManager, logger)
{
}
}

View File

@@ -35,15 +35,15 @@ public class BackOfficeSessionIdValidator
{
public const string CookieName = "UMB_UCONTEXT_C";
private readonly GlobalSettings _globalSettings;
private readonly ISystemClock _systemClock;
private readonly TimeProvider _timeProvider;
private readonly IBackOfficeUserManager _userManager;
/// <summary>
/// Initializes a new instance of the <see cref="BackOfficeSessionIdValidator" /> class.
/// </summary>
public BackOfficeSessionIdValidator(ISystemClock systemClock, IOptionsSnapshot<GlobalSettings> globalSettings, IBackOfficeUserManager userManager)
public BackOfficeSessionIdValidator(TimeProvider timeProvider, IOptionsSnapshot<GlobalSettings> globalSettings, IBackOfficeUserManager userManager)
{
_systemClock = systemClock;
_timeProvider = timeProvider;
_globalSettings = globalSettings.Value;
_userManager = userManager;
}
@@ -55,7 +55,7 @@ public class BackOfficeSessionIdValidator
return;
}
var valid = await ValidateSessionAsync(validateInterval, context.HttpContext, context.Options.CookieManager, _systemClock, context.Properties.IssuedUtc, context.Principal?.Identity as ClaimsIdentity);
var valid = await ValidateSessionAsync(validateInterval, context.HttpContext, context.Options.CookieManager, _timeProvider, context.Properties.IssuedUtc, context.Principal?.Identity as ClaimsIdentity);
if (valid == false)
{
@@ -68,7 +68,7 @@ public class BackOfficeSessionIdValidator
TimeSpan validateInterval,
HttpContext httpContext,
ICookieManager cookieManager,
ISystemClock systemClock,
TimeProvider timeProvider,
DateTimeOffset? authTicketIssueDate,
ClaimsIdentity? currentIdentity)
{
@@ -82,9 +82,9 @@ public class BackOfficeSessionIdValidator
throw new ArgumentNullException(nameof(cookieManager));
}
if (systemClock == null)
if (timeProvider == null)
{
throw new ArgumentNullException(nameof(systemClock));
throw new ArgumentNullException(nameof(timeProvider));
}
if (currentIdentity == null)
@@ -93,7 +93,7 @@ public class BackOfficeSessionIdValidator
}
DateTimeOffset? issuedUtc = null;
DateTimeOffset currentUtc = systemClock.UtcNow;
DateTimeOffset currentUtc = timeProvider.GetUtcNow();
// read the last checked time from a custom cookie
var lastCheckedCookie = cookieManager.GetRequestCookie(httpContext, CookieName);

View File

@@ -31,7 +31,7 @@ public class ConfigureBackOfficeCookieOptions : IConfigureNamedOptions<CookieAut
private readonly IRuntimeState _runtimeState;
private readonly SecuritySettings _securitySettings;
private readonly IServiceProvider _serviceProvider;
private readonly ISystemClock _systemClock;
private readonly TimeProvider _timeProvider;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly UmbracoRequestPaths _umbracoRequestPaths;
private readonly IUserService _userService;
@@ -48,7 +48,7 @@ public class ConfigureBackOfficeCookieOptions : IConfigureNamedOptions<CookieAut
/// <param name="dataProtection">The <see cref="IDataProtectionProvider" /></param>
/// <param name="userService">The <see cref="IUserService" /></param>
/// <param name="ipResolver">The <see cref="IIpResolver" /></param>
/// <param name="systemClock">The <see cref="ISystemClock" /></param>
/// <param name="timeProvider">The <see cref="TimeProvider" /></param>
/// <param name="umbracoRequestPaths">The <see cref="UmbracoRequestPaths"/></param>
/// <param name="basicAuthService">The <see cref="IBasicAuthService"/></param>
public ConfigureBackOfficeCookieOptions(
@@ -61,7 +61,7 @@ public class ConfigureBackOfficeCookieOptions : IConfigureNamedOptions<CookieAut
IDataProtectionProvider dataProtection,
IUserService userService,
IIpResolver ipResolver,
ISystemClock systemClock,
TimeProvider timeProvider,
UmbracoRequestPaths umbracoRequestPaths,
IBasicAuthService basicAuthService)
{
@@ -74,7 +74,7 @@ public class ConfigureBackOfficeCookieOptions : IConfigureNamedOptions<CookieAut
_dataProtection = dataProtection;
_userService = userService;
_ipResolver = ipResolver;
_systemClock = systemClock;
_timeProvider = timeProvider;
_umbracoRequestPaths = umbracoRequestPaths;
_basicAuthService = basicAuthService;
}
@@ -187,8 +187,8 @@ public class ConfigureBackOfficeCookieOptions : IConfigureNamedOptions<CookieAut
// When we then try and renew, the difference of issued and expires effectively becomes the new ExpireTimeSpan
// meaning we effectively lose 30 minutes of our ExpireTimeSpan for EVERY principal refresh if we don't
// https://github.com/dotnet/aspnetcore/blob/main/src/Security/Authentication/Cookies/src/CookieAuthenticationHandler.cs#L115
ctx.Properties.IssuedUtc = _systemClock.UtcNow;
ctx.Properties.ExpiresUtc = _systemClock.UtcNow.Add(_globalSettings.TimeOut);
ctx.Properties.IssuedUtc = _timeProvider.GetUtcNow();
ctx.Properties.ExpiresUtc = _timeProvider.GetUtcNow().Add(_globalSettings.TimeOut);
ctx.ShouldRenew = true;
},
OnSigningIn = ctx =>
@@ -296,7 +296,7 @@ public class ConfigureBackOfficeCookieOptions : IConfigureNamedOptions<CookieAut
return;
}
DateTimeOffset currentUtc = _systemClock.UtcNow;
DateTimeOffset currentUtc = _timeProvider.GetUtcNow();
DateTimeOffset? issuedUtc = context.Properties.IssuedUtc;
DateTimeOffset? expiresUtc = context.Properties.ExpiresUtc;

View File

@@ -14,8 +14,8 @@ public class MemberSecurityStampValidator : SecurityStampValidator<MemberIdentit
{
public MemberSecurityStampValidator(
IOptions<MemberSecurityStampValidatorOptions> options,
MemberSignInManager signInManager, ISystemClock clock, ILoggerFactory logger)
: base(options, signInManager, clock, logger)
MemberSignInManager signInManager, ILoggerFactory logger)
: base(options, signInManager, logger)
{
}

View File

@@ -27,11 +27,10 @@ public class TestAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions
IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock,
IBackOfficeSignInManager backOfficeSignInManager,
IUserService userService,
IUmbracoMapper umbracoMapper)
: base(options, logger, encoder, clock)
: base(options, logger, encoder)
{
_backOfficeSignInManager = backOfficeSignInManager;