Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ public sealed class LabSessionDiagnosticsPolicyService(IOptions<UAuthServerOptio
public LabSessionDiagnosticsPolicy Get()
{
var session = serverOptions.Value.Session;
var token = serverOptions.Value.Token;
var autoRefresh = clientOptions.Value.AutoRefresh;

return new LabSessionDiagnosticsPolicy(
session.Lifetime,
token.SessionIdLifetime,
session.TouchInterval,
session.IdleTimeout,
session.MaxLifetime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ public sealed class IssuedSession
public required UAuthSession Session { get; init; }

/// <summary>
/// Opaque session identifier returned to the client.
/// Populated only for opaque credential sessions.
/// Metadata-only sessions never expose an opaque grant.
/// </summary>
public required string OpaqueSessionId { get; init; }
public string? OpaqueSessionId { get; init; }

/// <summary>
/// Indicates whether this issuance is metadata-only
/// (used in SemiHybrid mode).
/// </summary>
public bool IsMetadataOnly { get; init; }
public bool IsMetadataOnly => Session.IsMetadataOnly;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public sealed class SessionIssuanceContext
public required SessionMetadata Metadata { get; init; }
public required UAuthMode Mode { get; init; }
public required UAuthSessionOptions SessionOptions { get; init; }
public required UAuthTokenOptions TokenOptions { get; init; }

/// <summary>
/// Optional chain identifier.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public sealed record SessionRotationContext
public required SessionMetadata Metadata { get; init; } = SessionMetadata.Empty;
public required UAuthMode Mode { get; init; }
public required UAuthSessionOptions SessionOptions { get; init; }
public required UAuthTokenOptions TokenOptions { get; init; }
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CodeBeam.UltimateAuth.Core.Domain;
using CodeBeam.UltimateAuth.Core.MultiTenancy;
using CodeBeam.UltimateAuth.Core.Options;

namespace CodeBeam.UltimateAuth.Core.Contracts;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace CodeBeam.UltimateAuth.Core.Domain;

/// <summary>
/// Defines whether a persisted session can authenticate an opaque credential or only represents JWT lineage.
/// </summary>
public enum SessionCredentialPurpose
{
/// <summary>
/// The session ID is an opaque authentication credential.
/// </summary>
Opaque = 0,

/// <summary>
/// The session ID is a non-secret correlation identifier.
/// It cannot authenticate an opaque request.
/// </summary>
MetadataOnly = 1
}
34 changes: 25 additions & 9 deletions src/CodeBeam.UltimateAuth.Core/Domain/Session/UAuthSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public sealed class UAuthSession : IVersionedEntity
public SessionMetadata Metadata { get; }
public long Version { get; set; }

public SessionCredentialPurpose CredentialPurpose { get; }

public bool IsOpaqueCredential => CredentialPurpose == SessionCredentialPurpose.Opaque;
public bool IsMetadataOnly => CredentialPurpose == SessionCredentialPurpose.MetadataOnly;

public bool IsRevoked => RevokedAt != null;

private UAuthSession(
Expand All @@ -33,7 +38,8 @@ private UAuthSession(
DeviceContext device,
ClaimsSnapshot claims,
SessionMetadata metadata,
long version)
long version,
SessionCredentialPurpose credentialPurpose)
{
SessionId = sessionId;
Tenant = tenant;
Expand All @@ -47,6 +53,7 @@ private UAuthSession(
Claims = claims;
Metadata = metadata;
Version = version;
CredentialPurpose = credentialPurpose;
}

public static UAuthSession Create(
Expand All @@ -59,8 +66,12 @@ public static UAuthSession Create(
long securityVersion,
DeviceContext device,
ClaimsSnapshot? claims,
SessionMetadata metadata)
SessionMetadata metadata,
SessionCredentialPurpose credentialPurpose = SessionCredentialPurpose.Opaque)
{
if (!Enum.IsDefined(credentialPurpose))
throw new ArgumentOutOfRangeException(nameof(credentialPurpose));

return new(
sessionId,
tenant,
Expand All @@ -73,8 +84,8 @@ public static UAuthSession Create(
device: device,
claims: claims ?? ClaimsSnapshot.Empty,
metadata: metadata,
version: 0
);
credentialPurpose: credentialPurpose,
version: 0);
}

public UAuthSession Revoke(DateTimeOffset at)
Expand All @@ -94,7 +105,8 @@ public UAuthSession Revoke(DateTimeOffset at)
Device,
Claims,
Metadata,
Version + 1
Version + 1,
credentialPurpose: CredentialPurpose
);
}

Expand All @@ -110,7 +122,8 @@ internal static UAuthSession FromProjection(
DeviceContext device,
ClaimsSnapshot claims,
SessionMetadata metadata,
long version)
long version,
SessionCredentialPurpose credentialPurpose)
{
return new UAuthSession(
sessionId,
Expand All @@ -124,7 +137,8 @@ internal static UAuthSession FromProjection(
device,
claims,
metadata,
version
version,
credentialPurpose
);
}

Expand Down Expand Up @@ -156,7 +170,8 @@ public UAuthSession ExtendExpiration(DateTimeOffset expiresAt)
Device,
Claims,
Metadata,
Version + 1
Version + 1,
credentialPurpose: CredentialPurpose
);
}

Expand All @@ -177,7 +192,8 @@ public UAuthSession WithChain(SessionChainId chainId)
device: Device,
claims: Claims,
metadata: Metadata,
version: Version + 1
version: Version + 1,
credentialPurpose: CredentialPurpose
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public async Task<RefreshTokenValidationResult> ValidateAsync(RefreshTokenValida
if (stored is null)
return RefreshTokenValidationResult.NotFound();

if (stored.Tenant != context.Tenant)
return RefreshTokenValidationResult.Invalid();

if (stored.IsExpired(context.Now))
return RefreshTokenValidationResult.Expired(stored);

Expand Down
99 changes: 25 additions & 74 deletions src/CodeBeam.UltimateAuth.Core/Options/UAuthSessionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace CodeBeam.UltimateAuth.Core.Options;

// TODO: Add rotate on refresh (especially for Hybrid). Default behavior should be single session in chain for Hybrid, but can be configured.
// And add RotateAsync method.
// Implement all options.

/// <summary>
/// Defines configuration settings that control the lifecycle, security behavior, and device constraints of UltimateAuth
Expand All @@ -14,10 +13,7 @@ namespace CodeBeam.UltimateAuth.Core.Options;
/// </summary>
public sealed class UAuthSessionOptions
{
/// <summary>
/// The standard lifetime of a session before it expires.
/// This is the duration added during login or refresh.
/// </summary>
[Obsolete("Use Token.SessionIdLifetime instead. This property has no effect and will be removed in a future version.")]
public TimeSpan Lifetime { get; set; } = TimeSpan.FromDays(7);

/// <summary>
Expand All @@ -40,99 +36,54 @@ public sealed class UAuthSessionOptions
/// </summary>
public TimeSpan? IdleTimeout { get; set; }

// TODO: Implement this option on PureJwt mode.
/// <summary>
/// Minimum interval between LastSeenAt updates.
/// Prevents excessive writes under high traffic.
/// </summary>
public TimeSpan? TouchInterval { get; set; } = TimeSpan.FromMinutes(5);

/// <summary>
/// Maximum number of device session chains a single user may have.
/// Set to zero to indicate no user-level chain limit.
///
/// NOTE:
/// Enforcement is not active in v0.0.1.
/// This option is reserved for future security policies.
/// </summary>
public int MaxChainsPerUser { get; set; } = 0;

/// <summary>
/// Maximum number of session rotations within a single chain.
/// Used for cleanup, replay protection, and analytics.
/// </summary>
public int MaxSessionsPerChain { get; set; } = 100;

/// <summary>
/// Optional limit on the number of session chains allowed per platform
/// (e.g. "web" = 1, "mobile" = 1).
///
/// NOTE:
/// Enforcement is not active in v0.0.1.
/// This option is reserved for future security policies.
/// </summary>
public Dictionary<string, int>? MaxChainsPerPlatform { get; set; }

/// <summary>
/// Defines platform categories that map multiple platforms
/// into a single abstract group (e.g. mobile: [ "ios", "android", "tablet" ]).
///
/// NOTE:
/// Enforcement is not active in v0.0.1.
/// This option is reserved for future security policies.
/// </summary>
public Dictionary<string, string[]>? PlatformCategories { get; set; }

/// <summary>
/// Limits how many session chains can exist per platform category
/// (e.g. mobile = 1, desktop = 2).
///
/// NOTE:
/// Enforcement is not active in v0.0.1.
/// This option is reserved for future security policies.
/// </summary>
public Dictionary<string, int>? MaxChainsPerCategory { get; set; }

/// <summary>
/// Enables binding sessions to the user's IP address.
/// When enabled, IP mismatches can invalidate a session.
///
/// NOTE:
/// Enforcement is not active in v0.0.1.
/// This option is reserved for future security policies.
/// </summary>
public bool EnableIpBinding { get; set; } = false;

/// <summary>
/// Enables binding sessions to the user's User-Agent header.
/// When enabled, UA mismatches can invalidate a session.
///
/// NOTE:
/// Enforcement is not active in v0.0.1.
/// This option is reserved for future security policies.
/// </summary>
public bool EnableUserAgentBinding { get; set; } = false;
// TODO: Implement these.
///// <summary>
///// Enables binding sessions to the user's IP address.
///// When enabled, IP mismatches can invalidate a session.
/////
///// NOTE:
///// Enforcement is not active in v0.0.1.
///// This option is reserved for future security policies.
///// </summary>
//public bool EnableIpBinding { get; set; } = false;

///// <summary>
///// Enables binding sessions to the user's User-Agent header.
///// When enabled, UA mismatches can invalidate a session.
/////
///// NOTE:
///// Enforcement is not active in v0.0.1.
///// This option is reserved for future security policies.
///// </summary>
//public bool EnableUserAgentBinding { get; set; } = false;

/// <summary>
/// NOTE:
/// Enforcement is not active in v0.0.1.
/// This option is reserved for future security policies.
/// Gets or sets the behavior when an authentication session is used by a mismatched device.
/// </summary>
public DeviceMismatchBehavior DeviceMismatchBehavior { get; set; } = DeviceMismatchBehavior.Reject;

internal UAuthSessionOptions Clone() => new()
{
SlidingExpiration = SlidingExpiration,
IdleTimeout = IdleTimeout,
Lifetime = Lifetime,
MaxLifetime = MaxLifetime,
TouchInterval = TouchInterval,
DeviceMismatchBehavior = DeviceMismatchBehavior,
MaxChainsPerUser = MaxChainsPerUser,
MaxSessionsPerChain = MaxSessionsPerChain,
MaxChainsPerPlatform = MaxChainsPerPlatform is null ? null : new Dictionary<string, int>(MaxChainsPerPlatform),
MaxChainsPerCategory = MaxChainsPerCategory is null ? null : new Dictionary<string, int>(MaxChainsPerCategory),
PlatformCategories = PlatformCategories is null ? null : PlatformCategories.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToArray()),
EnableIpBinding = EnableIpBinding,
EnableUserAgentBinding = EnableUserAgentBinding
//EnableIpBinding = EnableIpBinding,
//EnableUserAgentBinding = EnableUserAgentBinding
};
}
Loading
Loading