Skip to content

Commit 2ec02f2

Browse files
committed
Remove Original Options from AuthFlowContext and Refactor Session Validation, Fix Mode Based Options not Using
1 parent e2cc7fc commit 2ec02f2

44 files changed

Lines changed: 291 additions & 260 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/Contracts/Session/SessionIssuanceContext.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CodeBeam.UltimateAuth.Core.Domain;
22
using CodeBeam.UltimateAuth.Core.MultiTenancy;
3+
using CodeBeam.UltimateAuth.Core.Options;
34

45
namespace CodeBeam.UltimateAuth.Core.Contracts;
56

@@ -16,6 +17,7 @@ public sealed class SessionIssuanceContext
1617
public ClaimsSnapshot? Claims { get; init; }
1718
public required SessionMetadata Metadata { get; init; }
1819
public required UAuthMode Mode { get; init; }
20+
public required UAuthSessionOptions SessionOptions { get; init; }
1921

2022
/// <summary>
2123
/// Optional chain identifier.

‎src/CodeBeam.UltimateAuth.Core/Contracts/Session/SessionRotationContext.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CodeBeam.UltimateAuth.Core.Domain;
22
using CodeBeam.UltimateAuth.Core.MultiTenancy;
3+
using CodeBeam.UltimateAuth.Core.Options;
34

45
namespace CodeBeam.UltimateAuth.Core.Contracts;
56

@@ -13,4 +14,5 @@ public sealed record SessionRotationContext
1314
public ClaimsSnapshot? Claims { get; init; }
1415
public required SessionMetadata Metadata { get; init; } = SessionMetadata.Empty;
1516
public required UAuthMode Mode { get; init; }
17+
public required UAuthSessionOptions SessionOptions { get; init; }
1618
}

‎src/CodeBeam.UltimateAuth.Core/Contracts/Session/SessionValidationContext.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CodeBeam.UltimateAuth.Core.Domain;
22
using CodeBeam.UltimateAuth.Core.MultiTenancy;
3+
using CodeBeam.UltimateAuth.Core.Options;
34

45
namespace CodeBeam.UltimateAuth.Core.Contracts;
56

@@ -9,4 +10,9 @@ public sealed record SessionValidationContext
910
public AuthSessionId SessionId { get; init; }
1011
public DateTimeOffset Now { get; init; }
1112
public required DeviceContext Device { get; init; }
13+
14+
/// <summary>
15+
/// Required session options for validation. Remote session validators may not include.
16+
/// </summary>
17+
public UAuthSessionOptions? SessionOptions { get; init; }
1218
}

‎src/CodeBeam.UltimateAuth.Server/Auth/Context/AuthFlowContext.cs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ public sealed class AuthFlowContext
2121
public bool IsAuthenticated { get; }
2222
public UserKey? UserKey { get; }
2323

24-
public UAuthServerOptions OriginalOptions { get; }
25-
public EffectiveUAuthServerOptions EffectiveOptions { get; }
24+
public UAuthServerOptions Options { get; }
2625

2726
public EffectiveAuthResponse Response { get; }
2827
public ReturnUrlInfo ReturnUrlInfo { get; }
@@ -46,8 +45,7 @@ internal AuthFlowContext(
4645
bool isAuthenticated,
4746
UserKey? userKey,
4847
SessionSecurityContext? session,
49-
UAuthServerOptions originalOptions,
50-
EffectiveUAuthServerOptions effectiveOptions,
48+
UAuthServerOptions options,
5149
EffectiveAuthResponse response,
5250
PrimaryTokenKind primaryTokenKind,
5351
ReturnUrlInfo returnUrlInfo)
@@ -65,8 +63,7 @@ internal AuthFlowContext(
6563
IsAuthenticated = isAuthenticated;
6664
UserKey = userKey;
6765

68-
OriginalOptions = originalOptions;
69-
EffectiveOptions = effectiveOptions;
66+
Options = options;
7067

7168
Response = response;
7269
PrimaryTokenKind = primaryTokenKind;

‎src/CodeBeam.UltimateAuth.Server/Auth/Context/AuthFlowContextFactory.cs‎

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using CodeBeam.UltimateAuth.Core.Contracts;
33
using CodeBeam.UltimateAuth.Core.Domain;
44
using CodeBeam.UltimateAuth.Core.Infrastructure;
5-
using CodeBeam.UltimateAuth.Core.MultiTenancy;
65
using CodeBeam.UltimateAuth.Core.Options;
76
using CodeBeam.UltimateAuth.Server.Abstractions;
87
using CodeBeam.UltimateAuth.Server.Extensions;
@@ -50,19 +49,20 @@ public async ValueTask<AuthFlowContext> CreateAsync(HttpContext ctx, AuthFlowTyp
5049
var user = ctx.GetUserContext();
5150

5251
var clientProfile = await _clientProfileReader.ReadAsync(ctx);
53-
var originalOptions = _serverOptionsProvider.GetOriginal(ctx);
54-
var effectiveOptions = _serverOptionsProvider.GetEffective(ctx, flowType, clientProfile);
5552

56-
var allowedModes = originalOptions.AllowedModes;
53+
var effective = _serverOptionsProvider.GetEffective(ctx, clientProfile);
54+
var options = effective.Options;
5755

58-
if (allowedModes is { Count: > 0 } && !allowedModes.Contains(effectiveOptions.Mode))
56+
var effectiveMode = effective.Mode;
57+
var allowedModes = options.AllowedModes;
58+
59+
if (allowedModes is { Count: > 0 } && !allowedModes.Contains(effectiveMode))
5960
{
60-
throw new InvalidOperationException($"Auth mode '{effectiveOptions.Mode}' is not allowed by server configuration.");
61+
throw new InvalidOperationException($"Auth mode '{effectiveMode}' is not allowed by server configuration.");
6162
}
6263

63-
var effectiveMode = effectiveOptions.Mode;
6464
var primaryTokenKind = _primaryTokenResolver.Resolve(effectiveMode);
65-
var response = _authResponseResolver.Resolve(effectiveMode, flowType, clientProfile, effectiveOptions);
65+
var response = _authResponseResolver.Resolve(effectiveMode, flowType, clientProfile, effective);
6666
var deviceInfo = await _deviceResolver.ResolveAsync(ctx);
6767
var deviceContext = _deviceContextFactory.Create(deviceInfo);
6868
var returnUrl = await ctx.GetReturnUrlAsync();
@@ -78,6 +78,7 @@ public async ValueTask<AuthFlowContext> CreateAsync(HttpContext ctx, AuthFlowTyp
7878
Tenant = tenant,
7979
SessionId = sessionCtx.SessionId!.Value,
8080
Device = deviceContext,
81+
SessionOptions = options.Session,
8182
Now = _clock.UtcNow
8283
},
8384
ct);
@@ -100,8 +101,7 @@ public async ValueTask<AuthFlowContext> CreateAsync(HttpContext ctx, AuthFlowTyp
100101
user?.IsAuthenticated ?? false,
101102
user?.UserId,
102103
sessionSecurityContext,
103-
originalOptions,
104-
effectiveOptions,
104+
options,
105105
response,
106106
primaryTokenKind,
107107
returnUrlInfo
@@ -113,19 +113,19 @@ public async ValueTask<AuthFlowContext> RecreateWithClientProfileAsync(AuthFlowC
113113
var flowType = existing.FlowType;
114114
var tenant = existing.Tenant;
115115

116-
var originalOptions = existing.OriginalOptions;
117-
var effectiveOptions = _serverOptionsProvider.GetEffective(tenant, flowType, overriddenProfile);
116+
var effective = _serverOptionsProvider.GetEffective(tenant, overriddenProfile);
117+
var options = effective.Options;
118+
var effectiveMode = effective.Mode;
118119

119-
var allowedModes = originalOptions.AllowedModes;
120+
var allowedModes = options.AllowedModes;
120121

121-
if (allowedModes is { Count: > 0 } && !allowedModes.Contains(effectiveOptions.Mode))
122+
if (allowedModes is { Count: > 0 } && !allowedModes.Contains(effectiveMode))
122123
{
123-
throw new InvalidOperationException($"Auth mode '{effectiveOptions.Mode}' is not allowed by server configuration.");
124+
throw new InvalidOperationException($"Auth mode '{effectiveMode}' is not allowed by server configuration.");
124125
}
125126

126-
var effectiveMode = effectiveOptions.Mode;
127127
var primaryTokenKind = _primaryTokenResolver.Resolve(effectiveMode);
128-
var response = _authResponseResolver.Resolve(effectiveMode, flowType, overriddenProfile, effectiveOptions);
128+
var response = _authResponseResolver.Resolve(effectiveMode, flowType, overriddenProfile, effective);
129129

130130
var returnUrlInfo = existing.ReturnUrlInfo;
131131
var deviceContext = existing.Device;
@@ -140,8 +140,7 @@ public async ValueTask<AuthFlowContext> RecreateWithClientProfileAsync(AuthFlowC
140140
existing.IsAuthenticated,
141141
existing.UserKey,
142142
session,
143-
originalOptions,
144-
effectiveOptions,
143+
options,
145144
response,
146145
primaryTokenKind,
147146
returnUrlInfo
@@ -162,8 +161,7 @@ public ValueTask<AuthFlowContext> RecreateWithDeviceAsync(AuthFlowContext existi
162161
existing.IsAuthenticated,
163162
existing.UserKey,
164163
existing.Session,
165-
existing.OriginalOptions,
166-
existing.EffectiveOptions,
164+
existing.Options,
167165
existing.Response,
168166
existing.PrimaryTokenKind,
169167
existing.ReturnUrlInfo

‎src/CodeBeam.UltimateAuth.Server/Auth/EffectiveServerOptionsProvider.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ public UAuthServerOptions GetOriginal(HttpContext context)
2424
return _baseOptions.Value;
2525
}
2626

27-
public EffectiveUAuthServerOptions GetEffective(HttpContext context, AuthFlowType flowType, UAuthClientProfile clientProfile)
27+
public EffectiveUAuthServerOptions GetEffective(HttpContext context, UAuthClientProfile clientProfile)
2828
{
2929
var tenant = context.GetTenant();
30-
return GetEffective(tenant, flowType, clientProfile);
30+
return GetEffective(tenant, clientProfile);
3131
}
3232

33-
public EffectiveUAuthServerOptions GetEffective(TenantKey tenant, AuthFlowType flowType, UAuthClientProfile clientProfile)
33+
public EffectiveUAuthServerOptions GetEffective(TenantKey tenant, UAuthClientProfile clientProfile)
3434
{
3535
var original = _baseOptions.Value;
36-
var effectiveMode = _modeResolver.Resolve(clientProfile, flowType);
36+
var effectiveMode = _modeResolver.Resolve(clientProfile);
3737
var options = original.Clone();
3838

3939
ConfigureDefaults.ApplyModeDefaults(effectiveMode, options);

‎src/CodeBeam.UltimateAuth.Server/Auth/Response/EffectiveAuthModeResolver.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace CodeBeam.UltimateAuth.Server.Auth;
66

77
internal sealed class EffectiveAuthModeResolver : IEffectiveAuthModeResolver
88
{
9-
public UAuthMode Resolve(UAuthClientProfile clientProfile, AuthFlowType flowType)
9+
public UAuthMode Resolve(UAuthClientProfile clientProfile)
1010
{
1111
return clientProfile switch
1212
{

‎src/CodeBeam.UltimateAuth.Server/Auth/Response/IEffectiveAuthModeResolver.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ namespace CodeBeam.UltimateAuth.Server.Auth;
66

77
public interface IEffectiveAuthModeResolver
88
{
9-
UAuthMode Resolve(UAuthClientProfile clientProfile, AuthFlowType flowType);
9+
UAuthMode Resolve(UAuthClientProfile clientProfile);
1010
}

‎src/CodeBeam.UltimateAuth.Server/Authentication/AspNetCore/UAuthAuthenticationHandler.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
6161
Tenant = tenant,
6262
SessionId = sessionId,
6363
Device = _deviceContextFactory.Create(credential.Device),
64+
SessionOptions = _serverOptions.Session,
6465
Now = _clock.UtcNow
6566
});
6667

‎src/CodeBeam.UltimateAuth.Server/Authentication/AspNetCore/UAuthResourceAuthenticationHandler.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using CodeBeam.UltimateAuth.Core.Abstractions;
22
using CodeBeam.UltimateAuth.Core.Contracts;
33
using CodeBeam.UltimateAuth.Core.Domain;
4+
using CodeBeam.UltimateAuth.Server.Auth;
45
using CodeBeam.UltimateAuth.Server.Extensions;
56
using CodeBeam.UltimateAuth.Server.Infrastructure;
7+
using CodeBeam.UltimateAuth.Server.Options;
68
using Microsoft.AspNetCore.Authentication;
79
using Microsoft.Extensions.Logging;
810
using Microsoft.Extensions.Options;

0 commit comments

Comments
 (0)