Skip to content

Commit 70bebb6

Browse files
authored
First Implementation of Core Project (#2)
* First Implementation of Core Project * Extensions & Options & Validators * Errors & Utilities & Multi Tenancy Support & Events * Fixes * Add XML Definitions
1 parent 4e12f04 commit 70bebb6

78 files changed

Lines changed: 3446 additions & 5 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/CodeBeam.UltimateAuth.Core/Abstractions/.gitkeep

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace CodeBeam.UltimateAuth.Core.Abstractions
2+
{
3+
/// <summary>
4+
/// Represents the minimal user abstraction required by UltimateAuth.
5+
/// Includes the unique user identifier and an optional set of claims that
6+
/// may be used during authentication or session creation.
7+
/// </summary>
8+
public interface IUser<TUserId>
9+
{
10+
/// <summary>
11+
/// Gets the unique identifier of the user.
12+
/// </summary>
13+
TUserId UserId { get; }
14+
15+
/// <summary>
16+
/// Gets an optional collection of user claims that may be used to construct
17+
/// session-level claim snapshots. Implementations may return <c>null</c> if no claims are available.
18+
/// </summary>
19+
IReadOnlyDictionary<string, object>? Claims { get; }
20+
}
21+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace CodeBeam.UltimateAuth.Core.Abstractions
2+
{
3+
/// <summary>
4+
/// Defines conversion logic for transforming user identifiers between
5+
/// strongly typed values, string representations, and binary formats.
6+
/// Implementations enable consistent storage, token serialization,
7+
/// and multitenant key partitioning.
8+
/// </summary>
9+
public interface IUserIdConverter<TUserId>
10+
{
11+
/// <summary>
12+
/// Converts the typed user identifier into its canonical string representation.
13+
/// </summary>
14+
/// <param name="id">The user identifier to convert.</param>
15+
/// <returns>A stable and reversible string representation of the identifier.</returns>
16+
string ToString(TUserId id);
17+
18+
/// <summary>
19+
/// Converts the typed user identifier into a binary representation suitable for efficient storage or hashing operations.
20+
/// </summary>
21+
/// <param name="id">The user identifier to convert.</param>
22+
/// <returns>A byte array representing the identifier.</returns>
23+
byte[] ToBytes(TUserId id);
24+
25+
/// <summary>
26+
/// Reconstructs a typed user identifier from its string representation.
27+
/// </summary>
28+
/// <param name="value">The string-encoded identifier.</param>
29+
/// <returns>The reconstructed user identifier.</returns>
30+
/// <exception cref="FormatException">
31+
/// Thrown when the input value cannot be parsed into a valid identifier.
32+
/// </exception>
33+
TUserId FromString(string value);
34+
35+
/// <summary>
36+
/// Reconstructs a typed user identifier from its binary representation.
37+
/// </summary>
38+
/// <param name="binary">The byte array containing the encoded identifier.</param>
39+
/// <returns>The reconstructed user identifier.</returns>
40+
/// <exception cref="FormatException">
41+
/// Thrown when the input binary value cannot be parsed into a valid identifier.
42+
/// </exception>
43+
TUserId FromBytes(byte[] binary);
44+
}
45+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace CodeBeam.UltimateAuth.Core.Abstractions
2+
{
3+
/// <summary>
4+
/// Resolves the appropriate <see cref="IUserIdConverter{TUserId}"/> instance
5+
/// for a given user identifier type. Used internally by UltimateAuth to
6+
/// ensure consistent serialization and parsing of user IDs across all components.
7+
/// </summary>
8+
public interface IUserIdConverterResolver
9+
{
10+
/// <summary>
11+
/// Retrieves the registered <see cref="IUserIdConverter{TUserId}"/> for the specified user ID type.
12+
/// </summary>
13+
/// <typeparam name="TUserId">The type of the user identifier.</typeparam>
14+
/// <returns>
15+
/// A converter capable of transforming the user ID to and from its string
16+
/// and binary representations.
17+
/// </returns>
18+
/// <exception cref="InvalidOperationException">
19+
/// Thrown if no converter has been registered for the requested user ID type.
20+
/// </exception>
21+
IUserIdConverter<TUserId> GetConverter<TUserId>();
22+
}
23+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using CodeBeam.UltimateAuth.Core.Domain;
2+
using CodeBeam.UltimateAuth.Core.Models;
3+
4+
namespace CodeBeam.UltimateAuth.Core.Abstractions
5+
{
6+
/// <summary>
7+
/// Provides high-level session lifecycle operations such as creation, refresh, validation, and revocation.
8+
/// </summary>
9+
/// <typeparam name="TUserId">The type used to uniquely identify the user.</typeparam>
10+
public interface ISessionService<TUserId>
11+
{
12+
/// <summary>
13+
/// Creates a new login session for the specified user.
14+
/// </summary>
15+
/// <param name="tenantId">
16+
/// The tenant identifier. Use <c>null</c> for single-tenant applications.
17+
/// </param>
18+
/// <param name="userId">The user associated with the session.</param>
19+
/// <param name="deviceInfo">Information about the device initiating the session.</param>
20+
/// <param name="metadata">Optional metadata describing the session context.</param>
21+
/// <param name="now">The current UTC timestamp.</param>
22+
/// <returns>
23+
/// A result containing the newly created session, chain, and session root.
24+
/// </returns>
25+
Task<SessionResult<TUserId>> CreateLoginSessionAsync(string? tenantId, TUserId userId, DeviceInfo deviceInfo, SessionMetadata? metadata, DateTime now);
26+
27+
/// <summary>
28+
/// Rotates the specified session and issues a new one while preserving the session chain.
29+
/// </summary>
30+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
31+
/// <param name="currentSessionId">The active session identifier to be refreshed.</param>
32+
/// <param name="now">The current UTC timestamp.</param>
33+
/// <returns>
34+
/// A result containing the refreshed session and updated chain.
35+
/// </returns>
36+
/// <exception cref="UnauthorizedAccessException">
37+
/// Thrown if the session, its chain, or the user's session root is invalid.
38+
/// </exception>
39+
Task<SessionResult<TUserId>> RefreshSessionAsync(string? tenantId, AuthSessionId currentSessionId,DateTime now);
40+
41+
/// <summary>
42+
/// Revokes a single session, preventing further use.
43+
/// </summary>
44+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
45+
/// <param name="sessionId">The session identifier to revoke.</param>
46+
/// <param name="at">The UTC timestamp of the revocation.</param>
47+
Task RevokeSessionAsync(string? tenantId, AuthSessionId sessionId, DateTime at);
48+
49+
/// <summary>
50+
/// Revokes an entire session chain (device-level logout).
51+
/// </summary>
52+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
53+
/// <param name="chainId">The session chain identifier to revoke.</param>
54+
/// <param name="at">The UTC timestamp of the revocation.</param>
55+
Task RevokeChainAsync(string? tenantId, ChainId chainId, DateTime at);
56+
57+
/// <summary>
58+
/// Revokes the user's session root, invalidating all existing sessions across all chains.
59+
/// </summary>
60+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
61+
/// <param name="userId">The user whose root should be revoked.</param>
62+
/// <param name="at">The UTC timestamp of the revocation.</param>
63+
Task RevokeRootAsync(string? tenantId, TUserId userId, DateTime at);
64+
65+
/// <summary>
66+
/// Validates a session and evaluates its current state, including expiration, revocation, and security version alignment.
67+
/// </summary>
68+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
69+
/// <param name="sessionId">The session identifier to validate.</param>
70+
/// <param name="now">The current UTC timestamp.</param>
71+
/// <returns>
72+
/// A detailed validation result describing the session, chain, root,
73+
/// and computed session state.
74+
/// </returns>
75+
Task<SessionValidationResult<TUserId>> ValidateSessionAsync(string? tenantId, AuthSessionId sessionId, DateTime now);
76+
77+
/// <summary>
78+
/// Retrieves all session chains belonging to the specified user.
79+
/// </summary>
80+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
81+
/// <param name="userId">The user whose session chains are requested.</param>
82+
/// <returns>A read-only list of session chains.</returns>
83+
Task<IReadOnlyList<ISessionChain<TUserId>>> GetChainsAsync(string? tenantId, TUserId userId);
84+
85+
/// <summary>
86+
/// Retrieves all sessions belonging to a specific session chain.
87+
/// </summary>
88+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
89+
/// <param name="chainId">The session chain identifier.</param>
90+
/// <returns>A read-only list of sessions contained within the chain.</returns>
91+
Task<IReadOnlyList<ISession<TUserId>>> GetSessionsAsync(string? tenantId, ChainId chainId);
92+
}
93+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace CodeBeam.UltimateAuth.Core.Abstractions
2+
{
3+
/// <summary>
4+
/// Default session store factory that throws until a real store implementation is registered.
5+
/// </summary>
6+
public sealed class DefaultSessionStoreFactory : ISessionStoreFactory
7+
{
8+
/// <summary>Creates a session store instance for the given user ID type, but always throws because no store has been registered.</summary>
9+
/// <param name="tenantId">The tenant identifier, or <c>null</c> in single-tenant mode.</param>
10+
/// <typeparam name="TUserId">The type used to uniquely identify the user.</typeparam>
11+
/// <returns>Never returns; always throws.</returns>
12+
/// <exception cref="InvalidOperationException">Thrown when no session store implementation has been configured.</exception>
13+
public ISessionStore<TUserId> Create<TUserId>(string? tenantId)
14+
{
15+
throw new InvalidOperationException(
16+
"No ISessionStore<TUserId> implementation registered. " +
17+
"Call AddUltimateAuthServer().AddSessionStore<TStore>() to provide a real implementation."
18+
);
19+
}
20+
}
21+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using CodeBeam.UltimateAuth.Core.Domain;
2+
3+
namespace CodeBeam.UltimateAuth.Core.Abstractions
4+
{
5+
/// <summary>
6+
/// Defines the low-level persistence operations for sessions, session chains, and session roots in a multi-tenant or single-tenant environment.
7+
/// Store implementations provide durable and atomic data access.
8+
/// </summary>
9+
public interface ISessionStore<TUserId>
10+
{
11+
/// <summary>
12+
/// Retrieves a session by its identifier within the given tenant context.
13+
/// </summary>
14+
/// <param name="tenantId">The tenant identifier, or <c>null</c> for single-tenant mode.</param>
15+
/// <param name="sessionId">The session identifier.</param>
16+
/// <returns>The session instance or <c>null</c> if not found.</returns>
17+
Task<ISession<TUserId>?> GetSessionAsync(string? tenantId, AuthSessionId sessionId);
18+
19+
/// <summary>
20+
/// Persists a new session or updates an existing one within the tenant scope.
21+
/// Implementations must ensure atomic writes.
22+
/// </summary>
23+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
24+
/// <param name="session">The session to persist.</param>
25+
Task SaveSessionAsync(string? tenantId, ISession<TUserId> session);
26+
27+
/// <summary>
28+
/// Marks the specified session as revoked, preventing future authentication.
29+
/// Revocation timestamp must be stored reliably.
30+
/// </summary>
31+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
32+
/// <param name="sessionId">The session identifier.</param>
33+
/// <param name="at">The UTC timestamp of revocation.</param>
34+
Task RevokeSessionAsync(string? tenantId, AuthSessionId sessionId, DateTime at);
35+
36+
/// <summary>
37+
/// Returns all sessions belonging to the specified chain, ordered according to store implementation rules.
38+
/// </summary>
39+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
40+
/// <param name="chainId">The chain identifier.</param>
41+
/// <returns>A read-only list of sessions.</returns>
42+
Task<IReadOnlyList<ISession<TUserId>>> GetSessionsByChainAsync(string? tenantId, ChainId chainId);
43+
44+
/// <summary>
45+
/// Retrieves a session chain by identifier. Returns <c>null</c> if the chain does not exist in the provided tenant context.
46+
/// </summary>
47+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
48+
/// <param name="chainId">The chain identifier.</param>
49+
/// <returns>The chain or <c>null</c>.</returns>
50+
Task<ISessionChain<TUserId>?> GetChainAsync(string? tenantId, ChainId chainId);
51+
52+
/// <summary>
53+
/// Inserts a new session chain into the store. Implementations must ensure consistency with the related sessions and session root.
54+
/// </summary>
55+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
56+
/// <param name="chain">The chain to save.</param>
57+
Task SaveChainAsync(string? tenantId, ISessionChain<TUserId> chain);
58+
59+
/// <summary>
60+
/// Updates an existing session chain, typically after session rotation or revocation. Implementations must preserve atomicity.
61+
/// </summary>
62+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
63+
/// <param name="chain">The updated session chain.</param>
64+
Task UpdateChainAsync(string? tenantId, ISessionChain<TUserId> chain);
65+
66+
/// <summary>
67+
/// Marks the entire session chain as revoked, invalidating all associated sessions for the device or app family.
68+
/// </summary>
69+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
70+
/// <param name="chainId">The chain to revoke.</param>
71+
/// <param name="at">The UTC timestamp of revocation.</param>
72+
Task RevokeChainAsync(string? tenantId, ChainId chainId, DateTime at);
73+
74+
/// <summary>
75+
/// Retrieves the active session identifier for the specified chain.
76+
/// This is typically an O(1) lookup and used for session rotation.
77+
/// </summary>
78+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
79+
/// <param name="chainId">The chain whose active session is requested.</param>
80+
/// <returns>The active session identifier or <c>null</c>.</returns>
81+
Task<AuthSessionId?> GetActiveSessionIdAsync(string? tenantId, ChainId chainId);
82+
83+
/// <summary>
84+
/// Sets or replaces the active session identifier for the specified chain.
85+
/// Must be atomic to prevent race conditions during refresh.
86+
/// </summary>
87+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
88+
/// <param name="chainId">The chain whose active session is being set.</param>
89+
/// <param name="sessionId">The new active session identifier.</param>
90+
Task SetActiveSessionIdAsync(string? tenantId, ChainId chainId, AuthSessionId sessionId);
91+
92+
/// <summary>
93+
/// Retrieves all session chains belonging to the specified user within the tenant scope.
94+
/// </summary>
95+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
96+
/// <param name="userId">The user whose chains are being retrieved.</param>
97+
/// <returns>A read-only list of session chains.</returns>
98+
Task<IReadOnlyList<ISessionChain<TUserId>>> GetChainsByUserAsync(string? tenantId, TUserId userId);
99+
100+
/// <summary>
101+
/// Retrieves the session root for the user, which represents the full set of chains and their associated security metadata.
102+
/// Returns <c>null</c> if the root does not exist.
103+
/// </summary>
104+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
105+
/// <param name="userId">The user identifier.</param>
106+
/// <returns>The session root or <c>null</c>.</returns>
107+
Task<ISessionRoot<TUserId>?> GetSessionRootAsync(string? tenantId, TUserId userId);
108+
109+
/// <summary>
110+
/// Persists a session root structure, usually after chain creation, rotation, or security operations.
111+
/// </summary>
112+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
113+
/// <param name="root">The session root to save.</param>
114+
Task SaveSessionRootAsync(string? tenantId, ISessionRoot<TUserId> root);
115+
116+
/// <summary>
117+
/// Revokes the session root, invalidating all chains and sessions belonging to the specified user in the tenant scope.
118+
/// </summary>
119+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
120+
/// <param name="userId">The user whose root should be revoked.</param>
121+
/// <param name="at">The UTC timestamp of revocation.</param>
122+
Task RevokeSessionRootAsync(string? tenantId, TUserId userId, DateTime at);
123+
124+
/// <summary>
125+
/// Removes expired sessions from the store while leaving chains and session roots intact. Cleanup strategy is determined by the store implementation.
126+
/// </summary>
127+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
128+
/// <param name="now">The current UTC timestamp.</param>
129+
Task DeleteExpiredSessionsAsync(string? tenantId, DateTime now);
130+
131+
/// <summary>
132+
/// Retrieves the chain identifier associated with the specified session.
133+
/// </summary>
134+
/// <param name="tenantId">The tenant identifier, or <c>null</c>.</param>
135+
/// <param name="sessionId">The session identifier.</param>
136+
/// <returns>The chain identifier or <c>null</c>.</returns>
137+
Task<ChainId?> GetChainIdBySessionAsync(string? tenantId, AuthSessionId sessionId);
138+
}
139+
140+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace CodeBeam.UltimateAuth.Core.Abstractions
2+
{
3+
/// <summary>
4+
/// Provides a factory abstraction for creating tenant-scoped session store
5+
/// instances capable of persisting sessions, chains, and session roots.
6+
/// Implementations typically resolve concrete <see cref="ISessionStore{TUserId}"/> types from the dependency injection container.
7+
/// </summary>
8+
public interface ISessionStoreFactory
9+
{
10+
/// <summary>
11+
/// Creates and returns a session store instance for the specified user ID type within the given tenant context.
12+
/// </summary>
13+
/// <typeparam name="TUserId">The type used to uniquely identify users.</typeparam>
14+
/// <param name="tenantId">
15+
/// The tenant identifier for multi-tenant environments, or <c>null</c> for single-tenant mode.
16+
/// </param>
17+
/// <returns>
18+
/// An <see cref="ISessionStore{TUserId}"/> implementation able to perform session persistence operations.
19+
/// </returns>
20+
/// <exception cref="InvalidOperationException">
21+
/// Thrown if no compatible session store implementation is registered.
22+
/// </exception>
23+
ISessionStore<TUserId> Create<TUserId>(string? tenantId);
24+
}
25+
}

0 commit comments

Comments
 (0)