Skip to content

Commit 466b7c9

Browse files
committed
Refactoring on Credential Projects
1 parent 756e62c commit 466b7c9

File tree

120 files changed

+2362
-2536
lines changed

Some content is hidden

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

120 files changed

+2362
-2536
lines changed

samples/UAuthHub/CodeBeam.UltimateAuth.Sample.UAuthHub/Controllers/HubLoginController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CodeBeam.UltimateAuth.Core.Abstractions;
22
using CodeBeam.UltimateAuth.Core.Domain;
3+
using CodeBeam.UltimateAuth.Core.MultiTenancy;
34
using CodeBeam.UltimateAuth.Core.Options;
45
using CodeBeam.UltimateAuth.Server.Options;
56
using CodeBeam.UltimateAuth.Server.Stores;
@@ -41,7 +42,7 @@ public async Task<IActionResult> BeginLogin(
4142
hubSessionId: hubSessionId,
4243
flowType: HubFlowType.Login,
4344
clientProfile: client_profile,
44-
tenantId: null,
45+
tenant: TenantKeys.System,
4546
returnUrl: return_url,
4647
payload: payload,
4748
expiresAt: _clock.UtcNow.Add(_options.Hub.FlowLifetime));
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using CodeBeam.UltimateAuth.Client.Contracts;
22

3-
namespace CodeBeam.UltimateAuth.Client.Utilities
3+
namespace CodeBeam.UltimateAuth.Client.Utilities;
4+
5+
public interface IBrowserStorage
46
{
5-
public interface IBrowserStorage
6-
{
7-
ValueTask SetAsync(StorageScope scope, string key, string value);
8-
ValueTask<string?> GetAsync(StorageScope scope, string key);
9-
ValueTask RemoveAsync(StorageScope scope, string key);
10-
ValueTask<bool> ExistsAsync(StorageScope scope, string key);
11-
}
7+
ValueTask SetAsync(StorageScope scope, string key, string value);
8+
ValueTask<string?> GetAsync(StorageScope scope, string key);
9+
ValueTask RemoveAsync(StorageScope scope, string key);
10+
ValueTask<bool> ExistsAsync(StorageScope scope, string key);
1211
}
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
namespace CodeBeam.UltimateAuth.Client.Abstractions
1+
namespace CodeBeam.UltimateAuth.Client.Abstractions;
2+
3+
public interface ISessionCoordinator : IAsyncDisposable
24
{
3-
public interface ISessionCoordinator : IAsyncDisposable
4-
{
5-
/// <summary>
6-
/// Starts session coordination.
7-
/// Should be idempotent.
8-
/// </summary>
9-
Task StartAsync(CancellationToken cancellationToken = default);
5+
/// <summary>
6+
/// Starts session coordination.
7+
/// Should be idempotent.
8+
/// </summary>
9+
Task StartAsync(CancellationToken cancellationToken = default);
1010

11-
/// <summary>
12-
/// Stops coordination (optional).
13-
/// </summary>
14-
Task StopAsync();
11+
/// <summary>
12+
/// Stops coordination (optional).
13+
/// </summary>
14+
Task StopAsync();
1515

16-
event Action? ReauthRequired;
17-
}
16+
event Action? ReauthRequired;
1817
}
Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,54 @@
11
using CodeBeam.UltimateAuth.Client.Runtime;
22
using CodeBeam.UltimateAuth.Core.Abstractions;
33

4-
namespace CodeBeam.UltimateAuth.Client.Authentication
5-
{
6-
internal sealed class DefaultUAuthStateManager : IUAuthStateManager
7-
{
8-
private readonly IUAuthClient _client;
9-
private readonly IClock _clock;
10-
private readonly IUAuthClientBootstrapper _bootstrapper;
11-
12-
public UAuthState State { get; } = UAuthState.Anonymous();
13-
14-
public DefaultUAuthStateManager(IUAuthClient client, IClock clock, IUAuthClientBootstrapper bootstrapper)
15-
{
16-
_client = client;
17-
_clock = clock;
18-
_bootstrapper = bootstrapper;
19-
}
4+
namespace CodeBeam.UltimateAuth.Client.Authentication;
205

21-
public async Task EnsureAsync(CancellationToken ct = default)
22-
{
23-
if (State.IsAuthenticated && !State.IsStale)
24-
return;
6+
internal sealed class DefaultUAuthStateManager : IUAuthStateManager
7+
{
8+
private readonly IUAuthClient _client;
9+
private readonly IClock _clock;
10+
private readonly IUAuthClientBootstrapper _bootstrapper;
2511

26-
await _bootstrapper.EnsureStartedAsync();
27-
var result = await _client.Flows.ValidateAsync();
12+
public UAuthState State { get; } = UAuthState.Anonymous();
2813

29-
if (!result.IsValid)
30-
{
31-
State.Clear();
32-
return;
33-
}
14+
public DefaultUAuthStateManager(IUAuthClient client, IClock clock, IUAuthClientBootstrapper bootstrapper)
15+
{
16+
_client = client;
17+
_clock = clock;
18+
_bootstrapper = bootstrapper;
19+
}
3420

35-
State.ApplySnapshot(result.Snapshot, _clock.UtcNow);
36-
}
21+
public async Task EnsureAsync(CancellationToken ct = default)
22+
{
23+
if (State.IsAuthenticated && !State.IsStale)
24+
return;
3725

38-
public Task OnLoginAsync()
39-
{
40-
State.MarkStale();
41-
return Task.CompletedTask;
42-
}
26+
await _bootstrapper.EnsureStartedAsync();
27+
var result = await _client.Flows.ValidateAsync();
4328

44-
public Task OnLogoutAsync()
29+
if (!result.IsValid)
4530
{
4631
State.Clear();
47-
return Task.CompletedTask;
32+
return;
4833
}
4934

50-
public void MarkStale()
51-
{
52-
State.MarkStale();
53-
}
35+
State.ApplySnapshot(result.Snapshot, _clock.UtcNow);
36+
}
37+
38+
public Task OnLoginAsync()
39+
{
40+
State.MarkStale();
41+
return Task.CompletedTask;
42+
}
43+
44+
public Task OnLogoutAsync()
45+
{
46+
State.Clear();
47+
return Task.CompletedTask;
48+
}
49+
50+
public void MarkStale()
51+
{
52+
State.MarkStale();
5453
}
5554
}
Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
1-
namespace CodeBeam.UltimateAuth.Client.Authentication
1+
namespace CodeBeam.UltimateAuth.Client.Authentication;
2+
3+
/// <summary>
4+
/// Orchestrates the lifecycle of UAuthState.
5+
/// This is the single authority responsible for keeping
6+
/// client-side authentication state in sync with the server.
7+
/// </summary>
8+
public interface IUAuthStateManager
29
{
310
/// <summary>
4-
/// Orchestrates the lifecycle of UAuthState.
5-
/// This is the single authority responsible for keeping
6-
/// client-side authentication state in sync with the server.
11+
/// Current in-memory authentication state.
712
/// </summary>
8-
public interface IUAuthStateManager
9-
{
10-
/// <summary>
11-
/// Current in-memory authentication state.
12-
/// </summary>
13-
UAuthState State { get; }
13+
UAuthState State { get; }
1414

15-
/// <summary>
16-
/// Ensures the authentication state is valid.
17-
/// May call server validate/refresh if needed.
18-
/// </summary>
19-
Task EnsureAsync(CancellationToken ct = default);
15+
/// <summary>
16+
/// Ensures the authentication state is valid.
17+
/// May call server validate/refresh if needed.
18+
/// </summary>
19+
Task EnsureAsync(CancellationToken ct = default);
2020

21-
/// <summary>
22-
/// Called after a successful login.
23-
/// </summary>
24-
Task OnLoginAsync();
21+
/// <summary>
22+
/// Called after a successful login.
23+
/// </summary>
24+
Task OnLoginAsync();
2525

26-
/// <summary>
27-
/// Called after logout.
28-
/// </summary>
29-
Task OnLogoutAsync();
26+
/// <summary>
27+
/// Called after logout.
28+
/// </summary>
29+
Task OnLogoutAsync();
3030

31-
/// <summary>
32-
/// Forces state to be cleared and re-validation required.
33-
/// </summary>
34-
void MarkStale();
35-
}
31+
/// <summary>
32+
/// Forces state to be cleared and re-validation required.
33+
/// </summary>
34+
void MarkStale();
3635
}
Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
using CodeBeam.UltimateAuth.Client.Abstractions;
2-
using Microsoft.AspNetCore.Components.Authorization;
3-
using System.Security.Principal;
1+
using Microsoft.AspNetCore.Components.Authorization;
42

5-
namespace CodeBeam.UltimateAuth.Client.Authentication
6-
{
7-
internal sealed class UAuthAuthenticationStateProvider : AuthenticationStateProvider
8-
{
9-
private readonly IUAuthStateManager _stateManager;
3+
namespace CodeBeam.UltimateAuth.Client.Authentication;
104

11-
public UAuthAuthenticationStateProvider(IUAuthStateManager stateManager)
12-
{
13-
_stateManager = stateManager;
14-
_stateManager.State.Changed += _ => NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
15-
}
5+
internal sealed class UAuthAuthenticationStateProvider : AuthenticationStateProvider
6+
{
7+
private readonly IUAuthStateManager _stateManager;
168

17-
public override Task<AuthenticationState> GetAuthenticationStateAsync()
18-
{
19-
var principal = _stateManager.State.ToClaimsPrincipal();
20-
return Task.FromResult(new AuthenticationState(principal));
21-
}
9+
public UAuthAuthenticationStateProvider(IUAuthStateManager stateManager)
10+
{
11+
_stateManager = stateManager;
12+
_stateManager.State.Changed += _ => NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
13+
}
2214

15+
public override Task<AuthenticationState> GetAuthenticationStateAsync()
16+
{
17+
var principal = _stateManager.State.ToClaimsPrincipal();
18+
return Task.FromResult(new AuthenticationState(principal));
2319
}
20+
2421
}
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
using Microsoft.AspNetCore.Components;
22

3-
namespace CodeBeam.UltimateAuth.Client.Authentication
3+
namespace CodeBeam.UltimateAuth.Client.Authentication;
4+
5+
internal sealed class UAuthCascadingStateProvider : CascadingValueSource<UAuthState>, IDisposable
46
{
5-
internal sealed class UAuthCascadingStateProvider : CascadingValueSource<UAuthState>, IDisposable
6-
{
7-
private readonly IUAuthStateManager _stateManager;
7+
private readonly IUAuthStateManager _stateManager;
88

9-
public UAuthCascadingStateProvider(IUAuthStateManager stateManager)
10-
: base(() => stateManager.State, isFixed: false)
11-
{
12-
_stateManager = stateManager;
13-
_stateManager.State.Changed += OnStateChanged;
14-
}
9+
public UAuthCascadingStateProvider(IUAuthStateManager stateManager)
10+
: base(() => stateManager.State, isFixed: false)
11+
{
12+
_stateManager = stateManager;
13+
_stateManager.State.Changed += OnStateChanged;
14+
}
1515

16-
private void OnStateChanged(UAuthStateChangeReason _)
17-
{
18-
NotifyChangedAsync();
19-
}
16+
private void OnStateChanged(UAuthStateChangeReason _)
17+
{
18+
NotifyChangedAsync();
19+
}
2020

21-
public void Dispose()
22-
{
23-
_stateManager.State.Changed -= OnStateChanged;
24-
}
21+
public void Dispose()
22+
{
23+
_stateManager.State.Changed -= OnStateChanged;
2524
}
2625
}

0 commit comments

Comments
 (0)