-
Notifications
You must be signed in to change notification settings - Fork 12
feat: implement storage of session in database #52
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
Open
jhbritton-RSK
wants to merge
14
commits into
server-side-sessions
Choose a base branch
from
feat/35-session-store
base: server-side-sessions
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
35e5414
feat: implement server side session store to be used when storing aut…
jhbritton-RSK c13e871
feat: implement mapping auth ticket to a serialisable form
jhbritton-RSK 7fe083c
feat: implementing ITicketStore implementation
jhbritton-RSK 9e8c19d
feat: adding cofiguration of server side sessions using extention
jhbritton-RSK 3a1778a
feat: added telemetry calls to server side session stores
jhbritton-RSK a9f72f4
fix: create on renew when no existing session
jhbritton-RSK b671b2c
fix: added handling of protection envolope
jhbritton-RSK 00f7b4c
fix: added name and role claim type when deserializing claim principl…
jhbritton-RSK a34fe35
fix: missing XML docs
jhbritton-RSK a8700ab
feat: added in memory session store to allow for integration tests an…
jhbritton-RSK c13b357
feat: server side session integration tests [WIP]
jhbritton-RSK 2426413
test: added integration tests to check for session creation and removal
jhbritton-RSK 48b1c9e
fix: correcting warnings
jhbritton-RSK 35dcc6b
pr: action comments from PR review, missing tests, superfluous tags o…
jhbritton-RSK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/EntityFramework.Storage/src/Mappers/IdentityServerServerSideSessionsExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // Copyright (c) 2026, Rock Solid Knowledge Ltd | ||
| // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Open.IdentityServer.EntityFramework.Mappers; | ||
|
|
||
| /// <summary> | ||
| /// Mapping extension methods for IdentityServerServerSideSessions objects | ||
| /// </summary> | ||
| public static class IdentityServerServerSideSessionsExtensions | ||
| { | ||
| /// <summary> | ||
| /// Mapping extension methods for <see cref="Entities.IdentityServerServerSideSessions"/> | ||
| /// </summary> | ||
| /// <param name="sessionEntity">The entity.</param> | ||
| extension(Entities.IdentityServerServerSideSessions sessionEntity) | ||
| { | ||
| /// <summary> | ||
| /// Mapper for <see cref="Entities.IdentityServerServerSideSessions"/> to convert into an instance of <see cref="Models.IdentityServerServerSideSessions"/> | ||
| /// </summary> | ||
| /// <returns>mapped instance of <see cref="Models.IdentityServerServerSideSessions"/></returns> | ||
| public Models.IdentityServerServerSideSessions ToModel() | ||
| { | ||
| return new Models.IdentityServerServerSideSessions | ||
| { | ||
| Key = sessionEntity.Key, | ||
| Scheme = sessionEntity.Scheme, | ||
| SubjectId = sessionEntity.SubjectId, | ||
| SessionId = sessionEntity.SessionId, | ||
| DisplayName = sessionEntity.DisplayName, | ||
| Created = sessionEntity.Created, | ||
| Renewed = sessionEntity.Renewed, | ||
| Expires = sessionEntity.Expires, | ||
| Data = sessionEntity.Data | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Mapping extension methods for <see cref="Models.IdentityServerServerSideSessions"/> | ||
| /// </summary> | ||
| /// <param name="sessionModel">The model.</param> | ||
| extension(Models.IdentityServerServerSideSessions sessionModel) | ||
| { | ||
| /// <summary> | ||
| /// Mapper for <see cref="Models.IdentityServerServerSideSessions"/> to convert into an instance of <see cref="Entities.IdentityServerServerSideSessions"/> | ||
| /// </summary> | ||
| /// <returns>mapped instance of <see cref="Entities.IdentityServerServerSideSessions"/></returns> | ||
| public Entities.IdentityServerServerSideSessions ToEntity() | ||
| { | ||
| return new Entities.IdentityServerServerSideSessions | ||
| { | ||
| Key = sessionModel.Key, | ||
| Scheme = sessionModel.Scheme, | ||
| SubjectId = sessionModel.SubjectId, | ||
| SessionId = sessionModel.SessionId, | ||
| DisplayName = sessionModel.DisplayName, | ||
| Created = sessionModel.Created, | ||
| Renewed = sessionModel.Renewed, | ||
| Expires = sessionModel.Expires, | ||
| Data = sessionModel.Data | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Updates <see cref="Entities.IdentityServerServerSideSessions"/> with instance of <see cref="Models.IdentityServerServerSideSessions"/> | ||
| /// </summary> | ||
| /// <param name="existingEntity">The entity.</param> | ||
| public void UpdateEntity(Entities.IdentityServerServerSideSessions existingEntity) | ||
| { | ||
| existingEntity.Key = sessionModel.Key; | ||
| existingEntity.Scheme = sessionModel.Scheme; | ||
| existingEntity.SubjectId = sessionModel.SubjectId; | ||
| existingEntity.SessionId = sessionModel.SessionId; | ||
| existingEntity.DisplayName = sessionModel.DisplayName; | ||
| existingEntity.Created = sessionModel.Created; | ||
| existingEntity.Renewed = sessionModel.Renewed; | ||
| existingEntity.Expires = sessionModel.Expires; | ||
| existingEntity.Data = sessionModel.Data; | ||
| } | ||
| } | ||
| } |
124 changes: 124 additions & 0 deletions
124
src/EntityFramework.Storage/src/Stores/Compatibility/IdentityServerServerSideSessionStore.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Copyright (c) 2026, Rock Solid Knowledge Ltd | ||
| // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.Logging; | ||
| using Open.IdentityServer.EntityFramework.Interfaces; | ||
| using Open.IdentityServer.EntityFramework.Mappers; | ||
| using Open.IdentityServer.Services; | ||
| using Open.IdentityServer.Stores; | ||
| using IdentityServerServerSideSessions = Open.IdentityServer.Models.IdentityServerServerSideSessions; | ||
|
|
||
| namespace Open.IdentityServer.EntityFramework.Stores; | ||
|
|
||
| /// <summary> | ||
| /// Storage and retrieval of server-side sessions using entity framework core | ||
| /// </summary> | ||
| public class IdentityServerServerSideSessionStore( | ||
| IPersistedGrantDbContext dbContext, | ||
| ITelemetryService telemetry, | ||
| ILogger<IdentityServerServerSideSessionStore> logger): IIdentityServerServerSideSessionStore | ||
| { | ||
| /// <inheritdoc /> | ||
| public async Task<IdentityServerServerSideSessions?> GetSession(string key) | ||
| { | ||
| using var trace = telemetry.Trace(TelemetryConstants.TraceCategories.Stores, this); | ||
|
|
||
| ArgumentException.ThrowIfNullOrWhiteSpace(key); | ||
|
|
||
| Entities.IdentityServerServerSideSessions? session = await dbContext.ServerSideSessions | ||
| .SingleOrDefaultAsync(x => x.Key == key); | ||
|
|
||
| return session?.ToModel(); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public async Task CreateSession(IdentityServerServerSideSessions session) | ||
| { | ||
| using var trace = telemetry.Trace(TelemetryConstants.TraceCategories.Stores, this); | ||
|
|
||
| ArgumentException.ThrowIfNullOrWhiteSpace(session.Key); | ||
|
|
||
| Entities.IdentityServerServerSideSessions? existing = await dbContext.ServerSideSessions | ||
| .SingleOrDefaultAsync(x => x.Key == session.Key); | ||
|
|
||
| if (existing != null) | ||
| { | ||
| logger.LogError("failed storing '{SessionKey}' session in database, session with key already exists", session.Key); | ||
| return; | ||
| } | ||
|
|
||
| Entities.IdentityServerServerSideSessions sessionEntity = session.ToEntity(); | ||
|
|
||
| await dbContext.ServerSideSessions.AddAsync(sessionEntity); | ||
|
|
||
| try | ||
| { | ||
| await dbContext.SaveChangesAsync(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "exception storing '{SessionKey}' session in database", session.Key); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public async Task UpdateSession(IdentityServerServerSideSessions session) | ||
| { | ||
| using var trace = telemetry.Trace(TelemetryConstants.TraceCategories.Stores, this); | ||
|
|
||
| ArgumentException.ThrowIfNullOrWhiteSpace(session.Key); | ||
|
|
||
| Entities.IdentityServerServerSideSessions? existing = await dbContext.ServerSideSessions | ||
| .SingleOrDefaultAsync(x => x.Key == session.Key); | ||
|
|
||
| if (existing == null) | ||
| { | ||
| logger.LogError("failed updating '{SessionKey}' session in database, session not found", session.Key); | ||
| return; | ||
| } | ||
|
|
||
| session.UpdateEntity(existing); | ||
|
|
||
| try | ||
| { | ||
| await dbContext.SaveChangesAsync(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "exception updating '{SessionKey}' session in database", session.Key); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public async Task DeleteSession(string key) | ||
| { | ||
| using var trace = telemetry.Trace(TelemetryConstants.TraceCategories.Stores, this); | ||
|
|
||
| ArgumentException.ThrowIfNullOrWhiteSpace(key); | ||
|
|
||
| Entities.IdentityServerServerSideSessions? existing = await dbContext.ServerSideSessions | ||
| .SingleOrDefaultAsync(x => x.Key == key); | ||
|
|
||
| if (existing == null) | ||
| { | ||
| logger.LogError("failed deleting '{SessionKey}' session in database, session not found", key); | ||
| return; | ||
| } | ||
|
|
||
| dbContext.ServerSideSessions.Remove(existing); | ||
|
|
||
| try | ||
| { | ||
| await dbContext.SaveChangesAsync(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "exception deleting '{SessionKey}' session in database", key); | ||
| } | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
src/EntityFramework.Storage/test/IntegrationTests/MockLogger.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright (c) 2026, Rock Solid Knowledge Ltd | ||
| // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using Microsoft.Extensions.Logging; | ||
| using Moq; | ||
|
|
||
| namespace Open.IdentityServer.EntityFramework.IntegrationTests; | ||
|
|
||
| public class MockLogger<T> : ILogger<T> | ||
| { | ||
| private readonly ILogger<T> _mock = Mock.Of<ILogger<T>>(); | ||
|
|
||
| public static MockLogger<T> Create() => new(); | ||
|
|
||
| public IDisposable? BeginScope<TState>(TState state) where TState : notnull | ||
| => _mock.BeginScope(state); | ||
|
|
||
| public bool IsEnabled(LogLevel logLevel) | ||
| => _mock.IsEnabled(logLevel); | ||
|
|
||
| public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
| => _mock.Log(logLevel, eventId, state, exception, formatter); | ||
|
|
||
| public void VerifyLog(LogLevel level, string message, Times? times = null) | ||
| { | ||
| Mock.Get(_mock) | ||
| .Verify( | ||
| x => x.Log( | ||
| level, | ||
| It.IsAny<EventId>(), | ||
| It.Is<It.IsAnyType>((v, _) => v.ToString()!.Contains(message)), | ||
| It.IsAny<Exception?>(), | ||
| It.IsAny<Func<It.IsAnyType, Exception?, string>>()), | ||
| times ?? Times.Once()); | ||
| } | ||
|
|
||
| public void VerifyLog(LogLevel level, Times? times = null) | ||
| { | ||
| Mock.Get(_mock) | ||
| .Verify( | ||
| x => x.Log( | ||
| level, | ||
| It.IsAny<EventId>(), | ||
| It.IsAny<It.IsAnyType>(), | ||
| It.IsAny<Exception?>(), | ||
| It.IsAny<Func<It.IsAnyType, Exception?, string>>()), | ||
| times ?? Times.Once()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing unit tests for these mappers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done