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

minor reorg of session mgmt service/store/model names, add OTel support #862

Merged
merged 1 commit into from
Apr 23, 2022
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
16 changes: 16 additions & 0 deletions src/EntityFramework.Storage/Stores/ServerSideSessionStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public ServerSideSessionStore(IPersistedGrantDbContext context, ILogger<ServerSi
/// <inheritdoc/>
public virtual async Task CreateSessionAsync(ServerSideSession session, CancellationToken cancellationToken = default)
{
using var activity = Tracing.StoreActivitySource.StartActivity("ServerSideSessionStore.CreateSession");

cancellationToken = cancellationToken == CancellationToken.None ? CancellationTokenProvider.CancellationToken : cancellationToken;

var entity = new Entities.ServerSideSession
Expand Down Expand Up @@ -87,6 +89,8 @@ public virtual async Task CreateSessionAsync(ServerSideSession session, Cancella
/// <inheritdoc/>
public virtual async Task<ServerSideSession> GetSessionAsync(string key, CancellationToken cancellationToken = default)
{
using var activity = Tracing.StoreActivitySource.StartActivity("ServerSideSessionStore.GetSession");

cancellationToken = cancellationToken == CancellationToken.None ? CancellationTokenProvider.CancellationToken : cancellationToken;

var entity = (await Context.ServerSideSessions.AsNoTracking().Where(x => x.Key == key)
Expand Down Expand Up @@ -118,6 +122,8 @@ public virtual async Task<ServerSideSession> GetSessionAsync(string key, Cancell
/// <inheritdoc/>
public virtual async Task UpdateSessionAsync(ServerSideSession session, CancellationToken cancellationToken = default)
{
using var activity = Tracing.StoreActivitySource.StartActivity("ServerSideSessionStore.UpdateSession");

cancellationToken = cancellationToken == CancellationToken.None ? CancellationTokenProvider.CancellationToken : cancellationToken;

var entity = (await Context.ServerSideSessions.Where(x => x.Key == session.Key)
Expand Down Expand Up @@ -153,6 +159,8 @@ public virtual async Task UpdateSessionAsync(ServerSideSession session, Cancella
/// <inheritdoc/>
public virtual async Task DeleteSessionAsync(string key, CancellationToken cancellationToken = default)
{
using var activity = Tracing.StoreActivitySource.StartActivity("ServerSideSessionStore.DeleteSession");

cancellationToken = cancellationToken == CancellationToken.None ? CancellationTokenProvider.CancellationToken : cancellationToken;

var entity = (await Context.ServerSideSessions.AsNoTracking().Where(x => x.Key == key)
Expand Down Expand Up @@ -183,6 +191,8 @@ public virtual async Task DeleteSessionAsync(string key, CancellationToken cance
/// <inheritdoc/>
public virtual async Task<IReadOnlyCollection<ServerSideSession>> GetSessionsAsync(SessionFilter filter, CancellationToken cancellationToken = default)
{
using var activity = Tracing.StoreActivitySource.StartActivity("ServerSideSessionStore.GetSessions");

cancellationToken = cancellationToken == CancellationToken.None ? CancellationTokenProvider.CancellationToken : cancellationToken;

filter.Validate();
Expand Down Expand Up @@ -212,6 +222,8 @@ public virtual async Task<IReadOnlyCollection<ServerSideSession>> GetSessionsAsy
/// <inheritdoc/>
public virtual async Task DeleteSessionsAsync(SessionFilter filter, CancellationToken cancellationToken = default)
{
using var activity = Tracing.StoreActivitySource.StartActivity("ServerSideSessionStore.DeleteSessions");

cancellationToken = cancellationToken == CancellationToken.None ? CancellationTokenProvider.CancellationToken : cancellationToken;

filter.Validate();
Expand Down Expand Up @@ -251,6 +263,8 @@ public virtual async Task DeleteSessionsAsync(SessionFilter filter, Cancellation
/// <inheritdoc/>
public async Task<IReadOnlyCollection<ServerSideSession>> GetAndRemoveExpiredSessionsAsync(int count, CancellationToken cancellationToken = default)
{
using var activity = Tracing.StoreActivitySource.StartActivity("ServerSideSessionStore.GetAndRemoveExpiredSessions");

cancellationToken = cancellationToken == CancellationToken.None ? CancellationTokenProvider.CancellationToken : cancellationToken;

var entities = await Context.ServerSideSessions
Expand Down Expand Up @@ -288,6 +302,8 @@ public async Task<IReadOnlyCollection<ServerSideSession>> GetAndRemoveExpiredSes
/// <inheritdoc/>
public virtual async Task<QueryResult<ServerSideSession>> QuerySessionsAsync(SessionQuery filter = null, CancellationToken cancellationToken = default)
{
using var activity = Tracing.StoreActivitySource.StartActivity("ServerSideSessionStore.QuerySessions");

cancellationToken = cancellationToken == CancellationToken.None ? CancellationTokenProvider.CancellationToken : cancellationToken;

// it's possible that this implementation could have been done differently (e.g. use the page number for the token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static IIdentityServerBuilder AddServerSideSessions(this IIdentityServerB
builder.Services.AddSingleton<IServerSideSessionsMarker, NopIServerSideSessionsMarker>();
builder.Services.AddSingleton<IPostConfigureOptions<CookieAuthenticationOptions>, PostConfigureApplicationCookieTicketStore>();
builder.Services.TryAddTransient<ISessionManagementService, DefaultSessionManagementService>();
builder.Services.TryAddTransient<IServerSideTicketService, ServerSideTicketService>();
builder.Services.TryAddTransient<IServerSideTicketStore, ServerSideTicketStore>();

// wraps IRefreshTokenService to extend sessions
builder.Services.AddTransientDecorator<IRefreshTokenService, ServerSideSessionRefreshTokenService>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public TicketStoreShim(IHttpContextAccessor httpContextAccessor)
/// <summary>
/// The inner
/// </summary>
private IServerSideTicketService Inner => _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IServerSideTicketService>();
private IServerSideTicketStore Inner => _httpContextAccessor.HttpContext!.RequestServices.GetRequiredService<IServerSideTicketStore>();

/// <inheritdoc />
public Task RemoveAsync(string key)
Expand Down
5 changes: 1 addition & 4 deletions src/IdentityServer/Hosting/IdentityServerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
using Duende.IdentityServer.Events;
using Duende.IdentityServer.Services;
using Duende.IdentityServer.Validation;
using Microsoft.Extensions.DependencyInjection;
using Duende.IdentityServer.Stores;
using System.Collections.Generic;
using Duende.IdentityServer.Configuration;
using Duende.IdentityServer.Models;
using System.Linq;

namespace Duende.IdentityServer.Hosting;
Expand Down
5 changes: 3 additions & 2 deletions src/IdentityServer/Hosting/ServerSideSessionCleanupHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Extensions.Logging;
using Duende.IdentityServer.Configuration;
using Duende.IdentityServer.Services;
using Duende.IdentityServer.Stores;

namespace Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -119,14 +120,14 @@ async Task RunAsync(CancellationToken cancellationToken = default)
{
var logger = serviceScope.ServiceProvider.GetRequiredService<ILogger<ServerSideSessionCleanupHost>>();
var options = serviceScope.ServiceProvider.GetRequiredService<IdentityServerOptions>();
var serverSideTicketService = serviceScope.ServiceProvider.GetRequiredService<IServerSideTicketService>();
var serverSideTicketStore = serviceScope.ServiceProvider.GetRequiredService<IServerSideTicketStore>();
var sessionCoordinationService = serviceScope.ServiceProvider.GetRequiredService<ISessionCoordinationService>();

var found = Int32.MaxValue;

while (found > 0)
{
var sessions = await serverSideTicketService.GetAndRemoveExpiredSessionsAsync(options.ServerSideSessions.RemoveExpiredSessionsBatchSize, cancellationToken);
var sessions = await serverSideTicketStore.GetAndRemoveExpiredSessionsAsync(options.ServerSideSessions.RemoveExpiredSessionsBatchSize, cancellationToken);
found = sessions.Count;

if (found > 0)
Expand Down
60 changes: 60 additions & 0 deletions src/IdentityServer/Models/UserSession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Duende Software. All rights reserved.
// See LICENSE in the project root for license information.

using Microsoft.AspNetCore.Authentication;
using System;
using System.Collections.Generic;

namespace Duende.IdentityServer.Models;

/// <summary>
/// Results from querying user sessions from session management service.
/// </summary>
public class UserSession
{
/// <summary>
/// The subject ID
/// </summary>
public string SubjectId { get; set; } = default!;

/// <summary>
/// The session ID
/// </summary>
public string SessionId { get; set; } = default!;

/// <summary>
/// The display name for the user
/// </summary>
public string DisplayName { get; set; }

/// <summary>
/// The creation time
/// </summary>
public DateTime Created { get; set; }

/// <summary>
/// The renewal time
/// </summary>
public DateTime Renewed { get; set; }

/// <summary>
/// The expiration time
/// </summary>
public DateTime? Expires { get; set; }

/// <summary>
/// The issuer of the token service at login time.
/// </summary>
public string Issuer { get; set; }

/// <summary>
/// The client ids for the session
/// </summary>
public IReadOnlyCollection<string> ClientIds { get; set; } = default!;

/// <summary>
/// The underlying AuthenticationTicket
/// </summary>
public AuthenticationTicket AuthenticationTicket { get; set; } = default!;
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Duende.IdentityServer.Services;
/// </summary>
public class DefaultSessionManagementService : ISessionManagementService
{
private readonly IServerSideTicketService _serverSideTicketService;
private readonly IServerSideTicketStore _serverSideTicketStore;
private readonly IServerSideSessionStore _serverSideSessionStore;
private readonly IPersistedGrantStore _persistedGrantStore;
private readonly IBackChannelLogoutService _backChannelLogoutService;
Expand All @@ -24,12 +24,12 @@ public class DefaultSessionManagementService : ISessionManagementService
/// Ctor.
/// </summary>
public DefaultSessionManagementService(
IServerSideTicketService serverSideTicketService,
IServerSideTicketStore serverSideTicketStore,
IServerSideSessionStore serverSideSessionStore,
IPersistedGrantStore persistedGrantStore,
IBackChannelLogoutService backChannelLogoutService)
{
_serverSideTicketService = serverSideTicketService;
_serverSideTicketStore = serverSideTicketStore;
_serverSideSessionStore = serverSideSessionStore;
_persistedGrantStore = persistedGrantStore;
_backChannelLogoutService = backChannelLogoutService;
Expand All @@ -38,7 +38,9 @@ public DefaultSessionManagementService(
/// <inheritdoc/>
public Task<QueryResult<UserSession>> QuerySessionsAsync(SessionQuery filter = null, CancellationToken cancellationToken = default)
{
return _serverSideTicketService.QuerySessionsAsync(filter, cancellationToken);
using var activity = Tracing.ServiceActivitySource.StartActivity("DefaultSessionManagementService.QuerySessions");

return _serverSideTicketStore.QuerySessionsAsync(filter, cancellationToken);
}

static readonly string[] OnlyTokenTypes = new[] {
Expand All @@ -51,6 +53,8 @@ public Task<QueryResult<UserSession>> QuerySessionsAsync(SessionQuery filter = n
/// <inheritdoc/>
public async Task RemoveSessionsAsync(RemoveSessionsContext context, CancellationToken cancellationToken = default)
{
using var activity = Tracing.ServiceActivitySource.StartActivity("DefaultSessionManagementService.RemoveSessions");

if (context.RevokeTokens || context.RevokeConsents)
{
// delete the tokens
Expand Down Expand Up @@ -84,7 +88,7 @@ public async Task RemoveSessionsAsync(RemoveSessionsContext context, Cancellatio
if (context.SendBackchannelLogoutNotification)
{
// we might have more than one, so load them all
var sessions = await _serverSideTicketService.GetSessionsAsync(
var sessions = await _serverSideTicketStore.GetSessionsAsync(
new SessionFilter
{
SubjectId = context.SubjectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public async Task<TokenValidationResult> ValidateRefreshTokenAsync(string tokenH
{
var result = await Inner.ValidateRefreshTokenAsync(tokenHandle, client);

using var activity = Tracing.ServiceActivitySource.StartActivity("ServerSideSessionRefreshTokenService.ValidateRefreshToken");

if (!result.IsError)
{
var valid = await SessionCoordinationService.ValidateSessionAsync(new SessionValidationRequest
Expand Down
53 changes: 0 additions & 53 deletions src/IdentityServer/Services/ISessionManagementService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using Duende.IdentityServer.Models;
using Duende.IdentityServer.Stores;
using Microsoft.AspNetCore.Authentication;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -27,57 +25,6 @@ public interface ISessionManagementService
Task RemoveSessionsAsync(RemoveSessionsContext context, CancellationToken cancellationToken = default);
}

/// <summary>
/// Results from querying user sessions from session management service.
/// </summary>
public class UserSession
{
/// <summary>
/// The subject ID
/// </summary>
public string SubjectId { get; set; } = default!;

/// <summary>
/// The session ID
/// </summary>
public string SessionId { get; set; } = default!;

/// <summary>
/// The display name for the user
/// </summary>
public string DisplayName { get; set; }

/// <summary>
/// The creation time
/// </summary>
public DateTime Created { get; set; }

/// <summary>
/// The renewal time
/// </summary>
public DateTime Renewed { get; set; }

/// <summary>
/// The expiration time
/// </summary>
public DateTime? Expires { get; set; }

/// <summary>
/// The issuer of the token service at login time.
/// </summary>
public string Issuer { get; set; }

/// <summary>
/// The client ids for the session
/// </summary>
public IReadOnlyCollection<string> ClientIds { get; set; } = default!;

/// <summary>
/// The underlying AuthenticationTicket
/// </summary>
public AuthenticationTicket AuthenticationTicket { get; set; } = default!;
}

/// <summary>
/// Models the information to remove a user's session data.
/// </summary>
Expand Down
Loading