-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
181 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/Sitko.Core.Blazor.Server/BaseRevalidatingServerAuthenticationStateProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Diagnostics; | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Authorization; | ||
using Microsoft.AspNetCore.Components.Server; | ||
using Microsoft.AspNetCore.Components.Web; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Sitko.Core.Blazor.Server; | ||
|
||
public abstract class | ||
BaseRevalidatingServerAuthenticationStateProvider<TUserInfo> : RevalidatingServerAuthenticationStateProvider | ||
{ | ||
private readonly PersistentComponentState state; | ||
|
||
private readonly PersistingComponentStateSubscription subscription; | ||
|
||
private Task<AuthenticationState>? authenticationStateTask; | ||
|
||
protected BaseRevalidatingServerAuthenticationStateProvider(ILoggerFactory loggerFactory, | ||
PersistentComponentState persistentComponentState) : base(loggerFactory) | ||
{ | ||
state = persistentComponentState; | ||
|
||
AuthenticationStateChanged += OnAuthenticationStateChanged; | ||
subscription = state.RegisterOnPersisting(OnPersistingAsync, RenderMode.InteractiveWebAssembly); | ||
} | ||
|
||
protected override TimeSpan RevalidationInterval => TimeSpan.FromMinutes(30); | ||
|
||
protected override Task<bool> ValidateAuthenticationStateAsync( | ||
AuthenticationState authenticationState, CancellationToken cancellationToken) => | ||
Task.FromResult(true); | ||
|
||
private void OnAuthenticationStateChanged(Task<AuthenticationState> task) => authenticationStateTask = task; | ||
|
||
private async Task OnPersistingAsync() | ||
{ | ||
if (authenticationStateTask is null) | ||
{ | ||
throw new UnreachableException($"Authentication state not set in {nameof(OnPersistingAsync)}()."); | ||
} | ||
|
||
var authenticationState = await authenticationStateTask; | ||
var principal = authenticationState.User; | ||
|
||
if (principal.Identity?.IsAuthenticated == true) | ||
{ | ||
var userInfo = GetUserInfo(principal); | ||
if (userInfo is not null) | ||
{ | ||
state.PersistAsJson(typeof(TUserInfo).FullName!, userInfo); | ||
} | ||
} | ||
} | ||
|
||
protected abstract TUserInfo? GetUserInfo(ClaimsPrincipal principal); | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
subscription.Dispose(); | ||
AuthenticationStateChanged -= OnAuthenticationStateChanged; | ||
base.Dispose(disposing); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Sitko.Core.Blazor.Wasm/BasePersistentAuthenticationStateProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Authorization; | ||
|
||
namespace Sitko.Core.Blazor.Wasm; | ||
|
||
public abstract class BasePersistentAuthenticationStateProvider<TUser>: AuthenticationStateProvider | ||
{ | ||
// ReSharper disable once StaticMemberInGenericType | ||
private static readonly Task<AuthenticationState> DefaultUnauthenticatedTask = | ||
Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()))); | ||
|
||
private readonly Task<AuthenticationState> authenticationStateTask = DefaultUnauthenticatedTask; | ||
|
||
protected BasePersistentAuthenticationStateProvider(PersistentComponentState state) | ||
{ | ||
if (!state.TryTakeFromJson<TUser>(nameof(TUser), out var userInfo) || userInfo is null) | ||
{ | ||
return; | ||
} | ||
|
||
// ReSharper disable once VirtualMemberCallInConstructor | ||
var claims = GetClaims(userInfo); | ||
|
||
authenticationStateTask = Task.FromResult( | ||
new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(claims, | ||
authenticationType: nameof(BasePersistentAuthenticationStateProvider<TUser>))))); | ||
} | ||
|
||
protected abstract Claim[] GetClaims(TUser user); | ||
|
||
public override Task<AuthenticationState> GetAuthenticationStateAsync() => authenticationStateTask; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters