Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new clock abstraction #1341

Merged
merged 5 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion hosts/AspNetIdentity/Pages/Grants/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using System.ComponentModel.DataAnnotations;
using Duende.IdentityServer.Events;
using Duende.IdentityServer.Extensions;
using Duende.IdentityServer.Services;
Expand Down
2 changes: 0 additions & 2 deletions hosts/Configuration/Pages/Account/Create/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// See LICENSE in the project root for license information.

using Duende.IdentityServer;
using Duende.IdentityServer.Events;
using Duende.IdentityServer.Models;
using Duende.IdentityServer.Services;
using Duende.IdentityServer.Stores;
using Duende.IdentityServer.Test;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
Expand Down
1 change: 0 additions & 1 deletion hosts/Configuration/Pages/Grants/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using System.ComponentModel.DataAnnotations;
using Duende.IdentityServer.Events;
using Duende.IdentityServer.Extensions;
using Duende.IdentityServer.Services;
Expand Down
2 changes: 0 additions & 2 deletions hosts/EntityFramework/Pages/Account/Create/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// See LICENSE in the project root for license information.

using Duende.IdentityServer;
using Duende.IdentityServer.Events;
using Duende.IdentityServer.Models;
using Duende.IdentityServer.Services;
using Duende.IdentityServer.Stores;
using Duende.IdentityServer.Test;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
Expand Down
1 change: 0 additions & 1 deletion hosts/EntityFramework/Pages/Grants/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using System.ComponentModel.DataAnnotations;
using Duende.IdentityServer.Events;
using Duende.IdentityServer.Extensions;
using Duende.IdentityServer.Services;
Expand Down
2 changes: 0 additions & 2 deletions hosts/main/Pages/Account/Create/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
// See LICENSE in the project root for license information.

using Duende.IdentityServer;
using Duende.IdentityServer.Events;
using Duende.IdentityServer.Models;
using Duende.IdentityServer.Services;
using Duende.IdentityServer.Stores;
using Duende.IdentityServer.Test;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
Expand Down
1 change: 0 additions & 1 deletion hosts/main/Pages/Grants/Index.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using System.ComponentModel.DataAnnotations;
using Duende.IdentityServer.Events;
using Duende.IdentityServer.Extensions;
using Duende.IdentityServer.Services;
Expand Down
1 change: 0 additions & 1 deletion src/Configuration/Licensing/License.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#nullable disable

using System;
using System.Security.Claims;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Duende.IdentityServer.EntityFramework.Extensions;
Expand Down
1 change: 0 additions & 1 deletion src/EntityFramework/Services/CorsPolicyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Duende.IdentityServer.EntityFramework.Interfaces;
using Duende.IdentityServer.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,4 +549,16 @@ public static IIdentityServerBuilder AddBackchannelAuthenticationUserNotificatio

return builder;
}

/// <summary>
/// Adds the legacy clock based on the pre-.NET8 ISystemClock.
/// </summary>
/// <param name="builder">The builder.</param>
/// <returns></returns>
public static IIdentityServerBuilder AddLegacyClock(this IIdentityServerBuilder builder)
{
builder.Services.AddTransient<IClock, LegacyClock>();

return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
using Duende.IdentityServer.Models;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using static Duende.IdentityServer.Constants;
using static Duende.IdentityServer.IdentityServerConstants;
using Duende.IdentityServer.Extensions;
using Duende.IdentityServer.Hosting.FederatedSignOut;
Expand Down Expand Up @@ -219,6 +218,7 @@ public static IIdentityServerBuilder AddPluggableServices(this IIdentityServerBu
builder.Services.TryAddTransient<IScopeParser, DefaultScopeParser>();
builder.Services.TryAddTransient<ISessionCoordinationService, DefaultSessionCoordinationService>();
builder.Services.TryAddTransient<IReplayCache, DefaultReplayCache>();
builder.Services.TryAddTransient<IClock, DefaultClock>();

builder.Services.TryAddTransient<IBackchannelAuthenticationThrottlingService, DistributedBackchannelAuthenticationThrottlingService>();
builder.Services.TryAddTransient<IBackchannelAuthenticationUserNotificationService, NopBackchannelAuthenticationUserNotificationService>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// See LICENSE in the project root for license information.

using Duende.IdentityServer.Stores;
using Duende.IdentityServer.Validation;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
Expand Down
6 changes: 3 additions & 3 deletions src/IdentityServer/Duende.IdentityServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

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

<ItemGroup>
<ProjectReference Include="..\Storage\Duende.IdentityServer.Storage.csproj"/>
<ProjectReference Include="..\Storage\Duende.IdentityServer.Storage.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 3 additions & 4 deletions src/IdentityServer/Endpoints/Results/AuthorizeResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using Microsoft.AspNetCore.Authentication;
using System.Text.Encodings.Web;
using Duende.IdentityServer.Configuration;
using Duende.IdentityServer.Hosting;
Expand All @@ -34,7 +33,7 @@ internal AuthorizeResult(
IUserSession userSession,
IMessageStore<ErrorMessage> errorMessageStore,
IServerUrls urls,
ISystemClock clock)
IClock clock)
: this(response)
{
_options = options;
Expand All @@ -48,15 +47,15 @@ internal AuthorizeResult(
private IUserSession _userSession;
private IMessageStore<ErrorMessage> _errorMessageStore;
private IServerUrls _urls;
private ISystemClock _clock;
private IClock _clock;

private void Init(HttpContext context)
{
_options ??= context.RequestServices.GetRequiredService<IdentityServerOptions>();
_userSession ??= context.RequestServices.GetRequiredService<IUserSession>();
_errorMessageStore ??= context.RequestServices.GetRequiredService<IMessageStore<ErrorMessage>>();
_urls = _urls ?? context.RequestServices.GetRequiredService<IServerUrls>();
_clock ??= context.RequestServices.GetRequiredService<ISystemClock>();
_clock ??= context.RequestServices.GetRequiredService<IClock>();
}

public async Task ExecuteAsync(HttpContext context)
Expand Down
7 changes: 3 additions & 4 deletions src/IdentityServer/Endpoints/Results/EndSessionResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Duende.IdentityServer.Models;
using Duende.IdentityServer.Stores;
using Duende.IdentityServer.Validation;
using Microsoft.AspNetCore.Authentication;
using Duende.IdentityServer.Services;

namespace Duende.IdentityServer.Endpoints.Results;
Expand All @@ -38,7 +37,7 @@ public EndSessionResult(EndSessionValidationResult result)
internal EndSessionResult(
EndSessionValidationResult result,
IdentityServerOptions options,
ISystemClock clock,
IClock clock,
IServerUrls urls,
IMessageStore<LogoutMessage> logoutMessageStore)
: this(result)
Expand All @@ -50,14 +49,14 @@ internal EndSessionResult(
}

private IdentityServerOptions _options;
private ISystemClock _clock;
private IClock _clock;
private IServerUrls _urls;
private IMessageStore<LogoutMessage> _logoutMessageStore;

private void Init(HttpContext context)
{
_options = _options ?? context.RequestServices.GetRequiredService<IdentityServerOptions>();
_clock = _clock ?? context.RequestServices.GetRequiredService<ISystemClock>();
_clock = _clock ?? context.RequestServices.GetRequiredService<IClock>();
_urls = _urls ?? context.RequestServices.GetRequiredService<IServerUrls>();
_logoutMessageStore = _logoutMessageStore ?? context.RequestServices.GetRequiredService<IMessageStore<LogoutMessage>>();
}
Expand Down
2 changes: 1 addition & 1 deletion src/IdentityServer/Extensions/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ internal static async Task<string> GetIdentityServerSignoutFrameCallbackUrlAsync

if (endSessionMsg != null)
{
var clock = context.RequestServices.GetRequiredService<ISystemClock>();
var clock = context.RequestServices.GetRequiredService<IClock>();
var msg = new Message<LogoutNotificationContext>(endSessionMsg, clock.UtcNow.UtcDateTime);

var endSessionMessageStore = context.RequestServices.GetRequiredService<IMessageStore<LogoutNotificationContext>>();
Expand Down
3 changes: 1 addition & 2 deletions src/IdentityServer/Extensions/KeyManagementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@


using Duende.IdentityServer.Configuration;
using Microsoft.AspNetCore.Authentication;
using Microsoft.IdentityModel.Tokens;
using System;

Expand Down Expand Up @@ -34,7 +33,7 @@ internal static bool IsWithinInitializationDuration(this KeyManagementOptions op
return (age <= options.InitializationDuration);
}

internal static TimeSpan GetAge(this ISystemClock clock, DateTime date)
internal static TimeSpan GetAge(this IClock clock, DateTime date)
{
var now = clock.UtcNow.UtcDateTime;
if (date > now) now = date;
Expand Down
3 changes: 1 addition & 2 deletions src/IdentityServer/Extensions/TokenExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using IdentityModel;
using Duende.IdentityServer.Models;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
Expand All @@ -30,7 +29,7 @@ public static class TokenExtensions
/// <param name="logger"></param>
/// <returns></returns>
public static Dictionary<string, object> CreateJwtPayloadDictionary(this Token token,
IdentityServerOptions options, ISystemClock clock, ILogger logger)
IdentityServerOptions options, IClock clock, ILogger logger)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Duende.IdentityServer.Configuration;
using Duende.IdentityServer.Configuration.DependencyInjection;
using Duende.IdentityServer.Stores;
using Duende.IdentityServer.Validation;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ internal class IdentityServerAuthenticationService : IAuthenticationService
{
private readonly IAuthenticationService _inner;
private readonly IAuthenticationSchemeProvider _schemes;
private readonly ISystemClock _clock;
private readonly IClock _clock;
private readonly IUserSession _session;
private readonly ILogger<IdentityServerAuthenticationService> _logger;

public IdentityServerAuthenticationService(
Decorator<IAuthenticationService> decorator,
IAuthenticationSchemeProvider schemes,
ISystemClock clock,
IClock clock,
IUserSession session,
ILogger<IdentityServerAuthenticationService> logger)
{
Expand Down
1 change: 0 additions & 1 deletion src/IdentityServer/Hosting/IdentityServerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Threading.Tasks;
using Duende.IdentityServer.Events;
using Duende.IdentityServer.Services;
using Duende.IdentityServer.Validation;
using Duende.IdentityServer.Models;
using System.Linq;
using Duende.IdentityServer.Configuration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class LocalApiAuthenticationHandler : AuthenticationHandler<LocalApiAuthe
private readonly ILogger _logger;

/// <inheritdoc />
public LocalApiAuthenticationHandler(IOptionsMonitor<LocalApiAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, ITokenValidator tokenValidator)
: base(options, logger, encoder, clock)
public LocalApiAuthenticationHandler(IOptionsMonitor<LocalApiAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ITokenValidator tokenValidator)
: base(options, logger, encoder)
{
_tokenValidator = tokenValidator;
_logger = logger.CreateLogger<LocalApiAuthenticationHandler>();
Expand Down
5 changes: 2 additions & 3 deletions src/IdentityServer/IdentityServerTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Duende.IdentityServer.Models;
using Duende.IdentityServer.Services;
using IdentityModel;
using Microsoft.AspNetCore.Authentication;
using System;
using System.Collections.Generic;
using System.Security.Claims;
Expand All @@ -22,7 +21,7 @@ public class IdentityServerTools
internal readonly IServiceProvider ServiceProvider;
internal readonly IIssuerNameService IssuerNameService;
private readonly ITokenCreationService _tokenCreation;
private readonly ISystemClock _clock;
private readonly IClock _clock;

/// <summary>
/// Initializes a new instance of the <see cref="IdentityServerTools" /> class.
Expand All @@ -31,7 +30,7 @@ public class IdentityServerTools
/// <param name="issuerNameService">The issuer name service</param>
/// <param name="tokenCreation">The token creation service.</param>
/// <param name="clock">The clock.</param>
public IdentityServerTools(IServiceProvider serviceProvider, IIssuerNameService issuerNameService, ITokenCreationService tokenCreation, ISystemClock clock)
public IdentityServerTools(IServiceProvider serviceProvider, IIssuerNameService issuerNameService, ITokenCreationService tokenCreation, IClock clock)
{
ServiceProvider = serviceProvider;
IssuerNameService = issuerNameService;
Expand Down
24 changes: 24 additions & 0 deletions src/IdentityServer/Infrastructure/Clock/DefaultClock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.


using System;

namespace Duende.IdentityServer;

class DefaultClock : IClock
{
private readonly TimeProvider _timeProvider;

public DefaultClock()
{
_timeProvider = TimeProvider.System;
}

public DefaultClock(TimeProvider timeProvider)
{
_timeProvider = timeProvider;
}

public DateTimeOffset UtcNow { get => _timeProvider.GetUtcNow(); }
}
18 changes: 18 additions & 0 deletions src/IdentityServer/Infrastructure/Clock/IClock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.


using System;

namespace Duende.IdentityServer;

/// <summary>
/// Abstraction for the date/time.
/// </summary>
public interface IClock
{
/// <summary>
/// The current UTC date/time.
/// </summary>
DateTimeOffset UtcNow { get; }
}
Loading