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
19 changes: 0 additions & 19 deletions src/CodeBeam.UltimateAuth.Core/Contracts/Token/PrimaryToken.cs

This file was deleted.

This file was deleted.

8 changes: 0 additions & 8 deletions src/CodeBeam.UltimateAuth.Core/Domain/Principals/GrantKind.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Defines whether a persisted session can authenticate an opaque credential or only represents JWT lineage.
/// </summary>
public enum SessionCredentialPurpose
public enum SessionIdPurpose
{
/// <summary>
/// The session ID is an opaque authentication credential.
Expand Down
12 changes: 6 additions & 6 deletions src/CodeBeam.UltimateAuth.Core/Domain/Session/UAuthSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public sealed class UAuthSession : IVersionedEntity
public SessionMetadata Metadata { get; }
public long Version { get; set; }

public SessionCredentialPurpose CredentialPurpose { get; }
public SessionIdPurpose CredentialPurpose { get; }

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

public bool IsRevoked => RevokedAt != null;

Expand All @@ -39,7 +39,7 @@ private UAuthSession(
ClaimsSnapshot claims,
SessionMetadata metadata,
long version,
SessionCredentialPurpose credentialPurpose)
SessionIdPurpose credentialPurpose)
{
SessionId = sessionId;
Tenant = tenant;
Expand Down Expand Up @@ -67,7 +67,7 @@ public static UAuthSession Create(
DeviceContext device,
ClaimsSnapshot? claims,
SessionMetadata metadata,
SessionCredentialPurpose credentialPurpose = SessionCredentialPurpose.Opaque)
SessionIdPurpose credentialPurpose = SessionIdPurpose.Opaque)
{
if (!Enum.IsDefined(credentialPurpose))
throw new ArgumentOutOfRangeException(nameof(credentialPurpose));
Expand Down Expand Up @@ -123,7 +123,7 @@ internal static UAuthSession FromProjection(
ClaimsSnapshot claims,
SessionMetadata metadata,
long version,
SessionCredentialPurpose credentialPurpose)
SessionIdPurpose credentialPurpose)
{
return new UAuthSession(
sessionId,
Expand Down
23 changes: 23 additions & 0 deletions src/CodeBeam.UltimateAuth.Core/Domain/Surrogates/SurrogateKind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace CodeBeam.UltimateAuth.Core.Domain;

/// <summary>
/// Identifies the kind of secondary authentication data used to maintain authentication continuity.
/// </summary>
public enum SurrogateKind
{
/// <summary>
/// An authentication-capable session identifier.
/// Metadata-only session identifiers are not surrogates.
/// </summary>
SessionId = 0,

/// <summary>
/// A token used to authenticate subsequent requests.
/// </summary>
AccessToken = 10,

/// <summary>
/// A token used to renew authentication without presenting the original credential again.
/// </summary>
RefreshToken = 20
}
105 changes: 105 additions & 0 deletions src/CodeBeam.UltimateAuth.Core/Extensions/UAuthModeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using CodeBeam.UltimateAuth.Core.Contracts;
using CodeBeam.UltimateAuth.Core.Domain;

namespace CodeBeam.UltimateAuth.Core.Extensions;

public static class UAuthModeExtensions
{
public static SurrogateKind GetPrimarySurrogateKind(this UAuthMode mode)
=> mode switch
{
UAuthMode.PureOpaque or UAuthMode.Hybrid
=> SurrogateKind.SessionId,

UAuthMode.SemiHybrid or UAuthMode.PureJwt
=> SurrogateKind.AccessToken,

_ => throw UnsupportedMode(mode)
};

public static bool SupportsSessionLineage(this UAuthMode mode)
=> mode switch
{
UAuthMode.PureOpaque or
UAuthMode.Hybrid or
UAuthMode.SemiHybrid => true,

UAuthMode.PureJwt => false,

_ => throw UnsupportedMode(mode)
};

public static bool UsesSessionIdForAuthentication(this UAuthMode mode)
=> mode.GetPrimarySurrogateKind() == SurrogateKind.SessionId;

public static bool SupportsAccessToken(this UAuthMode mode)
=> mode switch
{
UAuthMode.PureOpaque => false,

UAuthMode.Hybrid or
UAuthMode.SemiHybrid or
UAuthMode.PureJwt => true,

_ => throw UnsupportedMode(mode)
};

public static bool SupportsRefreshToken(this UAuthMode mode)
=> mode switch
{
UAuthMode.PureOpaque => false,

UAuthMode.Hybrid or
UAuthMode.SemiHybrid or
UAuthMode.PureJwt => true,

_ => throw UnsupportedMode(mode)
};

public static TokenFormat? GetAccessTokenFormat(this UAuthMode mode)
=> mode switch
{
UAuthMode.PureOpaque => null,

UAuthMode.Hybrid => TokenFormat.Opaque,

UAuthMode.SemiHybrid or
UAuthMode.PureJwt => TokenFormat.Jwt,

_ => throw UnsupportedMode(mode)
};

public static bool SupportsSurrogate(this UAuthMode mode, SurrogateKind kind)
=> kind switch
{
SurrogateKind.SessionId
=> mode.UsesSessionIdForAuthentication(),

SurrogateKind.AccessToken
=> mode.SupportsAccessToken(),

SurrogateKind.RefreshToken
=> mode.SupportsRefreshToken(),

_ => throw new ArgumentOutOfRangeException(
nameof(kind), kind, "Unsupported surrogate kind.")
};

public static TokenFormat? GetTokenFormat(this UAuthMode mode, SurrogateKind kind)
{
if (!mode.SupportsSurrogate(kind))
return null;

return kind switch
{
SurrogateKind.SessionId => TokenFormat.Opaque,
SurrogateKind.AccessToken => mode.GetAccessTokenFormat(),
SurrogateKind.RefreshToken => TokenFormat.Opaque,

_ => throw new ArgumentOutOfRangeException(nameof(kind), kind, "Unsupported surrogate kind.")
};
}

private static ArgumentOutOfRangeException UnsupportedMode(UAuthMode mode)
=> new(nameof(mode), mode, "Unsupported authentication mode.");
}
10 changes: 0 additions & 10 deletions src/CodeBeam.UltimateAuth.Core/Options/TokenResponseMode.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace CodeBeam.UltimateAuth.Server.Abstractions;

public interface ISessionLineageValidator
{
Task<SessionValidationResult> ValidateAsync(SessionValidationContext context, SessionCredentialPurpose expectedPurpose, CancellationToken ct = default);
Task<SessionValidationResult> ValidateAsync(SessionValidationContext context, SessionIdPurpose expectedPurpose, CancellationToken ct = default);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using CodeBeam.UltimateAuth.Core.Contracts;
using CodeBeam.UltimateAuth.Core.Domain;
using Microsoft.AspNetCore.Http;

namespace CodeBeam.UltimateAuth.Server.Abstractions;

public interface ISurrogateResponseWriter
{
void Write(HttpContext context, SurrogateKind kind, AuthSessionId sessionId);
void Write(HttpContext context, SurrogateKind kind, AccessToken accessToken);
void Write(HttpContext context, SurrogateKind kind, RefreshTokenInfo refreshToken);
}
10 changes: 4 additions & 6 deletions src/CodeBeam.UltimateAuth.Server/Auth/Context/AuthFlowContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using CodeBeam.UltimateAuth.Core.Contracts;
using CodeBeam.UltimateAuth.Core.Domain;
using CodeBeam.UltimateAuth.Core.MultiTenancy;
using CodeBeam.UltimateAuth.Core.Options;
using CodeBeam.UltimateAuth.Core.Extensions;
using CodeBeam.UltimateAuth.Server.Domain;
using CodeBeam.UltimateAuth.Server.Infrastructure;
using CodeBeam.UltimateAuth.Server.Options;
Expand All @@ -26,12 +26,12 @@ public sealed class AuthFlowContext
public EffectiveAuthResponse Response { get; }
public ReturnUrlInfo ReturnUrlInfo { get; }

public PrimaryTokenKind PrimaryTokenKind { get; }
public SurrogateKind PrimarySurrogateKind => EffectiveMode.GetPrimarySurrogateKind();

// Helpers
public bool AllowsTokenIssuance =>
Response.AccessTokenDelivery.Mode != GrantDeliveryMode.None ||
Response.RefreshTokenDelivery.Mode != GrantDeliveryMode.None;
Response.AccessTokenTransport.Mode != SurrogateTransport.None ||
Response.RefreshTokenTransport.Mode != SurrogateTransport.None;

public bool IsSingleTenant => Tenant.IsSingle;
public bool IsMultiTenant => Tenant.IsNormal;
Expand All @@ -47,7 +47,6 @@ internal AuthFlowContext(
SessionSecurityContext? session,
UAuthServerOptions options,
EffectiveAuthResponse response,
PrimaryTokenKind primaryTokenKind,
ReturnUrlInfo returnUrlInfo)
{
if (!tenantKey.IsValid || tenantKey.IsUnresolved)
Expand All @@ -66,7 +65,6 @@ internal AuthFlowContext(
Options = options;

Response = response;
PrimaryTokenKind = primaryTokenKind;

ReturnUrlInfo = returnUrlInfo;
}
Expand Down
Loading
Loading