|
| 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 | +} |
0 commit comments